-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathsnapstate_try_test.go
123 lines (104 loc) · 3.34 KB
/
snapstate_try_test.go
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2020 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 snapstate_test
import (
"bytes"
"os"
"path/filepath"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/overlord/snapstate"
)
func (s *snapmgrTestSuite) TestTrySetsTryMode(c *C) {
s.testTrySetsTryMode(snapstate.Flags{}, c)
}
func (s *snapmgrTestSuite) TestTrySetsTryModeDevMode(c *C) {
s.testTrySetsTryMode(snapstate.Flags{DevMode: true}, c)
}
func (s *snapmgrTestSuite) TestTrySetsTryModeJailMode(c *C) {
s.testTrySetsTryMode(snapstate.Flags{JailMode: true}, c)
}
func (s *snapmgrTestSuite) TestTrySetsTryModeClassic(c *C) {
restore := maybeMockClassicSupport(c)
defer restore()
s.testTrySetsTryMode(snapstate.Flags{Classic: true}, c, "confinement: classic\n")
}
func (s *snapmgrTestSuite) testTrySetsTryMode(flags snapstate.Flags, c *C, extraYaml ...string) {
s.state.Lock()
defer s.state.Unlock()
// make mock try dir
d := c.MkDir()
c.Assert(os.Chmod(d, 0755), IsNil)
tryYaml := filepath.Join(d, "meta", "snap.yaml")
err := os.MkdirAll(filepath.Dir(tryYaml), 0755)
c.Assert(err, IsNil)
buf := bytes.Buffer{}
buf.WriteString("name: foo\nversion: 1.0\n")
if len(extraYaml) > 0 {
for _, extra := range extraYaml {
buf.WriteString(extra)
}
}
err = os.WriteFile(tryYaml, buf.Bytes(), 0644)
c.Assert(err, IsNil)
chg := s.state.NewChange("try", "try snap")
ts, err := snapstate.TryPath(s.state, "foo", d, flags)
c.Assert(err, IsNil)
chg.AddAll(ts)
s.settle(c)
c.Assert(chg.Err(), IsNil)
c.Assert(chg.IsReady(), Equals, true)
// verify snap is in TryMode
var snapst snapstate.SnapState
err = snapstate.Get(s.state, "foo", &snapst)
c.Assert(err, IsNil)
flags.TryMode = true
c.Check(snapst.Flags, DeepEquals, flags)
c.Check(s.state.TaskCount(), Equals, len(ts.Tasks()))
c.Check(taskKinds(ts.Tasks()), DeepEquals, []string{
"prerequisites",
"prepare-snap",
"mount-snap",
"copy-snap-data",
"setup-profiles",
"link-snap",
"auto-connect",
"set-auto-aliases",
"setup-aliases",
"run-hook[install]",
"run-hook[default-configure]",
"start-snap-services",
"run-hook[configure]",
"run-hook[check-health]",
})
}
func (s *snapmgrTestSuite) TestTryUndoRemovesTryFlag(c *C) {
restore := maybeMockClassicSupport(c)
defer restore()
s.testTrySetsTryMode(snapstate.Flags{}, c)
}
func (s *snapmgrTestSuite) TestTryUndoRemovesTryFlagLeavesDevMode(c *C) {
s.testTrySetsTryMode(snapstate.Flags{DevMode: true}, c)
}
func (s *snapmgrTestSuite) TestTryUndoRemovesTryFlagLeavesJailMode(c *C) {
s.testTrySetsTryMode(snapstate.Flags{JailMode: true}, c)
}
func (s *snapmgrTestSuite) TestTryUndoRemovesTryFlagLeavesClassic(c *C) {
restore := maybeMockClassicSupport(c)
defer restore()
s.testTrySetsTryMode(snapstate.Flags{Classic: true}, c, "confinement: classic\n")
}