|
| 1 | +package civogo |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | +) |
| 9 | + |
| 10 | +func TestGetKubernetesPool(t *testing.T) { |
| 11 | + client, server, _ := NewClientForTesting(map[string]string{ |
| 12 | + "/v2/kubernetes/clusters/e733ea47-cc80-443b-b3f2-cccfe7a61ef5/pools/fad8638d-efac-41e0-8787-23d37d845685": `{ |
| 13 | + "id": "fad8638d-efac-41e0-8787-23d37d845685", |
| 14 | + "size": "g4s.kube.small", |
| 15 | + "count": 1, |
| 16 | + "instance_names": [ |
| 17 | + "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r" |
| 18 | + ], |
| 19 | + "instances": [ |
| 20 | + { |
| 21 | + "id": "15e0a7dd-744e-4558-8113-2192e4eca040", |
| 22 | + "name": "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r", |
| 23 | + "hostname": "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r", |
| 24 | + "account_id": "eaef1dd6-1cec-4d9c-8480-96452bd94dea", |
| 25 | + "size": "g4s.kube.small", |
| 26 | + "firewall_id": "57416f92-1f81-4111-b87d-77ba77724e5b", |
| 27 | + "source_type": "diskimage", |
| 28 | + "source_id": "1.26.4-k3s1", |
| 29 | + "network_id": "01d7d363-93c5-4203-9f8f-39ea9e7d0098", |
| 30 | + "initial_user": "root", |
| 31 | + "initial_password": "IXnb@e#FGPB95@H6iHJL", |
| 32 | + "ssh_key": "-", |
| 33 | + "tags": [ |
| 34 | + "k3s" |
| 35 | + ], |
| 36 | + "script": "", |
| 37 | + "status": "ACTIVE", |
| 38 | + "civostatsd_token": "d14047b3-27ae-45da-ae11-f0ff906eb4d8", |
| 39 | + "public_ip": "74.220.20.152", |
| 40 | + "private_ip": "192.168.1.16", |
| 41 | + "namespace_id": "cust-default-eaef1dd6-7505", |
| 42 | + "notes": "", |
| 43 | + "reverse_dns": "", |
| 44 | + "cpu_cores": 1, |
| 45 | + "ram_mb": 2048, |
| 46 | + "disk_gb": 40, |
| 47 | + "created_at": "2023-09-15T10:48:13Z" |
| 48 | + } |
| 49 | + ] |
| 50 | + }`, |
| 51 | + }) |
| 52 | + defer server.Close() |
| 53 | + |
| 54 | + got, err := client.GetKubernetesClusterPool("e733ea47-cc80-443b-b3f2-cccfe7a61ef5", "fad8638d-efac-41e0-8787-23d37d845685") |
| 55 | + if err != nil { |
| 56 | + t.Errorf("Request returned an error: %s", err) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + expected := KubernetesPool{ |
| 61 | + ID: "fad8638d-efac-41e0-8787-23d37d845685", |
| 62 | + Count: 1, |
| 63 | + Size: "g4s.kube.small", |
| 64 | + InstanceNames: []string{"k3s-second-cluster-5359-217631-node-pool-b786-3ez7r"}, |
| 65 | + Instances: []KubernetesInstance{}, |
| 66 | + Labels: map[string]string{}, |
| 67 | + Taints: []corev1.Taint{}, |
| 68 | + PublicIPNodePool: false, |
| 69 | + } |
| 70 | + |
| 71 | + if got.ID != expected.ID { |
| 72 | + t.Errorf("Expected %+v, got %+v", expected.ID, got.ID) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if got.Count != expected.Count { |
| 77 | + t.Errorf("Expected %+v, got %+v", expected.Count, got.Count) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + if got.Size != expected.Size { |
| 82 | + t.Errorf("Expected %+v, got %+v", expected.Size, got.Size) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if len(got.InstanceNames) != len(expected.InstanceNames) { |
| 87 | + t.Errorf("Expected %+v, got %+v", len(expected.InstanceNames), len(got.InstanceNames)) |
| 88 | + return |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestDeleteKubernetesPool(t *testing.T) { |
| 93 | + client, server, _ := NewClientForTesting(map[string]string{ |
| 94 | + "/v2/kubernetes/clusters/e733ea47-cc80-443b-b3f2-cccfe7a61ef5/pools/fad8638d-efac-41e0-8787-23d37d845685": `{"result": "success"}`, |
| 95 | + }) |
| 96 | + defer server.Close() |
| 97 | + |
| 98 | + got, err := client.DeleteKubernetesClusterPool("e733ea47-cc80-443b-b3f2-cccfe7a61ef5", "fad8638d-efac-41e0-8787-23d37d845685") |
| 99 | + if err != nil { |
| 100 | + t.Errorf("Request returned an error: %s", err) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + expected := &SimpleResponse{Result: "success"} |
| 105 | + if !reflect.DeepEqual(got, expected) { |
| 106 | + t.Errorf("Expected %+v, got %+v", expected, got) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestUpdateKubernetesPool(t *testing.T) { |
| 111 | + client, server, _ := NewClientForTesting(map[string]string{ |
| 112 | + "/v2/kubernetes/clusters/e733ea47-cc80-443b-b3f2-cccfe7a61ef5/pools/fad8638d-efac-41e0-8787-23d37d845685": `{ |
| 113 | + "id": "fad8638d-efac-41e0-8787-23d37d845685", |
| 114 | + "size": "g4s.kube.small", |
| 115 | + "count": 3, |
| 116 | + "instance_names": [ |
| 117 | + "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r", |
| 118 | + "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r", |
| 119 | + "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r" |
| 120 | + ], |
| 121 | + "labels": { |
| 122 | + "label1": "label-result" |
| 123 | + }, |
| 124 | + "taints": [ |
| 125 | + { |
| 126 | + "key": "app", |
| 127 | + "value": "frontend", |
| 128 | + "effect": "NoSchedule" |
| 129 | + } |
| 130 | + ], |
| 131 | + "instances": [ |
| 132 | + { |
| 133 | + "id": "15e0a7dd-744e-4558-8113-2192e4eca040", |
| 134 | + "name": "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r", |
| 135 | + "hostname": "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r", |
| 136 | + "account_id": "eaef1dd6-1cec-4d9c-8480-96452bd94dea", |
| 137 | + "size": "g4s.kube.small", |
| 138 | + "firewall_id": "57416f92-1f81-4111-b87d-77ba77724e5b", |
| 139 | + "source_type": "diskimage", |
| 140 | + "source_id": "1.26.4-k3s1", |
| 141 | + "network_id": "01d7d363-93c5-4203-9f8f-39ea9e7d0098", |
| 142 | + "initial_user": "root", |
| 143 | + "initial_password": "IXnb@e#FGPB95@H6iHJL", |
| 144 | + "ssh_key": "-", |
| 145 | + "tags": [ |
| 146 | + "k3s" |
| 147 | + ], |
| 148 | + "script": "", |
| 149 | + "status": "ACTIVE", |
| 150 | + "civostatsd_token": "d14047b3-27ae-45da-ae11-f0ff906eb4d8", |
| 151 | + "public_ip": "74.220.20.152", |
| 152 | + "private_ip": "192.168.1.16", |
| 153 | + "namespace_id": "cust-default-eaef1dd6-7505", |
| 154 | + "notes": "", |
| 155 | + "reverse_dns": "", |
| 156 | + "cpu_cores": 1, |
| 157 | + "ram_mb": 2048, |
| 158 | + "disk_gb": 40, |
| 159 | + "created_at": "2023-09-15T10:48:13Z" |
| 160 | + } |
| 161 | + ] |
| 162 | + }`, |
| 163 | + }) |
| 164 | + defer server.Close() |
| 165 | + |
| 166 | + updatePool := &KubernetesClusterPoolUpdateConfig{ |
| 167 | + Count: 3, |
| 168 | + Labels: map[string]string{ |
| 169 | + "label1": "label", |
| 170 | + }, |
| 171 | + Taints: []corev1.Taint{ |
| 172 | + { |
| 173 | + Key: "app", |
| 174 | + Value: "frontend", |
| 175 | + Effect: "NoSchedule", |
| 176 | + }, |
| 177 | + }, |
| 178 | + } |
| 179 | + |
| 180 | + got, err := client.UpdateKubernetesClusterPool("e733ea47-cc80-443b-b3f2-cccfe7a61ef5", "fad8638d-efac-41e0-8787-23d37d845685", updatePool) |
| 181 | + if err != nil { |
| 182 | + t.Errorf("Request returned an error: %s", err) |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + expected := KubernetesPool{ |
| 187 | + ID: "fad8638d-efac-41e0-8787-23d37d845685", |
| 188 | + Count: 3, |
| 189 | + Size: "g4s.kube.small", |
| 190 | + InstanceNames: []string{"k3s-second-cluster-5359-217631-node-pool-b786-3ez7r", "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r", "k3s-second-cluster-5359-217631-node-pool-b786-3ez7r"}, |
| 191 | + Instances: []KubernetesInstance{}, |
| 192 | + Labels: map[string]string{ |
| 193 | + "label1": "label-result", |
| 194 | + }, |
| 195 | + Taints: []corev1.Taint{ |
| 196 | + { |
| 197 | + Key: "app", |
| 198 | + Value: "frontend", |
| 199 | + Effect: "NoSchedule", |
| 200 | + }, |
| 201 | + }, |
| 202 | + PublicIPNodePool: false, |
| 203 | + } |
| 204 | + |
| 205 | + if got.ID != expected.ID { |
| 206 | + t.Errorf("Expected %+v, got %+v", expected.ID, got.ID) |
| 207 | + return |
| 208 | + } |
| 209 | + |
| 210 | + if got.Count != expected.Count { |
| 211 | + t.Errorf("Expected %+v, got %+v", expected.Count, got.Count) |
| 212 | + return |
| 213 | + } |
| 214 | + |
| 215 | + if got.Size != expected.Size { |
| 216 | + t.Errorf("Expected %+v, got %+v", expected.Size, got.Size) |
| 217 | + return |
| 218 | + } |
| 219 | + |
| 220 | + if len(got.InstanceNames) != len(expected.InstanceNames) { |
| 221 | + t.Errorf("Expected %+v, got %+v", len(expected.InstanceNames), len(got.InstanceNames)) |
| 222 | + return |
| 223 | + } |
| 224 | + |
| 225 | + if !reflect.DeepEqual(got.Labels, expected.Labels) { |
| 226 | + t.Errorf("Expected %+v, got %+v", expected.Labels, got.Labels) |
| 227 | + } |
| 228 | + |
| 229 | + if !reflect.DeepEqual(got.Taints, expected.Taints) { |
| 230 | + t.Errorf("Expected %+v, got %+v", expected.Taints, got.Taints) |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +func TestDeleteKubernetesPoolInstance(t *testing.T) { |
| 235 | + client, server, _ := NewClientForTesting(map[string]string{ |
| 236 | + "/v2/kubernetes/clusters/e733ea47-cc80-443b-b3f2-cccfe7a61ef5/pools/fad8638d-efac-41e0-8787-23d37d845685/instances/15e0a7dd-744e-4558-8113-2192e4eca040": `{"result": "success"}`, |
| 237 | + }) |
| 238 | + defer server.Close() |
| 239 | + |
| 240 | + got, err := client.DeleteKubernetesClusterPoolInstance("e733ea47-cc80-443b-b3f2-cccfe7a61ef5", "fad8638d-efac-41e0-8787-23d37d845685", "15e0a7dd-744e-4558-8113-2192e4eca040") |
| 241 | + if err != nil { |
| 242 | + t.Errorf("Request returned an error: %s", err) |
| 243 | + return |
| 244 | + } |
| 245 | + |
| 246 | + expected := &SimpleResponse{Result: "success"} |
| 247 | + if !reflect.DeepEqual(got, expected) { |
| 248 | + t.Errorf("Expected %+v, got %+v", expected, got) |
| 249 | + } |
| 250 | +} |
0 commit comments