From 831135ff3becac66faca0aac326c32af1e29ffba Mon Sep 17 00:00:00 2001 From: samir-tahir Date: Thu, 14 Nov 2024 19:33:05 +0000 Subject: [PATCH] test --- .github/workflows/tests.yaml | 11 ++++++++++- Makefile | 6 +++++- api/v1/githubapp_webhook.go | 1 + api/v1/githubapp_webhook_test.go | 1 - internal/controller/githubapp_controller.go | 4 +++- internal/controller/githubapp_controller_test.go | 1 + internal/controller/suite_test.go | 4 +++- internal/controller/test_helpers/test_helpers.go | 1 + internal/controller/vault.go | 1 + test/utils/utils.go | 1 + 10 files changed, 26 insertions(+), 5 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 6acec69..758833a 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -45,7 +45,7 @@ jobs: cd scripts chmod +x install_and_setup_vault_k8s.sh ./install_and_setup_vault_k8s.sh - - name: Perform the test + - name: Perform the controller integration tests if: matrix.os != 'macos-latest' run: | export "GITHUB_PRIVATE_KEY=${{ secrets.GH_TEST_APP_PK }}" @@ -54,12 +54,21 @@ jobs: export "VAULT_ADDR=http://localhost:8200" export "VAULT_ROLE_AUDIENCE=githubapp" export "VAULT_ROLE=githubapp" + export ENABLE_WEBHOOKS=false # Run vault port forward in background kubectl port-forward vault-0 8200:8200 & # Run tests USE_EXISTING_CLUSTER=true make test + - name: Perform the webhook tests + if: matrix.os != 'macos-latest' + run: | + export "GH_APP_ID=${{ secrets.GH_APP_ID }}" + export "GH_INSTALL_ID=${{ secrets.GH_INSTALL_ID }}" + + # Run webhook tests + make test - name: Report failure uses: nashmaniac/create-issue-action@v1.2 # Only report failures of pushes (PRs have are visible through the Checks section) to the default branch diff --git a/Makefile b/Makefile index 6fbc506..82f988a 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,11 @@ 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|utils|cmd|test_helpers|vault') -v -ginkgo.v -coverprofile cover.out + 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') -v -ginkgo.v -coverprofile cover.out + +.PHONY: test-webhooks +test-webhooks: manifests generate fmt vet envtest ## Run tests. + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./api/v1/ -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.go b/api/v1/githubapp_webhook.go index cdd463c..6a9a516 100644 --- a/api/v1/githubapp_webhook.go +++ b/api/v1/githubapp_webhook.go @@ -18,6 +18,7 @@ package v1 import ( "fmt" + "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" diff --git a/api/v1/githubapp_webhook_test.go b/api/v1/githubapp_webhook_test.go index df06b83..1c04285 100644 --- a/api/v1/githubapp_webhook_test.go +++ b/api/v1/githubapp_webhook_test.go @@ -33,7 +33,6 @@ const ( ) var ( - privateKey = os.Getenv("GITHUB_PRIVATE_KEY") appId int installId int acessTokenSecretName string diff --git a/internal/controller/githubapp_controller.go b/internal/controller/githubapp_controller.go index 3a8be6f..898a868 100644 --- a/internal/controller/githubapp_controller.go +++ b/internal/controller/githubapp_controller.go @@ -20,7 +20,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/golang-jwt/jwt/v4" "math/rand" "net/http" "os" @@ -29,7 +28,10 @@ import ( "sync" "time" + "github.com/golang-jwt/jwt/v4" + githubappv1 "github-app-operator/api/v1" + vault "github.com/hashicorp/vault/api" // vault client appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" diff --git a/internal/controller/githubapp_controller_test.go b/internal/controller/githubapp_controller_test.go index 9713436..6dd331f 100644 --- a/internal/controller/githubapp_controller_test.go +++ b/internal/controller/githubapp_controller_test.go @@ -29,6 +29,7 @@ import ( test_helpers "github-app-operator/internal/controller/test_helpers" githubappv1 "github-app-operator/api/v1" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 44de1c9..4f4a898 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -21,6 +21,7 @@ import ( "fmt" "net/http" // http client "os" + //"os/exec" "path/filepath" "runtime" @@ -37,6 +38,7 @@ import ( . "github.com/onsi/gomega" test_helpers "github-app-operator/internal/controller/test_helpers" + "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" @@ -155,7 +157,7 @@ var _ = BeforeSuite(func() { // Verify if reconciliation was successful Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("Token request failed: %v", err)) } else { - // Set a dummy token just to satisfy the SetupWithManager fucntion + // Set a dummy token just to satisfy the SetupWithManager function // which will read the token and get the service account and and namespace. // This token is for 'default' service account in the 'namespace0' namespace token = `eyJhbGciOiJSUzI1NiIsImtpZCI6Ik5ieTJyVUk2ZzlQZ0k0anNGclRvTkJDM0FsUjJjLUJDVUhzNU9mVG9lcEUifQ. diff --git a/internal/controller/test_helpers/test_helpers.go b/internal/controller/test_helpers/test_helpers.go index 67462f2..51c4203 100644 --- a/internal/controller/test_helpers/test_helpers.go +++ b/internal/controller/test_helpers/test_helpers.go @@ -13,6 +13,7 @@ import ( gomega "github.com/onsi/gomega" githubappv1 "github-app-operator/api/v1" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" diff --git a/internal/controller/vault.go b/internal/controller/vault.go index 2e518bf..61990d4 100644 --- a/internal/controller/vault.go +++ b/internal/controller/vault.go @@ -20,6 +20,7 @@ import ( "context" "encoding/base64" "fmt" + "k8s.io/utils/ptr" auth "github.com/hashicorp/vault/api/auth/kubernetes" // vault k8s auth diff --git a/test/utils/utils.go b/test/utils/utils.go index 6e800f5..383320b 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -22,6 +22,7 @@ import ( "os/exec" "strings" "time" + //lint:ignore ST1001 this is boilerplate code from kubebuilder . "github.com/onsi/ginkgo/v2" //nolint:golint,revive )