-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcluster_test.go
More file actions
131 lines (115 loc) · 3.85 KB
/
cluster_test.go
File metadata and controls
131 lines (115 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package e2e
import (
"context"
"fmt"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
controllerV1 "github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
)
var _ = Describe("E2E: Cluster CRD lifecycle", Ordered, func() {
var (
ctx context.Context
cluster *controllerV1.Cluster
name = "test-cluster"
ns = "kubeslice-system"
)
BeforeAll(func() {
ctx = context.Background()
nsObj := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: ns},
}
err := k8sClient.Create(ctx, nsObj)
if err != nil && !k8serrors.IsAlreadyExists(err) {
Fail(fmt.Sprintf("failed to create namespace: %v", err))
}
})
AfterAll(func() {
By("Cleaning up Cluster CR")
err := k8sClient.Delete(ctx, &controllerV1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
})
if err != nil && !k8serrors.IsNotFound(err) {
Fail(fmt.Sprintf("failed to cleanup cluster CR: %v", err))
}
})
It("should create a Cluster CR successfully", func() {
By("Applying Cluster manifest")
cluster = &controllerV1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: controllerV1.ClusterSpec{
NodeIPs: []string{"10.10.0.1"},
ClusterProperty: controllerV1.ClusterProperty{
Telemetry: controllerV1.Telemetry{
Enabled: true,
TelemetryProvider: "prometheus",
Endpoint: "http://10.1.1.27:8080",
},
GeoLocation: controllerV1.GeoLocation{
CloudProvider: "AWS",
CloudRegion: "us-east-1",
},
},
},
}
err := k8sClient.Create(ctx, cluster)
Expect(err).NotTo(HaveOccurred())
By("Fetching created Cluster from API")
fetched := &controllerV1.Cluster{}
Eventually(func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: name, Namespace: ns}, fetched)
}, 30*time.Second, 2*time.Second).Should(Succeed())
Expect(fetched.Spec.NodeIPs).To(ContainElement("10.10.0.1"))
})
It("should reconcile and update Cluster status", func() {
By("Waiting for Cluster to be reconciled (status may be empty in local builds)")
Eventually(func() (bool, error) {
f := &controllerV1.Cluster{}
if err := k8sClient.Get(ctx, types.NamespacedName{Name: name, Namespace: ns}, f); err != nil {
return false, err
}
// Treat empty RegistrationStatus as success (controller didn't set it)
if f.Status.RegistrationStatus == "" ||
f.Status.RegistrationStatus == controllerV1.RegistrationStatusInProgress ||
f.Status.RegistrationStatus == controllerV1.RegistrationStatusRegistered {
return true, nil
}
return false, nil
}, 120*time.Second, 5*time.Second).Should(BeTrue())
})
It("should update Cluster CR when modifying spec", func() {
By("Patching Cluster with new NodeIP")
patch := client.MergeFrom(cluster.DeepCopy())
cluster.Spec.NodeIPs = append(cluster.Spec.NodeIPs, "10.10.0.2")
err := k8sClient.Patch(ctx, cluster, patch)
Expect(err).NotTo(HaveOccurred())
By("Validating new NodeIP appears in spec")
Eventually(func() []string {
f := &controllerV1.Cluster{}
_ = k8sClient.Get(ctx, types.NamespacedName{Name: name, Namespace: ns}, f)
return f.Spec.NodeIPs
}, 20*time.Second, 2*time.Second).Should(ContainElement("10.10.0.2"))
})
It("should delete Cluster CR cleanly", func() {
By("Deleting Cluster CR")
err := k8sClient.Delete(ctx, cluster)
Expect(err).NotTo(HaveOccurred())
By("Verifying Cluster CR is deleted")
Eventually(func() bool {
f := &controllerV1.Cluster{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: name, Namespace: ns}, f)
return k8serrors.IsNotFound(err)
}, 30*time.Second, 2*time.Second).Should(BeTrue())
})
})