Skip to content

Commit e02deb6

Browse files
committed
Compute V2: fix servergroups rules and tests
Use pointer to Rules in servergroups create options to prevent its addition to create options without microversion. Update servergroups package tests to test for cases with and without microversion 2.64.
1 parent cafb1ea commit e02deb6

6 files changed

Lines changed: 151 additions & 4 deletions

File tree

acceptance/openstack/compute/v2/compute.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,34 @@ func CreateServerGroup(t *testing.T, client *gophercloud.ServiceClient, policy s
614614
return sg, nil
615615
}
616616

617+
// CreateServerGroupMicroversion will create a server with a random name using 2.64 microversion. An error will be
618+
// returned if the server group failed to be created.
619+
func CreateServerGroupMicroversion(t *testing.T, client *gophercloud.ServiceClient) (*servergroups.ServerGroup, error) {
620+
name := tools.RandomString("ACPTTEST", 16)
621+
policy := "anti-affinity"
622+
maxServerPerHost := 3
623+
624+
t.Logf("Attempting to create %s server group with max server per host = %d: %s", policy, maxServerPerHost, name)
625+
626+
sg, err := servergroups.Create(client, &servergroups.CreateOpts{
627+
Name: name,
628+
Policy: policy,
629+
Rules: &servergroups.Rules{
630+
MaxServerPerHost: maxServerPerHost,
631+
},
632+
}).Extract()
633+
634+
if err != nil {
635+
return nil, err
636+
}
637+
638+
t.Logf("Successfully created server group %s", name)
639+
640+
th.AssertEquals(t, sg.Name, name)
641+
642+
return sg, nil
643+
}
644+
617645
// CreateServerInServerGroup works like CreateServer but places the instance in
618646
// a specified Server Group.
619647
func CreateServerInServerGroup(t *testing.T, client *gophercloud.ServiceClient, serverGroup *servergroups.ServerGroup) (*servers.Server, error) {

acceptance/openstack/compute/v2/servergroup_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,35 @@ func TestServergroupsAffinityPolicy(t *testing.T) {
6969

7070
th.AssertEquals(t, firstServer.HostID, secondServer.HostID)
7171
}
72+
73+
func TestServergroupsMicroversionCreateDelete(t *testing.T) {
74+
client, err := clients.NewComputeV2Client()
75+
th.AssertNoErr(t, err)
76+
77+
client.Microversion = "2.64"
78+
serverGroup, err := CreateServerGroupMicroversion(t, client)
79+
th.AssertNoErr(t, err)
80+
defer DeleteServerGroup(t, client, serverGroup)
81+
82+
serverGroup, err = servergroups.Get(client, serverGroup.ID).Extract()
83+
th.AssertNoErr(t, err)
84+
85+
tools.PrintResource(t, serverGroup)
86+
87+
allPages, err := servergroups.List(client).AllPages()
88+
th.AssertNoErr(t, err)
89+
90+
allServerGroups, err := servergroups.ExtractServerGroups(allPages)
91+
th.AssertNoErr(t, err)
92+
93+
var found bool
94+
for _, sg := range allServerGroups {
95+
tools.PrintResource(t, serverGroup)
96+
97+
if sg.ID == serverGroup.ID {
98+
found = true
99+
}
100+
}
101+
102+
th.AssertEquals(t, found, true)
103+
}

openstack/compute/v2/extensions/servergroups/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Example to Create a Server Group with additional microversion 2.64 fields
3434
createOpts := servergroups.CreateOpts{
3535
Name: "my_sg",
3636
Policy: "anti-affinity",
37-
Rules: servergroups.Rules{
37+
Rules: &servergroups.Rules{
3838
MaxServerPerHost: 3,
3939
},
4040
}

openstack/compute/v2/extensions/servergroups/requests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type CreateOpts struct {
3333

3434
// Rules specifies the set of rules.
3535
// Requires microversion 2.64 or later.
36-
Rules Rules `json:"rules,omitempty"`
36+
Rules *Rules `json:"rules,omitempty"`
3737
}
3838

3939
// ToServerGroupCreateMap constructs a request body from CreateOpts.

openstack/compute/v2/extensions/servergroups/testing/fixtures.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ const ListOutput = `
3838

3939
// GetOutput is a sample response to a Get call.
4040
const GetOutput = `
41+
{
42+
"server_group": {
43+
"id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
44+
"name": "test",
45+
"policies": [
46+
"anti-affinity"
47+
],
48+
"members": [],
49+
"metadata": {}
50+
}
51+
}
52+
`
53+
54+
// GetOutputMicroversion is a sample response to a Get call with microversion set to 2.64
55+
const GetOutputMicroversion = `
4156
{
4257
"server_group": {
4358
"id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
@@ -57,6 +72,21 @@ const GetOutput = `
5772

5873
// CreateOutput is a sample response to a Post call
5974
const CreateOutput = `
75+
{
76+
"server_group": {
77+
"id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
78+
"name": "test",
79+
"policies": [
80+
"anti-affinity"
81+
],
82+
"members": [],
83+
"metadata": {}
84+
}
85+
}
86+
`
87+
88+
// CreateOutputMicroversion is a sample response to a Post call with microversion set to 2.64
89+
const CreateOutputMicroversion = `
6090
{
6191
"server_group": {
6292
"id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
@@ -134,13 +164,47 @@ func HandleGetSuccessfully(t *testing.T) {
134164
})
135165
}
136166

167+
// HandleGetMicroversionSuccessfully configures the test server to respond to a Get request
168+
// for an existing server group with microversion set to 2.64
169+
func HandleGetMicroversionSuccessfully(t *testing.T) {
170+
th.Mux.HandleFunc("/os-server-groups/4d8c3732-a248-40ed-bebc-539a6ffd25c0", func(w http.ResponseWriter, r *http.Request) {
171+
th.TestMethod(t, r, "GET")
172+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
173+
174+
w.Header().Add("Content-Type", "application/json")
175+
fmt.Fprintf(w, GetOutputMicroversion)
176+
})
177+
}
178+
137179
// HandleCreateSuccessfully configures the test server to respond to a Create request
138180
// for a new server group
139181
func HandleCreateSuccessfully(t *testing.T) {
140182
th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
141183
th.TestMethod(t, r, "POST")
142184
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
143185
th.TestJSONRequest(t, r, `
186+
{
187+
"server_group": {
188+
"name": "test",
189+
"policies": [
190+
"anti-affinity"
191+
]
192+
}
193+
}
194+
`)
195+
196+
w.Header().Add("Content-Type", "application/json")
197+
fmt.Fprintf(w, CreateOutput)
198+
})
199+
}
200+
201+
// HandleCreateMicroversionSuccessfully configures the test server to respond to a Create request
202+
// for a new server group with microversion set to 2.64
203+
func HandleCreateMicroversionSuccessfully(t *testing.T) {
204+
th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
205+
th.TestMethod(t, r, "POST")
206+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
207+
th.TestJSONRequest(t, r, `
144208
{
145209
"server_group": {
146210
"name": "test",
@@ -156,7 +220,7 @@ func HandleCreateSuccessfully(t *testing.T) {
156220
`)
157221

158222
w.Header().Add("Content-Type", "application/json")
159-
fmt.Fprintf(w, CreateOutput)
223+
fmt.Fprintf(w, CreateOutputMicroversion)
160224
})
161225
}
162226

