github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/snapstate/handlers_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 snapstate_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/overlord/snapstate"
    26  	"github.com/snapcore/snapd/overlord/snapstate/snapstatetest"
    27  	"github.com/snapcore/snapd/snap"
    28  )
    29  
    30  type handlersSuite struct {
    31  	baseHandlerSuite
    32  
    33  	stateBackend *witnessRestartReqStateBackend
    34  }
    35  
    36  var _ = Suite(&handlersSuite{})
    37  
    38  func (s *handlersSuite) SetUpTest(c *C) {
    39  	s.setup(c, s.stateBackend)
    40  
    41  	s.AddCleanup(snapstatetest.MockDeviceModel(DefaultModel()))
    42  }
    43  
    44  func (s *handlersSuite) TestSetTaskSnapSetupFirstTask(c *C) {
    45  	s.state.Lock()
    46  	defer s.state.Unlock()
    47  
    48  	// make a new task which will be the snap-setup-task for other tasks and
    49  	// write a SnapSetup to it
    50  	t := s.state.NewTask("link-snap", "test")
    51  	snapsup := &snapstate.SnapSetup{
    52  		SideInfo: &snap.SideInfo{
    53  			RealName: "foo",
    54  			Revision: snap.R(33),
    55  			SnapID:   "foo-id",
    56  		},
    57  		Channel: "beta",
    58  		UserID:  2,
    59  	}
    60  	t.Set("snap-setup", snapsup)
    61  	s.state.NewChange("dummy", "...").AddTask(t)
    62  
    63  	// mutate it and rewrite it with the helper
    64  	snapsup.Channel = "edge"
    65  	err := snapstate.SetTaskSnapSetup(t, snapsup)
    66  	c.Assert(err, IsNil)
    67  
    68  	// get a fresh version of this task from state to check that the task's
    69  	/// snap-setup has the changes in it now
    70  	newT := s.state.Task(t.ID())
    71  	c.Assert(newT, Not(IsNil))
    72  	var newsnapsup snapstate.SnapSetup
    73  	err = newT.Get("snap-setup", &newsnapsup)
    74  	c.Assert(err, IsNil)
    75  	c.Assert(newsnapsup.Channel, Equals, snapsup.Channel)
    76  }
    77  
    78  func (s *handlersSuite) TestSetTaskSnapSetupLaterTask(c *C) {
    79  	s.state.Lock()
    80  	defer s.state.Unlock()
    81  	t := s.state.NewTask("link-snap", "test")
    82  
    83  	snapsup := &snapstate.SnapSetup{
    84  		SideInfo: &snap.SideInfo{
    85  			RealName: "foo",
    86  			Revision: snap.R(33),
    87  			SnapID:   "foo-id",
    88  		},
    89  		Channel: "beta",
    90  		UserID:  2,
    91  	}
    92  	// setup snap-setup for the first task
    93  	t.Set("snap-setup", snapsup)
    94  
    95  	// make a new task and reference the first one in snap-setup-task
    96  	t2 := s.state.NewTask("next-task-snap", "test2")
    97  	t2.Set("snap-setup-task", t.ID())
    98  
    99  	chg := s.state.NewChange("dummy", "...")
   100  	chg.AddTask(t)
   101  	chg.AddTask(t2)
   102  
   103  	// mutate it and rewrite it with the helper
   104  	snapsup.Channel = "edge"
   105  	err := snapstate.SetTaskSnapSetup(t2, snapsup)
   106  	c.Assert(err, IsNil)
   107  
   108  	// check that the original task's snap-setup is different now
   109  	newT := s.state.Task(t.ID())
   110  	c.Assert(newT, Not(IsNil))
   111  	var newsnapsup snapstate.SnapSetup
   112  	err = newT.Get("snap-setup", &newsnapsup)
   113  	c.Assert(err, IsNil)
   114  	c.Assert(newsnapsup.Channel, Equals, snapsup.Channel)
   115  }
   116  
   117  func (s *handlersSuite) TestComputeMissingDisabledServices(c *C) {
   118  	for _, tt := range []struct {
   119  		// inputs
   120  		stDisabledSvcsList []string
   121  		apps               map[string]*snap.AppInfo
   122  		// outputs
   123  		missing []string
   124  		found   []string
   125  		err     error
   126  		comment string
   127  	}{
   128  		// no apps
   129  		{
   130  			[]string{},
   131  			nil,
   132  			[]string{},
   133  			[]string{},
   134  			nil,
   135  			"no apps",
   136  		},
   137  		// only apps, no services
   138  		{
   139  			[]string{},
   140  			map[string]*snap.AppInfo{
   141  				"app": {
   142  					Daemon: "",
   143  				},
   144  			},
   145  			[]string{},
   146  			[]string{},
   147  			nil,
   148  			"no services",
   149  		},
   150  		// services in snap, but not disabled
   151  		{
   152  			[]string{},
   153  			map[string]*snap.AppInfo{
   154  				"svc1": {
   155  					Daemon: "simple",
   156  				},
   157  			},
   158  			[]string{},
   159  			[]string{},
   160  			nil,
   161  			"no disabled services",
   162  		},
   163  		// all disabled services, but not present in snap
   164  		{
   165  			[]string{"svc1"},
   166  			nil,
   167  			[]string{"svc1"},
   168  			[]string{},
   169  			nil,
   170  			"all missing disabled services",
   171  		},
   172  		// all disabled services, and found in snap
   173  		{
   174  			[]string{"svc1"},
   175  			map[string]*snap.AppInfo{
   176  				"svc1": {
   177  					Daemon: "simple",
   178  				},
   179  			},
   180  			[]string{},
   181  			[]string{"svc1"},
   182  			nil,
   183  			"all found disabled services",
   184  		},
   185  		// some disabled services, some missing, some present in snap
   186  		{
   187  			[]string{"svc1", "svc2"},
   188  			map[string]*snap.AppInfo{
   189  				"svc1": {
   190  					Daemon: "simple",
   191  				},
   192  			},
   193  			[]string{"svc2"},
   194  			[]string{"svc1"},
   195  			nil,
   196  			"some missing, some found disabled services",
   197  		},
   198  		// some disabled services, but app is not service
   199  		{
   200  			[]string{"svc1"},
   201  			map[string]*snap.AppInfo{
   202  				"svc1": {
   203  					Daemon: "",
   204  				},
   205  			},
   206  			[]string{"svc1"},
   207  			[]string{},
   208  			nil,
   209  			"some disabled services that are now apps",
   210  		},
   211  	} {
   212  		info := &snap.Info{Apps: tt.apps}
   213  
   214  		missing, found, err := snapstate.MissingDisabledServices(tt.stDisabledSvcsList, info)
   215  		c.Assert(missing, DeepEquals, tt.missing, Commentf(tt.comment))
   216  		c.Assert(found, DeepEquals, tt.found, Commentf(tt.comment))
   217  		c.Assert(err, Equals, tt.err, Commentf(tt.comment))
   218  	}
   219  }