Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: dttung2905 <[email protected]>
  • Loading branch information
dttung2905 committed Jan 14, 2025
1 parent c506a57 commit 7d4775c
Showing 1 changed file with 59 additions and 10 deletions.
69 changes: 59 additions & 10 deletions tests/secret-providers/hashicorp_vault/hashicorp_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"

. "github.com/kedacore/keda/v2/tests/helper"
. "github.com/kedacore/keda/v2/tests/helper" // import helper
"github.com/kedacore/keda/v2/tests/scalers/prometheus"
)

Expand All @@ -30,6 +30,7 @@ var (
testNamespace = fmt.Sprintf("%s-ns", testName)
vaultNamespace = "hashicorp-ns"
vaultPromDomain = "e2e.vault.keda.sh"
vaultSecretKey = "VaultSecretKey"
deploymentName = fmt.Sprintf("%s-deployment", testName)
scaledObjectName = fmt.Sprintf("%s-so", testName)
publishDeploymentName = fmt.Sprintf("%s-publish", testName)
Expand Down Expand Up @@ -70,6 +71,7 @@ type templateData struct {
MonitoredAppName string
PrometheusServerName string
VaultPkiCommonName string
VaultSecretKey string
}

const (
Expand Down Expand Up @@ -117,6 +119,7 @@ metadata:
type: Opaque
data:
postgresql_conn_str: {{.PostgreSQLConnectionStringBase64}}
{{.VaultSecretKey}}: {{.HashiCorpToken}}
`

triggerAuthenticationTemplate = `
Expand All @@ -136,6 +139,27 @@ spec:
key: connectionString
path: {{.VaultSecretPath}}
`
triggerAuthenticationTemplateTokenSecret = `
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
name: {{.TriggerAuthenticationName}}
namespace: {{.TestNamespace}}
spec:
hashiCorpVault:
address: http://vault.{{.VaultNamespace}}:8200
authentication: token
tokenSecret:
valueFrom:
secretKeyRef:
name: {{.SecretName}}
key: {{.VaultSecretKey}}
secrets:
- parameter: connection
key: connectionString
path: {{.VaultSecretPath}}
`

scaledObjectTemplate = `
apiVersion: keda.sh/v1alpha1
Expand Down Expand Up @@ -460,16 +484,31 @@ func TestSecretsEngine(t *testing.T) {
name string
vaultEngineVersion uint
vaultSecretPath string
useSecretRef bool
}{
{
name: "vault kv engine v1",
name: "vault kv engine v1 with secretRef",
vaultEngineVersion: 1,
vaultSecretPath: "secret/keda",
useSecretRef: true,
},
{
name: "vault kv engine v2",
name: "vault kv engine v2 with secretRef",
vaultEngineVersion: 2,
vaultSecretPath: "secret/data/keda",
useSecretRef: true,
},
{
name: "vault kv engine v1 without secretRef",
vaultEngineVersion: 1,
vaultSecretPath: "secret/keda",
useSecretRef: false,
},
{
name: "vault kv engine v2 without secretRef",
vaultEngineVersion: 2,
vaultSecretPath: "secret/data/keda",
useSecretRef: false,
},
}

Expand All @@ -492,7 +531,7 @@ func TestSecretsEngine(t *testing.T) {
assert.True(t, ok, "executing a command on PostreSQL Pod should work; Output: %s, ErrorOutput: %s, Error: %s", out, errOut, err)

// Create kubernetes resources for testing
data, templates := getTemplateData()
data, templates := getTemplateData(test.useSecretRef)
data.HashiCorpToken = RemoveANSI(hashiCorpToken)
data.VaultSecretPath = test.vaultSecretPath

Expand Down Expand Up @@ -651,6 +690,7 @@ var data = templateData{
VaultNamespace: vaultNamespace,
VaultPromDomain: vaultPromDomain,
VaultPkiCommonName: fmt.Sprintf("keda.%s.svc", testNamespace),
VaultSecretKey: vaultSecretKey,
}

func getPostgreSQLTemplateData() (templateData, []Template) {
Expand All @@ -670,11 +710,20 @@ func getPrometheusTemplateData() (templateData, []Template) {
}
}

func getTemplateData() (templateData, []Template) {
return data, []Template{
{Name: "secretTemplate", Config: secretTemplate},
{Name: "deploymentTemplate", Config: deploymentTemplate},
{Name: "triggerAuthenticationTemplate", Config: triggerAuthenticationTemplate},
{Name: "scaledObjectTemplate", Config: scaledObjectTemplate},
func getTemplateData(useSecretRef bool) (templateData, []Template) {
if useSecretRef {
return data, []Template{
{Name: "secretTemplate", Config: secretTemplate},
{Name: "deploymentTemplate", Config: deploymentTemplate},
{Name: "triggerAuthenticationTemplate", Config: triggerAuthenticationTemplateTokenSecret},
{Name: "scaledObjectTemplate", Config: scaledObjectTemplate},
}
} else {
return data, []Template{
{Name: "secretTemplate", Config: secretTemplate},
{Name: "deploymentTemplate", Config: deploymentTemplate},
{Name: "triggerAuthenticationTemplate", Config: triggerAuthenticationTemplate},
{Name: "scaledObjectTemplate", Config: scaledObjectTemplate},
}
}
}

0 comments on commit 7d4775c

Please sign in to comment.