openstack/compute/v2/extensions/servergroups/testing/requests_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,24 @@ func TestCreate(t *testing.T) {
3232
defer th.TeardownHTTP()
3333
HandleCreateSuccessfully(t)
3434

35+
actual, err := servergroups.Create(client.ServiceClient(), servergroups.CreateOpts{
36+
Name: "test",
37+
Policies: []string{"anti-affinity"},
38+
}).Extract()
39+
th.AssertNoErr(t, err)
40+
th.CheckDeepEquals(t, &CreatedServerGroup, actual)
41+
}
42+
43+
func TestCreateMicroversion(t *testing.T) {
44+
th.SetupHTTP()
45+
defer th.TeardownHTTP()
46+
HandleCreateMicroversionSuccessfully(t)
47+
3548
result := servergroups.Create(client.ServiceClient(), servergroups.CreateOpts{
3649
Name: "test",
3750
Policies: []string{"anti-affinity"},
3851
Policy: "anti-affinity",
39-
Rules: servergroups.Rules{
52+
Rules: &servergroups.Rules{
4053
MaxServerPerHost: 3,
4154
},
4255
})
@@ -61,6 +74,16 @@ func TestGet(t *testing.T) {
6174
defer th.TeardownHTTP()
6275
HandleGetSuccessfully(t)
6376

77+
actual, err := servergroups.Get(client.ServiceClient(), "4d8c3732-a248-40ed-bebc-539a6ffd25c0").Extract()
78+
th.AssertNoErr(t, err)
79+
th.CheckDeepEquals(t, &FirstServerGroup, actual)
80+
}
81+
82+
func TestGetMicroversion(t *testing.T) {
83+
th.SetupHTTP()
84+
defer th.TeardownHTTP()
85+
HandleGetMicroversionSuccessfully(t)
86+
6487
result := servergroups.Get(client.ServiceClient(), "4d8c3732-a248-40ed-bebc-539a6ffd25c0")
6588

6689
// Extract basic fields.

0 commit comments

Comments
 (0)