github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/bootloader/assets/grub_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 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 assets_test 21 22 import ( 23 "bytes" 24 "fmt" 25 "io/ioutil" 26 "testing" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/bootloader/assets" 31 "github.com/snapcore/snapd/testutil" 32 ) 33 34 // Hook up check.v1 into the "go test" runner 35 func Test(t *testing.T) { TestingT(t) } 36 37 type grubAssetsTestSuite struct{} 38 39 var _ = Suite(&grubAssetsTestSuite{}) 40 41 func (s *grubAssetsTestSuite) testGrubConfigContains(c *C, name string, keys ...string) { 42 a := assets.Internal(name) 43 c.Assert(a, NotNil) 44 as := string(a) 45 for _, canary := range keys { 46 c.Assert(as, testutil.Contains, canary) 47 } 48 idx := bytes.IndexRune(a, '\n') 49 c.Assert(idx, Not(Equals), -1) 50 c.Assert(string(a[:idx]), Equals, "# Snapd-Boot-Config-Edition: 1") 51 } 52 53 func (s *grubAssetsTestSuite) TestGrubConf(c *C) { 54 s.testGrubConfigContains(c, "grub.cfg", 55 "snapd_recovery_mode", 56 "set snapd_static_cmdline_args='console=ttyS0 console=tty1 panic=-1'", 57 ) 58 } 59 60 func (s *grubAssetsTestSuite) TestGrubRecoveryConf(c *C) { 61 s.testGrubConfigContains(c, "grub-recovery.cfg", 62 "snapd_recovery_mode", 63 "snapd_recovery_system", 64 "set snapd_static_cmdline_args='console=ttyS0 console=tty1 panic=-1'", 65 ) 66 } 67 68 func (s *grubAssetsTestSuite) TestGrubCmdlineSnippetEditions(c *C) { 69 for _, tc := range []struct { 70 asset string 71 edition uint 72 snip []byte 73 }{ 74 {"grub.cfg:static-cmdline", 1, []byte("console=ttyS0 console=tty1 panic=-1")}, 75 {"grub-recovery.cfg:static-cmdline", 1, []byte("console=ttyS0 console=tty1 panic=-1")}, 76 } { 77 snip := assets.SnippetForEdition(tc.asset, tc.edition) 78 c.Assert(snip, NotNil) 79 c.Check(snip, DeepEquals, tc.snip) 80 } 81 } 82 83 func (s *grubAssetsTestSuite) TestGrubCmdlineSnippetCrossCheck(c *C) { 84 for _, tc := range []struct { 85 asset string 86 snippet string 87 edition uint 88 content []byte 89 pattern string 90 }{ 91 { 92 asset: "grub.cfg", snippet: "grub.cfg:static-cmdline", edition: 1, 93 content: []byte("console=ttyS0 console=tty1 panic=-1"), 94 pattern: "set snapd_static_cmdline_args='%s'\n", 95 }, 96 { 97 asset: "grub-recovery.cfg", snippet: "grub-recovery.cfg:static-cmdline", edition: 1, 98 content: []byte("console=ttyS0 console=tty1 panic=-1"), 99 pattern: "set snapd_static_cmdline_args='%s'\n", 100 }, 101 } { 102 grubCfg := assets.Internal(tc.asset) 103 c.Assert(grubCfg, NotNil) 104 prefix := fmt.Sprintf("# Snapd-Boot-Config-Edition: %d", tc.edition) 105 c.Assert(bytes.HasPrefix(grubCfg, []byte(prefix)), Equals, true) 106 // get a matching snippet 107 snip := assets.SnippetForEdition(tc.snippet, tc.edition) 108 c.Assert(snip, NotNil) 109 c.Assert(snip, DeepEquals, tc.content) 110 c.Assert(string(grubCfg), testutil.Contains, fmt.Sprintf(tc.pattern, string(snip))) 111 } 112 } 113 114 func (s *grubAssetsTestSuite) TestGrubAssetsWereRegenerated(c *C) { 115 for _, tc := range []struct { 116 asset string 117 file string 118 }{ 119 {"grub.cfg", "data/grub.cfg"}, 120 {"grub-recovery.cfg", "data/grub-recovery.cfg"}, 121 } { 122 assetData := assets.Internal(tc.asset) 123 c.Assert(assetData, NotNil) 124 data, err := ioutil.ReadFile(tc.file) 125 c.Assert(err, IsNil) 126 c.Check(assetData, DeepEquals, data, Commentf("asset %q has not been updated", tc.asset)) 127 } 128 }