-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathquota_test.go
144 lines (128 loc) · 4.19 KB
/
quota_test.go
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package client_test
import (
"bytes"
"encoding/json"
"io/ioutil"
"gopkg.in/check.v1"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/gadget/quantity"
"github.com/snapcore/snapd/jsonutil"
)
func (cs *clientSuite) TestCreateQuotaGroupInvalidName(c *check.C) {
_, err := cs.cli.EnsureQuota("", "", nil, 0)
c.Check(err, check.ErrorMatches, `cannot create or update quota group without a name`)
}
func (cs *clientSuite) TestEnsureQuotaGroup(c *check.C) {
cs.status = 202
cs.rsp = `{
"type": "async",
"status-code": 202,
"change": "42"
}`
chgID, err := cs.cli.EnsureQuota("foo", "bar", []string{"snap-a", "snap-b"}, 1001)
c.Assert(err, check.IsNil)
c.Assert(chgID, check.Equals, "42")
c.Check(cs.req.Method, check.Equals, "POST")
c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas")
body, err := ioutil.ReadAll(cs.req.Body)
c.Assert(err, check.IsNil)
var req map[string]interface{}
err = jsonutil.DecodeWithNumber(bytes.NewReader(body), &req)
c.Assert(err, check.IsNil)
c.Assert(req, check.DeepEquals, map[string]interface{}{
"action": "ensure",
"group-name": "foo",
"parent": "bar",
"snaps": []interface{}{"snap-a", "snap-b"},
"constraints": map[string]interface{}{
"memory": json.Number("1001"),
},
})
}
func (cs *clientSuite) TestEnsureQuotaGroupError(c *check.C) {
cs.status = 500
cs.rsp = `{"type": "error"}`
_, err := cs.cli.EnsureQuota("foo", "bar", []string{"snap-a"}, 1)
c.Check(err, check.ErrorMatches, `server error: "Internal Server Error"`)
}
func (cs *clientSuite) TestGetQuotaGroupInvalidName(c *check.C) {
_, err := cs.cli.GetQuotaGroup("")
c.Assert(err, check.ErrorMatches, `cannot get quota group without a name`)
}
func (cs *clientSuite) TestGetQuotaGroup(c *check.C) {
cs.rsp = `{
"type": "sync",
"status-code": 200,
"result": {
"group-name":"foo",
"parent":"bar",
"subgroups":["foo-subgrp"],
"snaps":["snap-a"],
"constraints": { "memory": 999 },
"current": { "memory": 450 }
}
}`
grp, err := cs.cli.GetQuotaGroup("foo")
c.Assert(err, check.IsNil)
c.Check(cs.req.Method, check.Equals, "GET")
c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas/foo")
c.Check(grp, check.DeepEquals, &client.QuotaGroupResult{
GroupName: "foo",
Parent: "bar",
Subgroups: []string{"foo-subgrp"},
Constraints: &client.QuotaValues{Memory: quantity.Size(999)},
Current: &client.QuotaValues{Memory: quantity.Size(450)},
Snaps: []string{"snap-a"},
})
}
func (cs *clientSuite) TestGetQuotaGroupError(c *check.C) {
cs.status = 500
cs.rsp = `{"type": "error"}`
_, err := cs.cli.GetQuotaGroup("foo")
c.Check(err, check.ErrorMatches, `server error: "Internal Server Error"`)
}
func (cs *clientSuite) TestRemoveQuotaGroup(c *check.C) {
cs.status = 202
cs.rsp = `{
"type": "async",
"status-code": 202,
"change": "42"
}`
chgID, err := cs.cli.RemoveQuotaGroup("foo")
c.Assert(err, check.IsNil)
c.Assert(chgID, check.Equals, "42")
c.Check(cs.req.Method, check.Equals, "POST")
c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas")
body, err := ioutil.ReadAll(cs.req.Body)
c.Assert(err, check.IsNil)
var req map[string]interface{}
err = json.Unmarshal(body, &req)
c.Assert(err, check.IsNil)
c.Assert(req, check.DeepEquals, map[string]interface{}{
"action": "remove",
"group-name": "foo",
})
}
func (cs *clientSuite) TestRemoveQuotaGroupError(c *check.C) {
cs.status = 500
cs.rsp = `{"type": "error"}`
_, err := cs.cli.RemoveQuotaGroup("foo")
c.Check(err, check.ErrorMatches, `cannot remove quota group: server error: "Internal Server Error"`)
}