Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
samirtahir91 committed Nov 14, 2024
1 parent 48f8d1e commit f2cf3a9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
74 changes: 67 additions & 7 deletions api/v1/githubapp_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check failure on line 36 in api/v1/githubapp_webhook_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint and staticcheck

var `privateKey` is unused (unused)
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")
})
})

Expand Down
1 change: 0 additions & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -138,7 +138,7 @@ func InstallAndSetupVault() error {
if _, err := Run(cmd); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit f2cf3a9

Please sign in to comment.