github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/systems_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 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 26 "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/client" 29 "github.com/snapcore/snapd/snap" 30 ) 31 32 func (cs *clientSuite) TestListSystemsSome(c *check.C) { 33 cs.rsp = `{ 34 "type": "sync", 35 "status-code": 200, 36 "result": { 37 "systems": [ 38 { 39 "current": true, 40 "label": "20200101", 41 "model": { 42 "model": "this-is-model-id", 43 "brand-id": "brand-id-1", 44 "display-name": "wonky model" 45 }, 46 "brand": { 47 "id": "brand-id-1", 48 "username": "brand", 49 "display-name": "wonky publishing" 50 }, 51 "actions": [ 52 {"title": "recover", "mode": "recover"}, 53 {"title": "reinstall", "mode": "install"} 54 ] 55 }, { 56 "label": "20200311", 57 "model": { 58 "model": "different-model-id", 59 "brand-id": "bulky-brand-id-1", 60 "display-name": "bulky model" 61 }, 62 "brand": { 63 "id": "bulky-brand-id-1", 64 "username": "bulky-brand", 65 "display-name": "bulky publishing" 66 }, 67 "actions": [ 68 {"title": "factory-reset", "mode": "install"} 69 ] 70 } 71 ] 72 } 73 }` 74 systems, err := cs.cli.ListSystems() 75 c.Assert(err, check.IsNil) 76 c.Check(cs.req.Method, check.Equals, "GET") 77 c.Check(cs.req.URL.Path, check.Equals, "/v2/systems") 78 c.Check(systems, check.DeepEquals, []client.System{ 79 { 80 Current: true, 81 Label: "20200101", 82 Model: client.SystemModelData{ 83 Model: "this-is-model-id", 84 BrandID: "brand-id-1", 85 DisplayName: "wonky model", 86 }, 87 Brand: snap.StoreAccount{ 88 ID: "brand-id-1", 89 Username: "brand", 90 DisplayName: "wonky publishing", 91 }, 92 Actions: []client.SystemAction{ 93 {Title: "recover", Mode: "recover"}, 94 {Title: "reinstall", Mode: "install"}, 95 }, 96 }, { 97 Label: "20200311", 98 Model: client.SystemModelData{ 99 Model: "different-model-id", 100 BrandID: "bulky-brand-id-1", 101 DisplayName: "bulky model", 102 }, 103 Brand: snap.StoreAccount{ 104 ID: "bulky-brand-id-1", 105 Username: "bulky-brand", 106 DisplayName: "bulky publishing", 107 }, 108 Actions: []client.SystemAction{ 109 {Title: "factory-reset", Mode: "install"}, 110 }, 111 }, 112 }) 113 } 114 115 func (cs *clientSuite) TestListSystemsNone(c *check.C) { 116 cs.rsp = `{ 117 "type": "sync", 118 "status-code": 200, 119 "result": {} 120 }` 121 systems, err := cs.cli.ListSystems() 122 c.Assert(err, check.IsNil) 123 c.Check(cs.req.Method, check.Equals, "GET") 124 c.Check(cs.req.URL.Path, check.Equals, "/v2/systems") 125 c.Check(systems, check.HasLen, 0) 126 } 127 128 func (cs *clientSuite) TestRequestSystemActionHappy(c *check.C) { 129 cs.rsp = `{ 130 "type": "sync", 131 "status-code": 200, 132 "result": {} 133 }` 134 err := cs.cli.DoSystemAction("1234", &client.SystemAction{ 135 Title: "reinstall", 136 Mode: "install", 137 }) 138 c.Assert(err, check.IsNil) 139 c.Check(cs.req.Method, check.Equals, "POST") 140 c.Check(cs.req.URL.Path, check.Equals, "/v2/systems/1234") 141 142 body, err := ioutil.ReadAll(cs.req.Body) 143 c.Assert(err, check.IsNil) 144 var req map[string]interface{} 145 err = json.Unmarshal(body, &req) 146 c.Assert(err, check.IsNil) 147 c.Assert(req, check.DeepEquals, map[string]interface{}{ 148 "action": "do", 149 "title": "reinstall", 150 "mode": "install", 151 }) 152 } 153 154 func (cs *clientSuite) TestRequestSystemActionError(c *check.C) { 155 cs.rsp = `{ 156 "type": "error", 157 "status-code": 500, 158 "result": {"message": "failed"} 159 }` 160 err := cs.cli.DoSystemAction("1234", &client.SystemAction{Mode: "install"}) 161 c.Assert(err, check.ErrorMatches, "cannot request system action: failed") 162 c.Check(cs.req.Method, check.Equals, "POST") 163 c.Check(cs.req.URL.Path, check.Equals, "/v2/systems/1234") 164 } 165 166 func (cs *clientSuite) TestRequestSystemActionInvalid(c *check.C) { 167 err := cs.cli.DoSystemAction("", &client.SystemAction{}) 168 c.Assert(err, check.ErrorMatches, "cannot request an action without the system") 169 err = cs.cli.DoSystemAction("1234", nil) 170 c.Assert(err, check.ErrorMatches, "cannot request an action without one") 171 } 172 173 func (cs *clientSuite) TestRequestSystemRebootHappy(c *check.C) { 174 cs.rsp = `{ 175 "type": "sync", 176 "status-code": 200, 177 "result": {} 178 }` 179 err := cs.cli.RebootToSystem("20201212", "install") 180 c.Assert(err, check.IsNil) 181 c.Check(cs.req.Method, check.Equals, "POST") 182 c.Check(cs.req.URL.Path, check.Equals, "/v2/systems/20201212") 183 184 body, err := ioutil.ReadAll(cs.req.Body) 185 c.Assert(err, check.IsNil) 186 var req map[string]interface{} 187 err = json.Unmarshal(body, &req) 188 c.Assert(err, check.IsNil) 189 c.Assert(req, check.DeepEquals, map[string]interface{}{ 190 "action": "reboot", 191 "mode": "install", 192 }) 193 } 194 195 func (cs *clientSuite) TestRequestSystemRebootErrorNoSystem(c *check.C) { 196 cs.rsp = `{ 197 "type": "error", 198 "status-code": 500, 199 "result": {"message": "failed"} 200 }` 201 err := cs.cli.RebootToSystem("", "install") 202 c.Assert(err, check.ErrorMatches, `cannot request system reboot: failed`) 203 c.Check(cs.req.Method, check.Equals, "POST") 204 c.Check(cs.req.URL.Path, check.Equals, "/v2/systems") 205 } 206 207 func (cs *clientSuite) TestRequestSystemRebootErrorWithSystem(c *check.C) { 208 cs.rsp = `{ 209 "type": "error", 210 "status-code": 500, 211 "result": {"message": "failed"} 212 }` 213 err := cs.cli.RebootToSystem("1234", "install") 214 c.Assert(err, check.ErrorMatches, `cannot request system reboot into "1234": failed`) 215 c.Check(cs.req.Method, check.Equals, "POST") 216 c.Check(cs.req.URL.Path, check.Equals, "/v2/systems/1234") 217 }