From b62f9b9fb09a97a2a68a111dfb4a1e7ab96f27ba Mon Sep 17 00:00:00 2001 From: Chandan Kumar Date: Wed, 27 Nov 2024 17:02:03 +0530 Subject: [PATCH] Fix functional tests and pre-commit Note: It also bump timeout in order to make sure csv get installed. Signed-off-by: Chandan Kumar --- Makefile | 2 +- api/v1beta1/watcher_webhook.go | 2 +- api/v1beta1/watcherapi_webhook.go | 2 +- api/v1beta1/watcherapplier_webhook.go | 2 +- api/v1beta1/watcherdecisionengine_webhook.go | 2 +- api/v1beta1/webhook_suite_test.go | 141 ------------------- hack/clean_local_webhook.sh | 0 hack/run_with_local_webhook.sh | 0 main.go | 4 +- tests/functional/suite_test.go | 27 ++-- 10 files changed, 22 insertions(+), 160 deletions(-) delete mode 100644 api/v1beta1/webhook_suite_test.go mode change 100644 => 100755 hack/clean_local_webhook.sh mode change 100644 => 100755 hack/run_with_local_webhook.sh diff --git a/Makefile b/Makefile index a77f445..e266891 100644 --- a/Makefile +++ b/Makefile @@ -381,7 +381,7 @@ watcher: export CATALOG_IMG=${CATALOG_IMAGE} watcher: ## Install watcher operator via olm bash ci/olm.sh oc apply -f ci/olm.yaml - timeout 300s bash -c "while ! (oc get csv -n openstack-operators -l operators.coreos.com/watcher-operator.openstack-operators -o jsonpath='{.items[*].status.phase}' | grep Succeeded); do sleep 1; done" + timeout 600s bash -c "while ! (oc get csv -n openstack-operators -l operators.coreos.com/watcher-operator.openstack-operators -o jsonpath='{.items[*].status.phase}' | grep Succeeded); do sleep 1; done" .PHONY: watcher_deploy watcher_deploy: ## Deploy watcher service diff --git a/api/v1beta1/watcher_webhook.go b/api/v1beta1/watcher_webhook.go index 1c9c997..0f18f3d 100644 --- a/api/v1beta1/watcher_webhook.go +++ b/api/v1beta1/watcher_webhook.go @@ -60,7 +60,7 @@ func (r *Watcher) ValidateCreate() (admission.Warnings, error) { } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *Watcher) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +func (r *Watcher) ValidateUpdate(runtime.Object) (admission.Warnings, error) { watcherlog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. diff --git a/api/v1beta1/watcherapi_webhook.go b/api/v1beta1/watcherapi_webhook.go index 7e1c774..772dddb 100644 --- a/api/v1beta1/watcherapi_webhook.go +++ b/api/v1beta1/watcherapi_webhook.go @@ -60,7 +60,7 @@ func (r *WatcherAPI) ValidateCreate() (admission.Warnings, error) { } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *WatcherAPI) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +func (r *WatcherAPI) ValidateUpdate(runtime.Object) (admission.Warnings, error) { watcherapilog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. diff --git a/api/v1beta1/watcherapplier_webhook.go b/api/v1beta1/watcherapplier_webhook.go index 1de0662..70c79fb 100644 --- a/api/v1beta1/watcherapplier_webhook.go +++ b/api/v1beta1/watcherapplier_webhook.go @@ -60,7 +60,7 @@ func (r *WatcherApplier) ValidateCreate() (admission.Warnings, error) { } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *WatcherApplier) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +func (r *WatcherApplier) ValidateUpdate(runtime.Object) (admission.Warnings, error) { watcherapplierlog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. diff --git a/api/v1beta1/watcherdecisionengine_webhook.go b/api/v1beta1/watcherdecisionengine_webhook.go index 21f8cca..825988e 100644 --- a/api/v1beta1/watcherdecisionengine_webhook.go +++ b/api/v1beta1/watcherdecisionengine_webhook.go @@ -60,7 +60,7 @@ func (r *WatcherDecisionEngine) ValidateCreate() (admission.Warnings, error) { } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type -func (r *WatcherDecisionEngine) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { +func (r *WatcherDecisionEngine) ValidateUpdate(runtime.Object) (admission.Warnings, error) { watcherdecisionenginelog.Info("validate update", "name", r.Name) // TODO(user): fill in your validation logic upon object update. diff --git a/api/v1beta1/webhook_suite_test.go b/api/v1beta1/webhook_suite_test.go deleted file mode 100644 index 8ac63e2..0000000 --- a/api/v1beta1/webhook_suite_test.go +++ /dev/null @@ -1,141 +0,0 @@ -/* -Copyright 2024. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1beta1 - -import ( - "context" - "crypto/tls" - "fmt" - "net" - "path/filepath" - "testing" - "time" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - admissionv1beta1 "k8s.io/api/admission/v1beta1" - //+kubebuilder:scaffold:imports - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/rest" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/envtest" - logf "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/log/zap" -) - -// These tests use Ginkgo (BDD-style Go testing framework). Refer to -// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. - -var cfg *rest.Config -var k8sClient client.Client -var testEnv *envtest.Environment -var ctx context.Context -var cancel context.CancelFunc - -func TestAPIs(t *testing.T) { - RegisterFailHandler(Fail) - - RunSpecs(t, "Webhook Suite") -} - -var _ = BeforeSuite(func() { - logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) - - ctx, cancel = context.WithCancel(context.TODO()) - - By("bootstrapping test environment") - testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, - ErrorIfCRDPathMissing: false, - WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "config", "webhook")}, - }, - } - - var err error - // cfg is defined in this file globally. - cfg, err = testEnv.Start() - Expect(err).NotTo(HaveOccurred()) - Expect(cfg).NotTo(BeNil()) - - scheme := runtime.NewScheme() - err = AddToScheme(scheme) - Expect(err).NotTo(HaveOccurred()) - - err = admissionv1beta1.AddToScheme(scheme) - Expect(err).NotTo(HaveOccurred()) - - //+kubebuilder:scaffold:scheme - - k8sClient, err = client.New(cfg, client.Options{Scheme: scheme}) - Expect(err).NotTo(HaveOccurred()) - Expect(k8sClient).NotTo(BeNil()) - - // start webhook server using Manager - webhookInstallOptions := &testEnv.WebhookInstallOptions - mgr, err := ctrl.NewManager(cfg, ctrl.Options{ - Scheme: scheme, - Host: webhookInstallOptions.LocalServingHost, - Port: webhookInstallOptions.LocalServingPort, - CertDir: webhookInstallOptions.LocalServingCertDir, - LeaderElection: false, - MetricsBindAddress: "0", - }) - Expect(err).NotTo(HaveOccurred()) - - err = (&Watcher{}).SetupWebhookWithManager(mgr) - Expect(err).NotTo(HaveOccurred()) - - err = (&WatcherAPI{}).SetupWebhookWithManager(mgr) - Expect(err).NotTo(HaveOccurred()) - - err = (&WatcherDecisionEngine{}).SetupWebhookWithManager(mgr) - Expect(err).NotTo(HaveOccurred()) - - err = (&WatcherApplier{}).SetupWebhookWithManager(mgr) - Expect(err).NotTo(HaveOccurred()) - - //+kubebuilder:scaffold:webhook - - go func() { - defer GinkgoRecover() - err = mgr.Start(ctx) - Expect(err).NotTo(HaveOccurred()) - }() - - // wait for the webhook server to get ready - dialer := &net.Dialer{Timeout: time.Second} - addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort) - Eventually(func() error { - conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true}) - if err != nil { - return err - } - conn.Close() - return nil - }).Should(Succeed()) - -}) - -var _ = AfterSuite(func() { - cancel() - By("tearing down the test environment") - err := testEnv.Stop() - Expect(err).NotTo(HaveOccurred()) -}) diff --git a/hack/clean_local_webhook.sh b/hack/clean_local_webhook.sh old mode 100644 new mode 100755 diff --git a/hack/run_with_local_webhook.sh b/hack/run_with_local_webhook.sh old mode 100644 new mode 100755 diff --git a/main.go b/main.go index c89670c..73bd4ac 100644 --- a/main.go +++ b/main.go @@ -164,7 +164,7 @@ func main() { setupLog.Error(err, "unable to create webhook", "webhook", "WatcherDecisionEngine") os.Exit(1) } - } + } if err := mgr.AddHealthzCheck("healthz", checker); err != nil { setupLog.Error(err, "unable to set up health check") os.Exit(1) @@ -179,4 +179,4 @@ func main() { setupLog.Error(err, "problem running manager") os.Exit(1) } -} \ No newline at end of file +} diff --git a/tests/functional/suite_test.go b/tests/functional/suite_test.go index de7a2b3..8b1ce83 100644 --- a/tests/functional/suite_test.go +++ b/tests/functional/suite_test.go @@ -2,6 +2,9 @@ package functional import ( "context" + "crypto/tls" + "fmt" + "net" "path/filepath" "testing" "time" @@ -148,8 +151,8 @@ var _ = BeforeSuite(func() { watcherv1.SetupDefaults() - //err = (&watcherv1.Watcher{}).SetupWebhookWithManager(k8sManager) - //Expect(err).NotTo(HaveOccurred()) + err = (&watcherv1.Watcher{}).SetupWebhookWithManager(k8sManager) + Expect(err).NotTo(HaveOccurred()) go func() { defer GinkgoRecover() @@ -158,16 +161,16 @@ var _ = BeforeSuite(func() { }() // wait for the webhook server to get ready - //dialer := &net.Dialer{Timeout: 10 * time.Second} - //addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort) - //Eventually(func() error { - // conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true}) - // if err != nil { - // return err - // } - // conn.Close() - // return nil - //}).Should(Succeed()) + dialer := &net.Dialer{Timeout: 10 * time.Second} + addrPort := fmt.Sprintf("%s:%d", webhookInstallOptions.LocalServingHost, webhookInstallOptions.LocalServingPort) + Eventually(func() error { + conn, err := tls.DialWithDialer(dialer, "tcp", addrPort, &tls.Config{InsecureSkipVerify: true}) + if err != nil { + return err + } + conn.Close() + return nil + }).Should(Succeed()) }) var _ = AfterSuite(func() {