github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap/cmd_changes_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 main_test
    21  
    22  import (
    23  	"fmt"
    24  	"net/http"
    25  	"strings"
    26  
    27  	"gopkg.in/check.v1"
    28  
    29  	snap "github.com/snapcore/snapd/cmd/snap"
    30  )
    31  
    32  var mockChangeJSON = `{"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": "some 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  func (s *SnapSuite) TestChangeSimple(c *check.C) {
    44  	n := 0
    45  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    46  		if n < 2 {
    47  			c.Check(r.Method, check.Equals, "GET")
    48  			c.Check(r.URL.Path, check.Equals, "/v2/changes/42")
    49  			fmt.Fprintln(w, mockChangeJSON)
    50  		} else {
    51  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
    52  		}
    53  
    54  		n++
    55  	})
    56  	expectedChange := `(?ms)Status +Spawn +Ready +Summary
    57  Do +2016-04-21T01:02:03Z +2016-04-21T01:02:04Z +some summary
    58  `
    59  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"change", "--abs-time", "42"})
    60  	c.Assert(err, check.IsNil)
    61  	c.Assert(rest, check.DeepEquals, []string{})
    62  	c.Check(s.Stdout(), check.Matches, expectedChange)
    63  	c.Check(s.Stderr(), check.Equals, "")
    64  
    65  	rest, err = snap.Parser(snap.Client()).ParseArgs([]string{"tasks", "--abs-time", "42"})
    66  	c.Assert(err, check.IsNil)
    67  	c.Assert(rest, check.DeepEquals, []string{})
    68  	c.Check(s.Stdout(), check.Matches, expectedChange)
    69  	c.Check(s.Stderr(), check.Equals, "")
    70  }
    71  
    72  func (s *SnapSuite) TestChangeSimpleRebooting(c *check.C) {
    73  	n := 0
    74  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    75  		if n < 2 {
    76  			c.Check(r.Method, check.Equals, "GET")
    77  			c.Check(r.URL.Path, check.Equals, "/v2/changes/42")
    78  			fmt.Fprintln(w, strings.Replace(mockChangeJSON, `"type": "sync"`, `"type": "sync", "maintenance": {"kind": "system-restart", "message": "system is restarting"}`, 1))
    79  		} else {
    80  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
    81  		}
    82  
    83  		n++
    84  	})
    85  
    86  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"change", "42"})
    87  	c.Assert(err, check.IsNil)
    88  	c.Check(s.Stderr(), check.Equals, "WARNING: snapd is about to reboot the system\n")
    89  }
    90  
    91  var mockChangesJSON = `{"type": "sync", "result": [
    92    {
    93      "id":   "four",
    94      "kind": "install-snap",
    95      "summary": "...",
    96      "status": "Do",
    97      "ready": false,
    98      "spawn-time": "2015-02-21T01:02:03Z",
    99      "ready-time": "2015-02-21T01:02:04Z",
   100      "tasks": [{"kind": "bar", "summary": "some summary", "status": "Do", "progress": {"done": 0, "total": 1}, "spawn-time": "2015-02-21T01:02:03Z", "ready-time": "2015-02-21T01:02:04Z"}]
   101    },
   102    {
   103      "id":   "one",
   104      "kind": "remove-snap",
   105      "summary": "...",
   106      "status": "Do",
   107      "ready": false,
   108      "spawn-time": "2016-03-21T01:02:03Z",
   109      "ready-time": "2016-03-21T01:02:04Z",
   110      "tasks": [{"kind": "bar", "summary": "some summary", "status": "Do", "progress": {"done": 0, "total": 1}, "spawn-time": "2016-03-21T01:02:03Z", "ready-time": "2016-03-21T01:02:04Z"}]
   111    },
   112    {
   113      "id":   "two",
   114      "kind": "install-snap",
   115      "summary": "...",
   116      "status": "Do",
   117      "ready": false,
   118      "spawn-time": "2016-04-21T01:02:03Z",
   119      "ready-time": "2016-04-21T01:02:04Z",
   120      "tasks": [{"kind": "bar", "summary": "some summary", "status": "Do", "progress": {"done": 0, "total": 1}, "spawn-time": "2016-04-21T01:02:03Z", "ready-time": "2016-04-21T01:02:04Z"}]
   121    },
   122    {
   123      "id":   "three",
   124      "kind": "install-snap",
   125      "summary": "...",
   126      "status": "Do",
   127      "ready": false,
   128      "spawn-time": "2016-01-21T01:02:03Z",
   129      "ready-time": "2016-01-21T01:02:04Z",
   130      "tasks": [{"kind": "bar", "summary": "some summary", "status": "Do", "progress": {"done": 0, "total": 1}, "spawn-time": "2016-01-21T01:02:03Z", "ready-time": "2016-01-21T01:02:04Z"}]
   131    }
   132  ]}`
   133  
   134  func (s *SnapSuite) TestTasksLast(c *check.C) {
   135  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   136  		c.Check(r.Method, check.Equals, "GET")
   137  		if r.URL.Path == "/v2/changes" {
   138  			fmt.Fprintln(w, mockChangesJSON)
   139  			return
   140  		}
   141  		c.Assert(r.URL.Path, check.Equals, "/v2/changes/two")
   142  		fmt.Fprintln(w, mockChangeJSON)
   143  	})
   144  	expectedChange := `(?ms)Status +Spawn +Ready +Summary
   145  Do +2016-04-21T01:02:03Z +2016-04-21T01:02:04Z +some summary
   146  `
   147  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"tasks", "--abs-time", "--last=install"})
   148  	c.Assert(err, check.IsNil)
   149  	c.Assert(rest, check.DeepEquals, []string{})
   150  	c.Check(s.Stdout(), check.Matches, expectedChange)
   151  	c.Check(s.Stderr(), check.Equals, "")
   152  
   153  	_, err = snap.Parser(snap.Client()).ParseArgs([]string{"tasks", "--abs-time", "--last=foobar"})
   154  	c.Assert(err, check.NotNil)
   155  	c.Assert(err, check.ErrorMatches, `no changes of type "foobar" found`)
   156  }
   157  
   158  func (s *SnapSuite) TestTasksLastQuestionmark(c *check.C) {
   159  	n := 0
   160  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   161  		n++
   162  		c.Check(r.Method, check.Equals, "GET")
   163  		c.Assert(r.URL.Path, check.Equals, "/v2/changes")
   164  		switch n {
   165  		case 1, 2:
   166  			fmt.Fprintln(w, `{"type": "sync", "result": []}`)
   167  		case 3, 4:
   168  			fmt.Fprintln(w, mockChangesJSON)
   169  		default:
   170  			c.Errorf("expected 4 calls, now on %d", n)
   171  		}
   172  	})
   173  	for i := 0; i < 2; i++ {
   174  		rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"tasks", "--last=foobar?"})
   175  		c.Assert(err, check.IsNil)
   176  		c.Assert(rest, check.DeepEquals, []string{})
   177  		c.Check(s.Stdout(), check.Matches, "")
   178  		c.Check(s.Stderr(), check.Equals, "")
   179  
   180  		_, err = snap.Parser(snap.Client()).ParseArgs([]string{"tasks", "--last=foobar"})
   181  		if i == 0 {
   182  			c.Assert(err, check.ErrorMatches, `no changes found`)
   183  		} else {
   184  			c.Assert(err, check.ErrorMatches, `no changes of type "foobar" found`)
   185  		}
   186  	}
   187  
   188  	c.Check(n, check.Equals, 4)
   189  }
   190  
   191  func (s *SnapSuite) TestTasksSyntaxError(c *check.C) {
   192  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"tasks", "--abs-time", "--last=install", "42"})
   193  	c.Assert(err, check.NotNil)
   194  	c.Assert(err, check.ErrorMatches, `cannot use change ID and type together`)
   195  
   196  	_, err = snap.Parser(snap.Client()).ParseArgs([]string{"tasks"})
   197  	c.Assert(err, check.NotNil)
   198  	c.Assert(err, check.ErrorMatches, `please provide change ID or type with --last=<type>`)
   199  }
   200  
   201  var mockChangeInProgressJSON = `{"type": "sync", "result": {
   202    "id":   "uno",
   203    "kind": "foo",
   204    "summary": "...",
   205    "status": "Do",
   206    "ready": false,
   207    "spawn-time": "2016-04-21T01:02:03Z",
   208    "ready-time": "2016-04-21T01:02:04Z",
   209    "tasks": [{"kind": "bar", "summary": "some summary", "status": "Doing", "progress": {"done": 50, "total": 100}, "spawn-time": "2016-04-21T01:02:03Z", "ready-time": "2016-04-21T01:02:04Z"}]
   210  }}`
   211  
   212  func (s *SnapSuite) TestChangeProgress(c *check.C) {
   213  	n := 0
   214  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   215  		switch n {
   216  		case 0:
   217  			c.Check(r.Method, check.Equals, "GET")
   218  			c.Check(r.URL.Path, check.Equals, "/v2/changes/42")
   219  			fmt.Fprintln(w, mockChangeInProgressJSON)
   220  		default:
   221  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   222  		}
   223  
   224  		n++
   225  	})
   226  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"change", "--abs-time", "42"})
   227  	c.Assert(err, check.IsNil)
   228  	c.Assert(rest, check.DeepEquals, []string{})
   229  	c.Check(s.Stdout(), check.Matches, `(?ms)Status +Spawn +Ready +Summary
   230  Doing +2016-04-21T01:02:03Z +2016-04-21T01:02:04Z +some summary \(50.00%\)
   231  `)
   232  	c.Check(s.Stderr(), check.Equals, "")
   233  }