|
| 1 | +package cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + . "github.com/onsi/ginkgo" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + |
| 11 | + helpers "github.com/tarantool/tarantool-operator/test/helpers" |
| 12 | + |
| 13 | + corev1 "k8s.io/api/core/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + |
| 16 | + tarantoolv1alpha1 "github.com/tarantool/tarantool-operator/pkg/apis/tarantool/v1alpha1" |
| 17 | + |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | +) |
| 20 | + |
| 21 | +var _ = Describe("cluster_controller unit testing", func() { |
| 22 | + var ( |
| 23 | + namespace = "default" |
| 24 | + ctx = context.TODO() |
| 25 | + |
| 26 | + roleName = "" // setup for every spec in hook |
| 27 | + rsTemplateName = "" |
| 28 | + |
| 29 | + clusterName = "test" |
| 30 | + clusterId = clusterName |
| 31 | + |
| 32 | + defaultRolesToAssign = "[\"A\",\"B\"]" |
| 33 | + ) |
| 34 | + |
| 35 | + Describe("cluster_controller manage cluster resources", func() { |
| 36 | + BeforeEach(func() { |
| 37 | + // setup variables for each spec |
| 38 | + roleName = fmt.Sprintf("test-role-%s", RandStringRunes(4)) |
| 39 | + rsTemplateName = fmt.Sprintf("test-rs-%s", RandStringRunes(4)) |
| 40 | + |
| 41 | + By("create new Role " + roleName) |
| 42 | + role := helpers.NewRole(helpers.RoleParams{ |
| 43 | + Name: roleName, |
| 44 | + Namespace: namespace, |
| 45 | + RolesToAssign: defaultRolesToAssign, |
| 46 | + RsNum: int32(1), |
| 47 | + RsTemplateName: rsTemplateName, |
| 48 | + ClusterId: clusterId, |
| 49 | + }) |
| 50 | + // mock owner reference |
| 51 | + role.SetOwnerReferences([]metav1.OwnerReference{ |
| 52 | + { |
| 53 | + APIVersion: "v0", |
| 54 | + Kind: "mockRef", |
| 55 | + Name: "mockRef", |
| 56 | + UID: "-", |
| 57 | + }, |
| 58 | + }) |
| 59 | + Expect(k8sClient.Create(ctx, &role)).NotTo(HaveOccurred(), "failed to create Role") |
| 60 | + |
| 61 | + By("create new Cluster " + clusterName) |
| 62 | + cluster := helpers.NewCluster(helpers.ClusterParams{ |
| 63 | + Name: clusterName, |
| 64 | + Namespace: namespace, |
| 65 | + Id: clusterId, |
| 66 | + }) |
| 67 | + Expect(k8sClient.Create(ctx, &cluster)).NotTo(HaveOccurred(), "failed to create Cluster") |
| 68 | + }) |
| 69 | + |
| 70 | + AfterEach(func() { |
| 71 | + By("remove role object " + roleName) |
| 72 | + role := &tarantoolv1alpha1.Role{} |
| 73 | + Expect( |
| 74 | + k8sClient.Get(ctx, client.ObjectKey{Name: roleName, Namespace: namespace}, role), |
| 75 | + ).NotTo(HaveOccurred(), "failed to get Role") |
| 76 | + |
| 77 | + Expect(k8sClient.Delete(ctx, role)).NotTo(HaveOccurred(), "failed to delete Role") |
| 78 | + |
| 79 | + By("remove Cluster object " + clusterName) |
| 80 | + cluster := &tarantoolv1alpha1.Cluster{} |
| 81 | + Expect( |
| 82 | + k8sClient.Get(ctx, client.ObjectKey{Name: clusterName, Namespace: namespace}, cluster), |
| 83 | + ).NotTo(HaveOccurred(), "failed to get Cluster") |
| 84 | + |
| 85 | + Expect(k8sClient.Delete(ctx, cluster)).NotTo(HaveOccurred(), "failed to delete Cluster") |
| 86 | + }) |
| 87 | + |
| 88 | + Context("manage cluster leader: tarantool instance accepting admin requests", func() { |
| 89 | + BeforeEach(func() { |
| 90 | + By("create cluster endpoints") |
| 91 | + ep := corev1.Endpoints{ |
| 92 | + ObjectMeta: metav1.ObjectMeta{ |
| 93 | + Name: clusterId, |
| 94 | + Namespace: namespace, |
| 95 | + }, |
| 96 | + Subsets: []corev1.EndpointSubset{ |
| 97 | + { |
| 98 | + Addresses: []corev1.EndpointAddress{ |
| 99 | + {IP: "1.1.1.1"}, |
| 100 | + {IP: "2.2.2.2"}, |
| 101 | + {IP: "3.3.3.3"}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | + Expect(k8sClient.Create(ctx, &ep)).NotTo(HaveOccurred(), "failed to create cluster endpoints") |
| 107 | + }) |
| 108 | + |
| 109 | + AfterEach(func() { |
| 110 | + ep := corev1.Endpoints{} |
| 111 | + Expect( |
| 112 | + k8sClient.Get(ctx, client.ObjectKey{Name: clusterId, Namespace: namespace}, &ep), |
| 113 | + ).NotTo(HaveOccurred(), "failed to get cluster endpoints") |
| 114 | + |
| 115 | + Expect(k8sClient.Delete(ctx, &ep)).NotTo(HaveOccurred(), "failed to delete endpoints") |
| 116 | + }) |
| 117 | + |
| 118 | + It("change the leader if the previous one does not exist", func() { |
| 119 | + By("get the chosen leader") |
| 120 | + ep := corev1.Endpoints{} |
| 121 | + Eventually( |
| 122 | + func() bool { |
| 123 | + err := k8sClient.Get(ctx, client.ObjectKey{Name: clusterId, Namespace: namespace}, &ep) |
| 124 | + if err != nil { |
| 125 | + return false |
| 126 | + } |
| 127 | + |
| 128 | + if ep.GetAnnotations()["tarantool.io/leader"] != "" { |
| 129 | + return true |
| 130 | + } |
| 131 | + |
| 132 | + return false |
| 133 | + }, |
| 134 | + time.Second*10, time.Millisecond*500, |
| 135 | + ).Should(BeTrue()) |
| 136 | + |
| 137 | + By("save old leader") |
| 138 | + oldLeader := ep.GetAnnotations()["tarantool.io/leader"] |
| 139 | + |
| 140 | + By("set all new IP addresses") |
| 141 | + ep.Subsets = []corev1.EndpointSubset{ |
| 142 | + { |
| 143 | + Addresses: []corev1.EndpointAddress{ |
| 144 | + {IP: "4.4.4.4"}, |
| 145 | + {IP: "5.5.5.5"}, |
| 146 | + {IP: "6.6.6.6"}, |
| 147 | + }, |
| 148 | + }, |
| 149 | + } |
| 150 | + Expect(k8sClient.Update(ctx, &ep)).NotTo(HaveOccurred(), "failed to update cluster endpoints") |
| 151 | + |
| 152 | + By("check that the leader has changed") |
| 153 | + Eventually( |
| 154 | + func() bool { |
| 155 | + err := k8sClient.Get(ctx, client.ObjectKey{Name: clusterId, Namespace: namespace}, &ep) |
| 156 | + if err != nil { |
| 157 | + return false |
| 158 | + } |
| 159 | + |
| 160 | + if ep.GetAnnotations()["tarantool.io/leader"] != oldLeader { |
| 161 | + return true |
| 162 | + } |
| 163 | + return false |
| 164 | + }, |
| 165 | + time.Second*10, time.Millisecond*500, |
| 166 | + ).Should(BeTrue()) |
| 167 | + }) |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + Describe("cluster_contriller unit testing functions", func() { |
| 172 | + Describe("function IsLeaderExists must check for existence of leader in annotation of cluster Endpoints", func() { |
| 173 | + Context("positive cases (leader exist)", func() { |
| 174 | + It("should return True if leader assigned and exist", func() { |
| 175 | + leaderIP := "1.1.1.1" |
| 176 | + |
| 177 | + ep := &corev1.Endpoints{ |
| 178 | + ObjectMeta: metav1.ObjectMeta{ |
| 179 | + Name: "name", |
| 180 | + Namespace: "namespace", |
| 181 | + Annotations: map[string]string{ |
| 182 | + "tarantool.io/leader": fmt.Sprintf("%s:8081", leaderIP), |
| 183 | + }, |
| 184 | + }, |
| 185 | + Subsets: []corev1.EndpointSubset{ |
| 186 | + { |
| 187 | + Addresses: []corev1.EndpointAddress{ |
| 188 | + {IP: leaderIP}, |
| 189 | + }, |
| 190 | + }, |
| 191 | + }, |
| 192 | + } |
| 193 | + Expect(IsLeaderExists(ep)).To(BeTrue()) |
| 194 | + }) |
| 195 | + }) |
| 196 | + |
| 197 | + Context("negative cases (leader does not exist)", func() { |
| 198 | + It("should return False if leader not assigned", func() { |
| 199 | + ep := &corev1.Endpoints{ |
| 200 | + ObjectMeta: metav1.ObjectMeta{ |
| 201 | + Name: "name", |
| 202 | + Namespace: "namespace", |
| 203 | + }, |
| 204 | + } |
| 205 | + Expect(IsLeaderExists(ep)).To(BeFalse()) |
| 206 | + }) |
| 207 | + |
| 208 | + It("should return False if leader assigned, but IP not exists", func() { |
| 209 | + ep := &corev1.Endpoints{ |
| 210 | + ObjectMeta: metav1.ObjectMeta{ |
| 211 | + Name: "name", |
| 212 | + Namespace: "namespace", |
| 213 | + Annotations: map[string]string{ |
| 214 | + "tarantool.io/leader": "6.6.6.6:8081", |
| 215 | + }, |
| 216 | + }, |
| 217 | + Subsets: []corev1.EndpointSubset{ |
| 218 | + { |
| 219 | + Addresses: []corev1.EndpointAddress{ |
| 220 | + {IP: "0.0.0.0"}, |
| 221 | + }, |
| 222 | + }, |
| 223 | + }, |
| 224 | + } |
| 225 | + Expect(IsLeaderExists(ep)).To(BeFalse()) |
| 226 | + }) |
| 227 | + }) |
| 228 | + }) |
| 229 | + }) |
| 230 | +}) |
0 commit comments