gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap/cmd_warnings_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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  	"io/ioutil"
    25  	"net/http"
    26  	"time"
    27  
    28  	"gopkg.in/check.v1"
    29  
    30  	snap "gitee.com/mysnapcore/mysnapd/cmd/snap"
    31  )
    32  
    33  type warningSuite struct {
    34  	BaseSnapSuite
    35  }
    36  
    37  var _ = check.Suite(&warningSuite{})
    38  
    39  const twoWarnings = `{
    40  			"result": [
    41  			    {
    42  				"expire-after": "672h0m0s",
    43  				"first-added": "2018-09-19T12:41:18.505007495Z",
    44  				"last-added": "2018-09-19T12:41:18.505007495Z",
    45  				"message": "hello world number one",
    46  				"repeat-after": "24h0m0s"
    47  			    },
    48  			    {
    49  				"expire-after": "672h0m0s",
    50  				"first-added": "2018-09-19T12:44:19.680362867Z",
    51  				"last-added": "2018-09-19T12:44:19.680362867Z",
    52  				"message": "hello world number two",
    53  				"repeat-after": "24h0m0s"
    54  			    }
    55  			],
    56  			"status": "OK",
    57  			"status-code": 200,
    58  			"type": "sync"
    59  		}`
    60  
    61  func mkWarningsFakeHandler(c *check.C, body string) func(w http.ResponseWriter, r *http.Request) {
    62  	var called bool
    63  	return func(w http.ResponseWriter, r *http.Request) {
    64  		if called {
    65  			c.Fatalf("expected a single request")
    66  		}
    67  		called = true
    68  		c.Check(r.URL.Path, check.Equals, "/v2/warnings")
    69  		c.Check(r.URL.Query(), check.HasLen, 0)
    70  
    71  		buf, err := ioutil.ReadAll(r.Body)
    72  		c.Assert(err, check.IsNil)
    73  		c.Check(string(buf), check.Equals, "")
    74  		c.Check(r.Method, check.Equals, "GET")
    75  		w.WriteHeader(200)
    76  		fmt.Fprintln(w, body)
    77  	}
    78  }
    79  
    80  func (s *warningSuite) TestNoWarningsEver(c *check.C) {
    81  	s.RedirectClientToTestServer(mkWarningsFakeHandler(c, `{"type": "sync", "status-code": 200, "result": []}`))
    82  
    83  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"warnings", "--abs-time"})
    84  	c.Assert(err, check.IsNil)
    85  	c.Check(rest, check.HasLen, 0)
    86  	c.Check(s.Stderr(), check.Equals, "")
    87  	c.Check(s.Stdout(), check.Equals, "No warnings.\n")
    88  }
    89  
    90  func (s *warningSuite) TestNoFurtherWarnings(c *check.C) {
    91  	snap.WriteWarningTimestamp(time.Now())
    92  
    93  	s.RedirectClientToTestServer(mkWarningsFakeHandler(c, `{"type": "sync", "status-code": 200, "result": []}`))
    94  
    95  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"warnings", "--abs-time"})
    96  	c.Assert(err, check.IsNil)
    97  	c.Check(rest, check.HasLen, 0)
    98  	c.Check(s.Stderr(), check.Equals, "")
    99  	c.Check(s.Stdout(), check.Equals, "No further warnings.\n")
   100  }
   101  
   102  func (s *warningSuite) TestWarnings(c *check.C) {
   103  	s.RedirectClientToTestServer(mkWarningsFakeHandler(c, twoWarnings))
   104  
   105  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"warnings", "--abs-time", "--unicode=never"})
   106  	c.Assert(err, check.IsNil)
   107  	c.Check(rest, check.HasLen, 0)
   108  	c.Check(s.Stderr(), check.Equals, "")
   109  	c.Check(s.Stdout(), check.Equals, `
   110  last-occurrence:  2018-09-19T12:41:18Z
   111  warning: |
   112    hello world number one
   113  ---
   114  last-occurrence:  2018-09-19T12:44:19Z
   115  warning: |
   116    hello world number two
   117  `[1:])
   118  }
   119  
   120  func (s *warningSuite) TestVerboseWarnings(c *check.C) {
   121  	s.RedirectClientToTestServer(mkWarningsFakeHandler(c, twoWarnings))
   122  
   123  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"warnings", "--abs-time", "--verbose", "--unicode=never"})
   124  	c.Assert(err, check.IsNil)
   125  	c.Check(rest, check.HasLen, 0)
   126  	c.Check(s.Stderr(), check.Equals, "")
   127  	c.Check(s.Stdout(), check.Equals, `
   128  first-occurrence:  2018-09-19T12:41:18Z
   129  last-occurrence:   2018-09-19T12:41:18Z
   130  acknowledged:      --
   131  repeats-after:     1d00h
   132  expires-after:     28d0h
   133  warning: |
   134    hello world number one
   135  ---
   136  first-occurrence:  2018-09-19T12:44:19Z
   137  last-occurrence:   2018-09-19T12:44:19Z
   138  acknowledged:      --
   139  repeats-after:     1d00h
   140  expires-after:     28d0h
   141  warning: |
   142    hello world number two
   143  `[1:])
   144  }
   145  
   146  func (s *warningSuite) TestOkay(c *check.C) {
   147  	t0 := time.Now()
   148  	snap.WriteWarningTimestamp(t0)
   149  
   150  	var n int
   151  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   152  		n++
   153  		if n != 1 {
   154  			c.Fatalf("expected 1 request, now on %d", n)
   155  		}
   156  		c.Check(r.URL.Path, check.Equals, "/v2/warnings")
   157  		c.Check(r.URL.Query(), check.HasLen, 0)
   158  		c.Assert(DecodedRequestBody(c, r), check.DeepEquals, map[string]interface{}{"action": "okay", "timestamp": t0.Format(time.RFC3339Nano)})
   159  		c.Check(r.Method, check.Equals, "POST")
   160  		w.WriteHeader(200)
   161  		fmt.Fprintln(w, `{
   162  			"status": "OK",
   163  			"status-code": 200,
   164  			"type": "sync"
   165  		}`)
   166  	})
   167  
   168  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"okay"})
   169  	c.Assert(err, check.IsNil)
   170  	c.Check(rest, check.HasLen, 0)
   171  	c.Check(s.Stderr(), check.Equals, "")
   172  	c.Check(s.Stdout(), check.Equals, "")
   173  }
   174  
   175  func (s *warningSuite) TestOkayBeforeWarnings(c *check.C) {
   176  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"okay"})
   177  	c.Assert(err, check.ErrorMatches, "you must have looked at the warnings before acknowledging them. Try 'snap warnings'.")
   178  	c.Check(s.Stderr(), check.Equals, "")
   179  	c.Check(s.Stdout(), check.Equals, "")
   180  }
   181  
   182  func (s *warningSuite) TestListWithWarnings(c *check.C) {
   183  	var called bool
   184  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   185  		if called {
   186  			c.Fatalf("expected a single request")
   187  		}
   188  		called = true
   189  		c.Check(r.URL.Path, check.Equals, "/v2/snaps")
   190  		c.Check(r.URL.Query(), check.HasLen, 0)
   191  
   192  		buf, err := ioutil.ReadAll(r.Body)
   193  		c.Assert(err, check.IsNil)
   194  		c.Check(string(buf), check.Equals, "")
   195  		c.Check(r.Method, check.Equals, "GET")
   196  		w.WriteHeader(200)
   197  		fmt.Fprintln(w, `{
   198  				"result": [{}],
   199  				"status": "OK",
   200  				"status-code": 200,
   201  				"type": "sync",
   202  				"warning-count": 2,
   203  				"warning-timestamp": "2018-09-19T12:44:19.680362867Z"
   204  			}`)
   205  	})
   206  	cli := snap.Client()
   207  	rest, err := snap.Parser(cli).ParseArgs([]string{"list"})
   208  	c.Assert(err, check.IsNil)
   209  
   210  	{
   211  		// TODO: I hope to get to refactor run() so we can
   212  		// call it from tests and not have to do this (whole
   213  		// block) by hand
   214  
   215  		count, stamp := cli.WarningsSummary()
   216  		c.Check(count, check.Equals, 2)
   217  		c.Check(stamp, check.Equals, time.Date(2018, 9, 19, 12, 44, 19, 680362867, time.UTC))
   218  
   219  		snap.MaybePresentWarnings(count, stamp)
   220  	}
   221  
   222  	c.Check(rest, check.HasLen, 0)
   223  	c.Check(s.Stdout(), check.Equals, `
   224  Name  Version  Rev    Tracking  Publisher  Notes
   225        -        unset  -         -          disabled
   226  `[1:])
   227  	c.Check(s.Stderr(), check.Equals, "WARNING: There are 2 new warnings. See 'snap warnings'.\n")
   228  
   229  }