diff --git a/Makefile b/Makefile index 247c6f9..6fbc506 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ vet: ## Run go vet against code. .PHONY: test test: manifests generate fmt vet envtest ## Run tests. - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v -E '/e2e|v1|utils|cmd|test_helpers|vault') -coverprofile cover.out + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v -E '/e2e|utils|cmd|test_helpers|vault') -v -ginkgo.v -coverprofile cover.out # Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors. .PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up. diff --git a/api/v1/githubapp_webhook_test.go b/api/v1/githubapp_webhook_test.go index 43c391d..df06b83 100644 --- a/api/v1/githubapp_webhook_test.go +++ b/api/v1/githubapp_webhook_test.go @@ -17,22 +17,82 @@ limitations under the License. package v1 import ( + "fmt" + "os" + "strconv" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -var _ = Describe("GithubApp Webhook", func() { +const ( + // github app private key secret + privateKeySecret = "gh-app-key-test" +) - Context("When creating GithubApp under Validating Webhook", func() { - It("Should deny if a required field is empty", func() { +var ( + privateKey = os.Getenv("GITHUB_PRIVATE_KEY") + appId int + installId int + acessTokenSecretName string +) - // TODO(user): Add your logic here +// Function to initialise vars for github app +func init() { + var err error + appId, err = strconv.Atoi(os.Getenv("GH_APP_ID")) + if err != nil { + panic(err) + } + installId, err = strconv.Atoi(os.Getenv("GH_INSTALL_ID")) + if err != nil { + panic(err) + } + acessTokenSecretName = fmt.Sprintf("github-app-access-token-%s", strconv.Itoa(appId)) +} - }) +var _ = Describe("GithubApp Webhook", func() { + var ( + obj *GithubApp + validator GithubApp + rolloutDeploymentSpec *RolloutDeploymentSpec + vaultPrivateKeySpec *VaultPrivateKeySpec + gcpPrivateKeySecret string + ) + BeforeEach(func() { + obj = &GithubApp{ + ObjectMeta: metav1.ObjectMeta{ + Name: "gh-app-webhook-test", + Namespace: "default", + }, + Spec: GithubAppSpec{ + AppId: appId, + InstallId: installId, + PrivateKeySecret: privateKeySecret, + RolloutDeployment: rolloutDeploymentSpec, + VaultPrivateKey: vaultPrivateKeySpec, + AccessTokenSecret: acessTokenSecretName, + GcpPrivateKeySecret: gcpPrivateKeySecret, + }, + } + + validator = GithubApp{} - It("Should admit if all required fields are provided", func() { + Expect(obj).NotTo(BeNil(), "Expected obj to be initialized") + }) - // TODO(user): Add your logic here + AfterEach(func() { + // TODO (user): Add any teardown logic common to all tests + }) + Context("When creating GithubApp under Validating Webhook", func() { + It("Should deny creation if more than one of googlePrivateKeySecret, privateKeySecret, or vaultPrivateKey is specified", func() { + obj.Spec.GcpPrivateKeySecret = "this-should-fail" + Expect(validator.ValidateCreate()).Error().To( + MatchError(ContainSubstring("exactly one of googlePrivateKeySecret, privateKeySecret, or vaultPrivateKey must be specified")), + "Private key source validation to fail for more than one option") }) }) diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go index 0534ffd..2023f01 100644 --- a/test/e2e/e2e_test.go +++ b/test/e2e/e2e_test.go @@ -80,7 +80,6 @@ var _ = Describe("controller", Ordered, func() { cmd := exec.Command("/bin/sh", "-c", "eval $(minikube docker-env)") _, err = utils.Run(cmd) ExpectWithOffset(1, err).NotTo(HaveOccurred()) - By("building the manager(Operator) image") cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectimage)) diff --git a/test/utils/utils.go b/test/utils/utils.go index e9cb959..6e800f5 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -108,15 +108,15 @@ func InstallCertManager() error { retryInterval := time.Second * 5 retryTimeout := time.Minute * 2 start := time.Now() - + for time.Since(start) < retryTimeout { cmd := exec.Command("kubectl", "get", "mutatingwebhookconfiguration", "cert-manager-webhook", "-o", "jsonpath={.webhooks[*].clientConfig.caBundle}") output, err := Run(cmd) - if err == nil && strings.TrimSpace(string(output)) != ""{ + if err == nil && strings.TrimSpace(string(output)) != "" { return nil } - + time.Sleep(retryInterval) } return fmt.Errorf("failed to get caBundle from MutatingWebhookConfiguration") @@ -138,7 +138,7 @@ func InstallAndSetupVault() error { if _, err := Run(cmd); err != nil { return err } - + return nil }