Skip to content

Commit 5082648

Browse files
simcodvknabel
andauthored
Integration Tests: clusterctl move (#102)
Co-authored-by: Valentin Knabel <[email protected]>
1 parent 4a6a7bf commit 5082648

File tree

2 files changed

+128
-26
lines changed

2 files changed

+128
-26
lines changed

test/e2e/frmwrk/bootstrap_test.go

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,124 @@ import (
1111
. "github.com/onsi/ginkgo/v2"
1212
. "github.com/onsi/gomega"
1313

14+
apierrors "k8s.io/apimachinery/pkg/api/errors"
15+
"sigs.k8s.io/controller-runtime/pkg/client"
16+
17+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1418
"sigs.k8s.io/cluster-api/test/framework"
19+
"sigs.k8s.io/cluster-api/test/framework/clusterctl"
1520
)
1621

17-
var _ = Describe("Basic Cluster Creation", Ordered, func() {
18-
var (
19-
ec *E2ECluster
20-
)
22+
var _ = Describe("Basic Cluster", Ordered, func() {
2123

2224
BeforeAll(func() {
2325
e2eCtx = NewE2EContext()
2426
e2eCtx.ProvideBootstrapCluster()
2527
e2eCtx.CreateClusterctlConfig(context.TODO())
2628
e2eCtx.InitManagementCluster(context.TODO())
27-
28-
DeferCleanup(e2eCtx.Teardown, context.TODO())
29-
})
30-
31-
BeforeEach(func() {
32-
ec = nil
3329
})
3430

3531
kubernetesVersions := strings.Split(os.Getenv("E2E_KUBERNETES_VERSIONS"), ",")
3632
Expect(kubernetesVersions).ToNot(BeEmpty(), "E2E_KUBERNETES_VERSIONS must be set")
3733

3834
for i, v := range kubernetesVersions {
39-
It(fmt.Sprintf("create new cluster with kubernetes %s", v), func() {
40-
ctx := context.Background()
41-
42-
ec = createE2ECluster(ctx, e2eCtx, ClusterConfig{
43-
SpecName: "basic-cluster-creation-" + v,
44-
NamespaceName: fmt.Sprintf("e2e-basic-cluster-creation-%d", i),
45-
ClusterName: fmt.Sprintf("simple-%d", i),
46-
KubernetesVersion: v,
47-
ControlPlaneMachineImage: os.Getenv("E2E_CONTROL_PLANE_MACHINE_IMAGE_PREFIX") + strings.TrimPrefix(v, "v"),
48-
ControlPlaneMachineCount: 1,
49-
WorkerMachineImage: os.Getenv("E2E_WORKER_MACHINE_IMAGE_PREFIX") + strings.TrimPrefix(v, "v"),
50-
WorkerMachineCount: 1,
35+
Context(fmt.Sprintf("with kubernetes %s", v), Ordered, func() {
36+
var (
37+
ec *E2ECluster
38+
ctx context.Context
39+
)
40+
41+
BeforeEach(func() {
42+
ctx = context.Background()
43+
})
44+
45+
It("create new cluster", func() {
46+
ec = createE2ECluster(ctx, e2eCtx, ClusterConfig{
47+
SpecName: "basic-cluster-creation-" + v,
48+
NamespaceName: fmt.Sprintf("e2e-basic-cluster-creation-%d", i),
49+
ClusterName: fmt.Sprintf("simple-%d", i),
50+
KubernetesVersion: v,
51+
ControlPlaneMachineImage: os.Getenv("E2E_CONTROL_PLANE_MACHINE_IMAGE_PREFIX") + strings.TrimPrefix(v, "v"),
52+
ControlPlaneMachineCount: 1,
53+
WorkerMachineImage: os.Getenv("E2E_WORKER_MACHINE_IMAGE_PREFIX") + strings.TrimPrefix(v, "v"),
54+
WorkerMachineCount: 1,
55+
})
56+
Expect(ec).ToNot(BeNil())
57+
})
58+
59+
It("move from bootstrap to workload cluster", func() {
60+
Expect(ec).NotTo(BeNil(), "e2e cluster required")
61+
62+
clusterctl.InitManagementClusterAndWatchControllerLogs(ctx, clusterctl.InitManagementClusterAndWatchControllerLogsInput{
63+
ClusterProxy: ec.Refs.Workload,
64+
ClusterctlConfigPath: e2eCtx.Environment.ClusterctlConfigPath,
65+
InfrastructureProviders: e2eCtx.E2EConfig.InfrastructureProviders(),
66+
LogFolder: path.Join(e2eCtx.Environment.artifactsPath, "clusters", ec.ClusterName, "init"),
67+
})
68+
69+
clusterctl.Move(ctx, clusterctl.MoveInput{
70+
LogFolder: path.Join(ec.E2EContext.Environment.artifactsPath, "clusters", ec.ClusterName, "move-to"),
71+
ClusterctlConfigPath: ec.E2EContext.Environment.ClusterctlConfigPath,
72+
FromKubeconfigPath: ec.E2EContext.Environment.Bootstrap.GetKubeconfigPath(),
73+
ToKubeconfigPath: ec.Refs.Workload.GetKubeconfigPath(),
74+
Namespace: ec.NamespaceName,
75+
})
76+
77+
cluster := &clusterv1.Cluster{}
78+
err := e2eCtx.Environment.Bootstrap.GetClient().Get(ctx, client.ObjectKey{
79+
Namespace: ec.NamespaceName,
80+
Name: ec.ClusterName,
81+
}, cluster)
82+
Expect(err).To(Satisfy(apierrors.IsNotFound), "cluster should have been moved")
83+
84+
cluster = &clusterv1.Cluster{}
85+
err = ec.Refs.Workload.GetClient().Get(ctx, client.ObjectKey{
86+
Namespace: ec.NamespaceName,
87+
Name: ec.ClusterName,
88+
}, cluster)
89+
Expect(err).ToNot(HaveOccurred(), "cluster should be present")
90+
})
91+
92+
It("move from workload to bootstrap cluster", func() {
93+
Expect(ec).NotTo(BeNil(), "e2e cluster required")
94+
95+
clusterctl.Move(ctx, clusterctl.MoveInput{
96+
LogFolder: path.Join(ec.E2EContext.Environment.artifactsPath, "clusters", ec.ClusterName, "move-back"),
97+
ClusterctlConfigPath: ec.E2EContext.Environment.ClusterctlConfigPath,
98+
FromKubeconfigPath: ec.Refs.Workload.GetKubeconfigPath(),
99+
ToKubeconfigPath: ec.E2EContext.Environment.Bootstrap.GetKubeconfigPath(),
100+
Namespace: ec.NamespaceName,
101+
})
102+
103+
cluster := &clusterv1.Cluster{}
104+
err := ec.Refs.Workload.GetClient().Get(ctx, client.ObjectKey{
105+
Namespace: ec.NamespaceName,
106+
Name: ec.ClusterName,
107+
}, cluster)
108+
Expect(err).To(Satisfy(apierrors.IsNotFound), "cluster should have been moved")
109+
110+
cluster = &clusterv1.Cluster{}
111+
err = e2eCtx.Environment.Bootstrap.GetClient().Get(ctx, client.ObjectKey{
112+
Namespace: ec.NamespaceName,
113+
Name: ec.ClusterName,
114+
}, cluster)
115+
Expect(err).ToNot(HaveOccurred(), "cluster should be present")
116+
})
117+
118+
It("delete cluster", func() {
119+
ec.Teardown(ctx)
120+
// TODO: expect resources to be gone
51121
})
52-
Expect(ec).ToNot(BeNil())
53122
})
54123
}
124+
125+
It("teardown management cluster", func() {
126+
e2eCtx.Teardown(context.Background())
127+
})
55128
})
56129

57130
func createE2ECluster(ctx context.Context, e2eCtx *E2EContext, cfg ClusterConfig) *E2ECluster {
58131
ec := e2eCtx.NewE2ECluster(cfg)
59-
DeferCleanup(ec.Teardown, ctx)
60132

61133
ec.SetupMetalStackPreconditions(ctx)
62134
ec.SetupNamespace(ctx)

test/e2e/frmwrk/shared.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ func (e2e *E2ECluster) GenerateAndApplyClusterTemplate(ctx context.Context) {
527527
// TODO: why does this not work with clusterctl.DefaultInfrastructureProvider?
528528
InfrastructureProvider: "capms:v0.6.2",
529529
LogFolder: path.Join(e2e.E2EContext.Environment.artifactsPath, "clusters", e2e.ClusterName),
530-
// KubeconfigPath: "",
531-
ClusterctlVariables: e2e.Variables(),
530+
ClusterctlVariables: e2e.Variables(),
532531
})
533532

534533
By("Apply cluster template")
@@ -537,6 +536,12 @@ func (e2e *E2ECluster) GenerateAndApplyClusterTemplate(ctx context.Context) {
537536

538537
e2e.Refs.Workload = e2e.E2EContext.Environment.Bootstrap.GetWorkloadCluster(ctx, e2e.NamespaceName, e2e.ClusterName)
539538

539+
err = copyFile(
540+
e2e.Refs.Workload.GetKubeconfigPath(),
541+
path.Join(e2e.E2EContext.Environment.artifactsPath, "clusters", e2e.ClusterName, "kubeconfig"),
542+
)
543+
Expect(err).NotTo(HaveOccurred(), "cannot copy workload kubeconfig file")
544+
540545
e2e.Refs.Cluster = framework.DiscoveryAndWaitForCluster(ctx, framework.DiscoveryAndWaitForClusterInput{
541546
Namespace: e2e.NamespaceName,
542547
Name: e2e.ClusterName,
@@ -594,3 +599,28 @@ func deleteClusterAndWait(ctx context.Context, input framework.DeleteClusterAndW
594599
})
595600
}, retryableOperationTimeout, retryableOperationInterval).Should(BeEmpty(), "There are still Cluster API resources in the %q namespace", input.Cluster.Namespace)
596601
}
602+
603+
func copyFile(src, dst string) error {
604+
sourceFile, err := os.Open(src)
605+
if err != nil {
606+
return err
607+
}
608+
defer func() {
609+
Expect(sourceFile.Close()).ToNot(HaveOccurred(), "cannot close source file")
610+
}()
611+
612+
destinationFile, err := os.Create(dst)
613+
if err != nil {
614+
return err
615+
}
616+
defer func() {
617+
Expect(destinationFile.Close()).ToNot(HaveOccurred(), "cannot close destination file")
618+
}()
619+
620+
_, err = io.Copy(destinationFile, sourceFile)
621+
if err != nil {
622+
return err
623+
}
624+
625+
return nil
626+
}

0 commit comments

Comments
 (0)