File: devicectx_test.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (120 lines) | stat: -rw-r--r-- 4,337 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2022 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package devicestate_test

import (
	"github.com/snapcore/snapd/asserts"
	"github.com/snapcore/snapd/asserts/assertstest"
	"github.com/snapcore/snapd/boot/boottest"
	"github.com/snapcore/snapd/overlord/devicestate"
	"github.com/snapcore/snapd/overlord/snapstate"
	"github.com/snapcore/snapd/testutil"
	. "gopkg.in/check.v1"
)

type deviceCtxSuite struct {
	testutil.BaseTest
}

var _ = Suite(&deviceCtxSuite{})

func (s *deviceCtxSuite) SetUpTest(c *C) {
}

func testGroundDeviceContextCommonChecks(c *C, devCtx snapstate.DeviceContext, mode string) {
	c.Check(devCtx.RunMode(), Equals, mode == "run", Commentf("mode is %q", mode))
	c.Check(devCtx.GroundContext(), Equals, devCtx)
	c.Check(func() { devCtx.Store() }, PanicMatches,
		"retrieved ground context is not intended to drive store operations")
	c.Check(devCtx.ForRemodeling(), Equals, false)
	c.Check(devCtx.SystemMode(), Equals, mode)
}

func (s *deviceCtxSuite) testGroundDeviceContext(c *C, mode string) {
	var devCtx snapstate.DeviceContext

	// Classic with classic initramfs
	classicModel := assertstest.FakeAssertion(map[string]any{
		"type":         "model",
		"classic":      "true",
		"authority-id": "my-brand",
		"series":       "16",
		"brand-id":     "my-brand",
		"model":        "my-model",
		"display-name": "My Model",
		"architecture": "amd64",
		"timestamp":    "2018-01-01T08:00:00+00:00",
	}).(*asserts.Model)
	devCtx = devicestate.BuildGroundDeviceContext(classicModel, mode)
	c.Check(devCtx.Classic(), Equals, true)
	c.Check(devCtx.Kernel(), Equals, "")
	c.Check(devCtx.Base(), Equals, "")
	c.Check(devCtx.Gadget(), Equals, "")
	c.Check(devCtx.HasModeenv(), Equals, false)
	c.Check(devCtx.IsCoreBoot(), Equals, false)
	c.Check(devCtx.IsClassicBoot(), Equals, true)
	c.Check(devCtx.Model(), DeepEquals, classicModel)
	testGroundDeviceContextCommonChecks(c, devCtx, mode)

	// UC16/18
	legacyUCModel := boottest.MakeMockModel()
	devCtx = devicestate.BuildGroundDeviceContext(legacyUCModel, mode)
	c.Check(devCtx.Classic(), Equals, false)
	c.Check(devCtx.Kernel(), Equals, "pc-kernel")
	c.Check(devCtx.Base(), Equals, "core18")
	c.Check(devCtx.Gadget(), Equals, "pc")
	c.Check(devCtx.HasModeenv(), Equals, false)
	c.Check(devCtx.IsCoreBoot(), Equals, true)
	c.Check(devCtx.IsClassicBoot(), Equals, false)
	c.Check(devCtx.Model(), DeepEquals, legacyUCModel)
	testGroundDeviceContextCommonChecks(c, devCtx, mode)

	// UC20+
	ucModel := boottest.MakeMockUC20Model()
	devCtx = devicestate.BuildGroundDeviceContext(ucModel, mode)
	c.Check(devCtx.Classic(), Equals, false)
	c.Check(devCtx.Kernel(), Equals, "pc-kernel")
	c.Check(devCtx.Base(), Equals, "core20")
	c.Check(devCtx.Gadget(), Equals, "pc")
	c.Check(devCtx.HasModeenv(), Equals, true)
	c.Check(devCtx.IsCoreBoot(), Equals, true)
	c.Check(devCtx.IsClassicBoot(), Equals, false)
	c.Check(devCtx.Model(), DeepEquals, ucModel)
	testGroundDeviceContextCommonChecks(c, devCtx, mode)

	// Classic with modes
	classicWithModes := boottest.MakeMockClassicWithModesModel()
	devCtx = devicestate.BuildGroundDeviceContext(classicWithModes, mode)
	c.Check(devCtx.Classic(), Equals, true)
	c.Check(devCtx.Kernel(), Equals, "pc-kernel")
	c.Check(devCtx.Base(), Equals, "core20")
	c.Check(devCtx.Gadget(), Equals, "pc")
	c.Check(devCtx.HasModeenv(), Equals, true)
	c.Check(devCtx.IsCoreBoot(), Equals, true)
	c.Check(devCtx.IsClassicBoot(), Equals, false)
	c.Check(devCtx.Model(), DeepEquals, classicWithModes)
	testGroundDeviceContextCommonChecks(c, devCtx, mode)
}

func (s *deviceCtxSuite) TestGroundDeviceContext(c *C) {
	for _, mode := range []string{"run", "install", "recover", "factory-reset"} {
		s.testGroundDeviceContext(c, mode)
	}
}