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 { |
| 72 | if tenant.Name == name{ |
| 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() |
| 123 | if err != nil{ |
| 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")) |
| 128 | if err != nil{ |
| 129 | t.Fatalf("Id for Tenant named '%' not found. Please set OS_TENANT_NAME appropriately", os.Getenv("OS_TENANT_NAME")) |
| 130 | } |
| 131 | |
| 132 | |
| 133 | //save original quotas |
| 134 | orig, err := quotasets.Get(client,tenantid).Extract() |
| 135 | th.AssertNoErr(t,err) |
| 136 | |
| 137 | //Test Update |
| 138 | res, err := quotasets.Update(client,tenantid,UpdatQuotaOpts).Extract() |
| 139 | th.AssertNoErr(t,err) |
| 140 | th.AssertEquals(t,UpdatedQuotas,*res) |
| 141 | |
| 142 | //Test Delete |
| 143 | _, err = quotasets.Delete(client,tenantid).Extract() |
| 144 | th.AssertNoErr(t,err) |
| 145 | //We dont know the default quotas, so just check if the quotas are not the same as before |
| 146 | newres, err := quotasets.Get(client,tenantid).Extract() |
| 147 | if newres == res{ |
| 148 | t.Fatalf("Quotas after delete equal quotas before delete!") |
| 149 | } |
| 150 | |
| 151 | |
| 152 | restore := quotasets.UpdateOpts{} |
| 153 | FillUpdateOptsFromQuotaSet(*orig,&restore) |
| 154 | |
| 155 | //restore original quotas |
| 156 | res, err = quotasets.Update(client,tenantid,restore).Extract() |
| 157 | th.AssertNoErr(t,err) |
| 158 | |
| 159 | orig.ID = "" |
| 160 | th.AssertEquals(t,*orig,*res) |
| 161 | |
| 162 | } |
| 163 | |
| 164 | // Makes sure that the FillUpdateOptsFromQuotaSet() helper function works properly |
| 165 | func TestFillFromQuotaSetHelperFunction(t *testing.T){ |
| 166 | op := "asets.UpdateOpts{} |
| 167 | expected := ` |
| 168 | { |
| 169 | "fixed_ips": 10, |
| 170 | "floating_ips": 10, |
| 171 | "injected_file_content_bytes": 10240, |
| 172 | "injected_file_path_bytes": 255, |
| 173 | "injected_files": 5, |
| 174 | "key_pairs": 10, |
| 175 | "metadata_items": 128, |
| 176 | "ram": 20000, |
| 177 | "security_group_rules": 20, |
| 178 | "security_groups": 10, |
| 179 | "cores": 10, |
| 180 | "instances": 4, |
| 181 | "server_groups": 2, |
| 182 | "server_group_members": 3 |
| 183 | }` |
| 184 | FillUpdateOptsFromQuotaSet(UpdatedQuotas,op) |
| 185 | th.AssertJSONEquals(t,expected,op) |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 186 | } |