github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/bootloader/withbootassettesting_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  // +build withbootassetstesting
     3  
     4  /*
     5   * Copyright (C) 2021 Canonical Ltd
     6   *
     7   * This program is free software: you can redistribute it and/or modify
     8   * it under the terms of the GNU General Public License version 3 as
     9   * published by the Free Software Foundation.
    10   *
    11   * This program is distributed in the hope that it will be useful,
    12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14   * GNU General Public License for more details.
    15   *
    16   * You should have received a copy of the GNU General Public License
    17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18   *
    19   */
    20  
    21  package bootloader_test
    22  
    23  import (
    24  	"io/ioutil"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/bootloader"
    30  	"github.com/snapcore/snapd/bootloader/assets"
    31  	"github.com/snapcore/snapd/snapdenv"
    32  )
    33  
    34  type withbootasetstestingTestSuite struct {
    35  	baseBootenvTestSuite
    36  }
    37  
    38  var _ = Suite(&withbootasetstestingTestSuite{})
    39  
    40  func (s *withbootasetstestingTestSuite) TestInjects(c *C) {
    41  	d := c.MkDir()
    42  	c.Assert(ioutil.WriteFile(filepath.Join(d, "bootassetstesting"), []byte("with-bootassetstesting\n"), 0644), IsNil)
    43  	restore := bootloader.MockMaybeInjectOsReadlink(func(_ string) (string, error) {
    44  		return filepath.Join(d, "foo"), nil
    45  	})
    46  	defer restore()
    47  	restore = snapdenv.MockTesting(true)
    48  	defer restore()
    49  	restore = assets.MockSnippetsForEdition("grub.cfg:static-cmdline", []assets.ForEditions{
    50  		{FirstEdition: 2, Snippet: []byte(`foo bar baz`)},
    51  	})
    52  	defer restore()
    53  	restore = assets.MockInternal("grub.cfg", []byte(`# Snapd-Boot-Config-Edition: 5
    54  set snapd_static_cmdline_args='foo bar baz'
    55  this is mocked grub-recovery.conf
    56  `))
    57  	defer restore()
    58  
    59  	bootloader.MaybeInjectTestingBootloaderAssets()
    60  
    61  	bumped := assets.Internal("grub.cfg")
    62  	c.Check(string(bumped), Equals, `# Snapd-Boot-Config-Edition: 6
    63  set snapd_static_cmdline_args='foo bar baz with-bootassetstesting'
    64  this is mocked grub-recovery.conf
    65  `)
    66  	cmdline := bootloader.StaticCommandLineForGrubAssetEdition("grub.cfg", 6)
    67  	c.Check(cmdline, Equals, `foo bar baz with-bootassetstesting`)
    68  }
    69  
    70  func (s *withbootasetstestingTestSuite) TestNoMarker(c *C) {
    71  	d := c.MkDir()
    72  	restore := bootloader.MockMaybeInjectOsReadlink(func(_ string) (string, error) {
    73  		return filepath.Join(d, "foo"), nil
    74  	})
    75  	defer restore()
    76  	restore = snapdenv.MockTesting(true)
    77  	defer restore()
    78  	restore = assets.MockSnippetsForEdition("grub.cfg:static-cmdline", []assets.ForEditions{
    79  		{FirstEdition: 2, Snippet: []byte(`foo bar baz`)},
    80  	})
    81  	defer restore()
    82  	grubCfg := `# Snapd-Boot-Config-Edition: 5
    83  set snapd_static_cmdline_args='foo bar baz'
    84  this is mocked grub-recovery.conf
    85  `
    86  	restore = assets.MockInternal("grub.cfg", []byte(grubCfg))
    87  	defer restore()
    88  
    89  	bootloader.MaybeInjectTestingBootloaderAssets()
    90  
    91  	notBumped := assets.Internal("grub.cfg")
    92  	c.Check(string(notBumped), Equals, grubCfg)
    93  	cmdline := bootloader.StaticCommandLineForGrubAssetEdition("grub.cfg", 5)
    94  	c.Check(cmdline, Equals, `foo bar baz`)
    95  }