github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/testing/client_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk" 9 "github.com/huaweicloud/golangsdk/openstack" 10 th "github.com/huaweicloud/golangsdk/testhelper" 11 ) 12 13 const ID = "0123456789" 14 15 func TestAuthenticatedClientV3(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 20 fmt.Fprintf(w, ` 21 { 22 "versions": { 23 "values": [ 24 { 25 "status": "stable", 26 "id": "v3.0", 27 "links": [ 28 { "href": "%s", "rel": "self" } 29 ] 30 }, 31 { 32 "status": "stable", 33 "id": "v2.0", 34 "links": [ 35 { "href": "%s", "rel": "self" } 36 ] 37 } 38 ] 39 } 40 } 41 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") 42 }) 43 44 th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { 45 w.Header().Add("X-Subject-Token", ID) 46 47 w.WriteHeader(http.StatusCreated) 48 fmt.Fprintf(w, ` 49 { 50 "token": { 51 "expires_at": "2013-02-02T18:30:59.000000Z", 52 "project": { 53 "id": "" 54 } 55 } 56 } 57 `) 58 }) 59 60 options := golangsdk.AuthOptions{ 61 Username: "me", 62 Password: "secret", 63 DomainName: "default", 64 TenantName: "project", 65 IdentityEndpoint: th.Endpoint(), 66 } 67 client, err := openstack.AuthenticatedClient(options) 68 th.AssertNoErr(t, err) 69 th.CheckEquals(t, ID, client.TokenID) 70 } 71 72 func TestAuthenticatedClientV2(t *testing.T) { 73 th.SetupHTTP() 74 defer th.TeardownHTTP() 75 76 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 77 fmt.Fprintf(w, ` 78 { 79 "versions": { 80 "values": [ 81 { 82 "status": "experimental", 83 "id": "v3.0", 84 "links": [ 85 { "href": "%s", "rel": "self" } 86 ] 87 }, 88 { 89 "status": "stable", 90 "id": "v2.0", 91 "links": [ 92 { "href": "%s", "rel": "self" } 93 ] 94 } 95 ] 96 } 97 } 98 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") 99 }) 100 101 th.Mux.HandleFunc("/v2.0/tokens", func(w http.ResponseWriter, r *http.Request) { 102 fmt.Fprintf(w, ` 103 { 104 "access": { 105 "token": { 106 "id": "01234567890", 107 "expires": "2014-10-01T10:00:00.000000Z" 108 }, 109 "serviceCatalog": [ 110 { 111 "name": "Cloud Servers", 112 "type": "compute", 113 "endpoints": [ 114 { 115 "tenantId": "t1000", 116 "publicURL": "https://compute.north.host.com/v1/t1000", 117 "internalURL": "https://compute.north.internal/v1/t1000", 118 "region": "North", 119 "versionId": "1", 120 "versionInfo": "https://compute.north.host.com/v1/", 121 "versionList": "https://compute.north.host.com/" 122 }, 123 { 124 "tenantId": "t1000", 125 "publicURL": "https://compute.north.host.com/v1.1/t1000", 126 "internalURL": "https://compute.north.internal/v1.1/t1000", 127 "region": "North", 128 "versionId": "1.1", 129 "versionInfo": "https://compute.north.host.com/v1.1/", 130 "versionList": "https://compute.north.host.com/" 131 } 132 ], 133 "endpoints_links": [] 134 }, 135 { 136 "name": "Cloud Files", 137 "type": "object-store", 138 "endpoints": [ 139 { 140 "tenantId": "t1000", 141 "publicURL": "https://storage.north.host.com/v1/t1000", 142 "internalURL": "https://storage.north.internal/v1/t1000", 143 "region": "North", 144 "versionId": "1", 145 "versionInfo": "https://storage.north.host.com/v1/", 146 "versionList": "https://storage.north.host.com/" 147 }, 148 { 149 "tenantId": "t1000", 150 "publicURL": "https://storage.south.host.com/v1/t1000", 151 "internalURL": "https://storage.south.internal/v1/t1000", 152 "region": "South", 153 "versionId": "1", 154 "versionInfo": "https://storage.south.host.com/v1/", 155 "versionList": "https://storage.south.host.com/" 156 } 157 ] 158 } 159 ] 160 } 161 } 162 `) 163 }) 164 165 options := golangsdk.AuthOptions{ 166 Username: "me", 167 Password: "secret", 168 IdentityEndpoint: th.Endpoint(), 169 } 170 client, err := openstack.AuthenticatedClient(options) 171 th.AssertNoErr(t, err) 172 th.CheckEquals(t, "01234567890", client.TokenID) 173 } 174 175 func TestIdentityAdminV3Client(t *testing.T) { 176 th.SetupHTTP() 177 defer th.TeardownHTTP() 178 179 th.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 180 fmt.Fprintf(w, ` 181 { 182 "versions": { 183 "values": [ 184 { 185 "status": "stable", 186 "id": "v3.0", 187 "links": [ 188 { "href": "%s", "rel": "self" } 189 ] 190 }, 191 { 192 "status": "stable", 193 "id": "v2.0", 194 "links": [ 195 { "href": "%s", "rel": "self" } 196 ] 197 } 198 ] 199 } 200 } 201 `, th.Endpoint()+"v3/", th.Endpoint()+"v2.0/") 202 }) 203 204 th.Mux.HandleFunc("/v3/auth/tokens", func(w http.ResponseWriter, r *http.Request) { 205 w.Header().Add("X-Subject-Token", ID) 206 207 w.WriteHeader(http.StatusCreated) 208 fmt.Fprintf(w, ` 209 { 210 "token": { 211 "audit_ids": ["VcxU2JYqT8OzfUVvrjEITQ", "qNUTIJntTzO1-XUk5STybw"], 212 "catalog": [ 213 { 214 "endpoints": [ 215 { 216 "id": "39dc322ce86c4111b4f06c2eeae0841b", 217 "interface": "public", 218 "region": "RegionOne", 219 "url": "http://localhost:5000" 220 }, 221 { 222 "id": "ec642f27474842e78bf059f6c48f4e99", 223 "interface": "internal", 224 "region": "RegionOne", 225 "url": "http://localhost:5000" 226 }, 227 { 228 "id": "c609fc430175452290b62a4242e8a7e8", 229 "interface": "admin", 230 "region": "RegionOne", 231 "url": "http://localhost:35357" 232 } 233 ], 234 "id": "4363ae44bdf34a3981fde3b823cb9aa2", 235 "type": "identity", 236 "name": "keystone" 237 } 238 ], 239 "expires_at": "2013-02-27T18:30:59.999999Z", 240 "is_domain": false, 241 "issued_at": "2013-02-27T16:30:59.999999Z", 242 "methods": [ 243 "password" 244 ], 245 "project": { 246 "domain": { 247 "id": "1789d1", 248 "name": "example.com" 249 }, 250 "id": "263fd9", 251 "name": "project-x" 252 }, 253 "roles": [ 254 { 255 "id": "76e72a", 256 "name": "admin" 257 }, 258 { 259 "id": "f4f392", 260 "name": "member" 261 } 262 ], 263 "service_providers": [ 264 { 265 "auth_url":"https://example.com:5000/v3/OS-FEDERATION/identity_providers/acme/protocols/saml2/auth", 266 "id": "sp1", 267 "sp_url": "https://example.com:5000/Shibboleth.sso/SAML2/ECP" 268 }, 269 { 270 "auth_url":"https://other.example.com:5000/v3/OS-FEDERATION/identity_providers/acme/protocols/saml2/auth", 271 "id": "sp2", 272 "sp_url": "https://other.example.com:5000/Shibboleth.sso/SAML2/ECP" 273 } 274 ], 275 "user": { 276 "domain": { 277 "id": "1789d1", 278 "name": "example.com" 279 }, 280 "id": "0ca8f6", 281 "name": "Joe", 282 "password_expires_at": "2016-11-06T15:32:17.000000" 283 } 284 } 285 } 286 `) 287 }) 288 289 options := golangsdk.AuthOptions{ 290 Username: "me", 291 Password: "secret", 292 DomainID: "12345", 293 IdentityEndpoint: th.Endpoint(), 294 } 295 pc, err := openstack.AuthenticatedClient(options) 296 th.AssertNoErr(t, err) 297 sc, err := openstack.NewIdentityV3(pc, golangsdk.EndpointOpts{ 298 Region: "RegionOne", 299 Availability: golangsdk.AvailabilityAdmin, 300 }) 301 th.AssertNoErr(t, err) 302 th.CheckEquals(t, "http://localhost:35357/v3/", sc.Endpoint) 303 } 304 305 func testAuthenticatedClientFails(t *testing.T, endpoint string) { 306 options := golangsdk.AuthOptions{ 307 Username: "me", 308 Password: "secret", 309 DomainName: "default", 310 TenantName: "project", 311 IdentityEndpoint: endpoint, 312 } 313 _, err := openstack.AuthenticatedClient(options) 314 if err == nil { 315 t.Fatal("expected error but call succeeded") 316 } 317 } 318 319 func TestAuthenticatedClientV3Fails(t *testing.T) { 320 testAuthenticatedClientFails(t, "http://bad-address.example.com/v3") 321 } 322 323 func TestAuthenticatedClientV2Fails(t *testing.T) { 324 testAuthenticatedClientFails(t, "http://bad-address.example.com/v2.0") 325 }