diff --git a/Makefile b/Makefile index 4ba0415..07e8fc6 100644 --- a/Makefile +++ b/Makefile @@ -112,6 +112,7 @@ tidy: ## Run go mod tidy on every mod file in the repo .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 ./... -coverprofile cover.out +# KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" $(GINKGO) --cover --coverpkg=../../pkg/keystone,../../controllers,../../api/v1beta1 --coverprofile cover.out --covermode=atomic ${PROC_CMD} $(GINKGO_ARGS) ./tests/... ##@ Build diff --git a/api/go.mod b/api/go.mod index 2d11e11..a11f603 100644 --- a/api/go.mod +++ b/api/go.mod @@ -3,8 +3,8 @@ module github.com/openstack-k8s-operators/barbican-operator/api go 1.19 require ( - github.com/onsi/ginkgo/v2 v2.12.0 - github.com/onsi/gomega v1.27.10 + github.com/onsi/ginkgo/v2 v2.12.1 + github.com/onsi/gomega v1.28.0 github.com/openstack-k8s-operators/lib-common/modules/common v0.3.0 k8s.io/api v0.26.9 k8s.io/apimachinery v0.27.1 diff --git a/api/go.sum b/api/go.sum index 529f484..84d0c7a 100644 --- a/api/go.sum +++ b/api/go.sum @@ -224,10 +224,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/onsi/ginkgo/v2 v2.12.0 h1:UIVDowFPwpg6yMUpPjGkYvf06K3RAiJXUhCxEwQVHRI= -github.com/onsi/ginkgo/v2 v2.12.0/go.mod h1:ZNEzXISYlqpb8S36iN71ifqLi3vVD1rVJGvWRCJOUpQ= -github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= -github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= +github.com/onsi/ginkgo/v2 v2.12.1 h1:uHNEO1RP2SpuZApSkel9nEh1/Mu+hmQe7Q+Pepg5OYA= +github.com/onsi/gomega v1.28.0 h1:i2rg/p9n/UqIDAMFUJ6qIUUMcsqOuUHgbpbu235Vr1c= github.com/openstack-k8s-operators/lib-common/modules/common v0.3.0 h1:me/Wta3oXMKYaXWFfWdiNcBkZm103o1nUEou/S5r4sA= github.com/openstack-k8s-operators/lib-common/modules/common v0.3.0/go.mod h1:Ge7Yf6AUmjEvJK9AIW2bT5udLzBIcK59b1QxqymncaQ= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/go.mod b/go.mod index a23009b..bcd9a25 100644 --- a/go.mod +++ b/go.mod @@ -4,15 +4,18 @@ go 1.19 require ( github.com/go-logr/logr v1.2.4 + github.com/google/uuid v1.3.1 github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 - github.com/onsi/ginkgo/v2 v2.12.0 - github.com/onsi/gomega v1.27.10 + github.com/onsi/ginkgo/v2 v2.12.1 + github.com/onsi/gomega v1.28.0 github.com/openstack-k8s-operators/barbican-operator/api v0.0.0-00000000000000-000000000000 - github.com/openstack-k8s-operators/infra-operator/apis v0.0.0-20230420195930-f71e24e80596 - github.com/openstack-k8s-operators/keystone-operator/api v0.0.0-20230719150813-d341e7f8faa0 + github.com/openstack-k8s-operators/infra-operator/apis v0.3.0 + github.com/openstack-k8s-operators/keystone-operator/api v0.3.0 github.com/openstack-k8s-operators/lib-common/modules/common v0.3.0 github.com/openstack-k8s-operators/lib-common/modules/storage v0.0.0-20230427065458-ec1b923df88c + github.com/openstack-k8s-operators/lib-common/modules/test v0.1.2-0.20231001084618-12369665b166 github.com/openstack-k8s-operators/mariadb-operator/api v0.3.0 + go.uber.org/zap v1.26.0 k8s.io/api v0.26.9 k8s.io/apimachinery v0.27.1 k8s.io/client-go v0.26.9 @@ -38,8 +41,7 @@ require ( github.com/google/go-cmp v0.5.9 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect - github.com/google/uuid v1.3.1 // indirect - github.com/gophercloud/gophercloud v1.2.0 // indirect + github.com/gophercloud/gophercloud v1.7.0 // indirect github.com/imdario/mergo v0.3.16 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect @@ -49,7 +51,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/openshift/api v3.9.0+incompatible // indirect - github.com/openstack-k8s-operators/lib-common/modules/openstack v0.0.0-20230522113906-6f4206cbf317 // indirect + github.com/openstack-k8s-operators/lib-common/modules/openstack v0.1.1-0.20231001084618-12369665b166 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.14.0 // indirect github.com/prometheus/client_model v0.3.0 // indirect @@ -57,7 +59,7 @@ require ( github.com/prometheus/procfs v0.8.0 // indirect github.com/spf13/pflag v1.0.5 // indirect go.uber.org/multierr v1.10.0 // indirect - go.uber.org/zap v1.26.0 // indirect + golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.15.0 // indirect golang.org/x/oauth2 v0.4.0 // indirect golang.org/x/sys v0.13.0 // indirect diff --git a/go.sum b/go.sum index 38399c0..7ca0890 100644 --- a/go.sum +++ b/go.sum @@ -175,8 +175,8 @@ github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= -github.com/gophercloud/gophercloud v1.2.0 h1:1oXyj4g54KBg/kFtCdMM6jtxSzeIyg8wv4z1HoGPp1E= -github.com/gophercloud/gophercloud v1.2.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM= +github.com/gophercloud/gophercloud v1.7.0 h1:fyJGKh0LBvIZKLvBWvQdIgkaV5yTM3Jh9EYUh+UNCAs= +github.com/gophercloud/gophercloud v1.7.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -228,22 +228,23 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/onsi/ginkgo/v2 v2.12.0 h1:UIVDowFPwpg6yMUpPjGkYvf06K3RAiJXUhCxEwQVHRI= -github.com/onsi/ginkgo/v2 v2.12.0/go.mod h1:ZNEzXISYlqpb8S36iN71ifqLi3vVD1rVJGvWRCJOUpQ= -github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= -github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= +github.com/onsi/ginkgo/v2 v2.12.1 h1:uHNEO1RP2SpuZApSkel9nEh1/Mu+hmQe7Q+Pepg5OYA= +github.com/onsi/ginkgo/v2 v2.12.1/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o= +github.com/onsi/gomega v1.28.0 h1:i2rg/p9n/UqIDAMFUJ6qIUUMcsqOuUHgbpbu235Vr1c= +github.com/onsi/gomega v1.28.0/go.mod h1:A1H2JE76sI14WIP57LMKj7FVfCHx3g3BcZVjJG8bjX8= github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7 h1:rncLxJBpFGqBztyxCMwNRnMjhhIDOWHJowi6q8G6koI= github.com/openshift/api v0.0.0-20230414143018-3367bc7e6ac7/go.mod h1:ctXNyWanKEjGj8sss1KjjHQ3ENKFm33FFnS5BKaIPh4= -github.com/openstack-k8s-operators/infra-operator/apis v0.0.0-20230420195930-f71e24e80596 h1:LyKtMxcRpHbuBa1AcO5gUy5o+Nzpfd/wOS0t1ycn0HY= -github.com/openstack-k8s-operators/infra-operator/apis v0.0.0-20230420195930-f71e24e80596/go.mod h1:L6qvm+1R4HfEJO3Dn8ftTnDqP/QpSQzpNzd3+mapR/Y= -github.com/openstack-k8s-operators/keystone-operator/api v0.0.0-20230719150813-d341e7f8faa0 h1:AKacXXWbdDqPS6t4kDSKjGskdGd7lkQgJG4IxIq56Tg= -github.com/openstack-k8s-operators/keystone-operator/api v0.0.0-20230719150813-d341e7f8faa0/go.mod h1:BigFVS4i9JQOoAJXvCjYuji7zogCvvH3xUfIIyovHdI= +github.com/openstack-k8s-operators/infra-operator/apis v0.3.0 h1:omqNm2mG5YOXdNLuUs4fNCvi/2B13njLXfbS2Z4GNUE= +github.com/openstack-k8s-operators/infra-operator/apis v0.3.0/go.mod h1:zqFs5MrBKeaE4HQroUgMWwIkBwmmcygg6sghcidSdCA= +github.com/openstack-k8s-operators/keystone-operator/api v0.3.0 h1:CmqfZaaylSR71dG682Docv+cXpyWylXompt4/lPtFeY= +github.com/openstack-k8s-operators/keystone-operator/api v0.3.0/go.mod h1:5v0ngxNmFp8QsINo2bufx1/COJc0q6jm3FMhP3xIAWE= github.com/openstack-k8s-operators/lib-common/modules/common v0.3.0 h1:me/Wta3oXMKYaXWFfWdiNcBkZm103o1nUEou/S5r4sA= github.com/openstack-k8s-operators/lib-common/modules/common v0.3.0/go.mod h1:Ge7Yf6AUmjEvJK9AIW2bT5udLzBIcK59b1QxqymncaQ= -github.com/openstack-k8s-operators/lib-common/modules/openstack v0.0.0-20230522113906-6f4206cbf317 h1:1gQSTk3ereXLrhb9BhqbYn5kKIemplc8tyzvonXyKGk= -github.com/openstack-k8s-operators/lib-common/modules/openstack v0.0.0-20230522113906-6f4206cbf317/go.mod h1:doFbVg0WhS++gy17cbP9/BrCySjCpuqKx7TbvmKPChY= +github.com/openstack-k8s-operators/lib-common/modules/openstack v0.1.1-0.20231001084618-12369665b166 h1:zOnRGMdgq2XvOCCtF1lY4tFhKx3jXrcrtOiZZ1PR6M8= +github.com/openstack-k8s-operators/lib-common/modules/openstack v0.1.1-0.20231001084618-12369665b166/go.mod h1:LOXXvTQCwhOBNd+0FTlgllpa3wqlkI6Vf3Q5QVRVPlw= github.com/openstack-k8s-operators/lib-common/modules/storage v0.0.0-20230427065458-ec1b923df88c h1:TQ83pWR8Sm+ZtgIaMWxcYcwZLN/fJppPDXm5S+sC81c= github.com/openstack-k8s-operators/lib-common/modules/storage v0.0.0-20230427065458-ec1b923df88c/go.mod h1:jQptsKvwYn02U0k6jt24/4eX0sdIehwNs0fW1R/V/kw= +github.com/openstack-k8s-operators/lib-common/modules/test v0.1.2-0.20231001084618-12369665b166 h1:lh3WHM+3DcPlXK4I3QWHmvV+cPCy+dmiMdfImHF/Nqc= github.com/openstack-k8s-operators/mariadb-operator/api v0.3.0 h1:FB0xB6whYM6W4XIncYo2mPiOJWkFsIOWtCT+UOtvOaQ= github.com/openstack-k8s-operators/mariadb-operator/api v0.3.0/go.mod h1:xhiz5wFdKWwVM7BF/VYon4TT3NuUPXp/Pyn2hWcp0CE= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/tests/functional/barbican_controller_test.go b/tests/functional/barbican_controller_test.go new file mode 100644 index 0000000..5efa4d6 --- /dev/null +++ b/tests/functional/barbican_controller_test.go @@ -0,0 +1,61 @@ +package functional_test + +import ( + //"fmt" + "os" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + //. "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers" + //corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" + //"k8s.io/utils/ptr" + //memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1" + //condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" +) + +var _ = Describe("Barbican controller", func() { + + var barbicanName types.NamespacedName + //var dbSyncJobName types.NamespacedName + //var bootstrapJobName types.NamespacedName + //var deploymentName types.NamespacedName + + BeforeEach(func() { + + barbicanName = types.NamespacedName{ + Name: "barbican", + Namespace: namespace, + } + /* + dbSyncJobName = types.NamespacedName{ + Name: "keystone-db-sync", + Namespace: namespace, + } + bootstrapJobName = types.NamespacedName{ + Name: "keystone-bootstrap", + Namespace: namespace, + } + deploymentName = types.NamespacedName{ + Name: "keystone", + Namespace: namespace, + } + */ + + err := os.Setenv("OPERATOR_TEMPLATES", "../../templates") + Expect(err).NotTo(HaveOccurred()) + }) + + When("A Barbican instance is created", func() { + BeforeEach(func() { + DeferCleanup(th.DeleteInstance, CreateBarbican(barbicanName, GetDefaultBarbicanSpec())) + }) + + It("should have the Spec fields defaulted", func() { + Barbican := GetBarbican(barbicanName) + Expect(Barbican.Spec.ServiceUser).Should(Equal("barbican")) + Expect(Barbican.Spec.DatabaseInstance).Should(Equal("openstack")) + Expect(Barbican.Spec.DatabaseUser).Should(Equal("barbican")) + }) + }) +}) diff --git a/tests/functional/base_test.go b/tests/functional/base_test.go new file mode 100644 index 0000000..36ebc9b --- /dev/null +++ b/tests/functional/base_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2023. + +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 functional_test + +import ( + . "github.com/onsi/gomega" + + //batchv1 "k8s.io/api/batch/v1" + //corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + + barbicanv1 "github.com/openstack-k8s-operators/barbican-operator/api/v1beta1" + //condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" +) + +func GetDefaultBarbicanSpec() map[string]interface{} { + return map[string]interface{}{ + "databaseInstance": "openstack", + //"replicas": 1, + "secret": SecretName, + } +} + +func CreateBarbican(name types.NamespacedName, spec map[string]interface{}) client.Object { + + raw := map[string]interface{}{ + "apiVersion": "barbican.openstack.org/v1beta1", + "kind": "Barbican", + "metadata": map[string]interface{}{ + "name": name.Name, + "namespace": name.Namespace, + }, + "spec": spec, + } + return th.CreateUnstructured(raw) +} + +func GetBarbican(name types.NamespacedName) *barbicanv1.Barbican { + instance := &barbicanv1.Barbican{} + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(ctx, name, instance)).Should(Succeed()) + }, timeout, interval).Should(Succeed()) + return instance +} + +/* +func CreateKeystoneAPISecret(namespace string, name string) *corev1.Secret { + return th.CreateSecret( + types.NamespacedName{Namespace: namespace, Name: name}, + map[string][]byte{ + "AdminPassword": []byte("12345678"), + "KeystoneDatabasePassword": []byte("12345678"), + }, + ) +} + +func KeystoneConditionGetter(name types.NamespacedName) condition.Conditions { + instance := GetKeystoneAPI(name) + return instance.Status.Conditions +} + +func GetCronJob(name types.NamespacedName) *batchv1.CronJob { + instance := &batchv1.CronJob{} + Eventually(func(g Gomega) { + g.Expect(k8sClient.Get(ctx, name, instance)).Should(Succeed()) + }, timeout, interval).Should(Succeed()) + return instance +} +*/ diff --git a/tests/functional/suite_test.go b/tests/functional/suite_test.go new file mode 100644 index 0000000..fb28600 --- /dev/null +++ b/tests/functional/suite_test.go @@ -0,0 +1,201 @@ +package functional_test + +import ( + "context" + "crypto/tls" + "fmt" + "net" + "path/filepath" + "testing" + "time" + + "github.com/go-logr/logr" + "github.com/google/uuid" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "go.uber.org/zap/zapcore" + + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/scheme" + "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" + + //memcachedv1 "github.com/openstack-k8s-operators/infra-operator/apis/memcached/v1beta1" + //keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1" + barbicanv1 "github.com/openstack-k8s-operators/barbican-operator/api/v1beta1" + test "github.com/openstack-k8s-operators/lib-common/modules/test" + mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1" + + infra_test "github.com/openstack-k8s-operators/infra-operator/apis/test/helpers" + //keystone_test "github.com/openstack-k8s-operators/keystone-operator/api/test/helpers" + //"github.com/openstack-k8s-operators/keystone-operator/controllers" + "github.com/openstack-k8s-operators/barbican-operator/controllers" + common_test "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers" + mariadb_test "github.com/openstack-k8s-operators/mariadb-operator/api/test/helpers" + //+kubebuilder:scaffold:imports +) + +// 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 + k8sClient client.Client + testEnv *envtest.Environment + ctx context.Context + cancel context.CancelFunc + logger logr.Logger + th *common_test.TestHelper + //keystone *keystone_test.TestHelper + mariadb *mariadb_test.TestHelper + infra *infra_test.TestHelper + namespace string +) + +const ( + timeout = time.Second * 2 + + SecretName = "test-osp-secret" + + interval = time.Millisecond * 200 +) + +func TestAPIs(t *testing.T) { + RegisterFailHandler(Fail) + + RunSpecs(t, "Controller Suite") +} + +var _ = BeforeSuite(func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), func(o *zap.Options) { + o.Development = true + o.TimeEncoder = zapcore.ISO8601TimeEncoder + })) + + ctx, cancel = context.WithCancel(context.TODO()) + + mariaDBCRDs, err := test.GetCRDDirFromModule( + "github.com/openstack-k8s-operators/mariadb-operator/api", "../../go.mod", "bases") + Expect(err).ShouldNot(HaveOccurred()) + //memcachedCRDs, err := test.GetCRDDirFromModule( + // "github.com/openstack-k8s-operators/infra-operator/apis", "../../go.mod", "bases") + //Expect(err).ShouldNot(HaveOccurred()) + + By("bootstrapping test environment") + testEnv = &envtest.Environment{ + CRDDirectoryPaths: []string{ + filepath.Join("..", "..", "config", "crd", "bases"), + mariaDBCRDs, + //memcachedCRDs, + }, + ErrorIfCRDPathMissing: true, + WebhookInstallOptions: envtest.WebhookInstallOptions{ + Paths: []string{filepath.Join("..", "..", "config", "webhook")}, + // NOTE(gibi): if localhost is resolved to ::1 (ipv6) then starting + // the webhook fails as it try to parse the address as ipv4 and + // failing on the colons in ::1 + LocalServingHost: "127.0.0.1", + }, + } + + // cfg is defined in this file globally. + cfg, err = testEnv.Start() + Expect(err).NotTo(HaveOccurred()) + Expect(cfg).NotTo(BeNil()) + + //err = keystonev1.AddToScheme(scheme.Scheme) + //Expect(err).NotTo(HaveOccurred()) + err = barbicanv1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + err = mariadbv1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + //err = memcachedv1.AddToScheme(scheme.Scheme) + //Expect(err).NotTo(HaveOccurred()) + + //+kubebuilder:scaffold:scheme + + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + Expect(err).NotTo(HaveOccurred()) + Expect(k8sClient).NotTo(BeNil()) + th = common_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) + Expect(th).NotTo(BeNil()) + //keystone = keystone_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) + //Expect(keystone).NotTo(BeNil()) + mariadb = mariadb_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) + Expect(mariadb).NotTo(BeNil()) + infra = infra_test.NewTestHelper(ctx, k8sClient, timeout, interval, logger) + Expect(infra).NotTo(BeNil()) + + // Start the controller-manager if goroutine + webhookInstallOptions := &testEnv.WebhookInstallOptions + k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ + Scheme: scheme.Scheme, + // NOTE(gibi): disable metrics reporting in test to allow + // parallel test execution. Otherwise each instance would like to + // bind to the same port + MetricsBindAddress: "0", + Host: webhookInstallOptions.LocalServingHost, + Port: webhookInstallOptions.LocalServingPort, + CertDir: webhookInstallOptions.LocalServingCertDir, + LeaderElection: false, + }) + Expect(err).ToNot(HaveOccurred()) + + kclient, err := kubernetes.NewForConfig(cfg) + Expect(err).ToNot(HaveOccurred(), "failed to create kclient") + + //err = (&keystonev1.KeystoneAPI{}).SetupWebhookWithManager(k8sManager) + //Expect(err).NotTo(HaveOccurred()) + err = (&barbicanv1.Barbican{}).SetupWebhookWithManager(k8sManager) + Expect(err).NotTo(HaveOccurred()) + + //keystonev1.SetupDefaults() + barbicanv1.SetupDefaults() + + err = (&controllers.BarbicanReconciler{ + Client: k8sManager.GetClient(), + Scheme: k8sManager.GetScheme(), + Kclient: kclient, + }).SetupWithManager(k8sManager) + Expect(err).ToNot(HaveOccurred()) + + go func() { + defer GinkgoRecover() + err = k8sManager.Start(ctx) + Expect(err).ToNot(HaveOccurred(), "failed to run manager") + }() + + // 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()) +}) + +var _ = AfterSuite(func() { + By("tearing down the test environment") + cancel() + err := testEnv.Stop() + Expect(err).NotTo(HaveOccurred()) +}) + +var _ = BeforeEach(func() { + // NOTE(gibi): We need to create a unique namespace for each test run + // as namespaces cannot be deleted in a locally running envtest. See + // https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation + namespace = uuid.New().String() + th.CreateNamespace(namespace) + // We still request the delete of the Namespace to properly cleanup if + // we run the test in an existing cluster. + DeferCleanup(th.DeleteNamespace, namespace) +})