Skip to content

Commit 94c382a

Browse files
🌱 Export more func in test/e2e/common.go (kubernetes-sigs#10420)
* uppercase SetupSpecNamespace, DumpAllResources, DumpSpecResourcesAndCleanup * godoc for SetupSpecNamespace * move to test/framework/pec_helpers.go * update Copyright * dos2unix
1 parent 9f6e1c2 commit 94c382a

18 files changed

+155
-126
lines changed

test/e2e/autoscaler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func AutoscalerSpec(ctx context.Context, inputGetter func() AutoscalerSpecInput)
9898
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
9999

100100
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
101-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
101+
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
102102
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
103103
})
104104

@@ -306,6 +306,6 @@ func AutoscalerSpec(ctx context.Context, inputGetter func() AutoscalerSpecInput)
306306

307307
AfterEach(func() {
308308
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
309-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
309+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
310310
})
311311
}

test/e2e/cluster_upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func ClusterUpgradeConformanceSpec(ctx context.Context, inputGetter func() Clust
130130
}
131131

132132
// Setup a Namespace where to host objects for this spec and create a watcher for the Namespace events.
133-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
133+
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
134134
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
135135
})
136136

@@ -267,6 +267,6 @@ func ClusterUpgradeConformanceSpec(ctx context.Context, inputGetter func() Clust
267267

268268
AfterEach(func() {
269269
// Dumps all the resources in the spec Namespace, then cleanups the cluster object and the spec Namespace itself.
270-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
270+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
271271
})
272272
}

test/e2e/cluster_upgrade_runtimesdk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func clusterUpgradeWithRuntimeSDKSpec(ctx context.Context, inputGetter func() cl
134134
}
135135

136136
// Set up a Namespace where to host objects for this spec and create a watcher for the Namespace events.
137-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
137+
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
138138
clusterName = fmt.Sprintf("%s-%s", specName, util.RandomString(6))
139139
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
140140
})
@@ -286,7 +286,7 @@ func clusterUpgradeWithRuntimeSDKSpec(ctx context.Context, inputGetter func() cl
286286
}, 10*time.Second, 1*time.Second).Should(Succeed(), "delete extensionConfig failed")
287287

288288
// Dumps all the resources in the spec Namespace, then cleanups the cluster object and the spec Namespace itself.
289-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
289+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
290290
})
291291
}
292292

test/e2e/clusterclass_changes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func ClusterClassChangesSpec(ctx context.Context, inputGetter func() ClusterClas
154154
Expect(input.ModifyControlPlaneFields).ToNot(BeEmpty(), "Invalid argument. input.ModifyControlPlaneFields can't be empty when calling %s spec", specName)
155155

156156
// Set up a Namespace where to host objects for this spec and create a watcher for the namespace events.
157-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
157+
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
158158
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
159159
})
160160

@@ -232,7 +232,7 @@ func ClusterClassChangesSpec(ctx context.Context, inputGetter func() ClusterClas
232232

233233
AfterEach(func() {
234234
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
235-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
235+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
236236
})
237237
}
238238

test/e2e/clusterclass_rollout.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func ClusterClassRolloutSpec(ctx context.Context, inputGetter func() ClusterClas
112112
Expect(input.E2EConfig.Variables).To(HaveValidVersion(input.E2EConfig.GetVariable(KubernetesVersion)))
113113

114114
// Set up a Namespace where to host objects for this spec and create a watcher for the namespace events.
115-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
115+
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
116116
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
117117
})
118118

@@ -291,7 +291,7 @@ func ClusterClassRolloutSpec(ctx context.Context, inputGetter func() ClusterClas
291291

292292
AfterEach(func() {
293293
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
294-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
294+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
295295
})
296296
}
297297

test/e2e/clusterctl_upgrade.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
246246
Expect(os.MkdirAll(input.ArtifactFolder, 0750)).To(Succeed(), "Invalid argument. input.ArtifactFolder can't be created for %s spec", specName)
247247

248248
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
249-
managementClusterNamespace, managementClusterCancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
249+
managementClusterNamespace, managementClusterCancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
250250
managementClusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
251251
})
252252

