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