gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap-preseed/preseed_classic_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019-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 main_test 21 22 import ( 23 "testing" 24 25 "github.com/jessevdk/go-flags" 26 . "gopkg.in/check.v1" 27 28 "gitee.com/mysnapcore/mysnapd/cmd/snap-preseed" 29 "gitee.com/mysnapcore/mysnapd/dirs" 30 "gitee.com/mysnapcore/mysnapd/osutil/squashfs" 31 "gitee.com/mysnapcore/mysnapd/snap" 32 "gitee.com/mysnapcore/mysnapd/testutil" 33 ) 34 35 func Test(t *testing.T) { TestingT(t) } 36 37 var _ = Suite(&startPreseedSuite{}) 38 39 type startPreseedSuite struct { 40 testutil.BaseTest 41 } 42 43 func (s *startPreseedSuite) SetUpTest(c *C) { 44 s.BaseTest.SetUpTest(c) 45 restore := squashfs.MockNeedsFuse(false) 46 s.BaseTest.AddCleanup(restore) 47 } 48 49 func (s *startPreseedSuite) TearDownTest(c *C) { 50 s.BaseTest.TearDownTest(c) 51 dirs.SetRootDir("") 52 } 53 54 func testParser(c *C) *flags.Parser { 55 parser := main.Parser() 56 _, err := parser.ParseArgs([]string{}) 57 c.Assert(err, IsNil) 58 return parser 59 } 60 61 func (s *startPreseedSuite) TestRequiresRoot(c *C) { 62 restore := main.MockOsGetuid(func() int { 63 return 1000 64 }) 65 defer restore() 66 67 parser := testParser(c) 68 c.Check(main.Run(parser, []string{"/"}), ErrorMatches, `must be run as root`) 69 } 70 71 func (s *startPreseedSuite) TestMissingArg(c *C) { 72 restore := main.MockOsGetuid(func() int { 73 return 0 74 }) 75 defer restore() 76 77 parser := testParser(c) 78 c.Check(main.Run(parser, nil), ErrorMatches, `need chroot path as argument`) 79 } 80 81 func (s *startPreseedSuite) TestRunPreseedAgainstFilesystemRoot(c *C) { 82 restore := main.MockOsGetuid(func() int { return 0 }) 83 defer restore() 84 85 parser := testParser(c) 86 c.Assert(main.Run(parser, []string{"/"}), ErrorMatches, `cannot run snap-preseed against /`) 87 } 88 89 func (s *startPreseedSuite) TestRunPreseedClassicHappy(c *C) { 90 restore := main.MockOsGetuid(func() int { 91 return 0 92 }) 93 defer restore() 94 95 var called bool 96 restorePreseed := main.MockPreseedClassic(func(dir string) error { 97 c.Check(dir, Equals, "/a/dir") 98 called = true 99 return nil 100 }) 101 defer restorePreseed() 102 103 parser := testParser(c) 104 c.Assert(main.Run(parser, []string{"/a/dir"}), IsNil) 105 c.Check(called, Equals, true) 106 } 107 108 func (s *startPreseedSuite) TestReset(c *C) { 109 restore := main.MockOsGetuid(func() int { 110 return 0 111 }) 112 defer restore() 113 114 var called bool 115 main.MockResetPreseededChroot(func(dir string) error { 116 c.Check(dir, Equals, "/a/dir") 117 called = true 118 return nil 119 }) 120 121 parser := testParser(c) 122 c.Assert(main.Run(parser, []string{"--reset", "/a/dir"}), IsNil) 123 c.Check(called, Equals, true) 124 } 125 126 func (s *startPreseedSuite) TestReadInfoValidity(c *C) { 127 var called bool 128 inf := &snap.Info{ 129 BadInterfaces: make(map[string]string), 130 Plugs: map[string]*snap.PlugInfo{ 131 "foo": { 132 Interface: "bad"}, 133 }, 134 } 135 136 // set an empty sanitize method. 137 snap.SanitizePlugsSlots = func(*snap.Info) { called = true } 138 139 parser := testParser(c) 140 tmpDir := c.MkDir() 141 _ = main.Run(parser, []string{tmpDir}) 142 143 // real sanitize method should be set after Run() 144 snap.SanitizePlugsSlots(inf) 145 c.Assert(called, Equals, false) 146 c.Assert(inf.BadInterfaces, HasLen, 1) 147 }