Skip to content

Commit 03c3126

Browse files
committed
Make scale e2e test reusable for CAPM3 and other providers
- Export ScaleSpec and ScaleSpecInput to allow reuse in other providers. - Add PostScaleClusterNamespaceCreated hook to be called after generating cluster name and namespace but before applying the template. Signed-off-by: Mohammed Boukhalfa <[email protected]>
1 parent 0000f09 commit 03c3126

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

test/e2e/scale.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ const (
6060
scaleClusterNamespacePlaceholder = "scale-cluster-namespace-placeholder"
6161
)
6262

63-
// scaleSpecInput is the input for scaleSpec.
64-
type scaleSpecInput struct {
63+
// ScaleSpecInput is the input for ScaleSpec.
64+
type ScaleSpecInput struct {
6565
E2EConfig *clusterctl.E2EConfig
6666
ClusterctlConfigPath string
6767
BootstrapClusterProxy framework.ClusterProxy
@@ -119,6 +119,15 @@ type scaleSpecInput struct {
119119
// If not specified, this is a no-op.
120120
PostNamespaceCreated func(managementClusterProxy framework.ClusterProxy, workloadClusterNamespace string)
121121

122+
// Allows to inject a function to be run after test workload cluster name and namespace are generated and
123+
// before applying the clusterclass and the cluster template.
124+
// If not specified, this is a no-op.
125+
PostScaleClusterNamespaceCreated func(
126+
clusterProxy framework.ClusterProxy,
127+
clusterNamespace string,
128+
clusterName string,
129+
clusterClassYAML []byte,
130+
clusterTemplateYAML []byte) (clusterClassTemlate []byte, ClusterTemplate []byte)
122131
// FailFast if set to true will return immediately after the first cluster operation fails.
123132
// If set to false, the test suite will not exit immediately after the first cluster operation fails.
124133
// Example: When creating clusters from c1 to c20 consider c6 fails creation. If FailFast is set to true
@@ -141,11 +150,11 @@ type scaleSpecInput struct {
141150
SkipWaitForCreation bool
142151
}
143152

144-
// scaleSpec implements a scale test for clusters with MachineDeployments.
145-
func scaleSpec(ctx context.Context, inputGetter func() scaleSpecInput) {
153+
// ScaleSpec implements a scale test for clusters with MachineDeployments.
154+
func ScaleSpec(ctx context.Context, inputGetter func() ScaleSpecInput) {
146155
var (
147156
specName = "scale"
148-
input scaleSpecInput
157+
input ScaleSpecInput
149158
namespace *corev1.Namespace
150159
cancelWatches context.CancelFunc
151160
)
@@ -331,7 +340,7 @@ func scaleSpec(ctx context.Context, inputGetter func() scaleSpecInput) {
331340
Concurrency: concurrency,
332341
FailFast: input.FailFast,
333342
WorkerFunc: func(ctx context.Context, inputChan chan string, resultChan chan workResult, wg *sync.WaitGroup) {
334-
createClusterWorker(ctx, input.BootstrapClusterProxy, inputChan, resultChan, wg, namespace.Name, input.DeployClusterInSeparateNamespaces, baseClusterClassYAML, baseClusterTemplateYAML, creator)
343+
createClusterWorker(ctx, input.BootstrapClusterProxy, inputChan, resultChan, wg, namespace.Name, input.DeployClusterInSeparateNamespaces, baseClusterClassYAML, baseClusterTemplateYAML, creator, input.PostScaleClusterNamespaceCreated)
335344
},
336345
})
337346
if err != nil {
@@ -568,7 +577,9 @@ func getClusterCreateFn(clusterProxy framework.ClusterProxy) clusterCreator {
568577
}
569578
}
570579

571-
func createClusterWorker(ctx context.Context, clusterProxy framework.ClusterProxy, inputChan <-chan string, resultChan chan<- workResult, wg *sync.WaitGroup, defaultNamespace string, deployClusterInSeparateNamespaces bool, baseClusterClassYAML, baseClusterTemplateYAML []byte, create clusterCreator) {
580+
type PostScaleClusterNamespaceCreated func(clusterProxy framework.ClusterProxy, clusterNamespace string, clusterName string, clusterClassYAML []byte, clusterTemplateYAML []byte) (clusterClassTemlate []byte, ClusterTemplate []byte)
581+
582+
func createClusterWorker(ctx context.Context, clusterProxy framework.ClusterProxy, inputChan <-chan string, resultChan chan<- workResult, wg *sync.WaitGroup, defaultNamespace string, deployClusterInSeparateNamespaces bool, baseClusterClassYAML, baseClusterTemplateYAML []byte, create clusterCreator, postScaleClusterNamespaceCreated PostScaleClusterNamespaceCreated) {
572583
defer wg.Done()
573584

574585
for {
@@ -600,7 +611,13 @@ func createClusterWorker(ctx context.Context, clusterProxy framework.ClusterProx
600611
if deployClusterInSeparateNamespaces {
601612
namespaceName = clusterName
602613
}
603-
614+
// Call postScaleClusterNamespaceCreated hook to apply custom requirements based on the clustername and namespace
615+
customizedClusterTemplateYAML := baseClusterTemplateYAML
616+
customizedClusterClassYAML := baseClusterClassYAML
617+
if postScaleClusterNamespaceCreated != nil {
618+
log.Logf("Calling postScaleClusterNamespaceCreated for cluster %s in namespace %s", clusterName, namespaceName)
619+
customizedClusterClassYAML, customizedClusterTemplateYAML = postScaleClusterNamespaceCreated(clusterProxy, namespaceName, clusterName, baseClusterClassYAML, baseClusterTemplateYAML)
620+
}
604621
// If every cluster should be deployed in a separate namespace:
605622
// * Adjust namespace in ClusterClass YAML.
606623
// * Create new namespace.
@@ -614,14 +631,13 @@ func createClusterWorker(ctx context.Context, clusterProxy framework.ClusterProx
614631
}, "40s", "10s")
615632

616633
log.Logf("Apply ClusterClass in namespace %", namespaceName)
617-
clusterClassYAML := bytes.Replace(baseClusterClassYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
634+
clusterClassYAML := bytes.Replace(customizedClusterClassYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
618635
Eventually(func() error {
619636
return clusterProxy.CreateOrUpdate(ctx, clusterClassYAML)
620637
}, 1*time.Minute).Should(Succeed())
621638
}
622-
623639
// Adjust namespace and name in Cluster YAML
624-
clusterTemplateYAML := bytes.Replace(baseClusterTemplateYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
640+
clusterTemplateYAML := bytes.Replace(customizedClusterTemplateYAML, []byte(scaleClusterNamespacePlaceholder), []byte(namespaceName), -1)
625641
clusterTemplateYAML = bytes.Replace(clusterTemplateYAML, []byte(scaleClusterNamePlaceholder), []byte(clusterName), -1)
626642

627643
// Deploy Cluster.

test/e2e/scale_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626

2727
var _ = Describe("When testing the machinery for scale testing using in-memory provider", func() {
2828
// Note: This test does not support MachinePools.
29-
scaleSpec(ctx, func() scaleSpecInput {
30-
return scaleSpecInput{
29+
ScaleSpec(ctx, func() ScaleSpecInput {
30+
return ScaleSpecInput{
3131
E2EConfig: e2eConfig,
3232
ClusterctlConfigPath: clusterctlConfigPath,
3333
InfrastructureProvider: ptr.To("in-memory"),

0 commit comments

Comments
 (0)