Skip to content

Commit

Permalink
test: add e2e test for extra volume
Browse files Browse the repository at this point in the history
Signed-off-by: J. Harte <[email protected]>
  • Loading branch information
boonware committed Dec 3, 2024
1 parent b9d9734 commit e1d09e5
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/samples/instana_v1_extended_instanaagent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ spec:
volumes:
- name: my-secret-volume
secret:
secretName: my-secret
secretName: instana-agent-key
volumeMounts:
- name: my-secret-volume
mountPath: /secrets
Expand Down
64 changes: 64 additions & 0 deletions e2e/agent_test_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,70 @@ func ValidateAgentMultiBackendConfiguration() e2etypes.StepFunc {
}
}

func ValidateSecretsMountedFromExtraVolume() e2etypes.StepFunc {
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
log.Infof("Fetching secret %s", InstanaAgentConfigSecretName)
// Create a client to interact with the Kube API
r, err := resources.New(cfg.Client().RESTConfig())
if err != nil {
t.Fatal(err)
}

// Check if namespace exist, otherwise just skip over it
instanaAgentConfigSecret := &corev1.Secret{}
err = r.Get(ctx, InstanaAgentConfigSecretName, InstanaNamespace, instanaAgentConfigSecret)
if err != nil {
t.Fatal("Secret could not be fetched", InstanaAgentConfigSecretName, err)
}

pods := &corev1.PodList{}
listOps := resources.WithLabelSelector("app.kubernetes.io/component=instana-agent")
err = r.List(ctx, pods, listOps)
if err != nil || pods.Items == nil {
t.Error("error while getting pods", err)
}
var stdout, stderr bytes.Buffer
podName := pods.Items[0].Name
containerName := "instana-agent"

secretFileMatrix := []struct {
path string
content string
}{
{
path: "/secrets/key",
content: "xxx",
},
{
path: "/secrets/key-1",
content: "yyy",
},
}

for _, currentFile := range secretFileMatrix {
if err := r.ExecInPod(
ctx,
cfg.Namespace(),
podName,
containerName,
[]string{"cat", currentFile.path},
&stdout,
&stderr,
); err != nil {
t.Log(stderr.String())
t.Error(err)
}
if strings.Contains(stdout.String(), "xxx") {
t.Logf("ExecInPod returned expected secret value from file %s", currentFile.path)
} else {
t.Error(fmt.Sprintf("Expected to find %s in file %s", currentFile.content, currentFile.path), stdout.String())
}
}

return ctx
}
}

// Helper to produce test structs
func NewAgentCr(t *testing.T) v1.InstanaAgent {
boolTrue := true
Expand Down
55 changes: 55 additions & 0 deletions e2e/extra_volume_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* (c) Copyright IBM Corp. 2024
* (c) Copyright Instana Inc. 2024
*/

package e2e

import (
"context"
"fmt"
"testing"

"sigs.k8s.io/e2e-framework/klient/decoder"
"sigs.k8s.io/e2e-framework/klient/k8s/resources"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

func TestExtraVolumeWithSecret(t *testing.T) {
installCrWithExtraVolumeFeature := features.New("extra volume with secret").
Setup(SetupOperatorDevBuild()).
Setup(WaitForDeploymentToBecomeReady(InstanaOperatorDeploymentName)).
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
r, err := resources.New(cfg.Client().RESTConfig())
if err != nil {
t.Fatal(err)
}

t.Logf("Creating dummy secret")

err = decoder.ApplyWithManifestDir(ctx, r, "../config/samples", "external_secret_instana_agent_key.yaml", []resources.CreateOption{})
if err != nil {
t.Fatal(err)
}

t.Logf("Secret created")

t.Logf("Creating dummy agent CR with extra volume")
err = decoder.ApplyWithManifestDir(ctx, r, "../config/samples", "instana_v1_extended_instanaagent.yaml", []resources.CreateOption{})
if err != nil {
t.Fatal(err)
}
t.Logf("CR created")

return ctx
}).
Assess("wait for first k8sensor deployment to become ready", WaitForDeploymentToBecomeReady(K8sensorDeploymentName)).
Assess("wait for second k8sensor deployment to become ready", WaitForDeploymentToBecomeReady(fmt.Sprintf("%s-1", K8sensorDeploymentName))).
Assess("wait for agent daemonset to become ready", WaitForAgentDaemonSetToBecomeReady()).
Assess("validate secret files are created from extra mounted volume", ValidateSecretsMountedFromExtraVolume()).
Feature()

// test feature
testEnv.Test(t, installCrWithExtraVolumeFeature)
}

0 comments on commit e1d09e5

Please sign in to comment.