@@ -645,7 +645,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
645645
AfterEach(func() {
646646
if testNamespace != nil {
647647
// Dump all the logs from the workload cluster before deleting them.
648-
dumpAllResources(ctx, managementClusterProxy, input.ArtifactFolder, testNamespace, managementClusterResources.Cluster)
648+
framework.DumpAllResourcesAndLogs(ctx, managementClusterProxy, input.ArtifactFolder, testNamespace, managementClusterResources.Cluster)
649649

650650
if !input.SkipCleanup {
651651
Byf("Deleting all clusters in namespace %s in management cluster %s", testNamespace.Name, managementClusterName)
@@ -675,7 +675,7 @@ func ClusterctlUpgradeSpec(ctx context.Context, inputGetter func() ClusterctlUpg
675675
input.PreCleanupManagementCluster(managementClusterProxy)
676676
}
677677
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
678-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, managementClusterNamespace, managementClusterCancelWatches, managementClusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
678+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, managementClusterNamespace, managementClusterCancelWatches, managementClusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
679679
})
680680
}
681681

test/e2e/common.go

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,12 @@ package e2e
1919
import (
2020
"context"
2121
"fmt"
22-
"path/filepath"
2322

2423
"github.com/blang/semver/v4"
2524
. "github.com/onsi/ginkgo/v2"
2625
"github.com/onsi/gomega/types"
27-
corev1 "k8s.io/api/core/v1"
28-
"k8s.io/apimachinery/pkg/runtime/schema"
29-
"k8s.io/klog/v2"
30-
"sigs.k8s.io/controller-runtime/pkg/client"
31-
32-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
33-
"sigs.k8s.io/cluster-api/test/e2e/internal/log"
34-
"sigs.k8s.io/cluster-api/test/framework"
26+
3527
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
36-
"sigs.k8s.io/cluster-api/util"
3728
)
3829

3930
// Test suite constants for e2e config variables.
@@ -57,88 +48,6 @@ func Byf(format string, a ...interface{}) {
5748
By(fmt.Sprintf(format, a...))
5849
}
5950

60-
func setupSpecNamespace(ctx context.Context, specName string, clusterProxy framework.ClusterProxy, artifactFolder string, postNamespaceCreated func(framework.ClusterProxy, string)) (*corev1.Namespace, context.CancelFunc) {
61-
Byf("Creating a namespace for hosting the %q test spec", specName)
62-
namespace, cancelWatches := framework.CreateNamespaceAndWatchEvents(ctx, framework.CreateNamespaceAndWatchEventsInput{
63-
Creator: clusterProxy.GetClient(),
64-
ClientSet: clusterProxy.GetClientSet(),
65-
Name: fmt.Sprintf("%s-%s", specName, util.RandomString(6)),
66-
LogFolder: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName()),
67-
})
68-
69-
if postNamespaceCreated != nil {
70-
log.Logf("Calling postNamespaceCreated for namespace %s", namespace.Name)
71-
postNamespaceCreated(clusterProxy, namespace.Name)
72-
}
73-
74-
return namespace, cancelWatches
75-
}
76-
77-
// dumpAllResources dumps all the resources in the spec namespace and the workload cluster.
78-
func dumpAllResources(ctx context.Context, clusterProxy framework.ClusterProxy, artifactFolder string, namespace *corev1.Namespace, cluster *clusterv1.Cluster) {
79-
Byf("Dumping logs from the %q workload cluster", cluster.Name)
80-
81-
// Dump all the logs from the workload cluster.
82-
clusterProxy.CollectWorkloadClusterLogs(ctx, cluster.Namespace, cluster.Name, filepath.Join(artifactFolder, "clusters", cluster.Name))
83-
84-
Byf("Dumping all the Cluster API resources in the %q namespace", namespace.Name)
85-
86-
// Dump all Cluster API related resources to artifacts.
87-
framework.DumpAllResources(ctx, framework.DumpAllResourcesInput{
88-
Lister: clusterProxy.GetClient(),
89-
Namespace: namespace.Name,
90-
LogPath: filepath.Join(artifactFolder, "clusters", clusterProxy.GetName(), "resources"),
91-
})
92-
93-
// If the cluster still exists, dump pods and nodes of the workload cluster.
94-
if err := clusterProxy.GetClient().Get(ctx, client.ObjectKeyFromObject(cluster), &clusterv1.Cluster{}); err == nil {
95-
Byf("Dumping Pods and Nodes of Cluster %s", klog.KObj(cluster))
96-
framework.DumpResourcesForCluster(ctx, framework.DumpResourcesForClusterInput{
97-
Lister: clusterProxy.GetWorkloadCluster(ctx, cluster.Namespace, cluster.Name).GetClient(),
98-
Cluster: cluster,
99-
LogPath: filepath.Join(artifactFolder, "clusters", cluster.Name, "resources"),
100-
Resources: []framework.DumpNamespaceAndGVK{
101-
{
102-
GVK: schema.GroupVersionKind{
103-
Version: corev1.SchemeGroupVersion.Version,
104-
Kind: "Pod",
105-
},
106-
},
107-
{
108-
GVK: schema.GroupVersionKind{
109-
Version: corev1.SchemeGroupVersion.Version,
110-
Kind: "Node",
111-
},
112-
},
113-
},
114-
})
115-
}
116-
}
117-
118-
// dumpSpecResourcesAndCleanup dumps all the resources in the spec namespace and cleans up the spec namespace.
119-
func dumpSpecResourcesAndCleanup(ctx context.Context, specName string, clusterProxy framework.ClusterProxy, artifactFolder string, namespace *corev1.Namespace, cancelWatches context.CancelFunc, cluster *clusterv1.Cluster, intervalsGetter func(spec, key string) []interface{}, skipCleanup bool) {
120-
// Dump all the resources in the spec namespace and the workload cluster.
121-
dumpAllResources(ctx, clusterProxy, artifactFolder, namespace, cluster)
122-
123-
if !skipCleanup {
124-
Byf("Deleting cluster %s", klog.KObj(cluster))
125-
// While https://github.com/kubernetes-sigs/cluster-api/issues/2955 is addressed in future iterations, there is a chance
126-
// that cluster variable is not set even if the cluster exists, so we are calling DeleteAllClustersAndWait
127-
// instead of DeleteClusterAndWait
128-
framework.DeleteAllClustersAndWait(ctx, framework.DeleteAllClustersAndWaitInput{
129-
Client: clusterProxy.GetClient(),
130-
Namespace: namespace.Name,
131-
}, intervalsGetter(specName, "wait-delete-cluster")...)
132-
133-
Byf("Deleting namespace used for hosting the %q test spec", specName)
134-
framework.DeleteNamespace(ctx, framework.DeleteNamespaceInput{
135-
Deleter: clusterProxy.GetClient(),
136-
Name: namespace.Name,
137-
})
138-
}
139-
cancelWatches()
140-
}
141-
14251
// HaveValidVersion succeeds if version is a valid semver version.
14352
func HaveValidVersion(version string) types.GomegaMatcher {
14453
return &validVersionMatcher{version: version}

test/e2e/k8s_conformance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func K8SConformanceSpec(ctx context.Context, inputGetter func() K8SConformanceSp
8787
Expect(kubetestConfigFilePath).To(BeAnExistingFile(), "%s should be a valid kubetest config file")
8888

8989
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
90-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
90+
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
9191
clusterResources = new(clusterctl.ApplyClusterTemplateAndWaitResult)
9292
})
9393

