File: system_mode_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 (145 lines) | stat: -rw-r--r-- 4,148 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2021 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 ctlcmd_test

import (
	"fmt"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/overlord/devicestate"
	"github.com/snapcore/snapd/overlord/hookstate"
	"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
	"github.com/snapcore/snapd/overlord/hookstate/hooktest"
	"github.com/snapcore/snapd/overlord/state"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/testutil"
)

type systemModeSuite struct {
	testutil.BaseTest
	st          *state.State
	mockHandler *hooktest.MockHandler
}

var _ = Suite(&systemModeSuite{})

func (s *systemModeSuite) SetUpTest(c *C) {
	s.BaseTest.SetUpTest(c)
	dirs.SetRootDir(c.MkDir())
	s.AddCleanup(func() { dirs.SetRootDir("/") })
	s.st = state.New(nil)
	s.mockHandler = hooktest.NewMockHandler()
}

func (s *systemModeSuite) TestSystemMode(c *C) {
	s.st.Lock()
	task := s.st.NewTask("test-task", "my test task")
	setup := &hookstate.HookSetup{Snap: "snap1", Revision: snap.R(1), Hook: "test-hook"}
	mockContext, err := hookstate.NewContext(task, s.st, setup, s.mockHandler, "")
	c.Check(err, IsNil)
	s.st.Unlock()

	var smi *devicestate.SystemModeInfo
	var smiErr error
	r := ctlcmd.MockDevicestateSystemModeInfoFromState(func(s *state.State) (*devicestate.SystemModeInfo, error) {
		// the mocked function requires the state lock,
		// panic if it is not held
		s.Unlock()
		defer s.Lock()
		return smi, smiErr
	})
	defer r()

	var systemEncrypted bool
	var systemEncryptedErr error
	defer ctlcmd.MockFdestateSystemEncryptedFromState(func(s *state.State) (bool, error) {
		// the mocked function requires the state lock,
		// panic if it is not held
		s.Unlock()
		defer s.Lock()
		return systemEncrypted, systemEncryptedErr
	})()

	tests := []struct {
		smi                 devicestate.SystemModeInfo
		smiErr              error
		systemEncrypted     bool
		systemEncryptedErr  error
		stdout, stderr, err string
		exitCode            int
	}{
		{
			smiErr: fmt.Errorf("too early"),
			err:    "too early",
		}, {
			systemEncryptedErr: fmt.Errorf("cannot get fde state"),
			err:                "cannot get fde state",
		}, {
			smi: devicestate.SystemModeInfo{
				Mode:   "run",
				Seeded: true,
			},
			systemEncrypted: true,
			stdout:          "system-mode: run\nseed-loaded: true\nstorage-encrypted: managed\n",
		}, {
			smi: devicestate.SystemModeInfo{
				Mode:       "install",
				HasModeenv: true,
				Seeded:     true,
				BootFlags:  []string{"factory"},
			},
			systemEncrypted: false,
			stdout:          "system-mode: install\nseed-loaded: true\nfactory: true\n",
		}, {
			smi: devicestate.SystemModeInfo{
				Mode:       "run",
				HasModeenv: true,
				Seeded:     false,
			},
			stdout: "system-mode: run\nseed-loaded: false\n",
		},
	}

	for _, uid := range []uint32{0 /* root */, 1000 /* regular */} {
		for _, test := range tests {
			smi = &test.smi
			smiErr = test.smiErr
			systemEncrypted = test.systemEncrypted
			systemEncryptedErr = test.systemEncryptedErr

			stdout, stderr, err := ctlcmd.Run(mockContext, []string{"system-mode"}, uid)
			comment := Commentf("%v", test)
			if test.exitCode > 0 {
				c.Check(err, DeepEquals, &ctlcmd.UnsuccessfulError{ExitCode: test.exitCode}, comment)
			} else {
				if test.err == "" {
					c.Check(err, IsNil, comment)
				} else {
					c.Check(err, ErrorMatches, test.err, comment)
				}
			}

			c.Check(string(stdout), Equals, test.stdout, comment)
			c.Check(string(stderr), Equals, "", comment)
		}
	}
}