github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/client/validate_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 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 "encoding/json" 24 "io/ioutil" 25 "net/url" 26 27 "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/client" 30 ) 31 32 var errorResponseJSON = `{ 33 "type": "error", 34 "result": {"message": "failed"} 35 }` 36 37 func (cs *clientSuite) TestListValidationsSetsNone(c *check.C) { 38 cs.rsp = `{ 39 "type": "sync", 40 "status-code": 200, 41 "result": [] 42 }` 43 44 vsets, err := cs.cli.ListValidationsSets() 45 c.Assert(err, check.IsNil) 46 c.Check(cs.req.Method, check.Equals, "GET") 47 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets") 48 c.Check(vsets, check.HasLen, 0) 49 } 50 51 func (cs *clientSuite) TestListValidationsSetsError(c *check.C) { 52 cs.status = 500 53 cs.rsp = errorResponseJSON 54 55 _, err := cs.cli.ListValidationsSets() 56 c.Assert(err, check.ErrorMatches, "cannot list validation sets: failed") 57 c.Check(cs.req.Method, check.Equals, "GET") 58 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets") 59 } 60 61 func (cs *clientSuite) TestListValidationsSets(c *check.C) { 62 cs.rsp = `{ 63 "type": "sync", 64 "status-code": 200, 65 "result": [ 66 {"account-id": "abc", "name": "def", "mode": "monitor", "sequence": 0}, 67 {"account-id": "ghi", "name": "jkl", "mode": "enforce", "sequence": 2} 68 ] 69 }` 70 71 vsets, err := cs.cli.ListValidationsSets() 72 c.Assert(err, check.IsNil) 73 c.Check(cs.req.Method, check.Equals, "GET") 74 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets") 75 c.Check(vsets, check.DeepEquals, []*client.ValidationSetResult{ 76 {AccountID: "abc", Name: "def", Mode: "monitor", Sequence: 0, Valid: false}, 77 {AccountID: "ghi", Name: "jkl", Mode: "enforce", Sequence: 2, Valid: false}, 78 }) 79 } 80 81 func (cs *clientSuite) TestApplyValidationSet(c *check.C) { 82 cs.rsp = `{ 83 "type": "sync", 84 "status-code": 200 85 }` 86 opts := &client.ValidateApplyOptions{Mode: "monitor", Sequence: 3} 87 c.Assert(cs.cli.ApplyValidationSet("foo", "bar", opts), check.IsNil) 88 c.Check(cs.req.Method, check.Equals, "POST") 89 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets/foo/bar") 90 body, err := ioutil.ReadAll(cs.req.Body) 91 c.Assert(err, check.IsNil) 92 var req map[string]interface{} 93 err = json.Unmarshal(body, &req) 94 c.Assert(err, check.IsNil) 95 c.Assert(req, check.DeepEquals, map[string]interface{}{ 96 "action": "apply", 97 "mode": "monitor", 98 "sequence": float64(3), 99 }) 100 } 101 102 func (cs *clientSuite) TestApplyValidationSetError(c *check.C) { 103 cs.status = 500 104 cs.rsp = errorResponseJSON 105 opts := &client.ValidateApplyOptions{Mode: "monitor"} 106 err := cs.cli.ApplyValidationSet("foo", "bar", opts) 107 c.Assert(err, check.ErrorMatches, "cannot apply validation set: failed") 108 c.Check(cs.req.Method, check.Equals, "POST") 109 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets/foo/bar") 110 } 111 112 func (cs *clientSuite) TestApplyValidationSetInvalidArgs(c *check.C) { 113 opts := &client.ValidateApplyOptions{} 114 err := cs.cli.ApplyValidationSet("", "bar", opts) 115 c.Assert(err, check.ErrorMatches, `cannot apply validation set without account ID and name`) 116 err = cs.cli.ApplyValidationSet("", "bar", opts) 117 c.Assert(err, check.ErrorMatches, `cannot apply validation set without account ID and name`) 118 } 119 120 func (cs *clientSuite) TestForgetValidationSet(c *check.C) { 121 cs.rsp = `{ 122 "type": "sync", 123 "status-code": 200 124 }` 125 c.Assert(cs.cli.ForgetValidationSet("foo", "bar", 3), check.IsNil) 126 c.Check(cs.req.Method, check.Equals, "POST") 127 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets/foo/bar") 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": "forget", 135 "sequence": float64(3), 136 }) 137 } 138 139 func (cs *clientSuite) TestForgetValidationSetError(c *check.C) { 140 cs.status = 500 141 cs.rsp = errorResponseJSON 142 err := cs.cli.ForgetValidationSet("foo", "bar", 0) 143 c.Assert(err, check.ErrorMatches, "cannot forget validation set: failed") 144 c.Check(cs.req.Method, check.Equals, "POST") 145 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets/foo/bar") 146 } 147 148 func (cs *clientSuite) TestForgetValidationSetInvalidArgs(c *check.C) { 149 err := cs.cli.ForgetValidationSet("", "bar", 0) 150 c.Assert(err, check.ErrorMatches, `cannot forget validation set without account ID and name`) 151 err = cs.cli.ForgetValidationSet("", "bar", 0) 152 c.Assert(err, check.ErrorMatches, `cannot forget validation set without account ID and name`) 153 } 154 155 func (cs *clientSuite) TestValidationSet(c *check.C) { 156 cs.rsp = `{ 157 "type": "sync", 158 "status-code": 200, 159 "result": {"account-id": "abc", "name": "def", "mode": "monitor", "sequence": 0} 160 }` 161 162 vsets, err := cs.cli.ValidationSet("foo", "bar", 0) 163 c.Assert(err, check.IsNil) 164 c.Check(cs.req.Method, check.Equals, "GET") 165 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets/foo/bar") 166 c.Check(vsets, check.DeepEquals, &client.ValidationSetResult{ 167 AccountID: "abc", Name: "def", Mode: "monitor", Sequence: 0, Valid: false, 168 }) 169 } 170 171 func (cs *clientSuite) TestValidationSetError(c *check.C) { 172 cs.status = 500 173 cs.rsp = errorResponseJSON 174 175 _, err := cs.cli.ValidationSet("foo", "bar", 0) 176 c.Assert(err, check.ErrorMatches, "cannot query validation set: failed") 177 } 178 179 func (cs *clientSuite) TestValidationSetInvalidArgs(c *check.C) { 180 _, err := cs.cli.ValidationSet("foo", "", 0) 181 c.Assert(err, check.ErrorMatches, `cannot query validation set without account ID and name`) 182 _, err = cs.cli.ValidationSet("", "bar", 0) 183 c.Assert(err, check.ErrorMatches, `cannot query validation set without account ID and name`) 184 } 185 186 func (cs *clientSuite) TestValidationSetWithSequence(c *check.C) { 187 cs.rsp = `{ 188 "type": "sync", 189 "status-code": 200, 190 "result": {"account-id": "abc", "name": "def", "mode": "monitor", "sequence": 9} 191 }` 192 193 vsets, err := cs.cli.ValidationSet("foo", "bar", 9) 194 c.Assert(err, check.IsNil) 195 c.Check(cs.req.Method, check.Equals, "GET") 196 c.Check(cs.req.URL.Path, check.Equals, "/v2/validation-sets/foo/bar") 197 c.Check(cs.req.URL.Query(), check.DeepEquals, url.Values{"sequence": []string{"9"}}) 198 c.Check(vsets, check.DeepEquals, &client.ValidationSetResult{ 199 AccountID: "abc", Name: "def", Mode: "monitor", Sequence: 9, Valid: false, 200 }) 201 }