Skip to content

Commit

Permalink
Add functional test to validate samples
Browse files Browse the repository at this point in the history
This patch is adding a functional test to validate the Watcher sample.
This test whould have uncovered the previous issue with samples.

Note that this is only validating the Watcher CRD sample. For the rest,
we will add it while creating the basic structrue for each one.
  • Loading branch information
amoralej authored and openshift-merge-bot[bot] committed Nov 29, 2024
1 parent 0423b15 commit 46968a0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions tests/functional/sample_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit 46968a0

Please sign in to comment.