Skip to content

Commit e0fc4de

Browse files
committed
refactor: add capi crds to testenv
Changed the testenv creation so that it explicitly adds the CRDs from CAPI. This has been copied for CAPZ :) By making this change it caused some tests to fail as the `Machine` definitions weren't valid and so these have been updated. Signed-off-by: Richard Case <[email protected]>
1 parent 9e63a11 commit e0fc4de

File tree

2 files changed

+92
-27
lines changed

2 files changed

+92
-27
lines changed

controllers/awsmachine_controller_unit_test.go

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func TestAWSMachineReconciler(t *testing.T) {
119119
Name: "test",
120120
},
121121
Spec: clusterv1.MachineSpec{
122+
ClusterName: "capi-test",
122123
Bootstrap: clusterv1.Bootstrap{
123124
DataSecretName: pointer.StringPtr("bootstrap-data"),
124125
},
@@ -149,6 +150,7 @@ func TestAWSMachineReconciler(t *testing.T) {
149150
},
150151
Machine: &clusterv1.Machine{
151152
Spec: clusterv1.MachineSpec{
153+
ClusterName: "capi-test",
152154
Bootstrap: clusterv1.Bootstrap{
153155
DataSecretName: pointer.StringPtr("bootstrap-data"),
154156
},
@@ -1813,6 +1815,7 @@ func TestAWSMachineReconciler_AWSClusterToAWSMachines(t *testing.T) {
18131815
},
18141816
},
18151817
Spec: clusterv1.MachineSpec{
1818+
ClusterName: "capi-test",
18161819
InfrastructureRef: corev1.ObjectReference{
18171820
Kind: "AWSMachine",
18181821
Name: "aws-machine-6",
@@ -1852,6 +1855,7 @@ func TestAWSMachineReconciler_AWSClusterToAWSMachines(t *testing.T) {
18521855
Name: "aws-test-1",
18531856
},
18541857
Spec: clusterv1.MachineSpec{
1858+
ClusterName: "capi-test",
18551859
InfrastructureRef: corev1.ObjectReference{
18561860
Kind: "AWSMachine",
18571861
Name: "aws-machine-1",
@@ -1883,6 +1887,7 @@ func TestAWSMachineReconciler_AWSClusterToAWSMachines(t *testing.T) {
18831887
Name: "aws-test-2",
18841888
},
18851889
Spec: clusterv1.MachineSpec{
1890+
ClusterName: "capi-test",
18861891
InfrastructureRef: corev1.ObjectReference{
18871892
Kind: "AWSMachine",
18881893
Name: "aws-machine-2",
@@ -1911,6 +1916,7 @@ func TestAWSMachineReconciler_AWSClusterToAWSMachines(t *testing.T) {
19111916
Name: "aws-test-3",
19121917
},
19131918
Spec: clusterv1.MachineSpec{
1919+
ClusterName: "capi-test",
19141920
InfrastructureRef: corev1.ObjectReference{
19151921
Kind: "AWSMachine",
19161922
Name: "aws-machine-3",
@@ -1947,6 +1953,7 @@ func TestAWSMachineReconciler_AWSClusterToAWSMachines(t *testing.T) {
19471953
Kind: "Machine",
19481954
},
19491955
Spec: clusterv1.MachineSpec{
1956+
ClusterName: "capi-test",
19501957
InfrastructureRef: corev1.ObjectReference{
19511958
Kind: "Machine",
19521959
Name: "aws-machine-4",
@@ -1979,6 +1986,7 @@ func TestAWSMachineReconciler_AWSClusterToAWSMachines(t *testing.T) {
19791986
},
19801987
},
19811988
Spec: clusterv1.MachineSpec{
1989+
ClusterName: "capi-test",
19821990
InfrastructureRef: corev1.ObjectReference{
19831991
Kind: "AWSMachine",
19841992
APIVersion: infrav1.GroupVersion.String(),
@@ -2152,7 +2160,14 @@ func TestAWSMachineReconciler_Reconcile(t *testing.T) {
21522160
},
21532161
}, Spec: infrav1.AWSMachineSpec{InstanceType: "test"},
21542162
},
2155-
ownerMachine: &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "capi-test-machine"}},
2163+
ownerMachine: &clusterv1.Machine{
2164+
ObjectMeta: metav1.ObjectMeta{
2165+
Name: "capi-test-machine",
2166+
},
2167+
Spec: clusterv1.MachineSpec{
2168+
ClusterName: "capi-test",
2169+
},
2170+
},
21562171
ownerCluster: &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: "capi-test-1"}},
21572172
expectError: false,
21582173
},
@@ -2170,12 +2185,17 @@ func TestAWSMachineReconciler_Reconcile(t *testing.T) {
21702185
},
21712186
}, Spec: infrav1.AWSMachineSpec{InstanceType: "test"},
21722187
},
2173-
ownerMachine: &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{
2174-
Labels: map[string]string{
2175-
clusterv1.ClusterLabelName: "capi-test-1",
2188+
ownerMachine: &clusterv1.Machine{
2189+
ObjectMeta: metav1.ObjectMeta{
2190+
Labels: map[string]string{
2191+
clusterv1.ClusterLabelName: "capi-test-1",
2192+
},
2193+
Name: "capi-test-machine", Namespace: "default",
2194+
},
2195+
Spec: clusterv1.MachineSpec{
2196+
ClusterName: "capi-test",
21762197
},
2177-
Name: "capi-test-machine", Namespace: "default",
2178-
}},
2198+
},
21792199
ownerCluster: &clusterv1.Cluster{ObjectMeta: metav1.ObjectMeta{Name: "capi-test-1"}},
21802200
expectError: false,
21812201
},
@@ -2193,12 +2213,16 @@ func TestAWSMachineReconciler_Reconcile(t *testing.T) {
21932213
},
21942214
}, Spec: infrav1.AWSMachineSpec{InstanceType: "test"},
21952215
},
2196-
ownerMachine: &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{
2197-
Labels: map[string]string{
2198-
clusterv1.ClusterLabelName: "capi-test-1",
2216+
ownerMachine: &clusterv1.Machine{
2217+
ObjectMeta: metav1.ObjectMeta{
2218+
Labels: map[string]string{
2219+
clusterv1.ClusterLabelName: "capi-test-1",
2220+
},
2221+
Name: "capi-test-machine", Namespace: "default",
2222+
}, Spec: clusterv1.MachineSpec{
2223+
ClusterName: "capi-test",
21992224
},
2200-
Name: "capi-test-machine", Namespace: "default",
2201-
}},
2225+
},
22022226
ownerCluster: &clusterv1.Cluster{
22032227
ObjectMeta: metav1.ObjectMeta{Name: "capi-test-1"},
22042228
Spec: clusterv1.ClusterSpec{
@@ -2221,12 +2245,17 @@ func TestAWSMachineReconciler_Reconcile(t *testing.T) {
22212245
},
22222246
}, Spec: infrav1.AWSMachineSpec{InstanceType: "test"},
22232247
},
2224-
ownerMachine: &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{
2225-
Labels: map[string]string{
2226-
clusterv1.ClusterLabelName: "capi-test-1",
2248+
ownerMachine: &clusterv1.Machine{
2249+
ObjectMeta: metav1.ObjectMeta{
2250+
Labels: map[string]string{
2251+
clusterv1.ClusterLabelName: "capi-test-1",
2252+
},
2253+
Name: "capi-test-machine", Namespace: "default",
22272254
},
2228-
Name: "capi-test-machine", Namespace: "default",
2229-
}},
2255+
Spec: clusterv1.MachineSpec{
2256+
ClusterName: "capi-test",
2257+
},
2258+
},
22302259
ownerCluster: &clusterv1.Cluster{
22312260
ObjectMeta: metav1.ObjectMeta{Name: "capi-test-1"},
22322261
Spec: clusterv1.ClusterSpec{
@@ -2249,12 +2278,17 @@ func TestAWSMachineReconciler_Reconcile(t *testing.T) {
22492278
},
22502279
}, Spec: infrav1.AWSMachineSpec{InstanceType: "test"},
22512280
},
2252-
ownerMachine: &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{
2253-
Labels: map[string]string{
2254-
clusterv1.ClusterLabelName: "capi-test-1",
2281+
ownerMachine: &clusterv1.Machine{
2282+
ObjectMeta: metav1.ObjectMeta{
2283+
Labels: map[string]string{
2284+
clusterv1.ClusterLabelName: "capi-test-1",
2285+
},
2286+
Name: "capi-test-machine", Namespace: "default",
22552287
},
2256-
Name: "capi-test-machine", Namespace: "default",
2257-
}},
2288+
Spec: clusterv1.MachineSpec{
2289+
ClusterName: "capi-test",
2290+
},
2291+
},
22582292
ownerCluster: &clusterv1.Cluster{
22592293
ObjectMeta: metav1.ObjectMeta{Name: "capi-test-1"},
22602294
Spec: clusterv1.ClusterSpec{

test/helpers/envtest.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ package helpers
1919
import (
2020
"context"
2121
"fmt"
22+
"go/build"
2223
"net"
2324
"os"
2425
"path"
2526
"path/filepath"
27+
"regexp"
2628
goruntime "runtime"
2729
"strconv"
2830
"strings"
@@ -46,7 +48,6 @@ import (
4648
"sigs.k8s.io/controller-runtime/pkg/envtest"
4749
"sigs.k8s.io/controller-runtime/pkg/manager"
4850

49-
"sigs.k8s.io/cluster-api-provider-aws/test/helpers/external"
5051
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
5152
"sigs.k8s.io/cluster-api/cmd/clusterctl/log"
5253
utilyaml "sigs.k8s.io/cluster-api/util/yaml"
@@ -60,7 +61,8 @@ const (
6061
)
6162

6263
var (
63-
root string
64+
root string
65+
clusterAPIVersionRegex = regexp.MustCompile(`^(\W)sigs.k8s.io/cluster-api v(.+)`)
6466
)
6567

6668
func init() {
@@ -142,14 +144,14 @@ func NewTestEnvironmentConfiguration(crdDirectoryPaths []string) *TestEnvironmen
142144
resolvedCrdDirectoryPaths[i] = path.Join(root, p)
143145
}
144146

147+
if capiPath := getFilePathToCAPICRDs(root); capiPath != "" {
148+
resolvedCrdDirectoryPaths = append(resolvedCrdDirectoryPaths, capiPath)
149+
}
150+
145151
return &TestEnvironmentConfiguration{
146152
env: &envtest.Environment{
147153
ErrorIfCRDPathMissing: true,
148154
CRDDirectoryPaths: resolvedCrdDirectoryPaths,
149-
CRDs: []*apiextensionsv1.CustomResourceDefinition{
150-
external.TestClusterCRD.DeepCopy(),
151-
external.TestMachineCRD.DeepCopy(),
152-
},
153155
},
154156
}
155157
}
@@ -278,3 +280,32 @@ func (t *TestEnvironment) Stop() error {
278280
t.cancel()
279281
return t.env.Stop()
280282
}
283+
284+
func getFilePathToCAPICRDs(root string) string {
285+
modBits, err := os.ReadFile(filepath.Join(root, "go.mod")) //nolint:gosec
286+
if err != nil {
287+
return ""
288+
}
289+
290+
var clusterAPIVersion string
291+
for _, line := range strings.Split(string(modBits), "\n") {
292+
matches := clusterAPIVersionRegex.FindStringSubmatch(line)
293+
if len(matches) == 3 {
294+
clusterAPIVersion = matches[2]
295+
}
296+
}
297+
298+
if clusterAPIVersion == "" {
299+
return ""
300+
}
301+
302+
gopath := envOr("GOPATH", build.Default.GOPATH)
303+
return filepath.Join(gopath, "pkg", "mod", "sigs.k8s.io", fmt.Sprintf("cluster-api@v%s", clusterAPIVersion), "config", "crd", "bases")
304+
}
305+
306+
func envOr(envKey, defaultValue string) string {
307+
if value, ok := os.LookupEnv(envKey); ok {
308+
return value
309+
}
310+
return defaultValue
311+
}

0 commit comments

Comments
 (0)