github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap-bootstrap/cmd_recovery_chooser_trigger_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 main_test
    21  
    22  import (
    23  	"errors"
    24  	"io/ioutil"
    25  	"path/filepath"
    26  	"time"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	main "github.com/snapcore/snapd/cmd/snap-bootstrap"
    31  	"github.com/snapcore/snapd/cmd/snap-bootstrap/triggerwatch"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  func (s *cmdSuite) TestRecoveryChooserTriggerDefaults(c *C) {
    36  	n := 0
    37  	marker := filepath.Join(c.MkDir(), "marker")
    38  	passedTimeout := time.Duration(0)
    39  
    40  	restore := main.MockDefaultMarkerFile(marker)
    41  	defer restore()
    42  	restore = main.MockTriggerwatchWait(func(timeout time.Duration) error {
    43  		passedTimeout = timeout
    44  		n++
    45  		// trigger happened
    46  		return nil
    47  	})
    48  	defer restore()
    49  
    50  	rest, err := main.Parser().ParseArgs([]string{"recovery-chooser-trigger"})
    51  	c.Assert(err, IsNil)
    52  	c.Assert(rest, HasLen, 0)
    53  	c.Check(n, Equals, 1)
    54  	c.Check(passedTimeout, Equals, main.DefaultTimeout)
    55  	c.Check(marker, testutil.FilePresent)
    56  }
    57  
    58  func (s *cmdSuite) TestRecoveryChooserTriggerNoTrigger(c *C) {
    59  	n := 0
    60  	marker := filepath.Join(c.MkDir(), "marker")
    61  
    62  	restore := main.MockDefaultMarkerFile(marker)
    63  	defer restore()
    64  	restore = main.MockTriggerwatchWait(func(_ time.Duration) error {
    65  		n++
    66  		// trigger did not happen
    67  		return triggerwatch.ErrTriggerNotDetected
    68  	})
    69  	defer restore()
    70  
    71  	_, err := main.Parser().ParseArgs([]string{"recovery-chooser-trigger"})
    72  	c.Assert(err, IsNil)
    73  	c.Check(n, Equals, 1)
    74  	c.Check(marker, testutil.FileAbsent)
    75  }
    76  
    77  func (s *cmdSuite) TestRecoveryChooserTriggerTakesOptions(c *C) {
    78  	marker := filepath.Join(c.MkDir(), "foobar")
    79  	n := 0
    80  	passedTimeout := time.Duration(0)
    81  
    82  	restore := main.MockTriggerwatchWait(func(timeout time.Duration) error {
    83  		passedTimeout = timeout
    84  		n++
    85  		// trigger happened
    86  		return nil
    87  	})
    88  	defer restore()
    89  
    90  	rest, err := main.Parser().ParseArgs([]string{
    91  		"recovery-chooser-trigger",
    92  		"--wait-timeout", "2m",
    93  		"--marker-file", marker,
    94  	})
    95  	c.Assert(err, IsNil)
    96  	c.Assert(rest, HasLen, 0)
    97  	c.Check(n, Equals, 1)
    98  	c.Check(passedTimeout, Equals, 2*time.Minute)
    99  	c.Check(marker, testutil.FilePresent)
   100  }
   101  
   102  func (s *cmdSuite) TestRecoveryChooserTriggerDoesNothingWhenMarkerPresent(c *C) {
   103  	marker := filepath.Join(c.MkDir(), "foobar")
   104  	n := 0
   105  	restore := main.MockTriggerwatchWait(func(_ time.Duration) error {
   106  		n++
   107  		return errors.New("unexpected call")
   108  	})
   109  	defer restore()
   110  
   111  	err := ioutil.WriteFile(marker, nil, 0644)
   112  	c.Assert(err, IsNil)
   113  
   114  	rest, err := main.Parser().ParseArgs([]string{
   115  		"recovery-chooser-trigger",
   116  		"--marker-file", marker,
   117  	})
   118  	c.Assert(err, IsNil)
   119  	c.Assert(rest, HasLen, 0)
   120  	// not called
   121  	c.Check(n, Equals, 0)
   122  }
   123  
   124  func (s *cmdSuite) TestRecoveryChooserTriggerBadDurationFallback(c *C) {
   125  	n := 0
   126  	passedTimeout := time.Duration(0)
   127  	restore := main.MockDefaultMarkerFile(filepath.Join(c.MkDir(), "marker"))
   128  	defer restore()
   129  
   130  	restore = main.MockTriggerwatchWait(func(timeout time.Duration) error {
   131  		passedTimeout = timeout
   132  		n++
   133  		// trigger happened
   134  		return triggerwatch.ErrTriggerNotDetected
   135  	})
   136  	defer restore()
   137  
   138  	_, err := main.Parser().ParseArgs([]string{
   139  		"recovery-chooser-trigger",
   140  		"--wait-timeout=foobar",
   141  	})
   142  	c.Assert(err, IsNil)
   143  	c.Check(n, Equals, 1)
   144  	c.Check(passedTimeout, Equals, main.DefaultTimeout)
   145  }
   146  
   147  func (s *cmdSuite) TestRecoveryChooserTriggerNoInputDevsNoError(c *C) {
   148  	n := 0
   149  	marker := filepath.Join(c.MkDir(), "marker")
   150  
   151  	restore := main.MockDefaultMarkerFile(marker)
   152  	defer restore()
   153  	restore = main.MockTriggerwatchWait(func(_ time.Duration) error {
   154  		n++
   155  		// no input devices
   156  		return triggerwatch.ErrNoMatchingInputDevices
   157  	})
   158  	defer restore()
   159  
   160  	_, err := main.Parser().ParseArgs([]string{"recovery-chooser-trigger"})
   161  	// does not trigger an error
   162  	c.Assert(err, IsNil)
   163  	c.Check(n, Equals, 1)
   164  	c.Check(marker, testutil.FileAbsent)
   165  }