Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 1 | // +build acceptance compute quotasets |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 6 | "fmt" |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 7 | "testing" |
| 8 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud" |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/acceptance/clients" |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/acceptance/tools" |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 12 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/quotasets" |
| 13 | "github.com/gophercloud/gophercloud/openstack/identity/v2/tenants" |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 14 | th "github.com/gophercloud/gophercloud/testhelper" |
| 15 | "os" |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 16 | ) |
| 17 | |
Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 18 | func TestQuotasetGet(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 19 | client, err := clients.NewComputeV2Client() |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 20 | if err != nil { |
| 21 | t.Fatalf("Unable to create a compute client: %v", err) |
| 22 | } |
| 23 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 24 | identityClient, err := clients.NewIdentityV2Client() |
Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 25 | if err != nil { |
| 26 | t.Fatalf("Unable to get a new identity client: %v", err) |
| 27 | } |
| 28 | |
| 29 | tenantID, err := getTenantID(t, identityClient) |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | |
Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 34 | quotaSet, err := quotasets.Get(client, tenantID).Extract() |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 38 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 39 | tools.PrintResource(t, quotaSet) |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 40 | } |
| 41 | |
Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 42 | func getTenantID(t *testing.T, client *gophercloud.ServiceClient) (string, error) { |
| 43 | allPages, err := tenants.List(client, nil).AllPages() |
| 44 | if err != nil { |
| 45 | t.Fatalf("Unable to get list of tenants: %v", err) |
| 46 | } |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 47 | |
Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 48 | allTenants, err := tenants.ExtractTenants(allPages) |
| 49 | if err != nil { |
| 50 | t.Fatalf("Unable to extract tenants: %v", err) |
| 51 | } |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 52 | |
Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 53 | for _, tenant := range allTenants { |
| 54 | return tenant.ID, nil |
| 55 | } |
Dan Kirkwood | ed3f5fd | 2016-03-16 12:41:47 -0600 | [diff] [blame] | 56 | |
Joe Topjian | 713e51a | 2016-07-25 19:06:25 +0000 | [diff] [blame] | 57 | return "", fmt.Errorf("Unable to get tenant ID") |
| 58 | } |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 59 | |
| 60 | func getTenantIDByName(t *testing.T, client *gophercloud.ServiceClient, name string) (string, error) { |
| 61 | allPages, err := tenants.List(client, nil).AllPages() |
| 62 | if err != nil { |
| 63 | t.Fatalf("Unable to get list of tenants: %v", err) |
| 64 | } |
| 65 | |
| 66 | allTenants, err := tenants.ExtractTenants(allPages) |
| 67 | if err != nil { |
| 68 | t.Fatalf("Unable to extract tenants: %v", err) |
| 69 | } |
| 70 | |
| 71 | for _, tenant := range allTenants { |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 72 | if tenant.Name == name { |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 73 | return tenant.ID, nil |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return "", fmt.Errorf("Unable to get tenant ID") |
| 78 | } |
| 79 | |
| 80 | //What will be sent as desired Quotas to the Server |
| 81 | var UpdatQuotaOpts = quotasets.UpdateOpts{ |
| 82 | FixedIps: gophercloud.IntToPointer(10), |
| 83 | FloatingIps: gophercloud.IntToPointer(10), |
| 84 | InjectedFileContentBytes: gophercloud.IntToPointer(10240), |
| 85 | InjectedFilePathBytes: gophercloud.IntToPointer(255), |
| 86 | InjectedFiles: gophercloud.IntToPointer(5), |
| 87 | KeyPairs: gophercloud.IntToPointer(10), |
| 88 | MetadataItems: gophercloud.IntToPointer(128), |
| 89 | Ram: gophercloud.IntToPointer(20000), |
| 90 | SecurityGroupRules: gophercloud.IntToPointer(20), |
| 91 | SecurityGroups: gophercloud.IntToPointer(10), |
| 92 | Cores: gophercloud.IntToPointer(10), |
| 93 | Instances: gophercloud.IntToPointer(4), |
| 94 | ServerGroups: gophercloud.IntToPointer(2), |
| 95 | ServerGroupMembers: gophercloud.IntToPointer(3), |
| 96 | } |
| 97 | |
| 98 | //What the Server hopefully returns as the new Quotas |
| 99 | var UpdatedQuotas = quotasets.QuotaSet{ |
| 100 | FixedIps: 10, |
| 101 | FloatingIps: 10, |
| 102 | InjectedFileContentBytes: 10240, |
| 103 | InjectedFilePathBytes: 255, |
| 104 | InjectedFiles: 5, |
| 105 | KeyPairs: 10, |
| 106 | MetadataItems: 128, |
| 107 | Ram: 20000, |
| 108 | SecurityGroupRules: 20, |
| 109 | SecurityGroups: 10, |
| 110 | Cores: 10, |
| 111 | Instances: 4, |
| 112 | ServerGroups: 2, |
| 113 | ServerGroupMembers: 3, |
| 114 | } |
| 115 | |
| 116 | func TestQuotasetUpdateDelete(t *testing.T) { |
| 117 | client, err := clients.NewComputeV2Client() |
| 118 | if err != nil { |
| 119 | t.Fatalf("Unable to create a compute client: %v", err) |
| 120 | } |
| 121 | |
| 122 | idclient, err := clients.NewIdentityV2Client() |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 123 | if err != nil { |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 124 | t.Fatalf("Could not create IdentityClient to look up tenant id!") |
| 125 | } |
| 126 | |
| 127 | tenantid, err := getTenantIDByName(t, idclient, os.Getenv("OS_TENANT_NAME")) |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 128 | if err != nil { |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 129 | t.Fatalf("Id for Tenant named '%' not found. Please set OS_TENANT_NAME appropriately", os.Getenv("OS_TENANT_NAME")) |
| 130 | } |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 131 | |
| 132 | //save original quotas |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 133 | orig, err := quotasets.Get(client, tenantid).Extract() |
| 134 | th.AssertNoErr(t, err) |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 135 | |
| 136 | //Test Update |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 137 | res, err := quotasets.Update(client, tenantid, UpdatQuotaOpts).Extract() |
| 138 | th.AssertNoErr(t, err) |
| 139 | th.AssertEquals(t, UpdatedQuotas, *res) |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 140 | |
| 141 | //Test Delete |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 142 | _, err = quotasets.Delete(client, tenantid).Extract() |
| 143 | th.AssertNoErr(t, err) |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 144 | //We dont know the default quotas, so just check if the quotas are not the same as before |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 145 | newres, err := quotasets.Get(client, tenantid).Extract() |
| 146 | if newres == res { |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 147 | t.Fatalf("Quotas after delete equal quotas before delete!") |
| 148 | } |
| 149 | |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 150 | restore := quotasets.UpdateOpts{} |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 151 | FillUpdateOptsFromQuotaSet(*orig, &restore) |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 152 | |
| 153 | //restore original quotas |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 154 | res, err = quotasets.Update(client, tenantid, restore).Extract() |
| 155 | th.AssertNoErr(t, err) |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 156 | |
| 157 | orig.ID = "" |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 158 | th.AssertEquals(t, *orig, *res) |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 159 | |
| 160 | } |
| 161 | |
| 162 | // Makes sure that the FillUpdateOptsFromQuotaSet() helper function works properly |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 163 | func TestFillFromQuotaSetHelperFunction(t *testing.T) { |
dbaumgarten | c2bb491 | 2017-01-19 17:14:08 +0100 | [diff] [blame] | 164 | op := "asets.UpdateOpts{} |
| 165 | expected := ` |
| 166 | { |
| 167 | "fixed_ips": 10, |
| 168 | "floating_ips": 10, |
| 169 | "injected_file_content_bytes": 10240, |
| 170 | "injected_file_path_bytes": 255, |
| 171 | "injected_files": 5, |
| 172 | "key_pairs": 10, |
| 173 | "metadata_items": 128, |
| 174 | "ram": 20000, |
| 175 | "security_group_rules": 20, |
| 176 | "security_groups": 10, |
| 177 | "cores": 10, |
| 178 | "instances": 4, |
| 179 | "server_groups": 2, |
| 180 | "server_group_members": 3 |
| 181 | }` |
jrperritt | 5cb543c | 2017-02-20 14:03:36 -0600 | [diff] [blame^] | 182 | FillUpdateOptsFromQuotaSet(UpdatedQuotas, op) |
| 183 | th.AssertJSONEquals(t, expected, op) |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 184 | } |