gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/snapstate/devicectx_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018-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/overlord/state"
    28  )
    29  
    30  type deviceCtxSuite struct {
    31  	st *state.State
    32  }
    33  
    34  var _ = Suite(&deviceCtxSuite{})
    35  
    36  func (s *deviceCtxSuite) SetUpTest(c *C) {
    37  	s.st = state.New(nil)
    38  }
    39  
    40  func (s *deviceCtxSuite) TestDevicePastSeedingTooEarly(c *C) {
    41  	s.st.Lock()
    42  	defer s.st.Unlock()
    43  
    44  	r := snapstatetest.MockDeviceModel(nil)
    45  	defer r()
    46  
    47  	expectedErr := &snapstate.ChangeConflictError{
    48  		Message: "too early for operation, device not yet seeded or" +
    49  			" device model not acknowledged",
    50  		ChangeKind: "seed",
    51  	}
    52  
    53  	// not seeded, no model assertion
    54  	_, err := snapstate.DevicePastSeeding(s.st, nil)
    55  	c.Assert(err, DeepEquals, expectedErr)
    56  
    57  	// seeded, no model assertion
    58  	s.st.Set("seeded", true)
    59  	_, err = snapstate.DevicePastSeeding(s.st, nil)
    60  	c.Assert(err, DeepEquals, expectedErr)
    61  }
    62  
    63  func (s *deviceCtxSuite) TestDevicePastSeedingProvided(c *C) {
    64  	s.st.Lock()
    65  	defer s.st.Unlock()
    66  
    67  	r := snapstatetest.MockDeviceContext(nil)
    68  	defer r()
    69  
    70  	expectedErr := &snapstate.ChangeConflictError{
    71  		Message: "too early for operation, device not yet seeded or" +
    72  			" device model not acknowledged",
    73  		ChangeKind: "seed",
    74  	}
    75  
    76  	deviceCtx1 := &snapstatetest.TrivialDeviceContext{DeviceModel: MakeModel(nil)}
    77  
    78  	// not seeded
    79  	_, err := snapstate.DevicePastSeeding(s.st, deviceCtx1)
    80  	c.Assert(err, DeepEquals, expectedErr)
    81  
    82  	// seeded
    83  	s.st.Set("seeded", true)
    84  	deviceCtx, err := snapstate.DevicePastSeeding(s.st, deviceCtx1)
    85  	c.Assert(err, IsNil)
    86  	c.Assert(deviceCtx, Equals, deviceCtx1)
    87  
    88  	// remodeling is also ok
    89  	deviceCtx2 := &snapstatetest.TrivialDeviceContext{DeviceModel: MakeModel(nil), Remodeling: true}
    90  	defer snapstatetest.ReplaceRemodelingHook(func(*state.State) bool {
    91  		return true
    92  	})()
    93  	deviceCtx, err = snapstate.DevicePastSeeding(s.st, deviceCtx2)
    94  	c.Assert(err, IsNil)
    95  	c.Assert(deviceCtx, Equals, deviceCtx2)
    96  
    97  	expectedErr = &snapstate.ChangeConflictError{
    98  		Message: "remodeling in progress, no other " +
    99  			"changes allowed until this is done",
   100  		ChangeKind: "remodel",
   101  	}
   102  
   103  	// should not happen in practice but correct
   104  	deviceCtx, err = snapstate.DevicePastSeeding(s.st, deviceCtx1)
   105  	c.Assert(err, DeepEquals, expectedErr)
   106  	c.Check(deviceCtx, IsNil)
   107  }
   108  
   109  func (s *deviceCtxSuite) TestDevicePastSeedingReady(c *C) {
   110  	s.st.Lock()
   111  	defer s.st.Unlock()
   112  
   113  	// seeded and model assertion
   114  	s.st.Set("seeded", true)
   115  
   116  	r := snapstatetest.MockDeviceModel(DefaultModel())
   117  	defer r()
   118  
   119  	deviceCtx, err := snapstate.DevicePastSeeding(s.st, nil)
   120  	c.Assert(err, IsNil)
   121  	c.Check(deviceCtx.Model().Model(), Equals, "baz-3000")
   122  	c.Check(deviceCtx.SystemMode(), Equals, "run")
   123  }
   124  
   125  func (s *deviceCtxSuite) TestDevicePastSeedingReadyInstallMode(c *C) {
   126  	s.st.Lock()
   127  	defer s.st.Unlock()
   128  
   129  	// seeded and model assertion
   130  	s.st.Set("seeded", true)
   131  
   132  	r := snapstatetest.MockDeviceModelAndMode(DefaultModel(), "install")
   133  	defer r()
   134  
   135  	deviceCtx, err := snapstate.DevicePastSeeding(s.st, nil)
   136  	c.Assert(err, IsNil)
   137  	c.Check(deviceCtx.Model().Model(), Equals, "baz-3000")
   138  	c.Check(deviceCtx.SystemMode(), Equals, "install")
   139  }
   140  
   141  func (s *deviceCtxSuite) TestDevicePastSeedingButRemodeling(c *C) {
   142  	s.st.Lock()
   143  	defer s.st.Unlock()
   144  
   145  	// seeded and model assertion
   146  	s.st.Set("seeded", true)
   147  
   148  	r := snapstatetest.MockDeviceModel(DefaultModel())
   149  	defer r()
   150  	defer snapstatetest.ReplaceRemodelingHook(func(*state.State) bool {
   151  		return true
   152  	})()
   153  
   154  	expectedErr := &snapstate.ChangeConflictError{
   155  		Message: "remodeling in progress, no other " +
   156  			"changes allowed until this is done",
   157  		ChangeKind: "remodel",
   158  	}
   159  
   160  	_, err := snapstate.DevicePastSeeding(s.st, nil)
   161  	c.Assert(err, DeepEquals, expectedErr)
   162  }
   163  
   164  func (s *deviceCtxSuite) TestDeviceCtxFromStateReady(c *C) {
   165  	s.st.Lock()
   166  	defer s.st.Unlock()
   167  
   168  	// model assertion but not seeded yet
   169  	r := snapstatetest.MockDeviceModel(DefaultModel())
   170  	defer r()
   171  
   172  	deviceCtx, err := snapstate.DeviceCtxFromState(s.st, nil)
   173  	c.Assert(err, IsNil)
   174  	c.Check(deviceCtx.Model().Model(), Equals, "baz-3000")
   175  }
   176  
   177  func (s *deviceCtxSuite) TestDeviceCtxFromStateProvided(c *C) {
   178  	s.st.Lock()
   179  	defer s.st.Unlock()
   180  
   181  	r := snapstatetest.MockDeviceContext(nil)
   182  	defer r()
   183  
   184  	deviceCtx1 := &snapstatetest.TrivialDeviceContext{DeviceModel: MakeModel(nil)}
   185  
   186  	// not seeded
   187  	deviceCtx, err := snapstate.DeviceCtxFromState(s.st, deviceCtx1)
   188  	c.Assert(err, IsNil)
   189  	c.Assert(deviceCtx, Equals, deviceCtx1)
   190  
   191  	// seeded
   192  	s.st.Set("seeded", true)
   193  	deviceCtx, err = snapstate.DeviceCtxFromState(s.st, deviceCtx1)
   194  	c.Assert(err, IsNil)
   195  	c.Assert(deviceCtx, Equals, deviceCtx1)
   196  }
   197  
   198  func (s *deviceCtxSuite) TestDeviceCtxFromStateTooEarly(c *C) {
   199  	s.st.Lock()
   200  	defer s.st.Unlock()
   201  
   202  	r := snapstatetest.MockDeviceModel(nil)
   203  	defer r()
   204  
   205  	expectedErr := &snapstate.ChangeConflictError{
   206  		Message: "too early for operation, device model " +
   207  			"not yet acknowledged",
   208  		ChangeKind: "seed",
   209  	}
   210  
   211  	// not seeded, no model assertion
   212  	_, err := snapstate.DeviceCtxFromState(s.st, nil)
   213  	c.Assert(err, DeepEquals, expectedErr)
   214  
   215  	// seeded, no model assertion
   216  	s.st.Set("seeded", true)
   217  	_, err = snapstate.DeviceCtxFromState(s.st, nil)
   218  	c.Assert(err, DeepEquals, expectedErr)
   219  }