github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v2/tenants/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/openstack/identity/v2/tenants" 8 "github.com/gophercloud/gophercloud/pagination" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 func TestListTenants(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 HandleListTenantsSuccessfully(t) 17 18 count := 0 19 err := tenants.List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) { 20 count++ 21 22 actual, err := tenants.ExtractTenants(page) 23 th.AssertNoErr(t, err) 24 25 th.CheckDeepEquals(t, ExpectedTenantSlice, actual) 26 27 return true, nil 28 }) 29 th.AssertNoErr(t, err) 30 th.CheckEquals(t, count, 1) 31 } 32 33 func TestCreateTenant(t *testing.T) { 34 th.SetupHTTP() 35 defer th.TeardownHTTP() 36 37 mockCreateTenantResponse(t) 38 39 opts := tenants.CreateOpts{ 40 Name: "new_tenant", 41 Description: "This is new tenant", 42 Enabled: gophercloud.Enabled, 43 } 44 45 tenant, err := tenants.Create(client.ServiceClient(), opts).Extract() 46 47 th.AssertNoErr(t, err) 48 49 expected := &tenants.Tenant{ 50 Name: "new_tenant", 51 Description: "This is new tenant", 52 Enabled: true, 53 ID: "5c62ef576dc7444cbb73b1fe84b97648", 54 } 55 56 th.AssertDeepEquals(t, expected, tenant) 57 } 58 59 func TestDeleteTenant(t *testing.T) { 60 th.SetupHTTP() 61 defer th.TeardownHTTP() 62 63 mockDeleteTenantResponse(t) 64 65 err := tenants.Delete(client.ServiceClient(), "2466f69cd4714d89a548a68ed97ffcd4").ExtractErr() 66 th.AssertNoErr(t, err) 67 } 68 69 func TestUpdateTenant(t *testing.T) { 70 th.SetupHTTP() 71 defer th.TeardownHTTP() 72 73 mockUpdateTenantResponse(t) 74 75 id := "5c62ef576dc7444cbb73b1fe84b97648" 76 description := "This is new name" 77 opts := tenants.UpdateOpts{ 78 Name: "new_name", 79 Description: &description, 80 Enabled: gophercloud.Enabled, 81 } 82 83 tenant, err := tenants.Update(client.ServiceClient(), id, opts).Extract() 84 85 th.AssertNoErr(t, err) 86 87 expected := &tenants.Tenant{ 88 Name: "new_name", 89 ID: id, 90 Description: "This is new name", 91 Enabled: true, 92 } 93 94 th.AssertDeepEquals(t, expected, tenant) 95 } 96 97 func TestGetTenant(t *testing.T) { 98 th.SetupHTTP() 99 defer th.TeardownHTTP() 100 101 mockGetTenantResponse(t) 102 103 tenant, err := tenants.Get(client.ServiceClient(), "5c62ef576dc7444cbb73b1fe84b97648").Extract() 104 th.AssertNoErr(t, err) 105 106 expected := &tenants.Tenant{ 107 Name: "new_tenant", 108 ID: "5c62ef576dc7444cbb73b1fe84b97648", 109 Description: "This is new tenant", 110 Enabled: true, 111 } 112 113 th.AssertDeepEquals(t, expected, tenant) 114 }