blob: e0c4bae9a1168087f00fdaaaf584ae73f7c40be1 [file] [log] [blame]
Joe Topjian713e51a2016-07-25 19:06:25 +00001// +build acceptance compute quotasets
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -06002
3package v2
4
5import (
Joe Topjian713e51a2016-07-25 19:06:25 +00006 "fmt"
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -06007 "testing"
8
jrperritt3d966162016-06-06 14:08:54 -05009 "github.com/gophercloud/gophercloud"
Joe Topjian1c15e3f2016-08-08 10:48:38 -060010 "github.com/gophercloud/gophercloud/acceptance/clients"
Joe Topjian66a046c2017-01-19 22:07:26 -070011 "github.com/gophercloud/gophercloud/acceptance/tools"
jrperritt3d966162016-06-06 14:08:54 -050012 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/quotasets"
13 "github.com/gophercloud/gophercloud/openstack/identity/v2/tenants"
dbaumgartenc2bb4912017-01-19 17:14:08 +010014 th "github.com/gophercloud/gophercloud/testhelper"
15 "os"
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060016)
17
Joe Topjian713e51a2016-07-25 19:06:25 +000018func TestQuotasetGet(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060019 client, err := clients.NewComputeV2Client()
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060020 if err != nil {
21 t.Fatalf("Unable to create a compute client: %v", err)
22 }
23
Joe Topjian1c15e3f2016-08-08 10:48:38 -060024 identityClient, err := clients.NewIdentityV2Client()
Joe Topjian713e51a2016-07-25 19:06:25 +000025 if err != nil {
26 t.Fatalf("Unable to get a new identity client: %v", err)
27 }
28
29 tenantID, err := getTenantID(t, identityClient)
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060030 if err != nil {
31 t.Fatal(err)
32 }
33
Joe Topjian713e51a2016-07-25 19:06:25 +000034 quotaSet, err := quotasets.Get(client, tenantID).Extract()
35 if err != nil {
36 t.Fatal(err)
37 }
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060038
Joe Topjian66a046c2017-01-19 22:07:26 -070039 tools.PrintResource(t, quotaSet)
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060040}
41
Joe Topjian713e51a2016-07-25 19:06:25 +000042func 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 Kirkwooded3f5fd2016-03-16 12:41:47 -060047
Joe Topjian713e51a2016-07-25 19:06:25 +000048 allTenants, err := tenants.ExtractTenants(allPages)
49 if err != nil {
50 t.Fatalf("Unable to extract tenants: %v", err)
51 }
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060052
Joe Topjian713e51a2016-07-25 19:06:25 +000053 for _, tenant := range allTenants {
54 return tenant.ID, nil
55 }
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060056
Joe Topjian713e51a2016-07-25 19:06:25 +000057 return "", fmt.Errorf("Unable to get tenant ID")
58}
dbaumgartenc2bb4912017-01-19 17:14:08 +010059
60func 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
81var 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
99var 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
116func 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
165func TestFillFromQuotaSetHelperFunction(t *testing.T){
166 op := &quotasets.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 Topjian66a046c2017-01-19 22:07:26 -0700186}