Skip to content

Commit 28c49b3

Browse files
committed
Remove circular import common <-> test module
To remove circular import, this moves modules/test/helpers into common/test/helpers. Also integrates the modules/test-operators into modules/test and removes the need for the modules/test-operators module.
1 parent c894bf7 commit 28c49b3

33 files changed

+536
-916
lines changed

modules/common/go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/onsi/ginkgo/v2 v2.9.2
1010
github.com/onsi/gomega v1.27.6
1111
github.com/openshift/api v3.9.0+incompatible
12-
github.com/openstack-k8s-operators/lib-common/modules/test v0.0.0-20230612101529-af40f24b2b62
1312
github.com/pkg/errors v0.9.1
1413
go.uber.org/zap v1.24.0
1514
k8s.io/api v0.26.3
@@ -76,4 +75,7 @@ require (
7675
sigs.k8s.io/yaml v1.3.0 // indirect
7776
)
7877

79-
replace github.com/openstack-k8s-operators/lib-common/modules/test => ../test
78+
require (
79+
github.com/kr/pretty v0.3.1 // indirect
80+
github.com/rogpeppe/go-internal v1.10.0 // indirect
81+
)

modules/common/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
207207
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
208208
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
209209
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
210+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
210211
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
211212
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
212213
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -233,6 +234,7 @@ github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
233234
github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg=
234235
github.com/openshift/api v3.9.0+incompatible h1:fJ/KsefYuZAjmrr3+5U9yZIZbTOpVkDDLDLFresAeYs=
235236
github.com/openshift/api v3.9.0+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
237+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
236238
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
237239
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
238240
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -267,7 +269,9 @@ github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5
267269
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
268270
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
269271
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
272+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
270273
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
274+
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
271275
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
272276
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
273277
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=

modules/common/test/functional/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
batchv1 "k8s.io/api/batch/v1"
4040
corev1 "k8s.io/api/core/v1"
4141

42-
. "github.com/openstack-k8s-operators/lib-common/modules/test/helpers"
42+
. "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers"
4343
//+kubebuilder:scaffold:imports
4444
)
4545

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)