github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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  	"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  	},
    39  	"changes": {
    40  		"1": {
    41  			"id": "1",
    42  			"kind": "install-snap",
    43  			"summary": "install a snap",
    44  			"status": 0,
    45  			"data": {"snap-names": ["a"]},
    46  			"task-ids": ["11","12"]
    47  		},
    48  		"2": {
    49  			"id": "2",
    50  			"kind": "revert-snap",
    51  			"summary": "revert c snap",
    52  			"status": 0,
    53  			"data": {"snap-names": ["c"]},
    54  			"task-ids": ["21","31"]
    55  		}
    56  	},
    57  	"tasks": {
    58  		"11": {
    59  				"id": "11",
    60  				"change": "1",
    61  				"kind": "download-snap",
    62  				"summary": "Download snap a from channel edge",
    63  				"status": 4,
    64  				"data": {"snap-setup": {
    65  						"channel": "edge",
    66  						"flags": 1
    67  				}},
    68  				"halt-tasks": ["12"]
    69  		},
    70  		"12": {"id": "12", "change": "1", "kind": "some-other-task"},
    71  		"21": {
    72  				"id": "21",
    73  				"change": "2",
    74  				"kind": "download-snap",
    75  				"summary": "Download snap b from channel beta",
    76  				"status": 4,
    77  				"data": {"snap-setup": {
    78  						"channel": "beta",
    79  						"flags": 2
    80  				}},
    81  				"halt-tasks": ["12"]
    82  		},
    83  		"31": {
    84  				"id": "31",
    85  				"change": "2",
    86  				"kind": "prepare-snap",
    87  				"summary": "Prepare snap c",
    88  				"status": 4,
    89  				"data": {"snap-setup": {
    90  						"channel": "stable",
    91  						"flags": 1073741828
    92  				}},
    93  				"halt-tasks": ["12"],
    94  				"log": ["logline1", "logline2"]
    95  		}
    96  	}
    97  }
    98  `)
    99  
   100  func (s *SnapSuite) TestDebugChanges(c *C) {
   101  	dir := c.MkDir()
   102  	stateFile := filepath.Join(dir, "test-state.json")
   103  	c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil)
   104  
   105  	rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--changes", stateFile})
   106  	c.Assert(err, IsNil)
   107  	c.Assert(rest, DeepEquals, []string{})
   108  	c.Check(s.Stdout(), Matches,
   109  		"ID   Status  Spawn       Ready       Label         Summary\n"+
   110  			"1    Do      0001-01-01  0001-01-01  install-snap  install a snap\n"+
   111  			"2    Done    0001-01-01  0001-01-01  revert-snap   revert c snap\n")
   112  	c.Check(s.Stderr(), Equals, "")
   113  }
   114  
   115  func (s *SnapSuite) TestDebugChangesMissingState(c *C) {
   116  	_, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--changes", "/missing-state.json"})
   117  	c.Check(err, ErrorMatches, "cannot read the state file: open /missing-state.json: no such file or directory")
   118  }
   119  
   120  func (s *SnapSuite) TestDebugTask(c *C) {
   121  	dir := c.MkDir()
   122  	stateFile := filepath.Join(dir, "test-state.json")
   123  	c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil)
   124  
   125  	rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=31", stateFile})
   126  	c.Assert(err, IsNil)
   127  	c.Assert(rest, DeepEquals, []string{})
   128  	c.Check(s.Stdout(), Equals, "id: 31\n"+
   129  		"kind: prepare-snap\n"+
   130  		"summary: Prepare snap c\n"+
   131  		"status: Done\n"+
   132  		"log: |\n"+
   133  		"  logline1\n"+
   134  		"  logline2\n"+
   135  		"\n"+
   136  		"halt-tasks:\n"+
   137  		" - some-other-task (12)\n")
   138  	c.Check(s.Stderr(), Equals, "")
   139  }
   140  
   141  func (s *SnapSuite) TestDebugTaskEmptyLists(c *C) {
   142  	dir := c.MkDir()
   143  	stateFile := filepath.Join(dir, "test-state.json")
   144  	c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil)
   145  
   146  	rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=12", stateFile})
   147  	c.Assert(err, IsNil)
   148  	c.Assert(rest, DeepEquals, []string{})
   149  	c.Check(s.Stdout(), Equals, "id: 12\n"+
   150  		"kind: some-other-task\n"+
   151  		"summary: \n"+
   152  		"status: Do\n"+
   153  		"halt-tasks: []\n")
   154  	c.Check(s.Stderr(), Equals, "")
   155  }
   156  
   157  func (s *SnapSuite) TestDebugTaskMissingState(c *C) {
   158  	_, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=1", "/missing-state.json"})
   159  	c.Check(err, ErrorMatches, "cannot read the state file: open /missing-state.json: no such file or directory")
   160  }
   161  
   162  func (s *SnapSuite) TestDebugTaskNoSuchTaskError(c *C) {
   163  	dir := c.MkDir()
   164  	stateFile := filepath.Join(dir, "test-state.json")
   165  	c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil)
   166  
   167  	_, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=99", stateFile})
   168  	c.Check(err, ErrorMatches, "no such task: 99")
   169  }
   170  
   171  func (s *SnapSuite) TestDebugTaskMutuallyExclusiveCommands(c *C) {
   172  	dir := c.MkDir()
   173  	stateFile := filepath.Join(dir, "test-state.json")
   174  	c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil)
   175  
   176  	_, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--task=99", "--changes", stateFile})
   177  	c.Check(err, ErrorMatches, "cannot use --changes and --task= together")
   178  
   179  	_, err = main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--changes", "--change=1", stateFile})
   180  	c.Check(err, ErrorMatches, "cannot use --changes and --change= together")
   181  
   182  	_, err = main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--change=1", "--task=1", stateFile})
   183  	c.Check(err, ErrorMatches, "cannot use --change= and --task= together")
   184  }
   185  
   186  func (s *SnapSuite) TestDebugTasks(c *C) {
   187  	dir := c.MkDir()
   188  	stateFile := filepath.Join(dir, "test-state.json")
   189  	c.Assert(ioutil.WriteFile(stateFile, stateJSON, 0644), IsNil)
   190  
   191  	rest, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--change=1", stateFile})
   192  	c.Assert(err, IsNil)
   193  	c.Assert(rest, DeepEquals, []string{})
   194  	c.Check(s.Stdout(), Matches,
   195  		"Lanes  ID   Status  Spawn       Ready       Kind             Summary\n"+
   196  			"0      11   Done    0001-01-01  0001-01-01  download-snap    Download snap a from channel edge\n"+
   197  			"0      12   Do      0001-01-01  0001-01-01  some-other-task  \n")
   198  	c.Check(s.Stderr(), Equals, "")
   199  }
   200  
   201  func (s *SnapSuite) TestDebugTasksMissingState(c *C) {
   202  	_, err := main.Parser(main.Client()).ParseArgs([]string{"debug", "state", "--change=1", "/missing-state.json"})
   203  	c.Check(err, ErrorMatches, "cannot read the state file: open /missing-state.json: no such file or directory")
   204  }