@@ -158,6 +158,6 @@ func K8SConformanceSpec(ctx context.Context, inputGetter func() K8SConformanceSp
158158

159159
AfterEach(func() {
160160
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
161-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
161+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
162162
})
163163
}

test/e2e/kcp_adoption.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func KCPAdoptionSpec(ctx context.Context, inputGetter func() KCPAdoptionSpecInpu
102102
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
103103

104104
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
105-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
105+
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
106106
})
107107

108108
It("Should adopt up-to-date control plane Machines without modification", func() {
@@ -253,6 +253,6 @@ func KCPAdoptionSpec(ctx context.Context, inputGetter func() KCPAdoptionSpecInpu
253253

254254
AfterEach(func() {
255255
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
256-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
256+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
257257
})
258258
}

test/e2e/kcp_remediations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func KCPRemediationSpec(ctx context.Context, inputGetter func() KCPRemediationSp
107107
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
108108

109109
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
110-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
110+
namespace, cancelWatches = framework.SetupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, input.PostNamespaceCreated)
111111
})
112112

113113
It("Should replace unhealthy machines", func() {
@@ -418,7 +418,7 @@ func KCPRemediationSpec(ctx context.Context, inputGetter func() KCPRemediationSp
418418

419419
AfterEach(func() {
420420
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
421-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
421+
framework.DumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
422422
})
423423
}
424424

0 commit comments

Comments
 (0)