github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/hookstate/ctlcmd/refresh_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 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 ctlcmd_test
    21  
    22  import (
    23  	"time"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/dirs"
    28  	"github.com/snapcore/snapd/overlord/hookstate"
    29  	"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
    30  	"github.com/snapcore/snapd/overlord/hookstate/hooktest"
    31  	"github.com/snapcore/snapd/overlord/snapstate"
    32  	"github.com/snapcore/snapd/overlord/state"
    33  	"github.com/snapcore/snapd/snap"
    34  	"github.com/snapcore/snapd/testutil"
    35  )
    36  
    37  type refreshSuite struct {
    38  	testutil.BaseTest
    39  	st          *state.State
    40  	mockHandler *hooktest.MockHandler
    41  }
    42  
    43  var _ = Suite(&refreshSuite{})
    44  
    45  func mockRefreshCandidate(snapName, instanceKey, channel, version string, revision snap.Revision) interface{} {
    46  	sup := &snapstate.SnapSetup{
    47  		Channel:     channel,
    48  		InstanceKey: instanceKey,
    49  		SideInfo: &snap.SideInfo{
    50  			Revision: revision,
    51  			RealName: snapName,
    52  		},
    53  	}
    54  	return snapstate.MockRefreshCandidate(sup, version)
    55  }
    56  
    57  func (s *refreshSuite) SetUpTest(c *C) {
    58  	s.BaseTest.SetUpTest(c)
    59  	dirs.SetRootDir(c.MkDir())
    60  	s.AddCleanup(func() { dirs.SetRootDir("/") })
    61  	s.st = state.New(nil)
    62  	s.mockHandler = hooktest.NewMockHandler()
    63  }
    64  
    65  var refreshFromHookTests = []struct {
    66  	args                []string
    67  	base, restart       bool
    68  	inhibited           bool
    69  	refreshCandidates   map[string]interface{}
    70  	stdout, stderr, err string
    71  	exitCode            int
    72  }{{
    73  	args: []string{"refresh", "--proceed", "--hold"},
    74  	err:  "cannot use --proceed and --hold together",
    75  }, {
    76  	args: []string{"refresh", "--proceed"},
    77  	err:  "not implemented yet",
    78  }, {
    79  	args: []string{"refresh", "--hold"},
    80  	err:  "not implemented yet",
    81  }, {
    82  	args:              []string{"refresh", "--pending"},
    83  	refreshCandidates: map[string]interface{}{"snap1": mockRefreshCandidate("snap1", "", "edge", "v1", snap.Revision{N: 3})},
    84  	stdout:            "pending: ready\nchannel: edge\nversion: v1\nrevision: 3\nbase: false\nrestart: false\n",
    85  }, {
    86  	args:   []string{"refresh", "--pending"},
    87  	stdout: "pending: none\nchannel: stable\nbase: false\nrestart: false\n",
    88  }, {
    89  	args:    []string{"refresh", "--pending"},
    90  	base:    true,
    91  	restart: true,
    92  	stdout:  "pending: none\nchannel: stable\nbase: true\nrestart: true\n",
    93  }, {
    94  	args:      []string{"refresh", "--pending"},
    95  	inhibited: true,
    96  	stdout:    "pending: inhibited\nchannel: stable\nbase: false\nrestart: false\n",
    97  }}
    98  
    99  func (s *refreshSuite) TestRefreshFromHook(c *C) {
   100  	s.st.Lock()
   101  	task := s.st.NewTask("test-task", "my test task")
   102  	setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1), Hook: "gate-auto-refresh"}
   103  	mockContext, err := hookstate.NewContext(task, s.st, setup, s.mockHandler, "")
   104  	c.Check(err, IsNil)
   105  	s.st.Unlock()
   106  
   107  	for _, test := range refreshFromHookTests {
   108  		mockContext.Lock()
   109  		mockContext.Set("base", test.base)
   110  		mockContext.Set("restart", test.restart)
   111  		s.st.Set("refresh-candidates", test.refreshCandidates)
   112  		snapst := &snapstate.SnapState{
   113  			Active:          true,
   114  			Sequence:        []*snap.SideInfo{{RealName: "snap1", Revision: snap.R(1)}},
   115  			Current:         snap.R(2),
   116  			TrackingChannel: "stable",
   117  		}
   118  		if test.inhibited {
   119  			snapst.RefreshInhibitedTime = &time.Time{}
   120  		}
   121  		snapstate.Set(s.st, "snap1", snapst)
   122  		mockContext.Unlock()
   123  
   124  		stdout, stderr, err := ctlcmd.Run(mockContext, test.args, 0)
   125  		comment := Commentf("%s", test.args)
   126  		if test.exitCode > 0 {
   127  			c.Check(err, DeepEquals, &ctlcmd.UnsuccessfulError{ExitCode: test.exitCode}, comment)
   128  		} else {
   129  			if test.err == "" {
   130  				c.Check(err, IsNil, comment)
   131  			} else {
   132  				c.Check(err, ErrorMatches, test.err, comment)
   133  			}
   134  		}
   135  
   136  		c.Check(string(stdout), Equals, test.stdout, comment)
   137  		c.Check(string(stderr), Equals, "", comment)
   138  	}
   139  }
   140  
   141  func (s *refreshSuite) TestRefreshFromUnsupportedHook(c *C) {
   142  	s.st.Lock()
   143  
   144  	task := s.st.NewTask("test-task", "my test task")
   145  	setup := &hookstate.HookSetup{Snap: "snap", Revision: snap.R(1), Hook: "install"}
   146  	mockContext, err := hookstate.NewContext(task, s.st, setup, s.mockHandler, "")
   147  	c.Check(err, IsNil)
   148  	s.st.Unlock()
   149  
   150  	_, _, err = ctlcmd.Run(mockContext, []string{"refresh"}, 0)
   151  	c.Check(err, ErrorMatches, `can only be used from gate-auto-refresh hook`)
   152  }
   153  
   154  // TODO: support this case
   155  func (s *refreshSuite) TestRefreshFromApp(c *C) {
   156  	s.st.Lock()
   157  
   158  	setup := &hookstate.HookSetup{Snap: "snap", Revision: snap.R(1)}
   159  	mockContext, err := hookstate.NewContext(nil, s.st, setup, s.mockHandler, "")
   160  	c.Check(err, IsNil)
   161  	s.st.Unlock()
   162  
   163  	_, _, err = ctlcmd.Run(mockContext, []string{"refresh"}, 0)
   164  	c.Check(err, ErrorMatches, `cannot run outside of gate-auto-refresh hook`)
   165  }
   166  
   167  func (s *refreshSuite) TestRefreshRegularUserForbidden(c *C) {
   168  	s.st.Lock()
   169  	setup := &hookstate.HookSetup{Snap: "snap", Revision: snap.R(1)}
   170  	s.st.Unlock()
   171  
   172  	mockContext, err := hookstate.NewContext(nil, s.st, setup, s.mockHandler, "")
   173  	c.Assert(err, IsNil)
   174  	_, _, err = ctlcmd.Run(mockContext, []string{"refresh"}, 1000)
   175  	c.Assert(err, ErrorMatches, `cannot use "refresh" with uid 1000, try with sudo`)
   176  	forbidden, _ := err.(*ctlcmd.ForbiddenCommandError)
   177  	c.Assert(forbidden, NotNil)
   178  }