github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_debug_state_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 main_test 21 22 import ( 23 "io/ioutil" 24 "path/filepath" 25 26 . "gopkg.in/check.v1" 27 28 main "github.com/snapcore/snapd/cmd/snap" 29 ) 30 31 var stateJSON = []byte(` 32 { 33 "last-task-id": 31, 34 "last-change-id": 2, 35 36 "data": { 37 "snaps": {}, 38 "seeded": true 39 }, 40 "changes": { 41 "1": { 42 "id": "1", 43 "kind": "install-snap", 44 "summary": "install a snap", 45 "status": 0, 46 "data": {"snap-names": ["a"]}, 47 "task-ids": ["11","12"] 48 }, 49 "2": { 50 "id": "2", 51 "kind": "revert-snap", 52 "summary": "revert c snap", 53 "status": 0, 54 "data": {"snap-names": ["c"]}, 55 "task-ids": ["21","31"] 56 } 57 }, 58 "tasks": { 59 "11": { 60 "id": "11", 61 "change": "1", 62 "kind": "download-snap", 63 "summary": "Download snap a from channel edge", 64 "status": 4, 65 "data": {"snap-setup": { 66 "channel": "edge", 67 "flags": 1 68 }}, 69 "halt-tasks": ["12"] 70 }, 71 "12": {"id": "12", "change": "1", "kind": "some-other-task"}, 72 "21": { 73 "id": "21", 74 "change": "2", 75 "kind": "download-snap", 76 "summary": "Download snap b from channel beta", 77 "status": 4, 78 "data": {"snap-setup": { 79 "channel": "beta", 80 "flags": 2 81 }}, 82 "halt-tasks": ["12"] 83 }, 84 "31": { 85 "id": "31", 86 "change": "2", 87 "kind": "prepare-snap", 88 "summary": "Prepare snap c", 89 "status": 4, 90 "data": {"snap-setup": { 91 "channel": "stable", 92 "flags": 1073741828 93 }}, 94 "halt-tasks": ["12"], 95 "log": ["logline1", "logline2"] 96 } 97 } 98 } 99 `) 100 101 func (s *SnapSuite) TestDebugChanges(c *C) { 102 dir := c.MkDir() 103 stateFile := filepath.Join(dir, "test-state.json") 104 c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil) 105 106 rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--abs-time", "--changes", stateFile}) 107 c.Assert(err, IsNil) 108 c.Assert(rest, DeepEquals, []string{}) 109 c.Check(s.Stdout(), Matches, 110 "ID Status Spawn Ready Label Summary\n"+ 111 "1 Do 0001-01-01T00:00:00Z 0001-01-01T00:00:00Z install-snap install a snap\n"+ 112 "2 Done 0001-01-01T00:00:00Z 0001-01-01T00:00:00Z revert-snap revert c snap\n") 113 c.Check(s.Stderr(), Equals, "") 114 } 115 116 func (s *SnapSuite) TestDebugChangesMissingState(c *C) { 117 _, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--changes", "/missing-state.json"}) 118 c.Check(err, ErrorMatches, "cannot read the state file: open /missing-state.json: no such file or directory") 119 } 120 121 func (s *SnapSuite) TestDebugTask(c *C) { 122 dir := c.MkDir() 123 stateFile := filepath.Join(dir, "test-state.json") 124 c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil) 125 126 rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=31", stateFile}) 127 c.Assert(err, IsNil) 128 c.Assert(rest, DeepEquals, []string{}) 129 c.Check(s.Stdout(), Equals, "id: 31\n"+ 130 "kind: prepare-snap\n"+ 131 "summary: Prepare snap c\n"+ 132 "status: Done\n"+ 133 "log: |\n"+ 134 " logline1\n"+ 135 " logline2\n"+ 136 "\n"+ 137 "halt-tasks:\n"+ 138 " - some-other-task (12)\n") 139 c.Check(s.Stderr(), Equals, "") 140 } 141 142 func (s *SnapSuite) TestDebugTaskEmptyLists(c *C) { 143 dir := c.MkDir() 144 stateFile := filepath.Join(dir, "test-state.json") 145 c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil) 146 147 rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=12", stateFile}) 148 c.Assert(err, IsNil) 149 c.Assert(rest, DeepEquals, []string{}) 150 c.Check(s.Stdout(), Equals, "id: 12\n"+ 151 "kind: some-other-task\n"+ 152 "summary: \n"+ 153 "status: Do\n"+ 154 "halt-tasks: []\n") 155 c.Check(s.Stderr(), Equals, "") 156 } 157 158 func (s *SnapSuite) TestDebugTaskMissingState(c *C) { 159 _, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=1", "/missing-state.json"}) 160 c.Check(err, ErrorMatches, "cannot read the state file: open /missing-state.json: no such file or directory") 161 } 162 163 func (s *SnapSuite) TestDebugTaskNoSuchTaskError(c *C) { 164 dir := c.MkDir() 165 stateFile := filepath.Join(dir, "test-state.json") 166 c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil) 167 168 _, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=99", stateFile}) 169 c.Check(err, ErrorMatches, "no such task: 99") 170 } 171 172 func (s *SnapSuite) TestDebugTaskMutuallyExclusiveCommands(c *C) { 173 dir := c.MkDir() 174 stateFile := filepath.Join(dir, "test-state.json") 175 c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil) 176 177 _, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=99", "--changes", stateFile}) 178 c.Check(err, ErrorMatches, "cannot use --changes and --task= together") 179 180 _, err = main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--changes", "--change=1", stateFile}) 181 c.Check(err, ErrorMatches, "cannot use --changes and --change= together") 182 183 _, err = main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--change=1", "--task=1", stateFile}) 184 c.Check(err, ErrorMatches, "cannot use --change= and --task= together") 185 186 _, err = main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--change=1", "--is-seeded", stateFile}) 187 c.Check(err, ErrorMatches, "cannot use --change= and --is-seeded together") 188 } 189 190 func (s *SnapSuite) TestDebugTasks(c *C) { 191 dir := c.MkDir() 192 stateFile := filepath.Join(dir, "test-state.json") 193 c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil) 194 195 rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--abs-time", "--change=1", stateFile}) 196 c.Assert(err, IsNil) 197 c.Assert(rest, DeepEquals, []string{}) 198 c.Check(s.Stdout(), Matches, 199 "Lanes ID Status Spawn Ready Kind Summary\n"+ 200 "0 11 Done 0001-01-01T00:00:00Z 0001-01-01T00:00:00Z download-snap Download snap a from channel edge\n"+ 201 "0 12 Do 0001-01-01T00:00:00Z 0001-01-01T00:00:00Z some-other-task \n") 202 c.Check(s.Stderr(), Equals, "") 203 } 204 205 func (s *SnapSuite) TestDebugTasksMissingState(c *C) { 206 _, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--change=1", "/missing-state.json"}) 207 c.Check(err, ErrorMatches, "cannot read the state file: open /missing-state.json: no such file or directory") 208 } 209 210 func (s *SnapSuite) TestDebugIsSeededHappy(c *C) { 211 dir := c.MkDir() 212 stateFile := filepath.Join(dir, "test-state.json") 213 c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil) 214 215 rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--is-seeded", stateFile}) 216 c.Assert(err, IsNil) 217 c.Assert(rest, DeepEquals, []string{}) 218 c.Check(s.Stdout(), Matches, "true\n") 219 c.Check(s.Stderr(), Equals, "") 220 } 221 222 func (s *SnapSuite) TestDebugIsSeededNo(c *C) { 223 dir := c.MkDir() 224 stateFile := filepath.Join(dir, "test-state.json") 225 c.Assert(ioutil.WriteFile(stateFile, []byte("{}"), 0644), IsNil) 226 227 rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--is-seeded", stateFile}) 228 c.Assert(err, IsNil) 229 c.Assert(rest, DeepEquals, []string{}) 230 c.Check(s.Stdout(), Matches, "false\n") 231 c.Check(s.Stderr(), Equals, "") 232 }