forked from rancher/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule_test.go
More file actions
158 lines (135 loc) · 5.31 KB
/
schedule_test.go
File metadata and controls
158 lines (135 loc) · 5.31 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package schedule
import (
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher/fleet/integrationtests/utils"
"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
var _ = Describe("Schedule updates triggered by cluster updates", func() {
BeforeEach(func() {
var err error
namespace, err = utils.NewNamespaceName()
Expect(err).ToNot(HaveOccurred())
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}
Expect(k8sClient.Create(ctx, ns)).ToNot(HaveOccurred())
DeferCleanup(func() {
Expect(k8sClient.Delete(ctx, ns)).ToNot(HaveOccurred())
})
})
When("a Cluster living in the same namespace as a schedule is updated to match the schedule's targets", func() {
It("schedules the cluster", func() {
By("creating the cluster and schedule")
cluster, err := utils.CreateCluster(ctx, k8sClient, "cluster", namespace, nil, namespace)
Expect(err).NotTo(HaveOccurred())
Expect(cluster).To(Not(BeNil()))
schedule := v1alpha1.Schedule{
ObjectMeta: metav1.ObjectMeta{
Name: "my-schedule",
Namespace: namespace,
},
Spec: v1alpha1.ScheduleSpec{
Schedule: "0 */1 * * * *", // Every minute
Duration: metav1.Duration{Duration: 30 * time.Second},
Targets: v1alpha1.ScheduleTargets{
Clusters: []v1alpha1.ScheduleTarget{
{
ClusterSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{"can-be-scheduled": "yes"}, // initially doesn't match any cluster
},
},
},
},
},
}
err = k8sClient.Create(ctx, &schedule)
Expect(err).NotTo(HaveOccurred())
Eventually(func(g Gomega) {
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "my-schedule"}, &schedule)
g.Expect(err).NotTo(HaveOccurred())
}).Should(Succeed())
defer func() {
Expect(k8sClient.Delete(ctx, &v1alpha1.Schedule{ObjectMeta: metav1.ObjectMeta{
Name: "my-schedule",
Namespace: namespace,
}})).NotTo(HaveOccurred())
}()
By("checking that the cluster has not been scheduled")
cluster = &v1alpha1.Cluster{}
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "cluster"}, cluster)
Expect(err).NotTo(HaveOccurred())
Expect(cluster.Status.Scheduled).To(BeFalse())
By("updating the cluster's labels to match the schedule's selector")
Eventually(func() error {
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "cluster"}, cluster)
Expect(err).NotTo(HaveOccurred())
cluster.Labels = map[string]string{"can-be-scheduled": "yes"}
return k8sClient.Update(ctx, cluster)
}).ShouldNot(HaveOccurred())
By("validating that the cluster is scheduled")
Eventually(func(g Gomega) {
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "cluster"}, cluster)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(cluster.Status.Scheduled).To(BeTrue())
}).Should(Succeed())
})
})
When("another Cluster with a different shard ID and matching the schedule's targets is added into the same namespace", func() {
It("schedules the cluster", func() {
By("creating the cluster and schedule")
cluster, err := utils.CreateCluster(ctx, k8sClient, "cluster", namespace, nil, namespace)
Expect(err).NotTo(HaveOccurred())
Expect(cluster).To(Not(BeNil()))
schedule := v1alpha1.Schedule{
ObjectMeta: metav1.ObjectMeta{
Name: "my-schedule",
Namespace: namespace,
},
Spec: v1alpha1.ScheduleSpec{
Schedule: "0 */1 * * * *", // Every minute
Duration: metav1.Duration{Duration: 30 * time.Second},
Targets: v1alpha1.ScheduleTargets{
Clusters: []v1alpha1.ScheduleTarget{
{
ClusterSelector: &metav1.LabelSelector{},
},
},
},
},
}
err = k8sClient.Create(ctx, &schedule)
Expect(err).NotTo(HaveOccurred())
Eventually(func(g Gomega) {
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "my-schedule"}, &schedule)
g.Expect(err).NotTo(HaveOccurred())
}).Should(Succeed())
defer func() {
Expect(k8sClient.Delete(ctx, &v1alpha1.Schedule{ObjectMeta: metav1.ObjectMeta{
Name: "my-schedule",
Namespace: namespace,
}})).NotTo(HaveOccurred())
}()
By("validating that the cluster is scheduled")
Eventually(func(g Gomega) {
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "cluster"}, cluster)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(cluster.Status.Scheduled).To(BeTrue())
}).Should(Succeed())
By("adding another cluster with a different shard ID to the same namespace")
labels := map[string]string{"fleet.cattle.io/shard-ref": "different-shard"}
shardedCluster, err := utils.CreateCluster(ctx, k8sClient, "cluster2", namespace, labels, namespace)
Expect(err).NotTo(HaveOccurred())
Expect(shardedCluster).To(Not(BeNil()))
By("validating that the cluster is scheduled")
Eventually(func(g Gomega) {
var shardedCluster v1alpha1.Cluster
err = k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: "cluster2"}, &shardedCluster)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(shardedCluster.Status.Scheduled).To(BeTrue())
}).Should(Succeed())
})
})
})