Skip to content

Commit 7417de2

Browse files
Meulengrachtmvo5
authored andcommitted
cmd/snap,daemon: review feedback
add unit tests for the changes to api REST data, simplify test json data for cmd_quota
1 parent 11d5f38 commit 7417de2

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

cmd/snap/cmd_quota_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,20 @@ func (s *quotaSuite) TestSetQuotaCpuHappy(c *check.C) {
290290
cpuPercentage: 50,
291291
}
292292
// this data is not tested against, but it should still be valid
293-
const getJsonTemplate = `{
293+
const getJson = `{
294294
"type": "sync",
295295
"status-code": 200,
296296
"result": {
297297
"group-name":"foo",
298-
"constraints": { "cpu":{"count":%d, "percentage":%d} },
298+
"constraints": { "cpu":{"count":2, "percentage":50} },
299299
}
300300
}`
301301
routes := map[string]http.HandlerFunc{
302302
"/v2/quotas": s.makeFakeQuotaPostHandler(
303303
c,
304304
fakeHandlerOpts,
305305
),
306-
"/v2/quotas/foo": s.makeFakeGetQuotaGroupHandler(c, fmt.Sprintf(getJsonTemplate, 2, 50)),
306+
"/v2/quotas/foo": s.makeFakeGetQuotaGroupHandler(c, getJson),
307307
"/v2/changes/42": makeChangesHandler(c),
308308
}
309309
s.RedirectClientToTestServer(dispatchFakeHandlers(c, routes))
@@ -327,20 +327,20 @@ func (s *quotaSuite) TestSetQuotaSnapServices(c *check.C) {
327327
cpuPercentage: 50,
328328
}
329329
// this data is not tested against, but it should still be valid
330-
const getJsonTemplate = `{
330+
const getJson = `{
331331
"type": "sync",
332332
"status-code": 200,
333333
"result": {
334334
"group-name":"foo",
335-
"constraints": { "cpu":{"count":%d, "percentage":%d} },
335+
"constraints": { "cpu":{"count":2, "percentage":50} },
336336
}
337337
}`
338338
routes := map[string]http.HandlerFunc{
339339
"/v2/quotas": s.makeFakeQuotaPostHandler(
340340
c,
341341
fakeHandlerOpts,
342342
),
343-
"/v2/quotas/foo": s.makeFakeGetQuotaGroupHandler(c, fmt.Sprintf(getJsonTemplate, 2, 50)),
343+
"/v2/quotas/foo": s.makeFakeGetQuotaGroupHandler(c, getJson),
344344
"/v2/changes/42": makeChangesHandler(c),
345345
}
346346
s.RedirectClientToTestServer(dispatchFakeHandlers(c, routes))

daemon/api_quotas.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func getQuotaGroups(c *Command, r *http.Request, _ *auth.UserState) Response {
146146
Parent: group.ParentGroup,
147147
Subgroups: group.SubGroups,
148148
Snaps: group.Snaps,
149+
Services: group.Services,
149150
Constraints: createQuotaValues(group),
150151
Current: currentUsage,
151152
}

daemon/api_quotas_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func (s *apiQuotaSuite) SetUpTest(c *check.C) {
7474
}
7575

7676
func mockQuotas(st *state.State, c *check.C) {
77-
err := servicestatetest.MockQuotaInState(st, "foo", "", nil, nil, quota.NewResourcesBuilder().WithMemoryLimit(16*quantity.SizeMiB).Build())
77+
err := servicestatetest.MockQuotaInState(st, "foo", "", []string{"test-snap"}, nil, quota.NewResourcesBuilder().WithMemoryLimit(16*quantity.SizeMiB).Build())
7878
c.Assert(err, check.IsNil)
79-
err = servicestatetest.MockQuotaInState(st, "bar", "foo", nil, nil, quota.NewResourcesBuilder().WithMemoryLimit(4*quantity.SizeMiB).Build())
79+
err = servicestatetest.MockQuotaInState(st, "bar", "foo", nil, []string{"test-snap.svc1"}, quota.NewResourcesBuilder().WithMemoryLimit(4*quantity.SizeMiB).Build())
8080
c.Assert(err, check.IsNil)
8181
err = servicestatetest.MockQuotaInState(st, "baz", "foo", nil, nil, quota.NewResourcesBuilder().WithMemoryLimit(quantity.SizeMiB).Build())
8282
c.Assert(err, check.IsNil)
@@ -731,6 +731,7 @@ func (s *apiQuotaSuite) TestListQuotas(c *check.C) {
731731
{
732732
GroupName: "bar",
733733
Parent: "foo",
734+
Services: []string{"test-snap.svc1"},
734735
Constraints: &client.QuotaValues{Memory: 4 * quantity.SizeMiB},
735736
Current: &client.QuotaValues{Memory: quantity.Size(500)},
736737
},
@@ -743,6 +744,7 @@ func (s *apiQuotaSuite) TestListQuotas(c *check.C) {
743744
{
744745
GroupName: "foo",
745746
Subgroups: []string{"bar", "baz"},
747+
Snaps: []string{"test-snap"},
746748
Constraints: &client.QuotaValues{Memory: 16 * quantity.SizeMiB},
747749
Current: &client.QuotaValues{Memory: quantity.Size(5000)},
748750
},
@@ -840,6 +842,7 @@ func (s *apiQuotaSuite) TestGetQuota(c *check.C) {
840842
c.Check(res, check.DeepEquals, client.QuotaGroupResult{
841843
GroupName: "bar",
842844
Parent: "foo",
845+
Services: []string{"test-snap.svc1"},
843846
Constraints: &client.QuotaValues{Memory: quantity.Size(4194304)},
844847
Current: &client.QuotaValues{Memory: quantity.Size(500)},
845848
})

0 commit comments

Comments
 (0)