Skip to content

Commit

Permalink
Create utils testing for OpenShift validator
Browse files Browse the repository at this point in the history
Remove OpenShift validation from Webhook, and add a test for validating it in Utils package
  • Loading branch information
razo7 committed Jan 24, 2024
1 parent 472dae3 commit 9178a33
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ test: test-no-verify verify-unchanged ## Generate and format code, run tests, ge

.PHONY: test-no-verify
test-no-verify: manifests generate go-verify test-imports fmt vet envtest ginkgo ## Generate and format code, and run tests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path --bin-dir $(LOCALBIN))" $(GINKGO) -r --keep-going --require-suite --vv ./api/... ./controllers/... --coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path --bin-dir $(LOCALBIN))" $(GINKGO) -r --keep-going --require-suite --vv ./api/... ./pkg/... ./controllers/... --coverprofile cover.out

.PHONY: bundle-run
bundle-run: operator-sdk ## Run bundle image. Default NS is "openshift-workload-availability", redefine OPERATOR_NAMESPACE to override it.
Expand Down
6 changes: 0 additions & 6 deletions api/v1beta1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsServer "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/medik8s/node-maintenance-operator/pkg/utils"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
Expand Down Expand Up @@ -101,10 +99,6 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

openshiftCheck, err := utils.NewOpenshiftValidator(cfg)
Expect(err).NotTo(HaveOccurred(), "failed to check if we run on Openshift")
Expect(openshiftCheck.IsOpenshiftSupported()).To(BeFalse())

// start webhook server using Manager
webhookInstallOptions := &testEnv.WebhookInstallOptions
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Expand Down
40 changes: 40 additions & 0 deletions pkg/utils/utils_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package utils

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/zap/zapcore"

"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
var cfg *rest.Config

func TestEvents(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Utils Suite")
}

var _ = BeforeSuite(func() {
opts := zap.Options{
Development: true,
TimeEncoder: zapcore.RFC3339NanoTimeEncoder,
}
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseFlagOptions(&opts)))

var err error
testEnv := &envtest.Environment{}
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

// call start or refactor when moving to "normal" testEnv test
})

var _ = AfterSuite(func() {
// call stop or refactor when moving to "normal" testEnv test
})
2 changes: 1 addition & 1 deletion pkg/utils/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ func (v *OpenshiftValidator) validateIsOpenshift(config *rest.Config) error {
}
}
return nil
}
}
16 changes: 16 additions & 0 deletions pkg/utils/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Check OpenShift Existance Validation", func() {
When("Validator doesn't aware of OpenShift", func() {
It("should not support OpenShift", func() {
openshiftCheck, err := NewOpenshiftValidator(cfg)
Expect(err).NotTo(HaveOccurred(), "failed to check if we run on Openshift")
Expect(openshiftCheck.IsOpenshiftSupported()).To(BeFalse())
})
})
})

0 comments on commit 9178a33

Please sign in to comment.