Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: dttung2905 <ttdao.2015@accountancy.smu.edu.sg>
  • Loading branch information
dttung2905 committed Nov 20, 2023
1 parent 0a0cf4d commit 842d536
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions pkg/scaling/resolver/hashicorpvault_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func TestHashicorpVaultHandler_getSecretValue_specify_secret_type(t *testing.T)
}}
assert.Equalf(t, kedav1alpha1.VaultSecretTypeGeneric, secrets[0].Type, "Expected secret to not have a vlue")
secrets, _ = vaultHandler.ResolveSecrets(secrets)
assert.Len(t, secrets, 1, "Supposed to got back one secret")
assert.Len(t, secrets, 1, "Supposed to get back one secret")
secret := secrets[0]
assert.Equalf(t, kedav1alpha1.VaultSecretTypeSecretV2, secret.Type, "Expexted secret type be %s", kedav1alpha1.VaultSecretTypeSecretV2)
assert.Equalf(t, kedaSecretValue, secret.Value, "Expexted secret to be %s", kedaSecretValue)
Expand All @@ -202,7 +202,7 @@ func TestHashicorpVaultHandler_getSecretValue_specify_secret_type(t *testing.T)
}}
assert.Equalf(t, kedav1alpha1.VaultSecretTypeGeneric, secrets[0].Type, "Expected secret to not have a vlue")
secrets, _ = vaultHandler.ResolveSecrets(secrets)
assert.Len(t, secrets, 1, "Supposed to got back one secret")
assert.Len(t, secrets, 1, "Supposed to get back one secret")
secret = secrets[0]
assert.Equalf(t, kedav1alpha1.VaultSecretTypeSecret, secret.Type, "Expexted secret type be %s", kedav1alpha1.VaultSecretTypeSecret)
assert.Equalf(t, kedaSecretValue, secret.Value, "Expexted secret to be %s", kedaSecretValue)
Expand Down Expand Up @@ -287,7 +287,7 @@ var resolveRequestTestDataSet = []resolveRequestTestData{
key: "array",
isError: false,
expectedValue: "",
secretType: "inexisting_type",
secretType: "non_existing_type",
},
{
name: "existing_pki",
Expand Down Expand Up @@ -334,7 +334,7 @@ func TestHashicorpVaultHandler_ResolveSecret(t *testing.T) {
PkiData: testData.pkiData,
}}
secrets, err := vaultHandler.ResolveSecrets(secrets)
assert.Len(t, secrets, 1, "Supposed to got back one secret")
assert.Len(t, secrets, 1, "Supposed to get back one secret")
secret := secrets[0]
if testData.isError {
assert.NotNilf(t, err, "test %s: expected error but got success, testData - %+v", testData.name, testData)
Expand Down Expand Up @@ -518,6 +518,7 @@ func TestHashicorpVaultHandler_Initialize(t *testing.T) {
type tokenTestData struct {
name string
isError bool
errorMessage string
authentication kedav1alpha1.VaultAuthentication
credential kedav1alpha1.Credential
mount string
Expand All @@ -542,10 +543,21 @@ var tokenTestDataSet = []tokenTestData{
credential: kedav1alpha1.Credential{
ServiceAccount: "random/path",
},
role: "my-role",
mount: "my-mount",
role: "my-role",
mount: "my-mount",
errorMessage: "open random/path: no such file or directory",
},
{
name: "Wrong Authentication Method",
isError: true,
authentication: "random",
credential: kedav1alpha1.Credential{
ServiceAccount: "random/path",
},
role: "my-role",
mount: "my-mount",
errorMessage: "vault auth method random is not supported",
},
// TODO: test wrong authentication
}

func TestHashicorpVaultHandler_Token_VaultTokenAuth(t *testing.T) {
Expand All @@ -566,10 +578,12 @@ func TestHashicorpVaultHandler_Token_VaultTokenAuth(t *testing.T) {

config := vaultapi.DefaultConfig()
client, err := vaultapi.NewClient(config)
assert.Nil(t, err)
token, err := vaultHandler.token(client)
if testData.isError {
assert.Equalf(t, vaultHandler.vault.Credential.ServiceAccount, "random/path", "test %s: expected %s but found %s", testData.name, "random/path", vaultHandler.vault.Credential.ServiceAccount)
assert.Equalf(t, vaultHandler.vault.Credential.ServiceAccount, testData.credential.ServiceAccount, "test %s: expected %s but found %s", testData.name, "random/path", vaultHandler.vault.Credential.ServiceAccount)
assert.NotNilf(t, err, "test %s: expected error but got success, testData - %+v", testData.name, testData)
assert.Contains(t, err.Error(), testData.errorMessage)
} else {
assert.Equalf(t, token, vaultTestToken, "expected %s but got %s", vaultTestToken, token)
}
Expand Down

0 comments on commit 842d536

Please sign in to comment.