github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/blockstorage/v3/quotaset_test.go (about) 1 //go:build acceptance || quotasets 2 // +build acceptance quotasets 3 4 package v3 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/gophercloud/gophercloud" 11 "github.com/gophercloud/gophercloud/internal/acceptance/clients" 12 "github.com/gophercloud/gophercloud/internal/acceptance/tools" 13 "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/quotasets" 14 "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumetypes" 15 th "github.com/gophercloud/gophercloud/testhelper" 16 ) 17 18 func TestQuotasetGet(t *testing.T) { 19 clients.RequireAdmin(t) 20 21 client, projectID := getClientAndProject(t) 22 23 quotaSet, err := quotasets.Get(client, projectID).Extract() 24 th.AssertNoErr(t, err) 25 26 tools.PrintResource(t, quotaSet) 27 } 28 29 func TestQuotasetGetDefaults(t *testing.T) { 30 clients.RequireAdmin(t) 31 32 client, projectID := getClientAndProject(t) 33 34 quotaSet, err := quotasets.GetDefaults(client, projectID).Extract() 35 th.AssertNoErr(t, err) 36 37 tools.PrintResource(t, quotaSet) 38 } 39 40 func TestQuotasetGetUsage(t *testing.T) { 41 clients.RequireAdmin(t) 42 43 client, projectID := getClientAndProject(t) 44 45 quotaSetUsage, err := quotasets.GetUsage(client, projectID).Extract() 46 th.AssertNoErr(t, err) 47 48 tools.PrintResource(t, quotaSetUsage) 49 } 50 51 var UpdateQuotaOpts = quotasets.UpdateOpts{ 52 Volumes: gophercloud.IntToPointer(100), 53 Snapshots: gophercloud.IntToPointer(200), 54 Gigabytes: gophercloud.IntToPointer(300), 55 PerVolumeGigabytes: gophercloud.IntToPointer(50), 56 Backups: gophercloud.IntToPointer(2), 57 BackupGigabytes: gophercloud.IntToPointer(300), 58 Groups: gophercloud.IntToPointer(350), 59 Extra: map[string]interface{}{ 60 "volumes_foo": gophercloud.IntToPointer(100), 61 }, 62 } 63 64 var UpdatedQuotas = quotasets.QuotaSet{ 65 Volumes: 100, 66 Snapshots: 200, 67 Gigabytes: 300, 68 PerVolumeGigabytes: 50, 69 Backups: 2, 70 BackupGigabytes: 300, 71 Groups: 350, 72 } 73 74 var VolumeTypeIsPublic = true 75 var VolumeTypeCreateOpts = volumetypes.CreateOpts{ 76 Name: "foo", 77 IsPublic: &VolumeTypeIsPublic, 78 Description: "foo", 79 ExtraSpecs: map[string]string{}, 80 } 81 82 func TestQuotasetUpdate(t *testing.T) { 83 clients.RequireAdmin(t) 84 85 client, projectID := getClientAndProject(t) 86 87 // save original quotas 88 orig, err := quotasets.Get(client, projectID).Extract() 89 th.AssertNoErr(t, err) 90 91 // create volumeType to test volume type quota 92 volumeType, err := volumetypes.Create(client, VolumeTypeCreateOpts).Extract() 93 th.AssertNoErr(t, err) 94 95 defer func() { 96 restore := quotasets.UpdateOpts{} 97 FillUpdateOptsFromQuotaSet(*orig, &restore) 98 99 err := volumetypes.Delete(client, volumeType.ID).ExtractErr() 100 th.AssertNoErr(t, err) 101 102 _, err = quotasets.Update(client, projectID, restore).Extract() 103 th.AssertNoErr(t, err) 104 105 }() 106 107 // test Update 108 resultQuotas, err := quotasets.Update(client, projectID, UpdateQuotaOpts).Extract() 109 th.AssertNoErr(t, err) 110 111 // We dont know the default quotas, so just check if the quotas are not the 112 // same as before 113 newQuotas, err := quotasets.Get(client, projectID).Extract() 114 th.AssertNoErr(t, err) 115 th.AssertEquals(t, resultQuotas.Volumes, newQuotas.Volumes) 116 th.AssertEquals(t, resultQuotas.Extra["volumes_foo"], newQuotas.Extra["volumes_foo"]) 117 118 // test that resultQuotas.Extra is populated with the 3 new quota types 119 // for the new volumeType foo, don't take into account other volume types 120 count := 0 121 for k := range resultQuotas.Extra { 122 tools.PrintResource(t, k) 123 switch k { 124 case 125 "volumes_foo", 126 "snapshots_foo", 127 "gigabytes_foo": 128 count += 1 129 } 130 } 131 132 th.AssertEquals(t, count, 3) 133 134 // unpopulate resultQuotas.Extra as it is different per cloud and test 135 // rest of the quotaSet 136 resultQuotas.Extra = map[string]interface{}(nil) 137 th.AssertDeepEquals(t, UpdatedQuotas, *resultQuotas) 138 } 139 140 func TestQuotasetDelete(t *testing.T) { 141 clients.RequireAdmin(t) 142 143 client, projectID := getClientAndProject(t) 144 145 // save original quotas 146 orig, err := quotasets.Get(client, projectID).Extract() 147 th.AssertNoErr(t, err) 148 149 defer func() { 150 restore := quotasets.UpdateOpts{} 151 FillUpdateOptsFromQuotaSet(*orig, &restore) 152 153 _, err = quotasets.Update(client, projectID, restore).Extract() 154 th.AssertNoErr(t, err) 155 }() 156 157 // Obtain environment default quotaset values to validate deletion. 158 defaultQuotaSet, err := quotasets.GetDefaults(client, projectID).Extract() 159 th.AssertNoErr(t, err) 160 161 // Test Delete 162 err = quotasets.Delete(client, projectID).ExtractErr() 163 th.AssertNoErr(t, err) 164 165 newQuotas, err := quotasets.Get(client, projectID).Extract() 166 th.AssertNoErr(t, err) 167 168 th.AssertEquals(t, newQuotas.Volumes, defaultQuotaSet.Volumes) 169 } 170 171 // getClientAndProject reduces boilerplate by returning a new blockstorage v3 172 // ServiceClient and a project ID obtained from the OS_PROJECT_NAME envvar. 173 func getClientAndProject(t *testing.T) (*gophercloud.ServiceClient, string) { 174 client, err := clients.NewBlockStorageV3Client() 175 th.AssertNoErr(t, err) 176 177 projectID := os.Getenv("OS_PROJECT_NAME") 178 th.AssertNoErr(t, err) 179 return client, projectID 180 } 181 182 func FillUpdateOptsFromQuotaSet(src quotasets.QuotaSet, dest *quotasets.UpdateOpts) { 183 dest.Volumes = &src.Volumes 184 dest.Snapshots = &src.Snapshots 185 dest.Gigabytes = &src.Gigabytes 186 dest.PerVolumeGigabytes = &src.PerVolumeGigabytes 187 dest.Backups = &src.Backups 188 dest.BackupGigabytes = &src.BackupGigabytes 189 dest.Groups = &src.Groups 190 }