github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/client/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 client_test
    21  
    22  import (
    23  	"encoding/json"
    24  	"time"
    25  
    26  	"gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/client"
    29  )
    30  
    31  func (cs *clientSuite) testWarnings(c *check.C, all bool) {
    32  	t1 := time.Date(2018, 9, 19, 12, 41, 18, 505007495, time.UTC)
    33  	t2 := time.Date(2018, 9, 19, 12, 44, 19, 680362867, time.UTC)
    34  	cs.rsp = `{
    35  		"result": [
    36  		    {
    37  			"expire-after": "672h0m0s",
    38  			"first-added": "2018-09-19T12:41:18.505007495Z",
    39  			"last-added": "2018-09-19T12:41:18.505007495Z",
    40  			"message": "hello world number one",
    41  			"repeat-after": "24h0m0s"
    42  		    },
    43  		    {
    44  			"expire-after": "672h0m0s",
    45  			"first-added": "2018-09-19T12:44:19.680362867Z",
    46  			"last-added": "2018-09-19T12:44:19.680362867Z",
    47  			"message": "hello world number two",
    48  			"repeat-after": "24h0m0s"
    49  		    }
    50  		],
    51  		"status": "OK",
    52  		"status-code": 200,
    53  		"type": "sync",
    54  		"warning-count": 2,
    55  		"warning-timestamp": "2018-09-19T12:44:19.680362867Z"
    56  	}`
    57  
    58  	ws, err := cs.cli.Warnings(client.WarningsOptions{All: all})
    59  	c.Assert(err, check.IsNil)
    60  	c.Check(ws, check.DeepEquals, []*client.Warning{
    61  		{
    62  			Message:     "hello world number one",
    63  			FirstAdded:  t1,
    64  			LastAdded:   t1,
    65  			ExpireAfter: time.Hour * 24 * 28,
    66  			RepeatAfter: time.Hour * 24,
    67  		},
    68  		{
    69  			Message:     "hello world number two",
    70  			FirstAdded:  t2,
    71  			LastAdded:   t2,
    72  			ExpireAfter: time.Hour * 24 * 28,
    73  			RepeatAfter: time.Hour * 24,
    74  		},
    75  	})
    76  	c.Check(cs.req.Method, check.Equals, "GET")
    77  	c.Check(cs.req.URL.Path, check.Equals, "/v2/warnings")
    78  	query := cs.req.URL.Query()
    79  	if all {
    80  		c.Check(query, check.HasLen, 1)
    81  		c.Check(query.Get("select"), check.Equals, "all")
    82  	} else {
    83  		c.Check(query, check.HasLen, 0)
    84  	}
    85  
    86  	// this could be done at the end of any sync method
    87  	count, stamp := cs.cli.WarningsSummary()
    88  	c.Check(count, check.Equals, 2)
    89  	c.Check(stamp, check.Equals, t2)
    90  }
    91  
    92  func (cs *clientSuite) TestWarningsAll(c *check.C) {
    93  	cs.testWarnings(c, true)
    94  }
    95  
    96  func (cs *clientSuite) TestWarnings(c *check.C) {
    97  	cs.testWarnings(c, false)
    98  }
    99  
   100  func (cs *clientSuite) TestOkay(c *check.C) {
   101  	cs.rsp = `{
   102  		"type": "sync",
   103  		"status-code": 200,
   104  		"result": { }
   105  	}`
   106  	t0 := time.Now()
   107  	err := cs.cli.Okay(t0)
   108  	c.Assert(err, check.IsNil)
   109  	c.Check(cs.req.Method, check.Equals, "POST")
   110  	c.Check(cs.req.URL.Query(), check.HasLen, 0)
   111  	var body map[string]interface{}
   112  	c.Assert(json.NewDecoder(cs.req.Body).Decode(&body), check.IsNil)
   113  	c.Check(body, check.HasLen, 2)
   114  	c.Check(body["action"], check.Equals, "okay")
   115  	c.Check(body["timestamp"], check.Equals, t0.Format(time.RFC3339Nano))
   116  
   117  	// note there's no warnings summary in the response
   118  	count, stamp := cs.cli.WarningsSummary()
   119  	c.Check(count, check.Equals, 0)
   120  	c.Check(stamp, check.Equals, time.Time{})
   121  }