Skip to content

Commit 50f05ab

Browse files
committed
Completed second cluster e2e test
1 parent d4d7f0d commit 50f05ab

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

test/e2e/common.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/base64"
2222
"fmt"
23+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2324
"os"
2425
"path/filepath"
2526
"strings"
@@ -308,3 +309,26 @@ func HasMatchingUnhealthyConditions(machineHealthCheck *clusterv1.MachineHealthC
308309
}
309310
return false
310311
}
312+
313+
func ClusterExists(ctx context.Context, mgmtClient client.Client, cluster *clusterv1.Cluster) bool {
314+
key := client.ObjectKey{
315+
Namespace: cluster.GetNamespace(),
316+
Name: cluster.GetName(),
317+
}
318+
return !apierrors.IsNotFound(mgmtClient.Get(ctx, key, &clusterv1.Cluster{}))
319+
}
320+
321+
func IsClusterReady(ctx context.Context, mgmtClient client.Client, cluster *clusterv1.Cluster) bool {
322+
key := client.ObjectKey{
323+
Namespace: cluster.GetNamespace(),
324+
Name: cluster.GetName(),
325+
}
326+
c := &clusterv1.Cluster{}
327+
err := mgmtClient.Get(ctx, key, c)
328+
329+
if apierrors.IsNotFound(err) {
330+
return false
331+
}
332+
Expect(err).To(BeNil(), "Failed to get cluster status")
333+
return c.Status.ControlPlaneReady && c.Status.InfrastructureReady
334+
}

test/e2e/second_cluster.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ func SecondClusterSpec(ctx context.Context, inputGetter func() CommonSpecInput)
3838

3939
var (
4040
input CommonSpecInput
41-
namespace *corev1.Namespace
42-
cancelWatches context.CancelFunc
41+
namespace1 *corev1.Namespace
42+
namespace2 *corev1.Namespace
43+
cancelWatches1 context.CancelFunc
44+
cancelWatches2 context.CancelFunc
4345
clusterResources1 *clusterctl.ApplyClusterTemplateAndWaitResult
4446
clusterResources2 *clusterctl.ApplyClusterTemplateAndWaitResult
4547
)
4648

47-
createCluster := func(flavor string, resources *clusterctl.ApplyClusterTemplateAndWaitResult) {
49+
createCluster := func(flavor string, namespace *corev1.Namespace, resources *clusterctl.ApplyClusterTemplateAndWaitResult) {
4850
clusterctl.ApplyClusterTemplateAndWait(ctx, clusterctl.ApplyClusterTemplateAndWaitInput{
4951
ClusterProxy: input.BootstrapClusterProxy,
5052
CNIManifestPath: input.E2EConfig.GetVariable(CNIPath),
@@ -76,34 +78,42 @@ func SecondClusterSpec(ctx context.Context, inputGetter func() CommonSpecInput)
7678
Expect(input.E2EConfig.Variables).To(HaveKey(KubernetesVersion))
7779
Expect(input.E2EConfig.Variables).To(HaveValidVersion(input.E2EConfig.GetVariable(KubernetesVersion)))
7880

79-
// Setup a Namespace where to host objects for this spec and create a watcher for the namespace events.
80-
namespace, cancelWatches = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
81+
// Set up namespaces to host objects for this spec and create watchers for the namespace events.
82+
namespace1, cancelWatches1 = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
83+
namespace2, cancelWatches2 = setupSpecNamespace(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder)
8184
clusterResources1 = new(clusterctl.ApplyClusterTemplateAndWaitResult)
8285
clusterResources2 = new(clusterctl.ApplyClusterTemplateAndWaitResult)
8386
})
8487

8588
It("should successfully add and remove a second cluster", func() {
86-
By("create the first cluster")
87-
createCluster(clusterctl.DefaultFlavor, clusterResources1)
89+
mgmtClient := input.BootstrapClusterProxy.GetClient()
8890

89-
By("create the second cluster")
90-
createCluster("second-cluster", clusterResources2)
91+
By("create the first cluster and verify that it's ready")
92+
createCluster(clusterctl.DefaultFlavor, namespace1, clusterResources1)
93+
Expect(IsClusterReady(ctx, mgmtClient, clusterResources1.Cluster)).To(BeTrue())
9194

92-
By("verify that the second cluster works")
95+
By("create the second cluster and verify that it's ready")
96+
createCluster("second-cluster", namespace2, clusterResources2)
97+
Expect(IsClusterReady(ctx, mgmtClient, clusterResources2.Cluster)).To(BeTrue())
9398

9499
By("delete the second cluster")
95-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources2.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
100+
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace2,
101+
cancelWatches2, clusterResources2.Cluster, input.E2EConfig.GetIntervals, false)
96102

97-
By("verify that the second cluster is gone")
103+
By("verify the second cluster is gone")
104+
Expect(ClusterExists(ctx, mgmtClient, clusterResources2.Cluster)).To(BeFalse())
98105

99-
By("verify that the first cluster still works")
106+
By("verify the first cluster is still ready")
107+
Expect(IsClusterReady(ctx, mgmtClient, clusterResources1.Cluster)).To(BeTrue())
100108

101109
By("PASSED!")
102110
})
103111

104112
AfterEach(func() {
105113
// Dumps all the resources in the spec namespace, then cleanups the cluster object and the spec namespace itself.
106-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources1.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
107-
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace, cancelWatches, clusterResources2.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
114+
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace1, cancelWatches1, clusterResources1.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
115+
if clusterResources2.Cluster != nil && ClusterExists(ctx, input.BootstrapClusterProxy.GetClient(), clusterResources2.Cluster) {
116+
dumpSpecResourcesAndCleanup(ctx, specName, input.BootstrapClusterProxy, input.ArtifactFolder, namespace2, cancelWatches2, clusterResources2.Cluster, input.E2EConfig.GetIntervals, input.SkipCleanup)
117+
}
108118
})
109119
}

test/e2e/second_cluster_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
. "github.com/onsi/ginkgo"
2525
)
2626

27-
var _ = Describe("with a second cluster [SecondCluster]", func() {
27+
var _ = Describe("with a second cluster", func() {
2828

2929
SecondClusterSpec(context.TODO(), func() CommonSpecInput {
3030
return CommonSpecInput{

0 commit comments

Comments
 (0)