github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/cmd/snap/cmd_debug_bootvars_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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  	"gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/boot"
    26  	"github.com/snapcore/snapd/bootloader"
    27  	"github.com/snapcore/snapd/bootloader/bootloadertest"
    28  	snap "github.com/snapcore/snapd/cmd/snap"
    29  	"github.com/snapcore/snapd/release"
    30  )
    31  
    32  func (s *SnapSuite) TestDebugBootvars(c *check.C) {
    33  	restore := release.MockOnClassic(false)
    34  	defer restore()
    35  	bloader := bootloadertest.Mock("mock", c.MkDir())
    36  	bootloader.Force(bloader)
    37  	err := bloader.SetBootVars(map[string]string{
    38  		"snap_mode":       "try",
    39  		"unrelated":       "thing",
    40  		"snap_core":       "core18_1.snap",
    41  		"snap_try_core":   "core18_2.snap",
    42  		"snap_kernel":     "pc-kernel_3.snap",
    43  		"snap_try_kernel": "pc-kernel_4.snap",
    44  	})
    45  	c.Assert(err, check.IsNil)
    46  
    47  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "boot-vars"})
    48  	c.Assert(err, check.IsNil)
    49  	c.Assert(rest, check.HasLen, 0)
    50  	c.Check(s.Stdout(), check.Equals, `snap_mode=try
    51  snap_core=core18_1.snap
    52  snap_try_core=core18_2.snap
    53  snap_kernel=pc-kernel_3.snap
    54  snap_try_kernel=pc-kernel_4.snap
    55  `)
    56  	c.Check(s.Stderr(), check.Equals, "")
    57  }
    58  
    59  func (s *SnapSuite) TestDebugBootvarsNotOnClassic(c *check.C) {
    60  	restore := release.MockOnClassic(true)
    61  	defer restore()
    62  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "boot-vars"})
    63  	c.Assert(err, check.ErrorMatches, `the "boot-vars" command is not available on classic systems`)
    64  }
    65  
    66  func (s *SnapSuite) TestDebugSetBootvars(c *check.C) {
    67  	restore := release.MockOnClassic(false)
    68  	defer restore()
    69  	bloader := bootloadertest.Mock("mock", c.MkDir())
    70  	bootloader.Force(bloader)
    71  	err := bloader.SetBootVars(map[string]string{
    72  		"snap_mode":       "try",
    73  		"unrelated":       "thing",
    74  		"snap_core":       "core18_1.snap",
    75  		"snap_try_core":   "core18_2.snap",
    76  		"snap_kernel":     "pc-kernel_3.snap",
    77  		"snap_try_kernel": "",
    78  	})
    79  	c.Assert(err, check.IsNil)
    80  
    81  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "set-boot-vars",
    82  		"snap_mode=trying", "try_recovery_system=1234", "unrelated="})
    83  	c.Assert(err, check.IsNil)
    84  	c.Check(rest, check.HasLen, 0)
    85  	c.Check(bloader.BootVars, check.DeepEquals, map[string]string{
    86  		"snap_mode":           "trying",
    87  		"unrelated":           "",
    88  		"snap_core":           "core18_1.snap",
    89  		"snap_try_core":       "core18_2.snap",
    90  		"snap_kernel":         "pc-kernel_3.snap",
    91  		"snap_try_kernel":     "",
    92  		"try_recovery_system": "1234",
    93  	})
    94  }
    95  
    96  func (s *SnapSuite) TestDebugGetSetBootvarsWithParams(c *check.C) {
    97  	// the bootloader options are not intercepted by the mocks, so we can
    98  	// only observe the effect indirectly for boot-vars
    99  
   100  	restore := release.MockOnClassic(false)
   101  	defer restore()
   102  	bloader := bootloadertest.Mock("mock", c.MkDir())
   103  	bootloader.Force(bloader)
   104  	err := bloader.SetBootVars(map[string]string{
   105  		"snapd_recovery_system":  "1234",
   106  		"snapd_recovery_mode":    "run",
   107  		"unrelated":              "thing",
   108  		"snap_kernel":            "pc-kernel_3.snap",
   109  		"recovery_system_status": "try",
   110  		"try_recovery_system":    "9999",
   111  	})
   112  	c.Assert(err, check.IsNil)
   113  
   114  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "boot-vars", "--root-dir", boot.InitramfsUbuntuBootDir})
   115  	c.Assert(err, check.IsNil)
   116  	c.Assert(rest, check.HasLen, 0)
   117  	c.Check(s.Stdout(), check.Equals, `snapd_recovery_mode=run
   118  snapd_recovery_system=1234
   119  snapd_recovery_kernel=
   120  snap_kernel=pc-kernel_3.snap
   121  snap_try_kernel=
   122  kernel_status=
   123  recovery_system_status=try
   124  try_recovery_system=9999
   125  snapd_extra_cmdline_args=
   126  snapd_full_cmdline_args=
   127  `)
   128  	c.Check(s.Stderr(), check.Equals, "")
   129  	s.ResetStdStreams()
   130  
   131  	// and make sure that set does not blow up when passed a root dir
   132  	rest, err = snap.Parser(snap.Client()).ParseArgs([]string{"debug", "set-boot-vars", "--root-dir", boot.InitramfsUbuntuBootDir, "foo=bar"})
   133  	c.Assert(err, check.IsNil)
   134  	c.Assert(rest, check.HasLen, 0)
   135  
   136  	v, err := bloader.GetBootVars("foo")
   137  	c.Assert(err, check.IsNil)
   138  	c.Check(v, check.DeepEquals, map[string]string{
   139  		"foo": "bar",
   140  	})
   141  	// and make sure that set does not blow up when passed recover bootloader flag
   142  	rest, err = snap.Parser(snap.Client()).ParseArgs([]string{"debug", "set-boot-vars", "--recovery", "foo=recovery"})
   143  	c.Assert(err, check.IsNil)
   144  	c.Assert(rest, check.HasLen, 0)
   145  
   146  	v, err = bloader.GetBootVars("foo")
   147  	c.Assert(err, check.IsNil)
   148  	c.Check(v, check.DeepEquals, map[string]string{
   149  		"foo": "recovery",
   150  	})
   151  
   152  	// but basic sanity checks are still done
   153  	_, err = snap.Parser(snap.Client()).ParseArgs([]string{"debug", "set-boot-vars", "--recovery", "--root-dir", boot.InitramfsUbuntuBootDir, "foo=recovery"})
   154  	c.Assert(err, check.ErrorMatches, "cannot use run bootloader root-dir with a recovery flag")
   155  }