|
| 1 | +/* |
| 2 | +Copyright 2022 Red Hat |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package helpers |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "os" |
| 19 | + "strconv" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/onsi/gomega" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/openstack-k8s-operators/lib-common/modules/common/condition" |
| 26 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 31 | + |
| 32 | + corev1 "k8s.io/api/core/v1" |
| 33 | +) |
| 34 | + |
| 35 | +type conditionsGetter interface { |
| 36 | + GetConditions(name types.NamespacedName) condition.Conditions |
| 37 | +} |
| 38 | + |
| 39 | +// ConditionGetterFunc recieves custom condition getters for operators specific needs |
| 40 | +type ConditionGetterFunc func(name types.NamespacedName) condition.Conditions |
| 41 | + |
| 42 | +// GetConditions implements conditions getter for operators specific needs |
| 43 | +func (f ConditionGetterFunc) GetConditions(name types.NamespacedName) condition.Conditions { |
| 44 | + return f(name) |
| 45 | +} |
| 46 | + |
| 47 | +// TestHelper is a collection EnvTest helpers writing test to code that |
| 48 | +// interacts with the k8s resource. If you need to handle openstack-k8s-operators |
| 49 | +// specific resource then you should use the extended TestHelper from |
| 50 | +// modules/test-operator |
| 51 | +type TestHelper struct { |
| 52 | + K8sClient client.Client |
| 53 | + Ctx context.Context |
| 54 | + Timeout time.Duration |
| 55 | + Interval time.Duration |
| 56 | + Logger logr.Logger |
| 57 | +} |
| 58 | + |
| 59 | +// NewTestHelper returns a TestHelper |
| 60 | +func NewTestHelper( |
| 61 | + ctx context.Context, |
| 62 | + k8sClient client.Client, |
| 63 | + timeout time.Duration, |
| 64 | + interval time.Duration, |
| 65 | + logger logr.Logger, |
| 66 | +) *TestHelper { |
| 67 | + return &TestHelper{ |
| 68 | + Ctx: ctx, |
| 69 | + K8sClient: k8sClient, |
| 70 | + Timeout: getTestTimeout(timeout), |
| 71 | + Interval: interval, |
| 72 | + Logger: logger, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// getTestTimeout returns test timeout from TEST_TIMEOUT_SEC environment |
| 77 | +// variable, in seconds; or picks defaultTimeout, in milliseconds |
| 78 | +func getTestTimeout(defaultTimeout time.Duration) time.Duration { |
| 79 | + t := os.Getenv("TEST_TIMEOUT_SEC") |
| 80 | + timeout, err := strconv.Atoi(t) |
| 81 | + if err != nil { |
| 82 | + return defaultTimeout |
| 83 | + } |
| 84 | + return time.Duration(timeout) * time.Second |
| 85 | +} |
| 86 | + |
| 87 | +// CreateUnstructured creates an unstructured Kubernetes object from a map of key-value pairs. |
| 88 | +// |
| 89 | +// Example usage: |
| 90 | +// |
| 91 | +// rawObj := map[string]interface{}{ |
| 92 | +// "apiVersion": "nova.openstack.org/v1beta1", |
| 93 | +// "kind": "NovaAPI", |
| 94 | +// "metadata": map[string]interface{}{ |
| 95 | +// "name": name.Name, |
| 96 | +// "namespace": name.Namespace, |
| 97 | +// }, |
| 98 | +// "spec": spec, |
| 99 | +// }, |
| 100 | +// ... |
| 101 | +// } |
| 102 | +// unstructuredObj := tc.CreateUnstructured(rawObj) |
| 103 | +func (tc *TestHelper) CreateUnstructured(rawObj map[string]interface{}) *unstructured.Unstructured { |
| 104 | + tc.Logger.Info("Creating", "raw", rawObj) |
| 105 | + unstructuredObj := &unstructured.Unstructured{Object: rawObj} |
| 106 | + _, err := controllerutil.CreateOrPatch( |
| 107 | + tc.Ctx, tc.K8sClient, unstructuredObj, func() error { return nil }) |
| 108 | + gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 109 | + |
| 110 | + return unstructuredObj |
| 111 | +} |
| 112 | + |
| 113 | +// GetName function is used only in lib-common |
| 114 | +func (tc *TestHelper) GetName(obj client.Object) types.NamespacedName { |
| 115 | + return types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()} |
| 116 | +} |
| 117 | + |
| 118 | +// GetEnvVarValue returns the value of the EnvVar based on the name of the Var |
| 119 | +// or return the defaultValue if the list does not have EnvVar with the given name |
| 120 | +func GetEnvVarValue(envs []corev1.EnvVar, name string, defaultValue string) string { |
| 121 | + for _, e := range envs { |
| 122 | + if e.Name == name { |
| 123 | + return e.Value |
| 124 | + } |
| 125 | + } |
| 126 | + return defaultValue |
| 127 | +} |
0 commit comments