github.com/freetocompute/snapd@v0.0.0-20210618182524-2fb355d72fd9/client/change_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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 "io/ioutil" 24 "time" 25 26 "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/client" 29 ) 30 31 func (cs *clientSuite) TestClientChange(c *check.C) { 32 cs.rsp = `{"type": "sync", "result": { 33 "id": "uno", 34 "kind": "foo", 35 "summary": "...", 36 "status": "Do", 37 "ready": false, 38 "spawn-time": "2016-04-21T01:02:03Z", 39 "ready-time": "2016-04-21T01:02:04Z", 40 "tasks": [{"kind": "bar", "summary": "...", "status": "Do", "progress": {"done": 0, "total": 1}, "spawn-time": "2016-04-21T01:02:03Z", "ready-time": "2016-04-21T01:02:04Z"}] 41 }}` 42 43 chg, err := cs.cli.Change("uno") 44 c.Assert(err, check.IsNil) 45 c.Check(chg, check.DeepEquals, &client.Change{ 46 ID: "uno", 47 Kind: "foo", 48 Summary: "...", 49 Status: "Do", 50 Tasks: []*client.Task{{ 51 Kind: "bar", 52 Summary: "...", 53 Status: "Do", 54 Progress: client.TaskProgress{Done: 0, Total: 1}, 55 SpawnTime: time.Date(2016, 04, 21, 1, 2, 3, 0, time.UTC), 56 ReadyTime: time.Date(2016, 04, 21, 1, 2, 4, 0, time.UTC), 57 }}, 58 59 SpawnTime: time.Date(2016, 04, 21, 1, 2, 3, 0, time.UTC), 60 ReadyTime: time.Date(2016, 04, 21, 1, 2, 4, 0, time.UTC), 61 }) 62 } 63 64 func (cs *clientSuite) TestClientChangeData(c *check.C) { 65 cs.rsp = `{"type": "sync", "result": { 66 "id": "uno", 67 "kind": "foo", 68 "summary": "...", 69 "status": "Do", 70 "ready": false, 71 "data": {"n": 42} 72 }}` 73 74 chg, err := cs.cli.Change("uno") 75 c.Assert(err, check.IsNil) 76 var n int 77 err = chg.Get("n", &n) 78 c.Assert(err, check.IsNil) 79 c.Assert(n, check.Equals, 42) 80 81 err = chg.Get("missing", &n) 82 c.Assert(err, check.Equals, client.ErrNoData) 83 } 84 85 func (cs *clientSuite) TestClientChangeRestartingState(c *check.C) { 86 cs.rsp = `{"type": "sync", "result": { 87 "id": "uno", 88 "kind": "foo", 89 "summary": "...", 90 "status": "Do", 91 "ready": false 92 }, 93 "maintenance": {"kind": "system-restart", "message": "system is restarting"} 94 }` 95 96 chg, err := cs.cli.Change("uno") 97 c.Check(chg, check.NotNil) 98 c.Check(chg.ID, check.Equals, "uno") 99 c.Check(err, check.IsNil) 100 c.Check(cs.cli.Maintenance(), check.ErrorMatches, `system is restarting`) 101 } 102 103 func (cs *clientSuite) TestClientChangeError(c *check.C) { 104 cs.rsp = `{"type": "sync", "result": { 105 "id": "uno", 106 "kind": "foo", 107 "summary": "...", 108 "status": "Error", 109 "ready": true, 110 "tasks": [{"kind": "bar", "summary": "...", "status": "Error", "progress": {"done": 1, "total": 1}, "log": ["ERROR: something broke"]}], 111 "err": "error message" 112 }}` 113 114 chg, err := cs.cli.Change("uno") 115 c.Assert(err, check.IsNil) 116 c.Check(chg, check.DeepEquals, &client.Change{ 117 ID: "uno", 118 Kind: "foo", 119 Summary: "...", 120 Status: "Error", 121 Tasks: []*client.Task{{ 122 Kind: "bar", 123 Summary: "...", 124 Status: "Error", 125 Progress: client.TaskProgress{Done: 1, Total: 1}, 126 Log: []string{"ERROR: something broke"}, 127 }}, 128 Err: "error message", 129 Ready: true, 130 }) 131 } 132 133 func (cs *clientSuite) TestClientChangesString(c *check.C) { 134 for k, v := range map[client.ChangeSelector]string{ 135 client.ChangesAll: "all", 136 client.ChangesReady: "ready", 137 client.ChangesInProgress: "in-progress", 138 } { 139 c.Check(k.String(), check.Equals, v) 140 } 141 } 142 143 func (cs *clientSuite) TestClientChanges(c *check.C) { 144 cs.rsp = `{"type": "sync", "result": [{ 145 "id": "uno", 146 "kind": "foo", 147 "summary": "...", 148 "status": "Do", 149 "ready": false, 150 "tasks": [{"kind": "bar", "summary": "...", "status": "Do", "progress": {"done": 0, "total": 1}}] 151 }]}` 152 153 for _, i := range []*client.ChangesOptions{ 154 {Selector: client.ChangesAll}, 155 {Selector: client.ChangesReady}, 156 {Selector: client.ChangesInProgress}, 157 {SnapName: "foo"}, 158 nil, 159 } { 160 chg, err := cs.cli.Changes(i) 161 c.Assert(err, check.IsNil) 162 c.Check(chg, check.DeepEquals, []*client.Change{{ 163 ID: "uno", 164 Kind: "foo", 165 Summary: "...", 166 Status: "Do", 167 Tasks: []*client.Task{{Kind: "bar", Summary: "...", Status: "Do", Progress: client.TaskProgress{Done: 0, Total: 1}}}, 168 }}) 169 if i == nil { 170 c.Check(cs.req.URL.RawQuery, check.Equals, "") 171 } else { 172 if i.Selector != 0 { 173 c.Check(cs.req.URL.RawQuery, check.Equals, "select="+i.Selector.String()) 174 } else { 175 c.Check(cs.req.URL.RawQuery, check.Equals, "for="+i.SnapName) 176 } 177 } 178 } 179 180 } 181 182 func (cs *clientSuite) TestClientChangesData(c *check.C) { 183 cs.rsp = `{"type": "sync", "result": [{ 184 "id": "uno", 185 "kind": "foo", 186 "summary": "...", 187 "status": "Do", 188 "ready": false, 189 "data": {"n": 42} 190 }]}` 191 192 chgs, err := cs.cli.Changes(&client.ChangesOptions{Selector: client.ChangesAll}) 193 c.Assert(err, check.IsNil) 194 195 chg := chgs[0] 196 var n int 197 err = chg.Get("n", &n) 198 c.Assert(err, check.IsNil) 199 c.Assert(n, check.Equals, 42) 200 201 err = chg.Get("missing", &n) 202 c.Assert(err, check.Equals, client.ErrNoData) 203 } 204 205 func (cs *clientSuite) TestClientAbort(c *check.C) { 206 cs.rsp = `{"type": "sync", "result": { 207 "id": "uno", 208 "kind": "foo", 209 "summary": "...", 210 "status": "Hold", 211 "ready": true, 212 "spawn-time": "2016-04-21T01:02:03Z", 213 "ready-time": "2016-04-21T01:02:04Z" 214 }}` 215 216 chg, err := cs.cli.Abort("uno") 217 c.Assert(err, check.IsNil) 218 c.Check(cs.req.Method, check.Equals, "POST") 219 c.Check(chg, check.DeepEquals, &client.Change{ 220 ID: "uno", 221 Kind: "foo", 222 Summary: "...", 223 Status: "Hold", 224 Ready: true, 225 226 SpawnTime: time.Date(2016, 04, 21, 1, 2, 3, 0, time.UTC), 227 ReadyTime: time.Date(2016, 04, 21, 1, 2, 4, 0, time.UTC), 228 }) 229 230 body, err := ioutil.ReadAll(cs.req.Body) 231 c.Assert(err, check.IsNil) 232 233 c.Assert(string(body), check.Equals, "{\"action\":\"abort\"}\n") 234 }