github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/cce/v3/nodes/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/common" 9 "github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/nodes" 10 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 11 ) 12 13 func TestListNode(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes", func(w http.ResponseWriter, r *http.Request) { 18 th.TestMethod(t, r, "GET") 19 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 20 w.Header().Add("Content-Type", "application/json") 21 w.WriteHeader(http.StatusOK) 22 23 _, _ = fmt.Fprint(w, ` 24 { 25 "kind": "List", 26 "apiVersion": "v3", 27 "items": 28 [ 29 { 30 "kind": "Host", 31 "apiVersion": "v3", 32 "metadata": { 33 "name": "test-node-1234", 34 "uid": "b99acd73-5d7c-11e8-8e76-0255ac101929" 35 }, 36 "spec": { 37 "flavor": "s1.medium", 38 "az": "cn-east-2a", 39 "login": { 40 "sshKey": "test-keypair", 41 "userPassword": {} 42 }, 43 "rootVolume": { 44 "volumetype": "SATA", 45 "size": 40 46 }, 47 "dataVolumes": [ 48 { 49 "volumetype": "SATA", 50 "size": 100 51 } 52 ], 53 "runtime": { 54 "name": "containerd" 55 }, 56 "publicIP": { 57 "eip": { 58 "bandwidth": {} 59 } 60 }, 61 "billingMode": 0 62 }, 63 "status": { 64 "phase": "Active", 65 "serverId": "41748e56-33d4-46a1-aa57-2c8c29907995", 66 "privateIP": "192.168.0.3" 67 } 68 } 69 ] 70 } 71 `) 72 }) 73 74 listNodes := nodes.ListOpts{Name: "test-node-1234"} 75 actual, err := nodes.List(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", listNodes) 76 77 if err != nil { 78 t.Errorf("Failed to extract nodes: %v", err) 79 } 80 81 var expected = []nodes.Nodes{ 82 { 83 Kind: "Host", 84 Apiversion: "v3", 85 Metadata: nodes.Metadata{Name: "test-node-1234", 86 Id: "b99acd73-5d7c-11e8-8e76-0255ac101929"}, 87 Spec: nodes.Spec{Az: "cn-east-2a", 88 Login: nodes.LoginSpec{SshKey: "test-keypair"}, 89 RootVolume: nodes.VolumeSpec{Size: 40, VolumeType: "SATA"}, 90 BillingMode: 0, 91 DataVolumes: []nodes.VolumeSpec{ 92 { 93 VolumeType: "SATA", 94 Size: 100, 95 }}, 96 Flavor: "s1.medium", 97 Runtime: nodes.RuntimeSpec{ 98 Name: "containerd", 99 }, 100 }, 101 Status: nodes.Status{Phase: "Active", ServerID: "41748e56-33d4-46a1-aa57-2c8c29907995", PrivateIP: "192.168.0.3"}, 102 }, 103 } 104 th.AssertDeepEquals(t, expected, actual) 105 } 106 107 func TestGetV3Node(t *testing.T) { 108 th.SetupHTTP() 109 defer th.TeardownHTTP() 110 111 th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes/cf4bc001-58f1-11e8-ad73-0255ac101926", func(w http.ResponseWriter, r *http.Request) { 112 th.TestMethod(t, r, "GET") 113 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 114 w.Header().Add("Content-Type", "application/json") 115 w.WriteHeader(http.StatusOK) 116 _, _ = fmt.Fprint(w, Output) 117 }) 118 119 actual, err := nodes.Get(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", "cf4bc001-58f1-11e8-ad73-0255ac101926").Extract() 120 th.AssertNoErr(t, err) 121 expected := Expected 122 th.AssertDeepEquals(t, expected, actual) 123 124 } 125 126 func TestCreateV3Node(t *testing.T) { 127 th.SetupHTTP() 128 defer th.TeardownHTTP() 129 130 th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes", func(w http.ResponseWriter, r *http.Request) { 131 th.TestMethod(t, r, "POST") 132 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 133 th.TestHeader(t, r, "Content-Type", "application/json") 134 th.TestHeader(t, r, "Accept", "application/json") 135 136 th.TestJSONRequest(t, r, ` 137 { 138 "apiVersion": "v3", 139 "kind": "Node", 140 "metadata": { 141 "name": "test-node" 142 }, 143 "spec": { 144 "az": "eu-de-01", 145 "count": 1, 146 "extendParam": { 147 }, 148 "dataVolumes": [ 149 { 150 "size": 100, 151 "volumetype": "SATA" 152 } 153 ], 154 "flavor": "s3.large.2", 155 "login": { 156 "sshKey": "test-keypair", 157 "userPassword": { 158 "password": "", 159 "username": "" 160 } 161 }, 162 "nodeNicSpec": { 163 "primaryNic": {} 164 }, 165 "publicIP": { 166 "eip": { 167 "bandwidth": {} 168 } 169 }, 170 "rootVolume": { 171 "size": 40, 172 "volumetype": "SATA" 173 }, 174 "runtime": { 175 "name": "containerd" 176 } 177 } 178 } 179 `) 180 181 w.Header().Set("Content-Type", "application/json") 182 w.WriteHeader(http.StatusCreated) 183 _, _ = fmt.Fprint(w, Output) 184 }) 185 options := nodes.CreateOpts{Kind: "Node", 186 ApiVersion: "v3", 187 Metadata: nodes.CreateMetaData{Name: "test-node"}, 188 Spec: nodes.Spec{ 189 Flavor: "s3.large.2", Az: "eu-de-01", 190 Login: nodes.LoginSpec{ 191 SshKey: "test-keypair", 192 }, 193 RootVolume: nodes.VolumeSpec{ 194 Size: 40, 195 VolumeType: "SATA", 196 }, 197 DataVolumes: []nodes.VolumeSpec{ 198 { 199 Size: 100, 200 VolumeType: "SATA", 201 }, 202 }, 203 Count: 1, 204 Runtime: nodes.RuntimeSpec{ 205 Name: "containerd", 206 }, 207 }, 208 } 209 actual, err := nodes.Create(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", options).Extract() 210 th.AssertNoErr(t, err) 211 expected := Expected 212 th.AssertDeepEquals(t, expected, actual) 213 214 } 215 216 func TestUpdateV3Node(t *testing.T) { 217 th.SetupHTTP() 218 defer th.TeardownHTTP() 219 220 th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes/cf4bc001-58f1-11e8-ad73-0255ac101926", func(w http.ResponseWriter, r *http.Request) { 221 th.TestMethod(t, r, "PUT") 222 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 223 th.TestHeader(t, r, "Content-Type", "application/json") 224 th.TestHeader(t, r, "Accept", "application/json") 225 th.TestJSONRequest(t, r, ` 226 { 227 "metadata": { 228 "name": "test-node" 229 } 230 } `) 231 232 w.Header().Add("Content-Type", "application/json") 233 w.WriteHeader(http.StatusOK) 234 235 _, _ = fmt.Fprint(w, Output) 236 }) 237 options := nodes.UpdateOpts{Metadata: nodes.UpdateMetadata{Name: "test-node"}} 238 actual, err := nodes.Update(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", "cf4bc001-58f1-11e8-ad73-0255ac101926", options).Extract() 239 th.AssertNoErr(t, err) 240 expected := Expected 241 th.AssertDeepEquals(t, expected, actual) 242 } 243 244 func TestDeleteNode(t *testing.T) { 245 th.SetupHTTP() 246 defer th.TeardownHTTP() 247 248 th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/cec124c2-58f1-11e8-ad73-0255ac101926/nodes/cf4bc001-58f1-11e8-ad73-0255ac101926", func(w http.ResponseWriter, r *http.Request) { 249 th.TestMethod(t, r, "DELETE") 250 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 251 w.WriteHeader(http.StatusOK) 252 }) 253 254 err := nodes.Delete(fake.ServiceClient(), "cec124c2-58f1-11e8-ad73-0255ac101926", "cf4bc001-58f1-11e8-ad73-0255ac101926").ExtractErr() 255 th.AssertNoErr(t, err) 256 257 } 258 259 func TestGetV3Job(t *testing.T) { 260 th.SetupHTTP() 261 defer th.TeardownHTTP() 262 263 th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/jobs/73ce03fd-8b1b-11e8-8f9d-0255ac10193f", func(w http.ResponseWriter, r *http.Request) { 264 th.TestMethod(t, r, "GET") 265 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 266 w.Header().Add("Content-Type", "application/json") 267 w.WriteHeader(http.StatusOK) 268 _, _ = fmt.Fprint(w, JobOutput) 269 }) 270 271 actual, err := nodes.GetJobDetails(fake.ServiceClient(), "73ce03fd-8b1b-11e8-8f9d-0255ac10193f").ExtractJob() 272 th.AssertNoErr(t, err) 273 expected := ExpectedJob 274 th.AssertDeepEquals(t, expected, actual) 275 276 }