Skip to content

Commit 0c9cf27

Browse files
committed
Update test files name without _test
1 parent 8c21d6a commit 0c9cf27

File tree

7 files changed

+204
-132
lines changed

7 files changed

+204
-132
lines changed

e2e/aws_test.go renamed to e2e/aws.go

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2024 Red Hat, Inc.
2+
//
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+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
114
package e2e
215

316
import (
@@ -20,28 +33,49 @@ import (
2033
awsv1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
2134
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2235
"sigs.k8s.io/controller-runtime/pkg/client"
36+
"sigs.k8s.io/controller-runtime/pkg/client/config"
2337
yaml "sigs.k8s.io/yaml"
2438
)
2539

2640
const (
2741
awsMachineTemplateName = "aws-machine-template"
42+
infrastructureName = "cluster"
43+
infraAPIVersion = "infrastructure.cluster.x-k8s.io/v1beta1"
2844
)
2945

3046
var _ = Describe("Cluster API AWS MachineSet", Ordered, func() {
3147
var (
48+
cl client.Client
49+
ctx = context.Background()
3250
awsMachineTemplate *awsv1.AWSMachineTemplate
3351
machineSet *clusterv1.MachineSet
3452
mapiDefaultMS *mapiv1.MachineSet
3553
mapiDefaultProviderSpec *mapiv1.AWSMachineProviderConfig
3654
awsClient *ec2.EC2
55+
platform configv1.PlatformType
56+
clusterName string
3757
)
3858

3959
BeforeAll(func() {
60+
cfg, err := config.GetConfig()
61+
Expect(err).ToNot(HaveOccurred(), "Failed to GetConfig")
62+
63+
cl, err = client.New(cfg, client.Options{})
64+
Expect(err).ToNot(HaveOccurred(), "Failed to create Kubernetes client for test")
65+
66+
infra := &configv1.Infrastructure{}
67+
infraName := client.ObjectKey{
68+
Name: infrastructureName,
69+
}
70+
Expect(cl.Get(ctx, infraName, infra)).To(Succeed(), "Failed to get cluster infrastructure object")
71+
Expect(infra.Status.PlatformStatus).ToNot(BeNil(), "expected the infrastructure Status.PlatformStatus to not be nil")
72+
clusterName = infra.Status.InfrastructureName
73+
platform = infra.Status.PlatformStatus.Type
4074
if platform != configv1.AWSPlatformType {
4175
Skip("Skipping AWS E2E tests")
4276
}
4377
mapiDefaultMS, mapiDefaultProviderSpec = getDefaultAWSMAPIProviderSpec(cl)
44-
awsClient = createAWSClient(mapiDefaultProviderSpec.Placement.Region)
78+
awsClient = createAWSClient(cl, mapiDefaultProviderSpec.Placement.Region)
4579
})
4680

4781
AfterEach(func() {
@@ -75,13 +109,13 @@ var _ = Describe("Cluster API AWS MachineSet", Ordered, func() {
75109

76110
framework.WaitForMachineSet(cl, machineSet.Name)
77111

78-
compareInstances(awsClient, mapiDefaultMS.Name, "aws-machineset")
112+
compareInstances(cl, awsClient, mapiDefaultMS.Name, "aws-machineset")
79113
})
80114
})
81115

82116
func getDefaultAWSMAPIProviderSpec(cl client.Client) (*mapiv1.MachineSet, *mapiv1.AWSMachineProviderConfig) {
83117
machineSetList := &mapiv1.MachineSetList{}
84-
Expect(cl.List(ctx, machineSetList, client.InNamespace(framework.MAPINamespace))).To(Succeed())
118+
Expect(cl.List(framework.GetContext(), machineSetList, client.InNamespace(framework.MAPINamespace))).To(Succeed())
85119

86120
Expect(machineSetList.Items).ToNot(HaveLen(0))
87121
machineSet := &machineSetList.Items[0]
@@ -156,15 +190,17 @@ func newAWSMachineTemplate(mapiProviderSpec *mapiv1.AWSMachineProviderConfig) *a
156190
return awsMachineTemplate
157191
}
158192

159-
func createAWSClient(region string) *ec2.EC2 {
193+
func createAWSClient(cl client.Client, region string) *ec2.EC2 {
160194
var secret corev1.Secret
161-
Expect(cl.Get(context.Background(), client.ObjectKey{
195+
196+
Expect(cl.Get(framework.GetContext(), client.ObjectKey{
162197
Namespace: framework.CAPINamespace,
163198
Name: "capa-manager-bootstrap-credentials",
164199
}, &secret)).To(Succeed())
165200

166201
accessKey := secret.Data["aws_access_key_id"]
167202
Expect(accessKey).ToNot(BeNil())
203+
168204
secretAccessKey := secret.Data["aws_secret_access_key"]
169205
Expect(secretAccessKey).ToNot(BeNil())
170206

@@ -183,11 +219,12 @@ func createAWSClient(region string) *ec2.EC2 {
183219
return ec2.New(sess)
184220
}
185221

186-
func getMAPICreatedInstance(awsClient *ec2.EC2, msName string) ec2.Instance {
222+
func getMAPICreatedInstance(cl client.Client, awsClient *ec2.EC2, msName string) ec2.Instance {
187223
Expect(awsClient).ToNot(BeNil())
188224
Expect(msName).ToNot(BeEmpty())
225+
189226
mapiMachineList := &mapiv1.MachineList{}
190-
Expect(cl.List(ctx, mapiMachineList, client.InNamespace(framework.MAPINamespace), client.MatchingLabels{
227+
Expect(cl.List(framework.GetContext(), mapiMachineList, client.InNamespace(framework.MAPINamespace), client.MatchingLabels{
191228
"machine.openshift.io/cluster-api-machineset": msName,
192229
})).To(Succeed())
193230
Expect(len(mapiMachineList.Items)).To(BeNumerically(">", 0))
@@ -214,12 +251,13 @@ func getMAPICreatedInstance(awsClient *ec2.EC2, msName string) ec2.Instance {
214251
return *result.Reservations[0].Instances[0]
215252
}
216253

217-
func getCAPICreatedInstance(awsClient *ec2.EC2, msName string) ec2.Instance {
254+
func getCAPICreatedInstance(cl client.Client, awsClient *ec2.EC2, msName string) ec2.Instance {
218255
Expect(awsClient).ToNot(BeNil())
219256
Expect(msName).ToNot(BeEmpty())
257+
220258
capiMachineList := &awsv1.AWSMachineList{}
221259

222-
Expect(cl.List(ctx, capiMachineList, client.InNamespace(framework.CAPINamespace), client.MatchingLabels{
260+
Expect(cl.List(framework.GetContext(), capiMachineList, client.InNamespace(framework.CAPINamespace), client.MatchingLabels{
223261
"machine.openshift.io/cluster-api-machineset": msName,
224262
})).To(Succeed())
225263
Expect(capiMachineList.Items).To(HaveLen(1))
@@ -240,10 +278,11 @@ func getCAPICreatedInstance(awsClient *ec2.EC2, msName string) ec2.Instance {
240278
return *result.Reservations[0].Instances[0]
241279
}
242280

243-
func compareInstances(awsClient *ec2.EC2, mapiMsName, capiMsName string) {
281+
func compareInstances(cl client.Client, awsClient *ec2.EC2, mapiMsName, capiMsName string) {
244282
By("Comparing instances created by MAPI and CAPI")
245-
mapiEC2Instance := getMAPICreatedInstance(awsClient, mapiMsName)
246-
capiEC2Instance := getCAPICreatedInstance(awsClient, capiMsName)
283+
284+
mapiEC2Instance := getMAPICreatedInstance(cl, awsClient, mapiMsName)
285+
capiEC2Instance := getCAPICreatedInstance(cl, awsClient, capiMsName)
247286

248287
// Ignore fields that are unique for each instance
249288
ignoreInstanceFields := cmpopts.IgnoreFields(ec2.Instance{},
@@ -270,7 +309,7 @@ func compareInstances(awsClient *ec2.EC2, mapiMsName, capiMsName string) {
270309
"PrivateIpAddress",
271310
)
272311

273-
ignorePrivateIpFields := cmpopts.IgnoreFields(ec2.InstancePrivateIpAddress{},
312+
ignorePrivateIPFields := cmpopts.IgnoreFields(ec2.InstancePrivateIpAddress{},
274313
"PrivateDnsName",
275314
"PrivateIpAddress",
276315
)
@@ -282,7 +321,7 @@ func compareInstances(awsClient *ec2.EC2, mapiMsName, capiMsName string) {
282321
ignoreInstanceFields,
283322
ignoreBlockDeviceFields,
284323
ignoreNicFields,
285-
ignorePrivateIpFields,
324+
ignorePrivateIPFields,
286325
ignoreTags,
287326
}
288327

e2e/azure_test.go renamed to e2e/azure.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
azurev1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
1919
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2020
"sigs.k8s.io/controller-runtime/pkg/client"
21+
"sigs.k8s.io/controller-runtime/pkg/client/config"
2122
yaml "sigs.k8s.io/yaml"
2223
)
2324

@@ -28,11 +29,31 @@ const (
2829
)
2930

3031
var _ = Describe("Cluster API Azure MachineSet", Ordered, func() {
31-
var azureMachineTemplate *azurev1.AzureMachineTemplate
32-
var machineSet *clusterv1.MachineSet
33-
var mapiMachineSpec *mapiv1.AzureMachineProviderSpec
32+
var (
33+
cl client.Client
34+
ctx = context.Background()
35+
azureMachineTemplate *azurev1.AzureMachineTemplate
36+
machineSet *clusterv1.MachineSet
37+
mapiMachineSpec *mapiv1.AzureMachineProviderSpec
38+
platform configv1.PlatformType
39+
clusterName string
40+
)
3441

3542
BeforeAll(func() {
43+
cfg, err := config.GetConfig()
44+
Expect(err).ToNot(HaveOccurred(), "Failed to GetConfig")
45+
46+
cl, err = client.New(cfg, client.Options{})
47+
Expect(err).ToNot(HaveOccurred(), "Failed to create Kubernetes client for test")
48+
49+
infra := &configv1.Infrastructure{}
50+
infraName := client.ObjectKey{
51+
Name: infrastructureName,
52+
}
53+
Expect(cl.Get(ctx, infraName, infra)).To(Succeed(), "Failed to get cluster infrastructure object")
54+
Expect(infra.Status.PlatformStatus).ToNot(BeNil(), "expected the infrastructure Status.PlatformStatus to not be nil")
55+
clusterName = infra.Status.InfrastructureName
56+
platform = infra.Status.PlatformStatus.Type
3657
if platform != configv1.AzurePlatformType {
3758
Skip("Skipping Azure E2E tests")
3859
}
@@ -72,7 +93,7 @@ var _ = Describe("Cluster API Azure MachineSet", Ordered, func() {
7293

7394
func getAzureMAPIProviderSpec(cl client.Client) *mapiv1.AzureMachineProviderSpec {
7495
machineSetList := &mapiv1.MachineSetList{}
75-
Expect(cl.List(ctx, machineSetList, client.InNamespace(framework.MAPINamespace))).To(Succeed())
96+
Expect(cl.List(framework.GetContext(), machineSetList, client.InNamespace(framework.MAPINamespace))).To(Succeed())
7697

7798
Expect(machineSetList.Items).ToNot(HaveLen(0))
7899
machineSet := machineSetList.Items[0]
@@ -155,7 +176,7 @@ func createAzureMachineTemplate(cl client.Client, mapiProviderSpec *mapiv1.Azure
155176
},
156177
}
157178

158-
if err := cl.Create(ctx, azureMachineTemplate); err != nil && !apierrors.IsAlreadyExists(err) {
179+
if err := cl.Create(framework.GetContext(), azureMachineTemplate); err != nil && !apierrors.IsAlreadyExists(err) {
159180
Expect(err).ToNot(HaveOccurred())
160181
}
161182

e2e/e2e_test.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package e2e
22

33
import (
4-
"context"
54
"testing"
65

76
. "github.com/onsi/ginkgo/v2"
@@ -14,27 +13,11 @@ import (
1413
ibmpowervsv1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
1514
vspherev1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
1615
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
17-
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
18-
"sigs.k8s.io/controller-runtime/pkg/client/config"
1916

2017
configv1 "github.com/openshift/api/config/v1"
2118
mapiv1 "github.com/openshift/api/machine/v1beta1"
2219
)
2320

24-
const (
25-
infrastructureName = "cluster"
26-
infraAPIVersion = "infrastructure.cluster.x-k8s.io/v1beta1"
27-
managedByAnnotationValueClusterCAPIOperatorInfraClusterController = "cluster-capi-operator-infracluster-controller"
28-
)
29-
30-
var (
31-
cl runtimeclient.Client
32-
ctx = context.Background()
33-
platform configv1.PlatformType
34-
clusterName string
35-
mapiInfrastructure *configv1.Infrastructure
36-
)
37-
3821
func init() {
3922
utilruntime.Must(configv1.Install(scheme.Scheme))
4023
utilruntime.Must(awsv1.AddToScheme(scheme.Scheme))
@@ -50,21 +33,3 @@ func TestAPIs(t *testing.T) {
5033
RegisterFailHandler(Fail)
5134
RunSpecs(t, "Cluster API Suite")
5235
}
53-
54-
var _ = BeforeSuite(func() {
55-
cfg, err := config.GetConfig()
56-
Expect(err).ToNot(HaveOccurred())
57-
58-
cl, err = runtimeclient.New(cfg, runtimeclient.Options{})
59-
Expect(err).ToNot(HaveOccurred())
60-
61-
infra := &configv1.Infrastructure{}
62-
infraName := runtimeclient.ObjectKey{
63-
Name: infrastructureName,
64-
}
65-
Expect(cl.Get(ctx, infraName, infra)).To(Succeed())
66-
Expect(infra.Status.PlatformStatus).ToNot(BeNil())
67-
mapiInfrastructure = infra
68-
clusterName = infra.Status.InfrastructureName
69-
platform = infra.Status.PlatformStatus.Type
70-
})

e2e/framework/framework.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ func DeleteObjects(cl client.Client, objs ...client.Object) {
3434
Expect(cl.Delete(ctx, o)).To(Succeed())
3535
}
3636
}
37+
38+
// GetContext returns a context.
39+
func GetContext() context.Context {
40+
return context.Background()
41+
}

e2e/gcp_test.go renamed to e2e/gcp.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package e2e
22

33
import (
4+
"context"
45
"fmt"
56

67
. "github.com/onsi/ginkgo/v2"
@@ -11,6 +12,7 @@ import (
1112
gcpv1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
1213
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1314
"sigs.k8s.io/controller-runtime/pkg/client"
15+
"sigs.k8s.io/controller-runtime/pkg/client/config"
1416
yaml "sigs.k8s.io/yaml"
1517

1618
configv1 "github.com/openshift/api/config/v1"
@@ -23,11 +25,31 @@ const (
2325
)
2426

2527
var _ = Describe("Cluster API GCP MachineSet", Ordered, func() {
26-
var gcpMachineTemplate *gcpv1.GCPMachineTemplate
27-
var machineSet *clusterv1.MachineSet
28-
var mapiMachineSpec *mapiv1.GCPMachineProviderSpec
28+
var (
29+
cl client.Client
30+
ctx = context.Background()
31+
gcpMachineTemplate *gcpv1.GCPMachineTemplate
32+
machineSet *clusterv1.MachineSet
33+
mapiMachineSpec *mapiv1.GCPMachineProviderSpec
34+
platform configv1.PlatformType
35+
clusterName string
36+
)
2937

3038
BeforeAll(func() {
39+
cfg, err := config.GetConfig()
40+
Expect(err).ToNot(HaveOccurred(), "Failed to GetConfig")
41+
42+
cl, err = client.New(cfg, client.Options{})
43+
Expect(err).ToNot(HaveOccurred(), "Failed to create Kubernetes client for test")
44+
45+
infra := &configv1.Infrastructure{}
46+
infraName := client.ObjectKey{
47+
Name: infrastructureName,
48+
}
49+
Expect(cl.Get(ctx, infraName, infra)).To(Succeed(), "Failed to get cluster infrastructure object")
50+
Expect(infra.Status.PlatformStatus).ToNot(BeNil(), "expected the infrastructure Status.PlatformStatus to not be nil")
51+
clusterName = infra.Status.InfrastructureName
52+
platform = infra.Status.PlatformStatus.Type
3153
if platform != configv1.GCPPlatformType {
3254
Skip("Skipping GCP E2E tests")
3355
}
@@ -46,7 +68,7 @@ var _ = Describe("Cluster API GCP MachineSet", Ordered, func() {
4668
})
4769

4870
It("should be able to run a machine", func() {
49-
gcpMachineTemplate = createGCPMachineTemplate(cl, mapiMachineSpec)
71+
gcpMachineTemplate = createGCPMachineTemplate(cl, mapiMachineSpec, clusterName)
5072

5173
machineSet = framework.CreateMachineSet(cl, framework.NewMachineSetParams(
5274
"gcp-machineset",
@@ -66,7 +88,7 @@ var _ = Describe("Cluster API GCP MachineSet", Ordered, func() {
6688

6789
func getGCPMAPIProviderSpec(cl client.Client) *mapiv1.GCPMachineProviderSpec {
6890
machineSetList := &mapiv1.MachineSetList{}
69-
Expect(cl.List(ctx, machineSetList, client.InNamespace(framework.MAPINamespace))).To(Succeed())
91+
Expect(cl.List(framework.GetContext(), machineSetList, client.InNamespace(framework.MAPINamespace))).To(Succeed())
7092

7193
Expect(machineSetList.Items).ToNot(HaveLen(0))
7294
machineSet := machineSetList.Items[0]
@@ -78,7 +100,7 @@ func getGCPMAPIProviderSpec(cl client.Client) *mapiv1.GCPMachineProviderSpec {
78100
return providerSpec
79101
}
80102

81-
func createGCPMachineTemplate(cl client.Client, mapiProviderSpec *mapiv1.GCPMachineProviderSpec) *gcpv1.GCPMachineTemplate {
103+
func createGCPMachineTemplate(cl client.Client, mapiProviderSpec *mapiv1.GCPMachineProviderSpec, clusterName string) *gcpv1.GCPMachineTemplate {
82104
By("Creating GCP machine template")
83105

84106
Expect(mapiProviderSpec).ToNot(BeNil())
@@ -134,7 +156,7 @@ func createGCPMachineTemplate(cl client.Client, mapiProviderSpec *mapiv1.GCPMach
134156
},
135157
}
136158

137-
if err := cl.Create(ctx, gcpMachineTemplate); err != nil && !apierrors.IsAlreadyExists(err) {
159+
if err := cl.Create(framework.GetContext(), gcpMachineTemplate); err != nil && !apierrors.IsAlreadyExists(err) {
138160
Expect(err).ToNot(HaveOccurred())
139161
}
140162

0 commit comments

Comments
 (0)