github.com/civo/civogo@v0.3.65/fake_client_test.go (about) 1 package civogo 2 3 import ( 4 "errors" 5 "testing" 6 7 . "github.com/onsi/gomega" 8 ) 9 10 func TestClienter(t *testing.T) { 11 var c Clienter 12 13 c, _ = NewClient("foo", "NYC1") 14 c, _ = NewFakeClient() 15 _, _ = c.ListAllInstances() 16 c.ListIPs() 17 } 18 19 // TestIPs is a test for the IPs method. 20 func TestIPs(t *testing.T) { 21 g := NewWithT(t) 22 23 client, err := NewFakeClient() 24 g.Expect(err).To(BeNil()) 25 26 config := &CreateIPRequest{ 27 Name: "test-ip", 28 } 29 30 expected := &IP{ 31 Name: "test-ip", 32 } 33 34 ip, err := client.NewIP(config) 35 g.Expect(err).To(BeNil()) 36 expected.ID = ip.ID 37 g.Expect(ip.Name).To(Equal(expected.Name)) 38 39 ip, err = client.GetIP(ip.ID) 40 g.Expect(err).To(BeNil()) 41 g.Expect(ip.Name).To(Equal(expected.Name)) 42 43 ips, err := client.ListIPs() 44 g.Expect(err).To(BeNil()) 45 g.Expect(len(ips.Items)).To(Equal(1)) 46 47 ip, err = client.FindIP(ip.ID) 48 g.Expect(err).To(BeNil()) 49 g.Expect(ip.Name).To(Equal(expected.Name)) 50 51 resp, err := client.DeleteIP(ip.ID) 52 g.Expect(err).To(BeNil()) 53 g.Expect(resp).To(Equal(&SimpleResponse{Result: "success"})) 54 } 55 56 func TestInstances(t *testing.T) { 57 client, _ := NewFakeClient() 58 59 config := &InstanceConfig{ 60 Count: 1, 61 Hostname: "foo.example.com", 62 } 63 client.CreateInstance(config) 64 65 results, err := client.ListInstances(1, 10) 66 if err != nil { 67 t.Errorf("Request returned an error: %s", err) 68 return 69 } 70 if results.Page != 1 { 71 t.Errorf("Expected %+v, got %+v", 1, results.Page) 72 return 73 } 74 if results.Pages != 1 { 75 t.Errorf("Expected %+v, got %+v", 1, results.Pages) 76 return 77 } 78 if results.PerPage != 10 { 79 t.Errorf("Expected %+v, got %+v", 10, results.PerPage) 80 return 81 } 82 if len(results.Items) != 1 { 83 t.Errorf("Expected %+v, got %+v", 1, len(results.Items)) 84 return 85 } 86 } 87 88 // TestLoadBalancers is a test for the LoadBalancers method. 89 func TestLoadBalancers(t *testing.T) { 90 g := NewWithT(t) 91 92 client, err := NewFakeClient() 93 g.Expect(err).To(BeNil()) 94 95 backendConfig := []LoadBalancerBackendConfig{ 96 { 97 IP: "192.168.1.3", 98 Protocol: "TCP", 99 SourcePort: 80, 100 TargetPort: 31579, 101 }, 102 } 103 config := &LoadBalancerConfig{ 104 Name: "foo", 105 Algorithm: "round_robin", 106 Backends: backendConfig, 107 } 108 109 backends := []LoadBalancerBackend{ 110 { 111 IP: "192.168.1.3", 112 Protocol: "TCP", 113 SourcePort: 80, 114 TargetPort: 31579, 115 }, 116 } 117 118 expected := &LoadBalancer{ 119 Name: "foo", 120 Algorithm: "round_robin", 121 Backends: []LoadBalancerBackend(backends), 122 ExternalTrafficPolicy: "Cluster", 123 } 124 125 loadbalancer, err := client.CreateLoadBalancer(config) 126 g.Expect(err).To(BeNil()) 127 expected.ID = loadbalancer.ID 128 g.Expect(loadbalancer.Name).To(Equal(expected.Name)) 129 g.Expect(loadbalancer.Algorithm).To(Equal(expected.Algorithm)) 130 g.Expect(loadbalancer.Backends).To(Equal(expected.Backends)) 131 g.Expect(loadbalancer.ExternalTrafficPolicy).To(Equal(expected.ExternalTrafficPolicy)) 132 133 loadbalancer, err = client.GetLoadBalancer(loadbalancer.ID) 134 g.Expect(err).To(BeNil()) 135 g.Expect(loadbalancer.Name).To(Equal(expected.Name)) 136 g.Expect(loadbalancer.Algorithm).To(Equal(expected.Algorithm)) 137 g.Expect(loadbalancer.Backends).To(Equal(expected.Backends)) 138 g.Expect(loadbalancer.ExternalTrafficPolicy).To(Equal(expected.ExternalTrafficPolicy)) 139 140 loadbalancers, err := client.ListLoadBalancers() 141 g.Expect(err).To(BeNil()) 142 g.Expect(len(loadbalancers)).To(Equal(1)) 143 144 loadbalancer, err = client.FindLoadBalancer(loadbalancer.ID) 145 g.Expect(err).To(BeNil()) 146 g.Expect(loadbalancer.Name).To(Equal(expected.Name)) 147 g.Expect(loadbalancer.Algorithm).To(Equal(expected.Algorithm)) 148 g.Expect(loadbalancer.Backends).To(Equal(expected.Backends)) 149 g.Expect(loadbalancer.ExternalTrafficPolicy).To(Equal(expected.ExternalTrafficPolicy)) 150 151 resp, err := client.DeleteLoadBalancer(loadbalancer.ID) 152 g.Expect(err).To(BeNil()) 153 g.Expect(resp).To(Equal(&SimpleResponse{Result: "success"})) 154 } 155 156 // TestKubernetesClustersInstances is a test for the KubernetesClustersInstances method. 157 func TestKubernetesClustersInstances(t *testing.T) { 158 g := NewWithT(t) 159 160 client, err := NewFakeClient() 161 g.Expect(err).To(BeNil()) 162 163 client.Clusters = []KubernetesCluster{ 164 { 165 ID: "9c89d8b9-463d-45f2-8928-455eb3f3726", 166 Name: "foo-cluster", 167 Instances: []KubernetesInstance{ 168 { 169 ID: "ad0dbf3f-4036-47f5-b33b-6822cf90799c0", 170 Hostname: "foo", 171 }, 172 }, 173 }, 174 } 175 client.Instances = []Instance{ 176 { 177 ID: "ad0dbf3f-4036-47f5-b33b-6822cf90799c0", 178 Hostname: "foo", 179 }, 180 } 181 instances, err := client.ListKubernetesClusterInstances("9c89d8b9-463d-45f2-8928-455eb3f3726") 182 g.Expect(err).To(BeNil()) 183 g.Expect(len(instances)).To(Equal(1)) 184 185 instance, err := client.FindKubernetesClusterInstance("9c89d8b9-463d-45f2-8928-455eb3f3726", "ad0dbf3f-4036-47f5-b33b-6822cf90799c0") 186 g.Expect(err).To(BeNil()) 187 g.Expect(instance.ID).To(Equal("ad0dbf3f-4036-47f5-b33b-6822cf90799c0")) 188 g.Expect(instance.Hostname).To(Equal("foo")) 189 } 190 191 // TestKubernetesClustersPools is a test for the KubernetesClustersPools method. 192 func TestKubernetesClustersPools(t *testing.T) { 193 g := NewWithT(t) 194 195 client, err := NewFakeClient() 196 g.Expect(err).To(BeNil()) 197 198 client.Clusters = []KubernetesCluster{ 199 { 200 ID: "9c89d8b9-463d-45f2-8928-455eb3f3726", 201 Name: "foo-cluster", 202 Instances: []KubernetesInstance{ 203 { 204 ID: "ad0dbf3f-4036-47f5-b33b-6822cf90799c0", 205 Hostname: "foo", 206 }, 207 { 208 ID: "aa2de9f9-c26e-4faf-8af5-26d7c9e6facf", 209 Hostname: "bar", 210 }, 211 }, 212 Pools: []KubernetesPool{ 213 { 214 ID: "33de5de2-14fd-44ba-a621-f6efbeeb9639", 215 Count: 2, 216 Size: "small", 217 InstanceNames: []string{ 218 "foo", 219 "bar", 220 }, 221 Instances: []KubernetesInstance{ 222 { 223 ID: "ad0dbf3f-4036-47f5-b33b-6822cf90799c0", 224 Hostname: "foo", 225 }, 226 { 227 ID: "aa2de9f9-c26e-4faf-8af5-26d7c9e6facf", 228 Hostname: "bar", 229 }, 230 }, 231 }, 232 }, 233 }, 234 } 235 236 pools, err := client.ListKubernetesClusterPools("9c89d8b9-463d-45f2-8928-455eb3f3726") 237 g.Expect(err).To(BeNil()) 238 g.Expect(len(pools)).To(Equal(1)) 239 240 pool, err := client.GetKubernetesClusterPool("9c89d8b9-463d-45f2-8928-455eb3f3726", "33de5de2-14fd-44ba-a621-f6efbeeb9639") 241 g.Expect(err).To(BeNil()) 242 g.Expect(pool.ID).To(Equal("33de5de2-14fd-44ba-a621-f6efbeeb9639")) 243 g.Expect(len(pool.InstanceNames)).To(Equal(2)) 244 245 pool, err = client.FindKubernetesClusterPool("9c89d8b9-463d-45f2-8928-455eb3f3726", "33de5de2-14fd-44ba-a621-f6efbeeb9639") 246 g.Expect(err).To(BeNil()) 247 g.Expect(pool.ID).To(Equal("33de5de2-14fd-44ba-a621-f6efbeeb9639")) 248 g.Expect(len(pool.InstanceNames)).To(Equal(2)) 249 250 result, err := client.DeleteKubernetesClusterPoolInstance("9c89d8b9-463d-45f2-8928-455eb3f3726", "33de5de2-14fd-44ba-a621-f6efbeeb9639", "ad0dbf3f-4036-47f5-b33b-6822cf90799c0") 251 g.Expect(err).To(BeNil()) 252 g.Expect(string(result.Result)).To(Equal("success")) 253 254 pc := KubernetesClusterPoolUpdateConfig{ 255 Count: 4, 256 } 257 pool, err = client.UpdateKubernetesClusterPool("9c89d8b9-463d-45f2-8928-455eb3f3726", "33de5de2-14fd-44ba-a621-f6efbeeb9639", &pc) 258 g.Expect(err).To(BeNil()) 259 g.Expect(pool.Count).To(Equal(4)) 260 } 261 262 func TestPing(t *testing.T) { 263 // Create a new FakeClient with a non-nil PingErr 264 client := &FakeClient{ 265 PingErr: errors.New("ping error"), 266 } 267 268 // Call the Ping method 269 err := client.Ping() 270 271 // Check if the error returned by Ping is the same as the one we set 272 if err == nil || err.Error() != "ping error" { 273 t.Errorf("Expected 'ping error', got '%v'", err) 274 } 275 276 // Reset PingErr to nil 277 client.PingErr = nil 278 279 // Call the Ping method again 280 err = client.Ping() 281 282 // Check if the error is nil this time 283 if err != nil { 284 t.Errorf("Expected nil, got '%v'", err) 285 } 286 }