blob: 1a691c06c95d260001f345c7174e84b70f96acab [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 {
jrperritt5cb543c2017-02-20 14:03:36 -060072 if tenant.Name == name {
dbaumgartenc2bb4912017-01-19 17:14:08 +010073 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()
jrperritt5cb543c2017-02-20 14:03:36 -0600123 if err != nil {
dbaumgartenc2bb4912017-01-19 17:14:08 +0100124 t.Fatalf("Could not create IdentityClient to look up tenant id!")
125 }
126
127 tenantid, err := getTenantIDByName(t, idclient, os.Getenv("OS_TENANT_NAME"))
jrperritt5cb543c2017-02-20 14:03:36 -0600128 if err != nil {
dbaumgartenc2bb4912017-01-19 17:14:08 +0100129 t.Fatalf("Id for Tenant named '%' not found. Please set OS_TENANT_NAME appropriately", os.Getenv("OS_TENANT_NAME"))
130 }
dbaumgartenc2bb4912017-01-19 17:14:08 +0100131
132 //save original quotas
jrperritt5cb543c2017-02-20 14:03:36 -0600133 orig, err := quotasets.Get(client, tenantid).Extract()
134 th.AssertNoErr(t, err)
dbaumgartenc2bb4912017-01-19 17:14:08 +0100135
136 //Test Update
jrperritt5cb543c2017-02-20 14:03:36 -0600137 res, err := quotasets.Update(client, tenantid, UpdatQuotaOpts).Extract()
138 th.AssertNoErr(t, err)
139 th.AssertEquals(t, UpdatedQuotas, *res)
dbaumgartenc2bb4912017-01-19 17:14:08 +0100140
141 //Test Delete
jrperritt5cb543c2017-02-20 14:03:36 -0600142 _, err = quotasets.Delete(client, tenantid).Extract()
143 th.AssertNoErr(t, err)
dbaumgartenc2bb4912017-01-19 17:14:08 +0100144 //We dont know the default quotas, so just check if the quotas are not the same as before
jrperritt5cb543c2017-02-20 14:03:36 -0600145 newres, err := quotasets.Get(client, tenantid).Extract()
146 if newres == res {
dbaumgartenc2bb4912017-01-19 17:14:08 +0100147 t.Fatalf("Quotas after delete equal quotas before delete!")
148 }
149
dbaumgartenc2bb4912017-01-19 17:14:08 +0100150 restore := quotasets.UpdateOpts{}
jrperritt5cb543c2017-02-20 14:03:36 -0600151 FillUpdateOptsFromQuotaSet(*orig, &restore)
dbaumgartenc2bb4912017-01-19 17:14:08 +0100152
153 //restore original quotas
jrperritt5cb543c2017-02-20 14:03:36 -0600154 res, err = quotasets.Update(client, tenantid, restore).Extract()
155 th.AssertNoErr(t, err)
dbaumgartenc2bb4912017-01-19 17:14:08 +0100156
157 orig.ID = ""
jrperritt5cb543c2017-02-20 14:03:36 -0600158 th.AssertEquals(t, *orig, *res)
dbaumgartenc2bb4912017-01-19 17:14:08 +0100159
160}
161
162// Makes sure that the FillUpdateOptsFromQuotaSet() helper function works properly
jrperritt5cb543c2017-02-20 14:03:36 -0600163func TestFillFromQuotaSetHelperFunction(t *testing.T) {
dbaumgartenc2bb4912017-01-19 17:14:08 +0100164 op := &quotasets.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 }`
jrperritt5cb543c2017-02-20 14:03:36 -0600182 FillUpdateOptsFromQuotaSet(UpdatedQuotas, op)
183 th.AssertJSONEquals(t, expected, op)
Joe Topjian66a046c2017-01-19 22:07:26 -0700184}