gitee.com/mysnapcore/mysnapd@v0.1.0/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  	"gitee.com/mysnapcore/mysnapd/arch/archtest"
    31  	"gitee.com/mysnapcore/mysnapd/bootloader/assets"
    32  	"gitee.com/mysnapcore/mysnapd/testutil"
    33  )
    34  
    35  // Hook up check.v1 into the "go test" runner
    36  func Test(t *testing.T) { TestingT(t) }
    37  
    38  type grubAssetsTestSuite struct {
    39  	testutil.BaseTest
    40  }
    41  
    42  var _ = Suite(&grubAssetsTestSuite{})
    43  
    44  func (s *grubAssetsTestSuite) SetUpTest(c *C) {
    45  	s.BaseTest.SetUpTest(c)
    46  
    47  	// By default assume amd64 in the tests: there are specialized
    48  	// tests for other arches
    49  	s.AddCleanup(archtest.MockArchitecture("amd64"))
    50  	snippets := []assets.ForEditions{
    51  		{FirstEdition: 1, Snippet: []byte("console=ttyS0 console=tty1 panic=-1")},
    52  		{FirstEdition: 3, Snippet: []byte("console=ttyS0,115200n8 console=tty1 panic=-1")},
    53  	}
    54  	s.AddCleanup(assets.MockSnippetsForEdition("grub.cfg:static-cmdline", snippets))
    55  	s.AddCleanup(assets.MockSnippetsForEdition("grub-recovery.cfg:static-cmdline", snippets))
    56  }
    57  
    58  func (s *grubAssetsTestSuite) testGrubConfigContains(c *C, name string, edition int, keys ...string) {
    59  	a := assets.Internal(name)
    60  	c.Assert(a, NotNil)
    61  	as := string(a)
    62  	for _, canary := range keys {
    63  		c.Assert(as, testutil.Contains, canary)
    64  	}
    65  	idx := bytes.IndexRune(a, '\n')
    66  	c.Assert(idx, Not(Equals), -1)
    67  	prefix := fmt.Sprintf("# Snapd-Boot-Config-Edition: %d", edition)
    68  	c.Assert(string(a[:idx]), Equals, prefix)
    69  }
    70  
    71  func (s *grubAssetsTestSuite) TestGrubConf(c *C) {
    72  	s.testGrubConfigContains(c, "grub.cfg", 3,
    73  		"snapd_recovery_mode",
    74  		"set snapd_static_cmdline_args='console=ttyS0,115200n8 console=tty1 panic=-1'",
    75  	)
    76  }
    77  
    78  func (s *grubAssetsTestSuite) TestGrubRecoveryConf(c *C) {
    79  	s.testGrubConfigContains(c, "grub-recovery.cfg", 1,
    80  		"snapd_recovery_mode",
    81  		"snapd_recovery_system",
    82  		"set snapd_static_cmdline_args='console=ttyS0 console=tty1 panic=-1'",
    83  	)
    84  }
    85  
    86  func (s *grubAssetsTestSuite) TestGrubCmdlineSnippetEditions(c *C) {
    87  	for _, tc := range []struct {
    88  		asset   string
    89  		edition uint
    90  		snip    []byte
    91  	}{
    92  		{"grub.cfg:static-cmdline", 1, []byte("console=ttyS0 console=tty1 panic=-1")},
    93  		{"grub-recovery.cfg:static-cmdline", 1, []byte("console=ttyS0 console=tty1 panic=-1")},
    94  	} {
    95  		snip := assets.SnippetForEdition(tc.asset, tc.edition)
    96  		c.Assert(snip, NotNil)
    97  		c.Check(snip, DeepEquals, tc.snip)
    98  	}
    99  }
   100  
   101  func (s *grubAssetsTestSuite) TestGrubCmdlineSnippetEditionsForArm64(c *C) {
   102  	r := archtest.MockArchitecture("arm64")
   103  	defer r()
   104  	// Make sure to revert later to the prev arch snippets
   105  	r = assets.MockCleanState()
   106  	defer r()
   107  	assets.RegisterGrubSnippets()
   108  	for _, tc := range []struct {
   109  		asset   string
   110  		edition uint
   111  		snip    []byte
   112  	}{
   113  		{"grub.cfg:static-cmdline", 1, []byte("panic=-1")},
   114  		{"grub-recovery.cfg:static-cmdline", 1, []byte("panic=-1")},
   115  	} {
   116  		snip := assets.SnippetForEdition(tc.asset, tc.edition)
   117  		c.Assert(snip, NotNil)
   118  		c.Check(snip, DeepEquals, tc.snip)
   119  	}
   120  }
   121  
   122  func (s *grubAssetsTestSuite) TestGrubCmdlineSnippetCrossCheck(c *C) {
   123  	for _, tc := range []struct {
   124  		asset   string
   125  		snippet string
   126  		edition uint
   127  		content []byte
   128  		pattern string
   129  	}{
   130  		{
   131  			asset: "grub.cfg", snippet: "grub.cfg:static-cmdline", edition: 3,
   132  			content: []byte("console=ttyS0,115200n8 console=tty1 panic=-1"),
   133  			pattern: "set snapd_static_cmdline_args='%s'\n",
   134  		},
   135  		{
   136  			asset: "grub-recovery.cfg", snippet: "grub-recovery.cfg:static-cmdline", edition: 1,
   137  			content: []byte("console=ttyS0 console=tty1 panic=-1"),
   138  			pattern: "set snapd_static_cmdline_args='%s'\n",
   139  		},
   140  	} {
   141  		grubCfg := assets.Internal(tc.asset)
   142  		c.Assert(grubCfg, NotNil)
   143  		prefix := fmt.Sprintf("# Snapd-Boot-Config-Edition: %d", tc.edition)
   144  		c.Assert(bytes.HasPrefix(grubCfg, []byte(prefix)), Equals, true)
   145  		// get a matching snippet
   146  		snip := assets.SnippetForEdition(tc.snippet, tc.edition)
   147  		c.Assert(snip, NotNil)
   148  		c.Assert(snip, DeepEquals, tc.content, Commentf("%s: '%s' != '%s'", tc.asset, snip, tc.content))
   149  		c.Assert(string(grubCfg), testutil.Contains, fmt.Sprintf(tc.pattern, string(snip)))
   150  	}
   151  }
   152  
   153  func (s *grubAssetsTestSuite) TestGrubAssetsWereRegenerated(c *C) {
   154  	for _, tc := range []struct {
   155  		asset string
   156  		file  string
   157  	}{
   158  		{"grub.cfg", "data/grub.cfg"},
   159  		{"grub-recovery.cfg", "data/grub-recovery.cfg"},
   160  	} {
   161  		assetData := assets.Internal(tc.asset)
   162  		c.Assert(assetData, NotNil)
   163  		data, err := ioutil.ReadFile(tc.file)
   164  		c.Assert(err, IsNil)
   165  		c.Check(assetData, DeepEquals, data, Commentf("asset %q has not been updated", tc.asset))
   166  	}
   167  }