github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_watch_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  	"time"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	snap "github.com/snapcore/snapd/cmd/snap"
    30  	"github.com/snapcore/snapd/progress"
    31  	"github.com/snapcore/snapd/progress/progresstest"
    32  )
    33  
    34  var fmtWatchChangeJSON = `{"type": "sync", "result": {
    35    "id":   "two",
    36    "kind": "some-kind",
    37    "summary": "some summary...",
    38    "status": "Doing",
    39    "ready": false,
    40    "tasks": [{"id": "84", "kind": "bar", "summary": "some summary", "status": "Doing", "progress": {"label": "my-snap", "done": %d, "total": %d}, "spawn-time": "2016-04-21T01:02:03Z", "ready-time": "2016-04-21T01:02:04Z"}]
    41  }}`
    42  
    43  func (s *SnapSuite) TestCmdWatch(c *C) {
    44  	meter := &progresstest.Meter{}
    45  	defer progress.MockMeter(meter)()
    46  	defer snap.MockMaxGoneTime(time.Millisecond)()
    47  	defer snap.MockPollTime(time.Millisecond)()
    48  
    49  	n := 0
    50  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    51  		n++
    52  		switch n {
    53  		case 1:
    54  			c.Check(r.Method, Equals, "GET")
    55  			c.Check(r.URL.Path, Equals, "/v2/changes/two")
    56  			fmt.Fprintf(w, fmtWatchChangeJSON, 0, 100*1024)
    57  		case 2:
    58  			c.Check(r.Method, Equals, "GET")
    59  			c.Check(r.URL.Path, Equals, "/v2/changes/two")
    60  			fmt.Fprintf(w, fmtWatchChangeJSON, 50*1024, 100*1024)
    61  		case 3:
    62  			c.Check(r.Method, Equals, "GET")
    63  			c.Check(r.URL.Path, Equals, "/v2/changes/two")
    64  			fmt.Fprintln(w, `{"type": "sync", "result": {"id": "two", "ready": true, "status": "Done"}}`)
    65  		default:
    66  			c.Errorf("expected 3 queries, currently on %d", n)
    67  		}
    68  	})
    69  
    70  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"watch", "two"})
    71  	c.Assert(err, IsNil)
    72  	c.Assert(rest, HasLen, 0)
    73  	c.Check(n, Equals, 3)
    74  	c.Check(meter.Values, DeepEquals, []float64{51200})
    75  	c.Check(s.Stdout(), Equals, "")
    76  	c.Check(s.Stderr(), Equals, "")
    77  }
    78  
    79  func (s *SnapSuite) TestWatchLast(c *C) {
    80  	meter := &progresstest.Meter{}
    81  	defer progress.MockMeter(meter)()
    82  	defer snap.MockMaxGoneTime(time.Millisecond)()
    83  	defer snap.MockPollTime(time.Millisecond)()
    84  
    85  	n := 0
    86  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    87  		n++
    88  		switch n {
    89  		case 1:
    90  			c.Check(r.Method, Equals, "GET")
    91  			c.Check(r.URL.Path, Equals, "/v2/changes")
    92  			fmt.Fprintln(w, mockChangesJSON)
    93  		case 2:
    94  			c.Check(r.Method, Equals, "GET")
    95  			c.Check(r.URL.Path, Equals, "/v2/changes/two")
    96  			fmt.Fprintf(w, fmtWatchChangeJSON, 0, 100*1024)
    97  		case 3:
    98  			c.Check(r.Method, Equals, "GET")
    99  			c.Check(r.URL.Path, Equals, "/v2/changes/two")
   100  			fmt.Fprintf(w, fmtWatchChangeJSON, 50*1024, 100*1024)
   101  		case 4:
   102  			c.Check(r.Method, Equals, "GET")
   103  			c.Check(r.URL.Path, Equals, "/v2/changes/two")
   104  			fmt.Fprintln(w, `{"type": "sync", "result": {"id": "two", "ready": true, "status": "Done"}}`)
   105  		default:
   106  			c.Errorf("expected 4 queries, currently on %d", n)
   107  		}
   108  	})
   109  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"watch", "--last=install"})
   110  	c.Assert(err, IsNil)
   111  	c.Assert(rest, HasLen, 0)
   112  	c.Check(n, Equals, 4)
   113  	c.Check(meter.Values, DeepEquals, []float64{51200})
   114  	c.Check(s.Stdout(), Equals, "")
   115  	c.Check(s.Stderr(), Equals, "")
   116  }
   117  
   118  func (s *SnapSuite) TestWatchLastQuestionmark(c *C) {
   119  	meter := &progresstest.Meter{}
   120  	defer progress.MockMeter(meter)()
   121  	restore := snap.MockMaxGoneTime(time.Millisecond)
   122  	defer restore()
   123  
   124  	n := 0
   125  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   126  		n++
   127  		c.Check(r.Method, Equals, "GET")
   128  		c.Assert(r.URL.Path, Equals, "/v2/changes")
   129  		switch n {
   130  		case 1, 2:
   131  			fmt.Fprintln(w, `{"type": "sync", "result": []}`)
   132  		case 3, 4:
   133  			fmt.Fprintln(w, mockChangesJSON)
   134  		default:
   135  			c.Errorf("expected 4 calls, now on %d", n)
   136  		}
   137  	})
   138  	for i := 0; i < 2; i++ {
   139  		rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"watch", "--last=foobar?"})
   140  		c.Assert(err, IsNil)
   141  		c.Assert(rest, DeepEquals, []string{})
   142  		c.Check(s.Stdout(), Matches, "")
   143  		c.Check(s.Stderr(), Equals, "")
   144  
   145  		_, err = snap.Parser(snap.Client()).ParseArgs([]string{"watch", "--last=foobar"})
   146  		if i == 0 {
   147  			c.Assert(err, ErrorMatches, `no changes found`)
   148  		} else {
   149  			c.Assert(err, ErrorMatches, `no changes of type "foobar" found`)
   150  		}
   151  	}
   152  
   153  	c.Check(n, Equals, 4)
   154  }