-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try to create every secret instead of returning eraly
Signed-off-by: huabing zhao <zhaohuabing@gmail.com>
- Loading branch information
1 parent
af0837e
commit 71509cf
Showing
3 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
var ( | ||
envoyGatewaySecret = corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "envoy-gateway", | ||
Namespace: "envoy-gateway-system", | ||
}, | ||
} | ||
|
||
envoySecret = corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "envoy", | ||
Namespace: "envoy-gateway-system", | ||
}, | ||
} | ||
|
||
envoyRateLimitSecret = corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "envoy-rate-limit", | ||
Namespace: "envoy-gateway-system", | ||
}, | ||
} | ||
|
||
oidcHMACSecret = corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "envoy-oidc-hmac", | ||
Namespace: "envoy-gateway-system", | ||
}, | ||
} | ||
|
||
existingSecretsWithoutHMAC = []client.Object{ | ||
&envoyGatewaySecret, | ||
&envoySecret, | ||
&envoyRateLimitSecret, | ||
} | ||
|
||
existingSecretsWithHMAC = []client.Object{ | ||
&envoyGatewaySecret, | ||
&envoySecret, | ||
&envoyRateLimitSecret, | ||
&oidcHMACSecret, | ||
} | ||
|
||
SecretsToCreate = []corev1.Secret{ | ||
envoyGatewaySecret, | ||
envoySecret, | ||
envoyRateLimitSecret, | ||
oidcHMACSecret, | ||
} | ||
) | ||
|
||
func TestCreateSecretsWhenUpgrade(t *testing.T) { | ||
t.Run("create HMAC secret when it does not exist", func(t *testing.T) { | ||
cli := fakeclient.NewClientBuilder().WithObjects(existingSecretsWithoutHMAC...).Build() | ||
|
||
secrets, err := CreateOrUpdateSecrets(context.Background(), cli, SecretsToCreate, false) | ||
require.ErrorAs(t, err, &ErrSecretExists) | ||
require.Len(t, secrets, 1) | ||
require.Equal(t, "envoy-oidc-hmac", secrets[0].Name) | ||
|
||
err = cli.Get(context.Background(), client.ObjectKeyFromObject(&oidcHMACSecret), &corev1.Secret{}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("skip HMAC secret when it exist", func(t *testing.T) { | ||
cli := fakeclient.NewClientBuilder().WithObjects(existingSecretsWithHMAC...).Build() | ||
|
||
secrets, err := CreateOrUpdateSecrets(context.Background(), cli, SecretsToCreate, false) | ||
require.ErrorAs(t, err, &ErrSecretExists) | ||
require.Len(t, secrets, 0) | ||
}) | ||
|
||
t.Run("update secrets when they exist", func(t *testing.T) { | ||
cli := fakeclient.NewClientBuilder().WithObjects(existingSecretsWithHMAC...).Build() | ||
|
||
secrets, err := CreateOrUpdateSecrets(context.Background(), cli, SecretsToCreate, true) | ||
require.NoError(t, err) | ||
require.Len(t, secrets, 4) | ||
}) | ||
} |