From bb97134e10d41e748b3407e98f4f8a0591d95006 Mon Sep 17 00:00:00 2001 From: Volodymyr Zotov Date: Fri, 22 Aug 2025 08:30:35 -0500 Subject: [PATCH] Add comments on each test helper function --- pkg/testhelper/kind/kind.go | 2 +- pkg/testhelper/kube/kube.go | 20 +++++++++++++++++--- pkg/testhelper/operator/operator.go | 9 ++++++--- test/e2e/e2e_test.go | 13 +------------ 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/pkg/testhelper/kind/kind.go b/pkg/testhelper/kind/kind.go index a561f31..8b03879 100644 --- a/pkg/testhelper/kind/kind.go +++ b/pkg/testhelper/kind/kind.go @@ -13,7 +13,7 @@ import ( // LoadImageToKind loads a local docker image to the Kind cluster func LoadImageToKind(imageName string) { - By("loading the operator image on Kind") + By("Loading the operator image on Kind") clusterName := "kind" if value, ok := os.LookupEnv("KIND_CLUSTER"); ok { clusterName = value diff --git a/pkg/testhelper/kube/kube.go b/pkg/testhelper/kube/kube.go index 6ee441b..73339af 100644 --- a/pkg/testhelper/kube/kube.go +++ b/pkg/testhelper/kube/kube.go @@ -15,7 +15,10 @@ import ( "github.com/1Password/onepassword-operator/pkg/testhelper/system" ) +// CreateSecretFromEnvVar creates a kubernetes secret from an environment variable func CreateSecretFromEnvVar(envVar, secretName string) { + By("Creating '" + secretName + "' secret") + value, _ := os.LookupEnv(envVar) Expect(value).NotTo(BeEmpty()) @@ -23,11 +26,15 @@ func CreateSecretFromEnvVar(envVar, secretName string) { Expect(err).NotTo(HaveOccurred()) } +// CreateSecretFromFile creates a kubernetes secret from a file func CreateSecretFromFile(fileName, secretName string) { + By("Creating '" + secretName + "' secret from file") _, err := system.Run("kubectl", "create", "secret", "generic", secretName, "--from-file="+fileName) Expect(err).NotTo(HaveOccurred()) } +// CreateOpCredentialsSecret creates a kubernetes secret from 1password-credentials.json file +// encodes it in base64 and saves it to op-session file func CreateOpCredentialsSecret() { rootDir, err := system.GetProjectRoot() Expect(err).NotTo(HaveOccurred()) @@ -46,12 +53,16 @@ func CreateOpCredentialsSecret() { CreateSecretFromFile("op-session", "op-credentials") } +// DeleteSecret deletes a kubernetes secret func DeleteSecret(name string) { + By("Deleting '" + name + "' secret") _, err := system.Run("kubectl", "delete", "secret", name, "--ignore-not-found=true") Expect(err).NotTo(HaveOccurred()) } +// CheckSecretExists checks if a kubernetes secret exists func CheckSecretExists(name string) { + By("Checking '" + name + "' secret exists") Eventually(func(g Gomega) { output, err := system.Run("kubectl", "get", "secret", name, "-o", "jsonpath={.metadata.name}") g.Expect(err).NotTo(HaveOccurred()) @@ -59,11 +70,13 @@ func CheckSecretExists(name string) { }, defaults.E2ETimeout, defaults.E2EInterval).Should(Succeed()) } +// Apply applies a kubernetes manifest file func Apply(yamlPath string) { _, err := system.Run("kubectl", "apply", "-f", yamlPath) Expect(err).NotTo(HaveOccurred()) } +// SetContextNamespace sets the current kubernetes context namespace func SetContextNamespace(namespace string) { By("Set namespace to " + namespace) _, err := system.Run("kubectl", "config", "set-context", "--current", "--namespace="+namespace) @@ -71,7 +84,7 @@ func SetContextNamespace(namespace string) { } // PatchOperatorToUseServiceAccount sets `OP_SERVICE_ACCOUNT_TOKEN` env variable -var PatchOperatorToUseServiceAccount = WithOperatorRestart(func() { +var PatchOperatorToUseServiceAccount = withOperatorRestart(func() { By("patching the operator deployment with service account token") _, err := system.Run( "kubectl", "patch", "deployment", "onepassword-connect-operator", @@ -97,7 +110,8 @@ var PatchOperatorToUseServiceAccount = WithOperatorRestart(func() { Expect(err).NotTo(HaveOccurred()) }) -func WithOperatorRestart(operation func()) func() { +// withOperatorRestart is a helper function that restarts the operator deployment +func withOperatorRestart(operation func()) func() { return func() { operation() @@ -105,7 +119,7 @@ func WithOperatorRestart(operation func()) func() { "deployment/onepassword-connect-operator", "-n", "default", "--timeout=120s") Expect(err).NotTo(HaveOccurred()) - By("waiting for the operator pod to be 'Running'") + By("Waiting for the operator pod to be 'Running'") Eventually(func(g Gomega) { output, err := system.Run("kubectl", "get", "pods", "-l", "name=onepassword-connect-operator", diff --git a/pkg/testhelper/operator/operator.go b/pkg/testhelper/operator/operator.go index 41bd8cf..f3ff4d5 100644 --- a/pkg/testhelper/operator/operator.go +++ b/pkg/testhelper/operator/operator.go @@ -11,22 +11,24 @@ import ( "github.com/1Password/onepassword-operator/pkg/testhelper/system" ) +// BuildOperatorImage builds the Operator image using `make docker-build` func BuildOperatorImage() { - By("building the Operator image") + By("Building the Operator image") _, err := system.Run("make", "docker-build") ExpectWithOffset(1, err).NotTo(HaveOccurred()) } -// DeployOperator deploys the Onepassword Operator in the default namespace. +// DeployOperator deploys the Operator in the default namespace. // It waits for the operator pod to be in 'Running' state. // All the resources created using manifests in `config/` dir. // To make the operator use Connect or Service Accounts, patch `config/manager/manager.yaml` func DeployOperator() { - By("deploying the Operator") + By("Deploying the Operator") _, err := system.Run("make", "deploy") Expect(err).NotTo(HaveOccurred()) } +// WaitingForOperatorPod waits for the Operator pod to be in 'Running' state func WaitingForOperatorPod() { By("Waiting for the Operator pod to be 'Running'") Eventually(func(g Gomega) { @@ -38,6 +40,7 @@ func WaitingForOperatorPod() { }, 30*time.Second, 1*time.Second).Should(Succeed()) } +// WaitingForConnectPod waits for the Connect pod to be in 'Running' state func WaitingForConnectPod() { By("Waiting for the Connect pod to be 'Running'") Eventually(func(g Gomega) { diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index d2e57ef..11b54b9 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -19,26 +19,16 @@ const ( var _ = Describe("Onepassword Operator e2e", Ordered, func() { BeforeAll(func() { kube.SetContextNamespace("default") - operator.BuildOperatorImage() kind.LoadImageToKind(operatorImageName) - By("Create Connect 'op-credentials' credentials secret") kube.CreateOpCredentialsSecret() - - By("Checking Connect 'op-credentials' secret is created") kube.CheckSecretExists("op-credentials") - By("Create 'onepassword-token' secret") kube.CreateSecretFromEnvVar("OP_CONNECT_TOKEN", "onepassword-token") - - By("Checking 'onepassword-token' secret is created") kube.CheckSecretExists("onepassword-token") - By("Create 'onepassword-service-account-token' secret") kube.CreateSecretFromEnvVar("OP_SERVICE_ACCOUNT_TOKEN", "onepassword-service-account-token") - - By("Checking 'onepassword-service-account-token' secret is created") kube.CheckSecretExists("onepassword-service-account-token") operator.DeployOperator() @@ -63,6 +53,7 @@ var _ = Describe("Onepassword Operator e2e", Ordered, func() { }) }) +// runCommonTestCases contains test cases that are common to both Connect and Service Account authentication methods. func runCommonTestCases() { It("Should create secret from manifest file", func() { By("Creating secret") @@ -71,8 +62,6 @@ func runCommonTestCases() { yamlPath := filepath.Join(root, "test", "e2e", "manifests", "secret.yaml") kube.Apply(yamlPath) - - By("Checking for secret to be created") kube.CheckSecretExists("login") }) }