gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/snapstate/snapstate_try_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 "bytes" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/overlord/snapstate" 31 ) 32 33 func (s *snapmgrTestSuite) TestTrySetsTryMode(c *C) { 34 s.testTrySetsTryMode(snapstate.Flags{}, c) 35 } 36 37 func (s *snapmgrTestSuite) TestTrySetsTryModeDevMode(c *C) { 38 s.testTrySetsTryMode(snapstate.Flags{DevMode: true}, c) 39 } 40 func (s *snapmgrTestSuite) TestTrySetsTryModeJailMode(c *C) { 41 s.testTrySetsTryMode(snapstate.Flags{JailMode: true}, c) 42 } 43 func (s *snapmgrTestSuite) TestTrySetsTryModeClassic(c *C) { 44 restore := maybeMockClassicSupport(c) 45 defer restore() 46 47 s.testTrySetsTryMode(snapstate.Flags{Classic: true}, c, "confinement: classic\n") 48 } 49 50 func (s *snapmgrTestSuite) testTrySetsTryMode(flags snapstate.Flags, c *C, extraYaml ...string) { 51 s.state.Lock() 52 defer s.state.Unlock() 53 54 // make mock try dir 55 d := c.MkDir() 56 c.Assert(os.Chmod(d, 0755), IsNil) 57 tryYaml := filepath.Join(d, "meta", "snap.yaml") 58 err := os.MkdirAll(filepath.Dir(tryYaml), 0755) 59 c.Assert(err, IsNil) 60 buf := bytes.Buffer{} 61 buf.WriteString("name: foo\nversion: 1.0\n") 62 if len(extraYaml) > 0 { 63 for _, extra := range extraYaml { 64 buf.WriteString(extra) 65 } 66 } 67 err = ioutil.WriteFile(tryYaml, buf.Bytes(), 0644) 68 c.Assert(err, IsNil) 69 70 chg := s.state.NewChange("try", "try snap") 71 ts, err := snapstate.TryPath(s.state, "foo", d, flags) 72 c.Assert(err, IsNil) 73 chg.AddAll(ts) 74 75 s.state.Unlock() 76 defer s.se.Stop() 77 s.settle(c) 78 s.state.Lock() 79 80 c.Assert(chg.Err(), IsNil) 81 c.Assert(chg.IsReady(), Equals, true) 82 83 // verify snap is in TryMode 84 var snapst snapstate.SnapState 85 err = snapstate.Get(s.state, "foo", &snapst) 86 c.Assert(err, IsNil) 87 88 flags.TryMode = true 89 c.Check(snapst.Flags, DeepEquals, flags) 90 91 c.Check(s.state.TaskCount(), Equals, len(ts.Tasks())) 92 c.Check(taskKinds(ts.Tasks()), DeepEquals, []string{ 93 "prerequisites", 94 "prepare-snap", 95 "mount-snap", 96 "copy-snap-data", 97 "setup-profiles", 98 "link-snap", 99 "auto-connect", 100 "set-auto-aliases", 101 "setup-aliases", 102 "run-hook[install]", 103 "start-snap-services", 104 "run-hook[configure]", 105 "run-hook[check-health]", 106 }) 107 108 } 109 110 func (s *snapmgrTestSuite) TestTryUndoRemovesTryFlag(c *C) { 111 restore := maybeMockClassicSupport(c) 112 defer restore() 113 s.testTrySetsTryMode(snapstate.Flags{}, c) 114 } 115 116 func (s *snapmgrTestSuite) TestTryUndoRemovesTryFlagLeavesDevMode(c *C) { 117 s.testTrySetsTryMode(snapstate.Flags{DevMode: true}, c) 118 } 119 func (s *snapmgrTestSuite) TestTryUndoRemovesTryFlagLeavesJailMode(c *C) { 120 s.testTrySetsTryMode(snapstate.Flags{JailMode: true}, c) 121 } 122 func (s *snapmgrTestSuite) TestTryUndoRemovesTryFlagLeavesClassic(c *C) { 123 restore := maybeMockClassicSupport(c) 124 defer restore() 125 s.testTrySetsTryMode(snapstate.Flags{Classic: true}, c, "confinement: classic\n") 126 }