blob: 2fb7c992fa607de2de54237c709c509496bf3b50 [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"
jrperritt3d966162016-06-06 14:08:54 -050011 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/quotasets"
12 "github.com/gophercloud/gophercloud/openstack/identity/v2/tenants"
dbaumgartenc2bb4912017-01-19 17:14:08 +010013 th "github.com/gophercloud/gophercloud/testhelper"
14 "os"
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060015)
16
Joe Topjian713e51a2016-07-25 19:06:25 +000017func TestQuotasetGet(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060018 client, err := clients.NewComputeV2Client()
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060019 if err != nil {
20 t.Fatalf("Unable to create a compute client: %v", err)
21 }
22
Joe Topjian1c15e3f2016-08-08 10:48:38 -060023 identityClient, err := clients.NewIdentityV2Client()
Joe Topjian713e51a2016-07-25 19:06:25 +000024 if err != nil {
25 t.Fatalf("Unable to get a new identity client: %v", err)
26 }
27
28 tenantID, err := getTenantID(t, identityClient)
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060029 if err != nil {
30 t.Fatal(err)
31 }
32
Joe Topjian713e51a2016-07-25 19:06:25 +000033 quotaSet, err := quotasets.Get(client, tenantID).Extract()
34 if err != nil {
35 t.Fatal(err)
36 }
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060037
Joe Topjian1c15e3f2016-08-08 10:48:38 -060038 PrintQuotaSet(t, quotaSet)
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060039}
40
Joe Topjian713e51a2016-07-25 19:06:25 +000041func 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 Kirkwooded3f5fd2016-03-16 12:41:47 -060046
Joe Topjian713e51a2016-07-25 19:06:25 +000047 allTenants, err := tenants.ExtractTenants(allPages)
48 if err != nil {
49 t.Fatalf("Unable to extract tenants: %v", err)
50 }
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060051
Joe Topjian713e51a2016-07-25 19:06:25 +000052 for _, tenant := range allTenants {
53 return tenant.ID, nil
54 }
Dan Kirkwooded3f5fd2016-03-16 12:41:47 -060055
Joe Topjian713e51a2016-07-25 19:06:25 +000056 return "", fmt.Errorf("Unable to get tenant ID")
57}
dbaumgartenc2bb4912017-01-19 17:14:08 +010058
59func 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
80var 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
98var 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
115func 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
164func TestFillFromQuotaSetHelperFunction(t *testing.T){
165 op := &quotasets.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}