github.com/civo/civogo@v0.3.65/volume_test.go (about) 1 package civogo 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestListVolumes(t *testing.T) { 9 client, server, _ := NewClientForTesting(map[string]string{ 10 "/v2/volumes": `[{ 11 "id": "12345", 12 "name": "my-volume", 13 "instance_id": "null", 14 "mountpoint": "null", 15 "openstack_id": "null", 16 "size_gb": 25, 17 "bootable": false 18 }]`, 19 }) 20 defer server.Close() 21 got, err := client.ListVolumes() 22 23 if err != nil { 24 t.Errorf("Request returned an error: %s", err) 25 return 26 } 27 expected := []Volume{{ID: "12345", InstanceID: "null", Name: "my-volume", MountPoint: "null", SizeGigabytes: 25, Bootable: false}} 28 if !reflect.DeepEqual(got, expected) { 29 t.Errorf("Expected %+v, got %+v", expected, got) 30 } 31 } 32 33 func TestListVolumesForCluster(t *testing.T) { 34 client, server, _ := NewClientForTesting(map[string]string{ 35 36 "/v2/kubernetes/clusters": `{"page":1,"per_page":20,"pages":1,"items":[ 37 { "id": "69a23478-a89e-41d2-97b1-6f4c341cee70", "name": "your-first-cluster-name", "version": "2", "status": "ACTIVE", "ready": true, "num_target_nodes": 1, "target_nodes_size": "g2.xsmall", "built_at": "2019-09-23T13:04:23.000+01:00", "kubeconfig": "YAML_VERSION_OF_KUBECONFIG_HERE\n", "kubernetes_version": "0.8.1", "api_endpoint": "https://your.cluster.ip.address:6443", "dns_entry": "69a23478-a89e-41d2-97b1-6f4c341cee70.k8s.civo.com", "tags": [], "created_at": "2019-09-23T13:02:59.000+01:00", "firewall_id": "42118911-44c2-4cab-ad77-bcae062815b3", "instances": [{ "hostname": "kube-master-HEXDIGITS", "size": "g2.xsmall", "region": "lon1", "created_at": "2019-09-23T13:03:00.000+01:00", "status": "ACTIVE", "firewall_id": "5f0ba9ed-5ca7-4e14-9a09-449a84196d64", "public_ip": "your.cluster.ip.address", "tags": ["civo-kubernetes:installed", "civo-kubernetes:master"] }], "installed_applications": [{ "application": "Traefik", "title": null, "version": "(default)", "dependencies": null, "maintainer": "@Rancher_Labs", "description": "A reverse proxy/load-balancer that's easy, dynamic, automatic, fast, full-featured, open source, production proven and provides metrics.", "post_install": "Some documentation here\n", "installed": true, "url": "https://traefik.io", "category": "architecture", "updated_at": "2019-09-23T13:02:59.000+01:00", "image_url": "https://api.civo.com/k3s-marketplace/traefik.png", "plan": null, "configuration": {} }] } 38 ]}`, 39 "/v2/volumes": `[{ "id": "12345", "name": "my-volume", "size_gb": 25, "bootable": false, "cluster_id": "69a23478-a89e-41d2-97b1-6f4c341cee70"}]`, 40 }) 41 defer server.Close() 42 got, err := client.ListVolumesForCluster("69a23478-a89e-41d2-97b1-6f4c341cee70") 43 44 if err != nil { 45 t.Errorf("Request returned an error: %s", err) 46 return 47 } 48 expected := []Volume{{ID: "12345", Name: "my-volume", SizeGigabytes: 25, Bootable: false, ClusterID: "69a23478-a89e-41d2-97b1-6f4c341cee70"}} 49 if !reflect.DeepEqual(got, expected) { 50 t.Errorf("Expected %+v, got %+v", expected, got) 51 } 52 } 53 54 func TestListDanglingVolumes(t *testing.T) { 55 client, server, _ := NewClientForTesting(map[string]string{ 56 "/v2/volumes": `[{ 57 "id": "12345", 58 "name": "my-volume", 59 "cluster_id": "69a23478-a89e-41d2-97b1-6f4c341cee70", 60 "size_gb": 25, 61 "bootable": false 62 }, 63 { 64 "id": "34567", 65 "name": "my-volume-two", 66 "size_gb": 25, 67 "bootable": false 68 }]`, 69 }) 70 defer server.Close() 71 got, err := client.ListDanglingVolumes() 72 73 if err != nil { 74 t.Errorf("Request returned an error: %s", err) 75 return 76 } 77 // Should only return the dangling volume (Vol with a cluster id but cluster doesn't exist) 78 expected := []Volume{{ID: "12345", Name: "my-volume", ClusterID: "69a23478-a89e-41d2-97b1-6f4c341cee70", SizeGigabytes: 25, Bootable: false}} 79 if !reflect.DeepEqual(got, expected) { 80 t.Errorf("Expected %+v, got %+v", expected, got) 81 } 82 } 83 84 func TestFindVolume(t *testing.T) { 85 client, server, _ := NewClientForTesting(map[string]string{ 86 "/v2/volumes": `[ 87 { 88 "id": "12345", 89 "name": "my-volume", 90 "instance_id": "null", 91 "mountpoint": "null", 92 "openstack_id": "null", 93 "size_gb": 25, 94 "bootable": false 95 }, 96 { 97 "id": "67890", 98 "name": "other-volume", 99 "instance_id": "null", 100 "mountpoint": "null", 101 "openstack_id": "null", 102 "size_gb": 25, 103 "bootable": false 104 } 105 ]`, 106 }) 107 defer server.Close() 108 109 got, _ := client.FindVolume("34") 110 if got.ID != "12345" { 111 t.Errorf("Expected %s, got %s", "12345", got.ID) 112 } 113 114 got, _ = client.FindVolume("89") 115 if got.ID != "67890" { 116 t.Errorf("Expected %s, got %s", "67890", got.ID) 117 } 118 119 got, _ = client.FindVolume("my") 120 if got.ID != "12345" { 121 t.Errorf("Expected %s, got %s", "12345", got.ID) 122 } 123 124 got, _ = client.FindVolume("other") 125 if got.ID != "67890" { 126 t.Errorf("Expected %s, got %s", "67890", got.ID) 127 } 128 129 _, err := client.FindVolume("volume") 130 if err.Error() != "MultipleMatchesError: unable to find volume because there were multiple matches" { 131 t.Errorf("Expected %s, got %s", "unable to find volume because there were multiple matches", err.Error()) 132 } 133 134 _, err = client.FindVolume("missing") 135 if err.Error() != "ZeroMatchesError: unable to find missing, zero matches" { 136 t.Errorf("Expected %s, got %s", "unable to find missing, zero matches", err.Error()) 137 } 138 } 139 140 func TestNewVolume(t *testing.T) { 141 client, server, _ := NewClientForTesting(map[string]string{ 142 "/v2/volumes": `{ 143 "id": "76cc107f-fbef-4e2b-b97f-f5d34f4075d3", 144 "name": "my-volume", 145 "result": "success" 146 }`, 147 }) 148 defer server.Close() 149 150 cfg := &VolumeConfig{Name: "my-volume", SizeGigabytes: 25, Bootable: false} 151 got, err := client.NewVolume(cfg) 152 if err != nil { 153 t.Errorf("Request returned an error: %s", err) 154 return 155 } 156 157 expected := &VolumeResult{ 158 ID: "76cc107f-fbef-4e2b-b97f-f5d34f4075d3", 159 Name: "my-volume", 160 Result: "success", 161 } 162 163 if expected.ID != got.ID { 164 t.Errorf("Expected %s, got %s", expected.ID, got.ID) 165 } 166 167 if expected.Name != got.Name { 168 t.Errorf("Expected %s, got %s", expected.Name, got.Name) 169 } 170 171 if expected.Result != got.Result { 172 t.Errorf("Expected %s, got %s", expected.Result, got.Result) 173 } 174 } 175 176 func TestResizeVolumes(t *testing.T) { 177 client, server, _ := NewClientForTesting(map[string]string{ 178 "/v2/volumes/12346/resize": `{"result": "success"}`, 179 }) 180 defer server.Close() 181 got, err := client.ResizeVolume("12346", 25) 182 if err != nil { 183 t.Errorf("Request returned an error: %s", err) 184 return 185 } 186 187 expected := &SimpleResponse{Result: "success"} 188 if !reflect.DeepEqual(got, expected) { 189 t.Errorf("Expected %+v, got %+v", expected, got) 190 } 191 } 192 193 func TestAttachVolumes(t *testing.T) { 194 client, server, _ := NewClientForTesting(map[string]string{ 195 "/v2/volumes/12346/attach": `{"result": "success"}`, 196 }) 197 defer server.Close() 198 got, err := client.AttachVolume("12346", "123456") 199 if err != nil { 200 t.Errorf("Request returned an error: %s", err) 201 return 202 } 203 204 expected := &SimpleResponse{Result: "success"} 205 if !reflect.DeepEqual(got, expected) { 206 t.Errorf("Expected %+v, got %+v", expected, got) 207 } 208 } 209 210 func TestDetachVolumes(t *testing.T) { 211 client, server, _ := NewClientForTesting(map[string]string{ 212 "/v2/volumes/12346/detach": `{"result": "success"}`, 213 }) 214 defer server.Close() 215 got, err := client.DetachVolume("12346") 216 if err != nil { 217 t.Errorf("Request returned an error: %s", err) 218 return 219 } 220 221 expected := &SimpleResponse{Result: "success"} 222 if !reflect.DeepEqual(got, expected) { 223 t.Errorf("Expected %+v, got %+v", expected, got) 224 } 225 } 226 227 func TestDeleteVolumes(t *testing.T) { 228 client, server, _ := NewClientForTesting(map[string]string{ 229 "/v2/volumes/12346": `{"result": "success"}`, 230 }) 231 defer server.Close() 232 got, err := client.DeleteVolume("12346") 233 if err != nil { 234 t.Errorf("Request returned an error: %s", err) 235 return 236 } 237 238 expected := &SimpleResponse{Result: "success"} 239 if !reflect.DeepEqual(got, expected) { 240 t.Errorf("Expected %+v, got %+v", expected, got) 241 } 242 } 243 244 func TestResizeVolume(t *testing.T) { 245 client, server, _ := NewClientForTesting(map[string]string{ 246 "/v2/volumes/12346/resize": `{"result": "success"}`, 247 }) 248 defer server.Close() 249 got, err := client.ResizeVolume("12346", 20) 250 if err != nil { 251 t.Errorf("Request returned an error: %s", err) 252 return 253 } 254 255 expected := &SimpleResponse{Result: "success"} 256 if !reflect.DeepEqual(got, expected) { 257 t.Errorf("Expected %+v, got %+v", expected, got) 258 } 259 }