github.com/ubuntu-core/snappy@v0.0.0-20210827154228-9e584df982bb/client/quota_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2021 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package client_test 21 22 import ( 23 "bytes" 24 "encoding/json" 25 "io/ioutil" 26 27 "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/client" 30 "github.com/snapcore/snapd/gadget/quantity" 31 "github.com/snapcore/snapd/jsonutil" 32 ) 33 34 func (cs *clientSuite) TestCreateQuotaGroupInvalidName(c *check.C) { 35 _, err := cs.cli.EnsureQuota("", "", nil, 0) 36 c.Check(err, check.ErrorMatches, `cannot create or update quota group without a name`) 37 } 38 39 func (cs *clientSuite) TestEnsureQuotaGroup(c *check.C) { 40 cs.status = 202 41 cs.rsp = `{ 42 "type": "async", 43 "status-code": 202, 44 "change": "42" 45 }` 46 47 chgID, err := cs.cli.EnsureQuota("foo", "bar", []string{"snap-a", "snap-b"}, 1001) 48 c.Assert(err, check.IsNil) 49 c.Assert(chgID, check.Equals, "42") 50 c.Check(cs.req.Method, check.Equals, "POST") 51 c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas") 52 body, err := ioutil.ReadAll(cs.req.Body) 53 c.Assert(err, check.IsNil) 54 var req map[string]interface{} 55 err = jsonutil.DecodeWithNumber(bytes.NewReader(body), &req) 56 c.Assert(err, check.IsNil) 57 c.Assert(req, check.DeepEquals, map[string]interface{}{ 58 "action": "ensure", 59 "group-name": "foo", 60 "parent": "bar", 61 "snaps": []interface{}{"snap-a", "snap-b"}, 62 "constraints": map[string]interface{}{ 63 "memory": json.Number("1001"), 64 }, 65 }) 66 } 67 68 func (cs *clientSuite) TestEnsureQuotaGroupError(c *check.C) { 69 cs.status = 500 70 cs.rsp = `{"type": "error"}` 71 _, err := cs.cli.EnsureQuota("foo", "bar", []string{"snap-a"}, 1) 72 c.Check(err, check.ErrorMatches, `server error: "Internal Server Error"`) 73 } 74 75 func (cs *clientSuite) TestGetQuotaGroupInvalidName(c *check.C) { 76 _, err := cs.cli.GetQuotaGroup("") 77 c.Assert(err, check.ErrorMatches, `cannot get quota group without a name`) 78 } 79 80 func (cs *clientSuite) TestGetQuotaGroup(c *check.C) { 81 cs.rsp = `{ 82 "type": "sync", 83 "status-code": 200, 84 "result": { 85 "group-name":"foo", 86 "parent":"bar", 87 "subgroups":["foo-subgrp"], 88 "snaps":["snap-a"], 89 "constraints": { "memory": 999 }, 90 "current": { "memory": 450 } 91 } 92 }` 93 94 grp, err := cs.cli.GetQuotaGroup("foo") 95 c.Assert(err, check.IsNil) 96 c.Check(cs.req.Method, check.Equals, "GET") 97 c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas/foo") 98 c.Check(grp, check.DeepEquals, &client.QuotaGroupResult{ 99 GroupName: "foo", 100 Parent: "bar", 101 Subgroups: []string{"foo-subgrp"}, 102 Constraints: &client.QuotaValues{Memory: quantity.Size(999)}, 103 Current: &client.QuotaValues{Memory: quantity.Size(450)}, 104 Snaps: []string{"snap-a"}, 105 }) 106 } 107 108 func (cs *clientSuite) TestGetQuotaGroupError(c *check.C) { 109 cs.status = 500 110 cs.rsp = `{"type": "error"}` 111 _, err := cs.cli.GetQuotaGroup("foo") 112 c.Check(err, check.ErrorMatches, `server error: "Internal Server Error"`) 113 } 114 115 func (cs *clientSuite) TestRemoveQuotaGroup(c *check.C) { 116 cs.status = 202 117 cs.rsp = `{ 118 "type": "async", 119 "status-code": 202, 120 "change": "42" 121 }` 122 123 chgID, err := cs.cli.RemoveQuotaGroup("foo") 124 c.Assert(err, check.IsNil) 125 c.Assert(chgID, check.Equals, "42") 126 c.Check(cs.req.Method, check.Equals, "POST") 127 c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas") 128 body, err := ioutil.ReadAll(cs.req.Body) 129 c.Assert(err, check.IsNil) 130 var req map[string]interface{} 131 err = json.Unmarshal(body, &req) 132 c.Assert(err, check.IsNil) 133 c.Assert(req, check.DeepEquals, map[string]interface{}{ 134 "action": "remove", 135 "group-name": "foo", 136 }) 137 } 138 139 func (cs *clientSuite) TestRemoveQuotaGroupError(c *check.C) { 140 cs.status = 500 141 cs.rsp = `{"type": "error"}` 142 _, err := cs.cli.RemoveQuotaGroup("foo") 143 c.Check(err, check.ErrorMatches, `cannot remove quota group: server error: "Internal Server Error"`) 144 }