1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
//go:build acceptance || compute || quotasets
// +build acceptance compute quotasets
package v2
import (
"fmt"
"os"
"testing"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/quotasets"
"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
th "github.com/gophercloud/gophercloud/testhelper"
)
func TestQuotasetGet(t *testing.T) {
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)
identityClient, err := clients.NewIdentityV3Client()
th.AssertNoErr(t, err)
projectID, err := getProjectID(t, identityClient)
th.AssertNoErr(t, err)
quotaSet, err := quotasets.Get(client, projectID).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, quotaSet)
th.AssertEquals(t, quotaSet.FixedIPs, -1)
}
func getProjectID(t *testing.T, client *gophercloud.ServiceClient) (string, error) {
allPages, err := projects.ListAvailable(client).AllPages()
th.AssertNoErr(t, err)
allProjects, err := projects.ExtractProjects(allPages)
th.AssertNoErr(t, err)
for _, project := range allProjects {
return project.ID, nil
}
return "", fmt.Errorf("Unable to get project ID")
}
func getProjectIDByName(t *testing.T, client *gophercloud.ServiceClient, name string) (string, error) {
allPages, err := projects.List(client, nil).AllPages()
th.AssertNoErr(t, err)
allProjects, err := projects.ExtractProjects(allPages)
th.AssertNoErr(t, err)
for _, project := range allProjects {
if project.Name == name {
return project.ID, nil
}
}
return "", fmt.Errorf("Unable to get project ID")
}
// What will be sent as desired Quotas to the Server
var UpdateQuotaOpts = quotasets.UpdateOpts{
FixedIPs: gophercloud.IntToPointer(10),
FloatingIPs: gophercloud.IntToPointer(10),
InjectedFileContentBytes: gophercloud.IntToPointer(10240),
InjectedFilePathBytes: gophercloud.IntToPointer(255),
InjectedFiles: gophercloud.IntToPointer(5),
KeyPairs: gophercloud.IntToPointer(10),
MetadataItems: gophercloud.IntToPointer(128),
RAM: gophercloud.IntToPointer(20000),
SecurityGroupRules: gophercloud.IntToPointer(20),
SecurityGroups: gophercloud.IntToPointer(10),
Cores: gophercloud.IntToPointer(10),
Instances: gophercloud.IntToPointer(4),
ServerGroups: gophercloud.IntToPointer(2),
ServerGroupMembers: gophercloud.IntToPointer(3),
}
// What the Server hopefully returns as the new Quotas
var UpdatedQuotas = quotasets.QuotaSet{
FixedIPs: 10,
FloatingIPs: 10,
InjectedFileContentBytes: 10240,
InjectedFilePathBytes: 255,
InjectedFiles: 5,
KeyPairs: 10,
MetadataItems: 128,
RAM: 20000,
SecurityGroupRules: 20,
SecurityGroups: 10,
Cores: 10,
Instances: 4,
ServerGroups: 2,
ServerGroupMembers: 3,
}
func TestQuotasetUpdateDelete(t *testing.T) {
clients.RequireAdmin(t)
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)
idclient, err := clients.NewIdentityV3Client()
th.AssertNoErr(t, err)
projectid, err := getProjectIDByName(t, idclient, os.Getenv("OS_PROJECT_NAME"))
th.AssertNoErr(t, err)
// save original quotas
orig, err := quotasets.Get(client, projectid).Extract()
th.AssertNoErr(t, err)
// Test Update
res, err := quotasets.Update(client, projectid, UpdateQuotaOpts).Extract()
th.AssertNoErr(t, err)
th.AssertEquals(t, UpdatedQuotas, *res)
// Test Delete
_, err = quotasets.Delete(client, projectid).Extract()
th.AssertNoErr(t, err)
// We dont know the default quotas, so just check if the quotas are not the same as before
newres, err := quotasets.Get(client, projectid).Extract()
th.AssertNoErr(t, err)
if newres.RAM == res.RAM {
t.Fatalf("Failed to update quotas")
}
restore := quotasets.UpdateOpts{}
FillUpdateOptsFromQuotaSet(*orig, &restore)
// restore original quotas
res, err = quotasets.Update(client, projectid, restore).Extract()
th.AssertNoErr(t, err)
orig.ID = ""
th.AssertDeepEquals(t, orig, res)
}
|