github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/snapstate/snapstate_config_defaults_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2020 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  	"github.com/snapcore/snapd/release"
    29  	"github.com/snapcore/snapd/snap"
    30  	"github.com/snapcore/snapd/snap/snaptest"
    31  )
    32  
    33  func (s *snapmgrTestSuite) TestConfigDefaults(c *C) {
    34  	r := release.MockOnClassic(false)
    35  	defer r()
    36  
    37  	// using MockSnap, we want to read the bits on disk
    38  	snapstate.MockSnapReadInfo(snap.ReadInfo)
    39  
    40  	s.state.Lock()
    41  	defer s.state.Unlock()
    42  
    43  	s.prepareGadget(c)
    44  
    45  	deviceCtx := deviceWithGadgetContext("the-gadget")
    46  
    47  	snapstate.Set(s.state, "some-snap", &snapstate.SnapState{
    48  		Active: true,
    49  		Sequence: []*snap.SideInfo{
    50  			{RealName: "some-snap", Revision: snap.R(11), SnapID: "somesnapidididididididididididid"},
    51  		},
    52  		Current:  snap.R(11),
    53  		SnapType: "app",
    54  	})
    55  	makeInstalledMockCoreSnap(c)
    56  
    57  	defls, err := snapstate.ConfigDefaults(s.state, deviceCtx, "some-snap")
    58  	c.Assert(err, IsNil)
    59  	c.Assert(defls, DeepEquals, map[string]interface{}{"key": "value"})
    60  
    61  	snapstate.Set(s.state, "local-snap", &snapstate.SnapState{
    62  		Active: true,
    63  		Sequence: []*snap.SideInfo{
    64  			{RealName: "local-snap", Revision: snap.R(5)},
    65  		},
    66  		Current:  snap.R(5),
    67  		SnapType: "app",
    68  	})
    69  	_, err = snapstate.ConfigDefaults(s.state, deviceCtx, "local-snap")
    70  	c.Assert(err, Equals, state.ErrNoState)
    71  }
    72  
    73  func (s *snapmgrTestSuite) TestConfigDefaultsSmokeUC20(c *C) {
    74  	r := release.MockOnClassic(false)
    75  	defer r()
    76  
    77  	// using MockSnap, we want to read the bits on disk
    78  	snapstate.MockSnapReadInfo(snap.ReadInfo)
    79  
    80  	s.state.Lock()
    81  	defer s.state.Unlock()
    82  
    83  	// provide a uc20 gadget structure
    84  	s.prepareGadget(c, `
    85          bootloader: grub
    86          structure:
    87          - name: ubuntu-seed
    88            role: system-seed
    89            filesystem: vfat
    90            type: EF,C12A7328-F81F-11D2-BA4B-00A0C93EC93B
    91            size: 1200M
    92          - name: ubuntu-boot
    93            role: system-boot
    94            filesystem: ext4
    95            type: 83,0FC63DAF-8483-4772-8E79-3D69D8477DE4
    96            # whats the appropriate size?
    97            size: 750M
    98          - name: ubuntu-data
    99            role: system-data
   100            filesystem: ext4
   101            type: 83,0FC63DAF-8483-4772-8E79-3D69D8477DE4
   102            size: 1G
   103  `)
   104  	// use a UC20 model context
   105  	deviceCtx := deviceWithGadgetContext20("the-gadget")
   106  
   107  	snapstate.Set(s.state, "some-snap", &snapstate.SnapState{
   108  		Active: true,
   109  		Sequence: []*snap.SideInfo{
   110  			{RealName: "some-snap", Revision: snap.R(11), SnapID: "somesnapidididididididididididid"},
   111  		},
   112  		Current:  snap.R(11),
   113  		SnapType: "app",
   114  	})
   115  	makeInstalledMockCoreSnap(c)
   116  
   117  	defls, err := snapstate.ConfigDefaults(s.state, deviceCtx, "some-snap")
   118  	c.Assert(err, IsNil)
   119  	c.Assert(defls, DeepEquals, map[string]interface{}{"key": "value"})
   120  }
   121  
   122  func (s *snapmgrTestSuite) TestConfigDefaultsNoGadget(c *C) {
   123  	r := release.MockOnClassic(false)
   124  	defer r()
   125  
   126  	// using MockSnap, we want to read the bits on disk
   127  	snapstate.MockSnapReadInfo(snap.ReadInfo)
   128  
   129  	s.state.Lock()
   130  	defer s.state.Unlock()
   131  
   132  	deviceCtxNoGadget := deviceWithoutGadgetContext()
   133  
   134  	snapstate.Set(s.state, "some-snap", &snapstate.SnapState{
   135  		Active: true,
   136  		Sequence: []*snap.SideInfo{
   137  			{RealName: "some-snap", Revision: snap.R(11), SnapID: "somesnapidididididididididididid"},
   138  		},
   139  		Current:  snap.R(11),
   140  		SnapType: "app",
   141  	})
   142  	makeInstalledMockCoreSnap(c)
   143  
   144  	_, err := snapstate.ConfigDefaults(s.state, deviceCtxNoGadget, "some-snap")
   145  	c.Assert(err, Equals, state.ErrNoState)
   146  }
   147  
   148  func (s *snapmgrTestSuite) TestConfigDefaultsSystemWithCore(c *C) {
   149  	r := release.MockOnClassic(false)
   150  	defer r()
   151  
   152  	// using MockSnapReadInfo, we want to read the bits on disk
   153  	snapstate.MockSnapReadInfo(snap.ReadInfo)
   154  
   155  	s.state.Lock()
   156  	defer s.state.Unlock()
   157  
   158  	s.prepareGadget(c, `
   159  defaults:
   160      system:
   161          foo: bar
   162  `)
   163  
   164  	deviceCtx := deviceWithGadgetContext("the-gadget")
   165  
   166  	snapstate.Set(s.state, "core", &snapstate.SnapState{
   167  		Active: true,
   168  		Sequence: []*snap.SideInfo{
   169  			{RealName: "some-snap", Revision: snap.R(11), SnapID: "the-core-ididididididididididid"},
   170  		},
   171  		Current:  snap.R(11),
   172  		SnapType: "os",
   173  	})
   174  
   175  	makeInstalledMockCoreSnap(c)
   176  
   177  	defls, err := snapstate.ConfigDefaults(s.state, deviceCtx, "core")
   178  	c.Assert(err, IsNil)
   179  	c.Assert(defls, DeepEquals, map[string]interface{}{"foo": "bar"})
   180  }
   181  
   182  var snapdSnapYaml = `name: snapd
   183  version: 1.0
   184  type: snapd
   185  `
   186  
   187  func (s *snapmgrTestSuite) TestConfigDefaultsSystemWithSnapdNoCore(c *C) {
   188  	r := release.MockOnClassic(false)
   189  	defer r()
   190  
   191  	// using MockSnapReadInfo, we want to read the bits on disk
   192  	snapstate.MockSnapReadInfo(snap.ReadInfo)
   193  
   194  	s.state.Lock()
   195  	defer s.state.Unlock()
   196  
   197  	s.prepareGadget(c, `
   198  defaults:
   199      system:
   200          foo: bar
   201  `)
   202  
   203  	deviceCtx := &snapstatetest.TrivialDeviceContext{
   204  		DeviceModel: MakeModel(map[string]interface{}{
   205  			"gadget": "the-gadget",
   206  			"base":   "the-base",
   207  		}),
   208  	}
   209  
   210  	snapstate.Set(s.state, "core", nil)
   211  	snapstate.Set(s.state, "snapd", &snapstate.SnapState{
   212  		Active: true,
   213  		Sequence: []*snap.SideInfo{
   214  			{RealName: "snapd", SnapID: "the-snapd-snapidididididididididi", Revision: snap.R(1)},
   215  		},
   216  		Current:  snap.R(1),
   217  		SnapType: "snapd",
   218  	})
   219  
   220  	snaptest.MockSnap(c, snapdSnapYaml, &snap.SideInfo{
   221  		RealName: "snapd",
   222  		Revision: snap.R(1),
   223  	})
   224  
   225  	defls, err := snapstate.ConfigDefaults(s.state, deviceCtx, "core")
   226  	c.Assert(err, IsNil)
   227  	c.Assert(defls, DeepEquals, map[string]interface{}{"foo": "bar"})
   228  }
   229  
   230  func (s *snapmgrTestSuite) TestConfigDefaultsSystemConflictsCoreSnapId(c *C) {
   231  	r := release.MockOnClassic(false)
   232  	defer r()
   233  
   234  	// using MockSnapReadInfo, we want to read the bits on disk
   235  	snapstate.MockSnapReadInfo(snap.ReadInfo)
   236  
   237  	s.state.Lock()
   238  	defer s.state.Unlock()
   239  
   240  	s.prepareGadget(c, `
   241  defaults:
   242      system:
   243          foo: bar
   244      thecoresnapididididididididididi:
   245          foo: other-bar
   246          other-key: other-key-default
   247  `)
   248  
   249  	deviceCtx := deviceWithGadgetContext("the-gadget")
   250  
   251  	snapstate.Set(s.state, "core", &snapstate.SnapState{
   252  		Active: true,
   253  		Sequence: []*snap.SideInfo{
   254  			{RealName: "core", SnapID: "thecoresnapididididididididididi", Revision: snap.R(1)},
   255  		},
   256  		Current:  snap.R(1),
   257  		SnapType: "os",
   258  	})
   259  
   260  	makeInstalledMockCoreSnap(c)
   261  
   262  	// 'system' key defaults take precedence over snap-id ones
   263  	defls, err := snapstate.ConfigDefaults(s.state, deviceCtx, "core")
   264  	c.Assert(err, IsNil)
   265  	c.Assert(defls, DeepEquals, map[string]interface{}{"foo": "bar"})
   266  }
   267  
   268  func (s *snapmgrTestSuite) TestTransitionCoreTasksNoUbuntuCore(c *C) {
   269  	s.state.Lock()
   270  	defer s.state.Unlock()
   271  
   272  	snapstate.Set(s.state, "core", &snapstate.SnapState{
   273  		Active:   true,
   274  		Sequence: []*snap.SideInfo{{RealName: "corecore", SnapID: "core-snap-id", Revision: snap.R(1)}},
   275  		Current:  snap.R(1),
   276  		SnapType: "os",
   277  	})
   278  
   279  	_, err := snapstate.TransitionCore(s.state, "ubuntu-core", "core")
   280  	c.Assert(err, ErrorMatches, `cannot transition snap "ubuntu-core": not installed`)
   281  }