github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/deh/v1/hosts/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/huaweicloud/golangsdk/openstack/deh/v1/common" 9 "github.com/huaweicloud/golangsdk/openstack/deh/v1/hosts" 10 th "github.com/huaweicloud/golangsdk/testhelper" 11 "github.com/huaweicloud/golangsdk/testhelper/client" 12 ) 13 14 func TestGet(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc(dehEndpoint+"/"+HostID, func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "GET") 20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 21 22 w.Header().Add("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 25 fmt.Fprintf(w, getResponse) 26 }) 27 28 s, err := hosts.Get(client.ServiceClient(), HostID).Extract() 29 th.AssertNoErr(t, err) 30 th.AssertEquals(t, "66156a61-27c2-4169-936b-910dd9c73da3", s.ID) 31 th.AssertEquals(t, "test-aj2", s.Name) 32 th.AssertEquals(t, "eu-de-02", s.Az) 33 th.AssertEquals(t, "available", s.State) 34 th.AssertDeepEquals(t, hosts.HostPropertiesOpts{ 35 HostTypeName: "High performance", 36 HostType: "h1", 37 Vcpus: 36, 38 Memory: 270336, 39 Cores: 12, 40 Sockets: 2, 41 }, s.HostProperties) 42 } 43 44 func TestList(t *testing.T) { 45 th.SetupHTTP() 46 defer th.TeardownHTTP() 47 48 th.Mux.HandleFunc(dehEndpoint, func(w http.ResponseWriter, r *http.Request) { 49 th.TestMethod(t, r, "GET") 50 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 51 52 w.Header().Add("Content-Type", "application/json") 53 w.WriteHeader(http.StatusOK) 54 55 fmt.Fprintf(w, listResponse) 56 }) 57 58 //count := 0 59 60 host, err := hosts.List(client.ServiceClient(), hosts.ListOpts{}).AllPages() 61 62 actual, err := hosts.ExtractHosts(host) 63 64 if err != nil { 65 t.Errorf("Failed to extract hosts: %v", err) 66 } 67 68 expected := []hosts.Host{ 69 { 70 Az: "eu-de-01", 71 Name: "c2c-deh-test", 72 AvailableMemory: 262144, 73 AvailableVcpus: 70, 74 ID: "671611d2-b45c-4648-9e78-06eb24522291", 75 State: "available", 76 InstanceTotal: 2, 77 AutoPlacement: "off", 78 TenantId: "17fbda95add24720a4038ba4b1c705ed", 79 HostProperties: hosts.HostPropertiesOpts{ 80 HostType: "general", 81 Vcpus: 72, 82 Memory: 270336, 83 Cores: 12, 84 Sockets: 2, 85 HostTypeName: "General computing", 86 }, 87 InstanceUuids: []string{"3de1ce75-2550-4a46-a689-dd33ca2b62d6", 88 "885dc71d-905d-48b5-bae7-db66801dc175"}, 89 }, 90 } 91 92 th.AssertDeepEquals(t, expected, actual) 93 } 94 95 func TestListServer(t *testing.T) { 96 th.SetupHTTP() 97 defer th.TeardownHTTP() 98 99 th.Mux.HandleFunc(dehEndpoint+"/"+HostID+"/servers", func(w http.ResponseWriter, r *http.Request) { 100 th.TestMethod(t, r, "GET") 101 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 102 103 w.Header().Add("Content-Type", "application/json") 104 w.WriteHeader(http.StatusOK) 105 106 fmt.Fprintf(w, listserverResponse) 107 }) 108 109 actual, err := hosts.ListServer(client.ServiceClient(), HostID, hosts.ListServerOpts{}) 110 th.AssertNoErr(t, err) 111 112 expected := []hosts.Server{ 113 { 114 Status: "ACTIVE", 115 Addresses: map[string]interface{}{ 116 "0b98c646-617f-4d90-9ca5-385f0cd73ea7": []interface{}{ 117 map[string]interface{}{ 118 "version": float64(4), 119 "addr": "192.168.3.133", 120 }, 121 }, 122 }, 123 Flavor: map[string]interface{}{ 124 "id": "normal1", 125 }, 126 ID: "3de1ce75-2550-4a46-a689-dd33ca2b62d6", 127 UserID: "6d78fa8550ae45d6932a1fadfb1fa552", 128 Name: "c2c-ecs-test-2", 129 TenantID: "17fbda95add24720a4038ba4b1c705ed", 130 Metadata: map[string]string{ 131 "metering.image_id": "c0ea3ff1-432e-4650-8a1b-372a80b2d2be", 132 "metering.imagetype": "gold", 133 "metering.resourcespeccode": "deh.linux", 134 "metering.cloudServiceType": "sys.service.type.ec2", 135 "image_name": "Standard_CentOS_7_latest", 136 "metering.resourcetype": "1", 137 "os_bit": "64", 138 "vpc_id": "0b98c646-617f-4d90-9ca5-385f0cd73ea7", 139 "os_type": "Linux", 140 "charging_mode": "0", 141 }, 142 }, 143 } 144 th.AssertDeepEquals(t, expected, actual) 145 } 146 147 func TestAllocateDeH(t *testing.T) { 148 th.SetupHTTP() 149 defer th.TeardownHTTP() 150 th.Mux.HandleFunc(dehEndpoint, func(w http.ResponseWriter, r *http.Request) { 151 th.TestMethod(t, r, "POST") 152 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 153 th.TestHeader(t, r, "Content-Type", "application/json") 154 th.TestHeader(t, r, "Accept", "application/json") 155 th.TestJSONRequest(t, r, allocateRequest) 156 w.Header().Add("Content-Type", "application/json") 157 w.WriteHeader(http.StatusOK) 158 fmt.Fprintf(w, allocateResponse) 159 }) 160 161 c := client.ServiceClient() 162 allocateOpts := hosts.AllocateOpts{Name: "Test-1", 163 Az: "eu-de-02", 164 HostType: "h1", 165 AutoPlacement: "off", 166 Quantity: 2} 167 s, err := hosts.Allocate(c, allocateOpts).ExtractHost() 168 169 th.AssertNoErr(t, err) 170 th.AssertDeepEquals(t, s, &hosts.AllocatedHosts{ 171 AllocatedHostIds: []string{"fb4733fd-70a3-44e1-a1cb-0311f028d7e5", 172 "7408f985-047d-4313-b3c8-8e12bef01d12"}, 173 }) 174 } 175 176 func TestUpdateDeH(t *testing.T) { 177 th.SetupHTTP() 178 defer th.TeardownHTTP() 179 th.Mux.HandleFunc(dehEndpoint+"/"+HostID, func(w http.ResponseWriter, r *http.Request) { 180 th.TestMethod(t, r, "PUT") 181 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 182 th.TestJSONRequest(t, r, updateRequest) 183 w.WriteHeader(http.StatusNoContent) 184 }) 185 186 c := client.ServiceClient() 187 updateOpts := hosts.UpdateOpts{Name: "Test-2", 188 AutoPlacement: "off", 189 } 190 s := hosts.Update(c, HostID, updateOpts) 191 th.AssertNoErr(t, s.Err) 192 } 193 194 func TestDeleteDeH(t *testing.T) { 195 th.SetupHTTP() 196 defer th.TeardownHTTP() 197 198 th.Mux.HandleFunc(dehEndpoint+"/"+HostID, func(w http.ResponseWriter, r *http.Request) { 199 th.TestMethod(t, r, "DELETE") 200 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 201 w.WriteHeader(http.StatusAccepted) 202 }) 203 204 result := hosts.Delete(client.ServiceClient(), HostID) 205 th.AssertNoErr(t, result.Err) 206 }