gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap-preseed/preseed_uc20_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2022 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 "io/ioutil" 24 "os" 25 "path/filepath" 26 27 . "gopkg.in/check.v1" 28 29 main "gitee.com/mysnapcore/mysnapd/cmd/snap-preseed" 30 "gitee.com/mysnapcore/mysnapd/dirs" 31 ) 32 33 func (s *startPreseedSuite) TestRunPreseedUC20Happy(c *C) { 34 tmpDir := c.MkDir() 35 dirs.SetRootDir(tmpDir) 36 37 restore := main.MockOsGetuid(func() int { 38 return 0 39 }) 40 defer restore() 41 42 // for UC20 probing 43 c.Assert(os.MkdirAll(filepath.Join(tmpDir, "system-seed/systems/20220203"), 0755), IsNil) 44 // we don't run tar, so create a fake artifact to make FileDigest happy 45 c.Assert(ioutil.WriteFile(filepath.Join(tmpDir, "system-seed/systems/20220203/preseed.tgz"), nil, 0644), IsNil) 46 47 var called bool 48 restorePreseed := main.MockPreseedCore20(func(dir, key, aaDir string) error { 49 c.Check(dir, Equals, tmpDir) 50 c.Check(key, Equals, "key") 51 c.Check(aaDir, Equals, "/custom/aa/features") 52 called = true 53 return nil 54 }) 55 defer restorePreseed() 56 57 parser := testParser(c) 58 c.Assert(main.Run(parser, []string{"--preseed-sign-key", "key", "--apparmor-features-dir", "/custom/aa/features", tmpDir}), IsNil) 59 c.Check(called, Equals, true) 60 } 61 62 func (s *startPreseedSuite) TestRunPreseedUC20HappyNoArgs(c *C) { 63 tmpDir := c.MkDir() 64 dirs.SetRootDir(tmpDir) 65 66 restore := main.MockOsGetuid(func() int { 67 return 0 68 }) 69 defer restore() 70 71 // for UC20 probing 72 c.Assert(os.MkdirAll(filepath.Join(tmpDir, "system-seed/systems/20220203"), 0755), IsNil) 73 c.Assert(ioutil.WriteFile(filepath.Join(tmpDir, "system-seed/systems/20220203/preseed.tgz"), nil, 0644), IsNil) 74 75 var called bool 76 restorePreseed := main.MockPreseedCore20(func(dir, key, aaDir string) error { 77 c.Check(dir, Equals, tmpDir) 78 c.Check(key, Equals, "") 79 c.Check(aaDir, Equals, "") 80 called = true 81 return nil 82 }) 83 defer restorePreseed() 84 85 parser := testParser(c) 86 c.Assert(main.Run(parser, []string{tmpDir}), IsNil) 87 c.Check(called, Equals, true) 88 }