gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/snapstate/handlers_prepare_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 snapstate_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"
    29  	"github.com/snapcore/snapd/overlord/snapstate"
    30  	"github.com/snapcore/snapd/overlord/snapstate/snapstatetest"
    31  	"github.com/snapcore/snapd/overlord/state"
    32  	"github.com/snapcore/snapd/snap"
    33  	"github.com/snapcore/snapd/testutil"
    34  )
    35  
    36  type baseHandlerSuite struct {
    37  	testutil.BaseTest
    38  
    39  	state   *state.State
    40  	runner  *state.TaskRunner
    41  	se      *overlord.StateEngine
    42  	snapmgr *snapstate.SnapManager
    43  
    44  	fakeBackend *fakeSnappyBackend
    45  }
    46  
    47  func (s *baseHandlerSuite) setup(c *C, b state.Backend) {
    48  	s.BaseTest.SetUpTest(c)
    49  
    50  	dirs.SetRootDir(c.MkDir())
    51  
    52  	s.fakeBackend = &fakeSnappyBackend{}
    53  	s.state = state.New(b)
    54  	s.runner = state.NewTaskRunner(s.state)
    55  
    56  	var err error
    57  	s.snapmgr, err = snapstate.Manager(s.state, s.runner)
    58  	c.Assert(err, IsNil)
    59  
    60  	s.se = overlord.NewStateEngine(s.state)
    61  	s.se.AddManager(s.snapmgr)
    62  	s.se.AddManager(s.runner)
    63  	c.Assert(s.se.StartUp(), IsNil)
    64  
    65  	AddForeignTaskHandlers(s.runner, s.fakeBackend)
    66  
    67  	snapstate.SetSnapManagerBackend(s.snapmgr, s.fakeBackend)
    68  
    69  	reset1 := snapstate.MockSnapReadInfo(s.fakeBackend.ReadInfo)
    70  	reset2 := snapstate.MockReRefreshRetryTimeout(time.Second / 200)
    71  
    72  	s.AddCleanup(func() { dirs.SetRootDir("/") })
    73  	s.AddCleanup(reset1)
    74  	s.AddCleanup(reset2)
    75  	s.AddCleanup(snapstatetest.MockDeviceModel(nil))
    76  
    77  	restoreCheckFreeSpace := snapstate.MockOsutilCheckFreeSpace(func(string, uint64) error { return nil })
    78  	s.AddCleanup(restoreCheckFreeSpace)
    79  
    80  	restoreSecurityProfilesDiscardLate := snapstate.MockSecurityProfilesDiscardLate(func(snapName string, rev snap.Revision, typ snap.Type) error {
    81  		return nil
    82  	})
    83  	s.AddCleanup(restoreSecurityProfilesDiscardLate)
    84  }
    85  
    86  func (s *baseHandlerSuite) SetUpTest(c *C) {
    87  	s.setup(c, nil)
    88  }
    89  
    90  type prepareSnapSuite struct {
    91  	baseHandlerSuite
    92  }
    93  
    94  var _ = Suite(&prepareSnapSuite{})
    95  
    96  func (s *prepareSnapSuite) TestDoPrepareSnapSimple(c *C) {
    97  	s.state.Lock()
    98  	t := s.state.NewTask("prepare-snap", "test")
    99  	t.Set("snap-setup", &snapstate.SnapSetup{
   100  		SideInfo: &snap.SideInfo{
   101  			RealName: "foo",
   102  		},
   103  	})
   104  	s.state.NewChange("dummy", "...").AddTask(t)
   105  
   106  	s.state.Unlock()
   107  
   108  	s.se.Ensure()
   109  	s.se.Wait()
   110  
   111  	s.state.Lock()
   112  	defer s.state.Unlock()
   113  
   114  	var snapsup snapstate.SnapSetup
   115  	t.Get("snap-setup", &snapsup)
   116  	c.Check(snapsup.SideInfo, DeepEquals, &snap.SideInfo{
   117  		RealName: "foo",
   118  		Revision: snap.R(-1),
   119  	})
   120  	c.Check(t.Status(), Equals, state.DoneStatus)
   121  }