mirror of
https://github.com/1Password/onepassword-operator.git
synced 2025-10-22 07:28:06 +00:00
Add comments on each test helper function
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
// LoadImageToKind loads a local docker image to the Kind cluster
|
// LoadImageToKind loads a local docker image to the Kind cluster
|
||||||
func LoadImageToKind(imageName string) {
|
func LoadImageToKind(imageName string) {
|
||||||
By("loading the operator image on Kind")
|
By("Loading the operator image on Kind")
|
||||||
clusterName := "kind"
|
clusterName := "kind"
|
||||||
if value, ok := os.LookupEnv("KIND_CLUSTER"); ok {
|
if value, ok := os.LookupEnv("KIND_CLUSTER"); ok {
|
||||||
clusterName = value
|
clusterName = value
|
||||||
|
@@ -15,7 +15,10 @@ import (
|
|||||||
"github.com/1Password/onepassword-operator/pkg/testhelper/system"
|
"github.com/1Password/onepassword-operator/pkg/testhelper/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// CreateSecretFromEnvVar creates a kubernetes secret from an environment variable
|
||||||
func CreateSecretFromEnvVar(envVar, secretName string) {
|
func CreateSecretFromEnvVar(envVar, secretName string) {
|
||||||
|
By("Creating '" + secretName + "' secret")
|
||||||
|
|
||||||
value, _ := os.LookupEnv(envVar)
|
value, _ := os.LookupEnv(envVar)
|
||||||
Expect(value).NotTo(BeEmpty())
|
Expect(value).NotTo(BeEmpty())
|
||||||
|
|
||||||
@@ -23,11 +26,15 @@ func CreateSecretFromEnvVar(envVar, secretName string) {
|
|||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateSecretFromFile creates a kubernetes secret from a file
|
||||||
func CreateSecretFromFile(fileName, secretName string) {
|
func CreateSecretFromFile(fileName, secretName string) {
|
||||||
|
By("Creating '" + secretName + "' secret from file")
|
||||||
_, err := system.Run("kubectl", "create", "secret", "generic", secretName, "--from-file="+fileName)
|
_, err := system.Run("kubectl", "create", "secret", "generic", secretName, "--from-file="+fileName)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
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() {
|
func CreateOpCredentialsSecret() {
|
||||||
rootDir, err := system.GetProjectRoot()
|
rootDir, err := system.GetProjectRoot()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
@@ -46,12 +53,16 @@ func CreateOpCredentialsSecret() {
|
|||||||
CreateSecretFromFile("op-session", "op-credentials")
|
CreateSecretFromFile("op-session", "op-credentials")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteSecret deletes a kubernetes secret
|
||||||
func DeleteSecret(name string) {
|
func DeleteSecret(name string) {
|
||||||
|
By("Deleting '" + name + "' secret")
|
||||||
_, err := system.Run("kubectl", "delete", "secret", name, "--ignore-not-found=true")
|
_, err := system.Run("kubectl", "delete", "secret", name, "--ignore-not-found=true")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CheckSecretExists checks if a kubernetes secret exists
|
||||||
func CheckSecretExists(name string) {
|
func CheckSecretExists(name string) {
|
||||||
|
By("Checking '" + name + "' secret exists")
|
||||||
Eventually(func(g Gomega) {
|
Eventually(func(g Gomega) {
|
||||||
output, err := system.Run("kubectl", "get", "secret", name, "-o", "jsonpath={.metadata.name}")
|
output, err := system.Run("kubectl", "get", "secret", name, "-o", "jsonpath={.metadata.name}")
|
||||||
g.Expect(err).NotTo(HaveOccurred())
|
g.Expect(err).NotTo(HaveOccurred())
|
||||||
@@ -59,11 +70,13 @@ func CheckSecretExists(name string) {
|
|||||||
}, defaults.E2ETimeout, defaults.E2EInterval).Should(Succeed())
|
}, defaults.E2ETimeout, defaults.E2EInterval).Should(Succeed())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply applies a kubernetes manifest file
|
||||||
func Apply(yamlPath string) {
|
func Apply(yamlPath string) {
|
||||||
_, err := system.Run("kubectl", "apply", "-f", yamlPath)
|
_, err := system.Run("kubectl", "apply", "-f", yamlPath)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetContextNamespace sets the current kubernetes context namespace
|
||||||
func SetContextNamespace(namespace string) {
|
func SetContextNamespace(namespace string) {
|
||||||
By("Set namespace to " + namespace)
|
By("Set namespace to " + namespace)
|
||||||
_, err := system.Run("kubectl", "config", "set-context", "--current", "--namespace="+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
|
// 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")
|
By("patching the operator deployment with service account token")
|
||||||
_, err := system.Run(
|
_, err := system.Run(
|
||||||
"kubectl", "patch", "deployment", "onepassword-connect-operator",
|
"kubectl", "patch", "deployment", "onepassword-connect-operator",
|
||||||
@@ -97,7 +110,8 @@ var PatchOperatorToUseServiceAccount = WithOperatorRestart(func() {
|
|||||||
Expect(err).NotTo(HaveOccurred())
|
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() {
|
return func() {
|
||||||
operation()
|
operation()
|
||||||
|
|
||||||
@@ -105,7 +119,7 @@ func WithOperatorRestart(operation func()) func() {
|
|||||||
"deployment/onepassword-connect-operator", "-n", "default", "--timeout=120s")
|
"deployment/onepassword-connect-operator", "-n", "default", "--timeout=120s")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
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) {
|
Eventually(func(g Gomega) {
|
||||||
output, err := system.Run("kubectl", "get", "pods",
|
output, err := system.Run("kubectl", "get", "pods",
|
||||||
"-l", "name=onepassword-connect-operator",
|
"-l", "name=onepassword-connect-operator",
|
||||||
|
@@ -11,22 +11,24 @@ import (
|
|||||||
"github.com/1Password/onepassword-operator/pkg/testhelper/system"
|
"github.com/1Password/onepassword-operator/pkg/testhelper/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// BuildOperatorImage builds the Operator image using `make docker-build`
|
||||||
func BuildOperatorImage() {
|
func BuildOperatorImage() {
|
||||||
By("building the Operator image")
|
By("Building the Operator image")
|
||||||
_, err := system.Run("make", "docker-build")
|
_, err := system.Run("make", "docker-build")
|
||||||
ExpectWithOffset(1, err).NotTo(HaveOccurred())
|
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.
|
// It waits for the operator pod to be in 'Running' state.
|
||||||
// All the resources created using manifests in `config/` dir.
|
// All the resources created using manifests in `config/` dir.
|
||||||
// To make the operator use Connect or Service Accounts, patch `config/manager/manager.yaml`
|
// To make the operator use Connect or Service Accounts, patch `config/manager/manager.yaml`
|
||||||
func DeployOperator() {
|
func DeployOperator() {
|
||||||
By("deploying the Operator")
|
By("Deploying the Operator")
|
||||||
_, err := system.Run("make", "deploy")
|
_, err := system.Run("make", "deploy")
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WaitingForOperatorPod waits for the Operator pod to be in 'Running' state
|
||||||
func WaitingForOperatorPod() {
|
func WaitingForOperatorPod() {
|
||||||
By("Waiting for the Operator pod to be 'Running'")
|
By("Waiting for the Operator pod to be 'Running'")
|
||||||
Eventually(func(g Gomega) {
|
Eventually(func(g Gomega) {
|
||||||
@@ -38,6 +40,7 @@ func WaitingForOperatorPod() {
|
|||||||
}, 30*time.Second, 1*time.Second).Should(Succeed())
|
}, 30*time.Second, 1*time.Second).Should(Succeed())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WaitingForConnectPod waits for the Connect pod to be in 'Running' state
|
||||||
func WaitingForConnectPod() {
|
func WaitingForConnectPod() {
|
||||||
By("Waiting for the Connect pod to be 'Running'")
|
By("Waiting for the Connect pod to be 'Running'")
|
||||||
Eventually(func(g Gomega) {
|
Eventually(func(g Gomega) {
|
||||||
|
@@ -19,26 +19,16 @@ const (
|
|||||||
var _ = Describe("Onepassword Operator e2e", Ordered, func() {
|
var _ = Describe("Onepassword Operator e2e", Ordered, func() {
|
||||||
BeforeAll(func() {
|
BeforeAll(func() {
|
||||||
kube.SetContextNamespace("default")
|
kube.SetContextNamespace("default")
|
||||||
|
|
||||||
operator.BuildOperatorImage()
|
operator.BuildOperatorImage()
|
||||||
kind.LoadImageToKind(operatorImageName)
|
kind.LoadImageToKind(operatorImageName)
|
||||||
|
|
||||||
By("Create Connect 'op-credentials' credentials secret")
|
|
||||||
kube.CreateOpCredentialsSecret()
|
kube.CreateOpCredentialsSecret()
|
||||||
|
|
||||||
By("Checking Connect 'op-credentials' secret is created")
|
|
||||||
kube.CheckSecretExists("op-credentials")
|
kube.CheckSecretExists("op-credentials")
|
||||||
|
|
||||||
By("Create 'onepassword-token' secret")
|
|
||||||
kube.CreateSecretFromEnvVar("OP_CONNECT_TOKEN", "onepassword-token")
|
kube.CreateSecretFromEnvVar("OP_CONNECT_TOKEN", "onepassword-token")
|
||||||
|
|
||||||
By("Checking 'onepassword-token' secret is created")
|
|
||||||
kube.CheckSecretExists("onepassword-token")
|
kube.CheckSecretExists("onepassword-token")
|
||||||
|
|
||||||
By("Create 'onepassword-service-account-token' secret")
|
|
||||||
kube.CreateSecretFromEnvVar("OP_SERVICE_ACCOUNT_TOKEN", "onepassword-service-account-token")
|
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")
|
kube.CheckSecretExists("onepassword-service-account-token")
|
||||||
|
|
||||||
operator.DeployOperator()
|
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() {
|
func runCommonTestCases() {
|
||||||
It("Should create secret from manifest file", func() {
|
It("Should create secret from manifest file", func() {
|
||||||
By("Creating secret")
|
By("Creating secret")
|
||||||
@@ -71,8 +62,6 @@ func runCommonTestCases() {
|
|||||||
|
|
||||||
yamlPath := filepath.Join(root, "test", "e2e", "manifests", "secret.yaml")
|
yamlPath := filepath.Join(root, "test", "e2e", "manifests", "secret.yaml")
|
||||||
kube.Apply(yamlPath)
|
kube.Apply(yamlPath)
|
||||||
|
|
||||||
By("Checking for secret to be created")
|
|
||||||
kube.CheckSecretExists("login")
|
kube.CheckSecretExists("login")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user