diff --git a/go.mod b/go.mod index 8863afc..3982874 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/openstack-k8s-operators/lib-common/modules/test v0.5.0 github.com/openstack-k8s-operators/mariadb-operator/api v0.5.0 go.uber.org/zap v1.27.0 + gopkg.in/yaml.v3 v3.0.1 k8s.io/api v0.29.9 k8s.io/apimachinery v0.29.9 k8s.io/client-go v0.29.9 @@ -67,7 +68,6 @@ require ( google.golang.org/protobuf v1.34.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apiextensions-apiserver v0.29.9 // indirect k8s.io/component-base v0.29.9 // indirect k8s.io/klog/v2 v2.110.1 // indirect diff --git a/tests/functional/sample_test.go b/tests/functional/sample_test.go new file mode 100644 index 0000000..95b3461 --- /dev/null +++ b/tests/functional/sample_test.go @@ -0,0 +1,65 @@ +/* +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 functional + +import ( + "os" + "path/filepath" + + . "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports + . "github.com/onsi/gomega" //revive:disable:dot-imports + "gopkg.in/yaml.v3" + "k8s.io/apimachinery/pkg/types" +) + +const SamplesDir = "../../config/samples/" + +func ReadSample(sampleFileName string) map[string]interface{} { + rawSample := make(map[string]interface{}) + + bytes, err := os.ReadFile(filepath.Join(SamplesDir, sampleFileName)) + Expect(err).ShouldNot(HaveOccurred()) + Expect(yaml.Unmarshal(bytes, rawSample)).Should(Succeed()) + + return rawSample +} + +// TODO(amoralej) This is creating the test to validate Watcher sample. +// For the rest of CRs we will add additional tests when creating the basic structure +// and envtest. + +func CreateWatcherFromSample(sampleFileName string, name types.NamespacedName) types.NamespacedName { + raw := ReadSample(sampleFileName) + instance := CreateWatcher(name, raw["spec"].(map[string]interface{})) + DeferCleanup(th.DeleteInstance, instance) + return types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()} +} + +// This is a set of test for our samples. It only validates that the sample +// file has all the required field with proper types. But it does not +// validate that using a sample file will result in a working deployment. +// TODO(gibi): By building up all the prerequisites (e.g. MariaDBDatabase) in +// the test and by simulating Job and Deployment success we could assert +// that each sample creates a CR in Ready state. +var _ = Describe("Samples", func() { + + When("watcher_v1beta1_watcher.yaml sample is applied", func() { + It("Watcher is created", func() { + name := CreateWatcherFromSample("watcher_v1beta1_watcher.yaml", watcherTest.Instance) + GetWatcher(name) + }) + }) +})