github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/client/change_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 client_test
    21  
    22  import (
    23  	"gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/client"
    26  	"io/ioutil"
    27  	"time"
    28  )
    29  
    30  func (cs *clientSuite) TestClientChange(c *check.C) {
    31  	cs.rsp = `{"type": "sync", "result": {
    32    "id":   "uno",
    33    "kind": "foo",
    34    "summary": "...",
    35    "status": "Do",
    36    "ready": false,
    37    "spawn-time": "2016-04-21T01:02:03Z",
    38    "ready-time": "2016-04-21T01:02:04Z",
    39    "tasks": [{"kind": "bar", "summary": "...", "status": "Do", "progress": {"done": 0, "total": 1}, "spawn-time": "2016-04-21T01:02:03Z", "ready-time": "2016-04-21T01:02:04Z"}]
    40  }}`
    41  
    42  	chg, err := cs.cli.Change("uno")
    43  	c.Assert(err, check.IsNil)
    44  	c.Check(chg, check.DeepEquals, &client.Change{
    45  		ID:      "uno",
    46  		Kind:    "foo",
    47  		Summary: "...",
    48  		Status:  "Do",
    49  		Tasks: []*client.Task{{
    50  			Kind:      "bar",
    51  			Summary:   "...",
    52  			Status:    "Do",
    53  			Progress:  client.TaskProgress{Done: 0, Total: 1},
    54  			SpawnTime: time.Date(2016, 04, 21, 1, 2, 3, 0, time.UTC),
    55  			ReadyTime: time.Date(2016, 04, 21, 1, 2, 4, 0, time.UTC),
    56  		}},
    57  
    58  		SpawnTime: time.Date(2016, 04, 21, 1, 2, 3, 0, time.UTC),
    59  		ReadyTime: time.Date(2016, 04, 21, 1, 2, 4, 0, time.UTC),
    60  	})
    61  }
    62  
    63  func (cs *clientSuite) TestClientChangeData(c *check.C) {
    64  	cs.rsp = `{"type": "sync", "result": {
    65    "id":   "uno",
    66    "kind": "foo",
    67    "summary": "...",
    68    "status": "Do",
    69    "ready": false,
    70    "data": {"n": 42}
    71  }}`
    72  
    73  	chg, err := cs.cli.Change("uno")
    74  	c.Assert(err, check.IsNil)
    75  	var n int
    76  	err = chg.Get("n", &n)
    77  	c.Assert(err, check.IsNil)
    78  	c.Assert(n, check.Equals, 42)
    79  
    80  	err = chg.Get("missing", &n)
    81  	c.Assert(err, check.Equals, client.ErrNoData)
    82  }
    83  
    84  func (cs *clientSuite) TestClientChangeRestartingState(c *check.C) {
    85  	cs.rsp = `{"type": "sync", "result": {
    86    "id":   "uno",
    87    "kind": "foo",
    88    "summary": "...",
    89    "status": "Do",
    90    "ready": false
    91  },
    92   "maintenance": {"kind": "system-restart", "message": "system is restarting"}
    93  }`
    94  
    95  	chg, err := cs.cli.Change("uno")
    96  	c.Check(chg, check.NotNil)
    97  	c.Check(chg.ID, check.Equals, "uno")
    98  	c.Check(err, check.IsNil)
    99  	c.Check(cs.cli.Maintenance(), check.ErrorMatches, `system is restarting`)
   100  }
   101  
   102  func (cs *clientSuite) TestClientChangeError(c *check.C) {
   103  	cs.rsp = `{"type": "sync", "result": {
   104    "id":   "uno",
   105    "kind": "foo",
   106    "summary": "...",
   107    "status": "Error",
   108    "ready": true,
   109    "tasks": [{"kind": "bar", "summary": "...", "status": "Error", "progress": {"done": 1, "total": 1}, "log": ["ERROR: something broke"]}],
   110    "err": "error message"
   111  }}`
   112  
   113  	chg, err := cs.cli.Change("uno")
   114  	c.Assert(err, check.IsNil)
   115  	c.Check(chg, check.DeepEquals, &client.Change{
   116  		ID:      "uno",
   117  		Kind:    "foo",
   118  		Summary: "...",
   119  		Status:  "Error",
   120  		Tasks: []*client.Task{{
   121  			Kind:     "bar",
   122  			Summary:  "...",
   123  			Status:   "Error",
   124  			Progress: client.TaskProgress{Done: 1, Total: 1},
   125  			Log:      []string{"ERROR: something broke"},
   126  		}},
   127  		Err:   "error message",
   128  		Ready: true,
   129  	})
   130  }
   131  
   132  func (cs *clientSuite) TestClientChangesString(c *check.C) {
   133  	for k, v := range map[client.ChangeSelector]string{
   134  		client.ChangesAll:        "all",
   135  		client.ChangesReady:      "ready",
   136  		client.ChangesInProgress: "in-progress",
   137  	} {
   138  		c.Check(k.String(), check.Equals, v)
   139  	}
   140  }
   141  
   142  func (cs *clientSuite) TestClientChanges(c *check.C) {
   143  	cs.rsp = `{"type": "sync", "result": [{
   144    "id":   "uno",
   145    "kind": "foo",
   146    "summary": "...",
   147    "status": "Do",
   148    "ready": false,
   149    "tasks": [{"kind": "bar", "summary": "...", "status": "Do", "progress": {"done": 0, "total": 1}}]
   150  }]}`
   151  
   152  	for _, i := range []*client.ChangesOptions{
   153  		{Selector: client.ChangesAll},
   154  		{Selector: client.ChangesReady},
   155  		{Selector: client.ChangesInProgress},
   156  		{SnapName: "foo"},
   157  		nil,
   158  	} {
   159  		chg, err := cs.cli.Changes(i)
   160  		c.Assert(err, check.IsNil)
   161  		c.Check(chg, check.DeepEquals, []*client.Change{{
   162  			ID:      "uno",
   163  			Kind:    "foo",
   164  			Summary: "...",
   165  			Status:  "Do",
   166  			Tasks:   []*client.Task{{Kind: "bar", Summary: "...", Status: "Do", Progress: client.TaskProgress{Done: 0, Total: 1}}},
   167  		}})
   168  		if i == nil {
   169  			c.Check(cs.req.URL.RawQuery, check.Equals, "")
   170  		} else {
   171  			if i.Selector != 0 {
   172  				c.Check(cs.req.URL.RawQuery, check.Equals, "select="+i.Selector.String())
   173  			} else {
   174  				c.Check(cs.req.URL.RawQuery, check.Equals, "for="+i.SnapName)
   175  			}
   176  		}
   177  	}
   178  
   179  }
   180  
   181  func (cs *clientSuite) TestClientChangesData(c *check.C) {
   182  	cs.rsp = `{"type": "sync", "result": [{
   183    "id":   "uno",
   184    "kind": "foo",
   185    "summary": "...",
   186    "status": "Do",
   187    "ready": false,
   188    "data": {"n": 42}
   189  }]}`
   190  
   191  	chgs, err := cs.cli.Changes(&client.ChangesOptions{Selector: client.ChangesAll})
   192  	c.Assert(err, check.IsNil)
   193  
   194  	chg := chgs[0]
   195  	var n int
   196  	err = chg.Get("n", &n)
   197  	c.Assert(err, check.IsNil)
   198  	c.Assert(n, check.Equals, 42)
   199  
   200  	err = chg.Get("missing", &n)
   201  	c.Assert(err, check.Equals, client.ErrNoData)
   202  }
   203  
   204  func (cs *clientSuite) TestClientAbort(c *check.C) {
   205  	cs.rsp = `{"type": "sync", "result": {
   206    "id":   "uno",
   207    "kind": "foo",
   208    "summary": "...",
   209    "status": "Hold",
   210    "ready": true,
   211    "spawn-time": "2016-04-21T01:02:03Z",
   212    "ready-time": "2016-04-21T01:02:04Z"
   213  }}`
   214  
   215  	chg, err := cs.cli.Abort("uno")
   216  	c.Assert(err, check.IsNil)
   217  	c.Check(cs.req.Method, check.Equals, "POST")
   218  	c.Check(chg, check.DeepEquals, &client.Change{
   219  		ID:      "uno",
   220  		Kind:    "foo",
   221  		Summary: "...",
   222  		Status:  "Hold",
   223  		Ready:   true,
   224  
   225  		SpawnTime: time.Date(2016, 04, 21, 1, 2, 3, 0, time.UTC),
   226  		ReadyTime: time.Date(2016, 04, 21, 1, 2, 4, 0, time.UTC),
   227  	})
   228  
   229  	body, err := ioutil.ReadAll(cs.req.Body)
   230  	c.Assert(err, check.IsNil)
   231  
   232  	c.Assert(string(body), check.Equals, "{\"action\":\"abort\"}\n")
   233  }