github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/interfaces/udev/udev_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2018 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 udev_test
    21  
    22  import (
    23  	"testing"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/interfaces"
    28  	"github.com/snapcore/snapd/interfaces/udev"
    29  	"github.com/snapcore/snapd/testutil"
    30  )
    31  
    32  func Test(t *testing.T) {
    33  	TestingT(t)
    34  }
    35  
    36  type uDevSuite struct {
    37  	backend *udev.Backend
    38  }
    39  
    40  var _ = Suite(&uDevSuite{})
    41  
    42  // Tests for ReloadRules()
    43  
    44  func (s *uDevSuite) SetUpTest(c *C) {
    45  	s.backend = &udev.Backend{}
    46  	c.Assert(s.backend.Initialize(nil), IsNil)
    47  }
    48  
    49  func (s *uDevSuite) TestReloadUDevRulesRunsUDevAdm(c *C) {
    50  	cmd := testutil.MockCommand(c, "udevadm", "")
    51  	defer cmd.Restore()
    52  	err := s.backend.ReloadRules(nil)
    53  	c.Assert(err, IsNil)
    54  	c.Assert(cmd.Calls(), DeepEquals, [][]string{
    55  		{"udevadm", "control", "--reload-rules"},
    56  		{"udevadm", "trigger", "--subsystem-nomatch=input"},
    57  		// FIXME: temporary until spec.TriggerSubsystem() can be
    58  		// called during disconnect
    59  		{"udevadm", "trigger", "--property-match=ID_INPUT_JOYSTICK=1"},
    60  		{"udevadm", "settle", "--timeout=10"},
    61  	})
    62  }
    63  
    64  func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromReloadRules(c *C) {
    65  	cmd := testutil.MockCommand(c, "udevadm", `
    66  if [ "$1" = "control" ]; then
    67  	echo "failure 1"
    68  	exit 1
    69  fi
    70  	`)
    71  	defer cmd.Restore()
    72  	err := s.backend.ReloadRules(nil)
    73  	c.Assert(err.Error(), Equals, ""+
    74  		"cannot reload udev rules: exit status 1\n"+
    75  		"udev output:\n"+
    76  		"failure 1\n")
    77  	c.Assert(cmd.Calls(), DeepEquals, [][]string{
    78  		{"udevadm", "control", "--reload-rules"},
    79  	})
    80  }
    81  
    82  func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromDefaultTrigger(c *C) {
    83  	cmd := testutil.MockCommand(c, "udevadm", `
    84  if [ "$1" = "trigger" ]; then
    85  	echo "failure 2"
    86  	exit 2
    87  fi
    88  	`)
    89  	defer cmd.Restore()
    90  	err := s.backend.ReloadRules(nil)
    91  	c.Assert(err.Error(), Equals, ""+
    92  		"cannot run udev triggers: exit status 2\n"+
    93  		"udev output:\n"+
    94  		"failure 2\n")
    95  	c.Assert(cmd.Calls(), DeepEquals, [][]string{
    96  		{"udevadm", "control", "--reload-rules"},
    97  		{"udevadm", "trigger", "--subsystem-nomatch=input"},
    98  	})
    99  }
   100  
   101  func (s *uDevSuite) TestReloadUDevRulesRunsUDevAdmWithSubsystem(c *C) {
   102  	cmd := testutil.MockCommand(c, "udevadm", "")
   103  	defer cmd.Restore()
   104  	err := s.backend.ReloadRules([]string{"input"})
   105  	c.Assert(err, IsNil)
   106  	c.Assert(cmd.Calls(), DeepEquals, [][]string{
   107  		{"udevadm", "control", "--reload-rules"},
   108  		{"udevadm", "trigger", "--subsystem-nomatch=input"},
   109  		{"udevadm", "trigger", "--subsystem-match=input"},
   110  		{"udevadm", "settle", "--timeout=10"},
   111  	})
   112  }
   113  
   114  func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromSubsystemTrigger(c *C) {
   115  	cmd := testutil.MockCommand(c, "udevadm", `
   116  if [ "$2" = "--subsystem-match=input" ]; then
   117  	echo "failure 2"
   118  	exit 2
   119  fi
   120  	`)
   121  	defer cmd.Restore()
   122  	err := s.backend.ReloadRules([]string{"input"})
   123  	c.Assert(err.Error(), Equals, ""+
   124  		"cannot run udev triggers for input subsystem: exit status 2\n"+
   125  		"udev output:\n"+
   126  		"failure 2\n")
   127  	c.Assert(cmd.Calls(), DeepEquals, [][]string{
   128  		{"udevadm", "control", "--reload-rules"},
   129  		{"udevadm", "trigger", "--subsystem-nomatch=input"},
   130  		{"udevadm", "trigger", "--subsystem-match=input"},
   131  	})
   132  }
   133  
   134  func (s *uDevSuite) TestReloadUDevRulesRunsUDevAdmWithJoystick(c *C) {
   135  	cmd := testutil.MockCommand(c, "udevadm", "")
   136  	defer cmd.Restore()
   137  	err := s.backend.ReloadRules([]string{"input/joystick"})
   138  	c.Assert(err, IsNil)
   139  	c.Assert(cmd.Calls(), DeepEquals, [][]string{
   140  		{"udevadm", "control", "--reload-rules"},
   141  		{"udevadm", "trigger", "--subsystem-nomatch=input"},
   142  		{"udevadm", "trigger", "--property-match=ID_INPUT_JOYSTICK=1"},
   143  		{"udevadm", "settle", "--timeout=10"},
   144  	})
   145  }
   146  
   147  func (s *uDevSuite) TestReloadUDevRulesReportsErrorsFromJoystickTrigger(c *C) {
   148  	cmd := testutil.MockCommand(c, "udevadm", `
   149  if [ "$2" = "--property-match=ID_INPUT_JOYSTICK=1" ]; then
   150  	echo "failure 2"
   151  	exit 2
   152  fi
   153  	`)
   154  	defer cmd.Restore()
   155  	err := s.backend.ReloadRules([]string{"input/joystick"})
   156  	c.Assert(err.Error(), Equals, ""+
   157  		"cannot run udev triggers for joysticks: exit status 2\n"+
   158  		"udev output:\n"+
   159  		"failure 2\n")
   160  	c.Assert(cmd.Calls(), DeepEquals, [][]string{
   161  		{"udevadm", "control", "--reload-rules"},
   162  		{"udevadm", "trigger", "--subsystem-nomatch=input"},
   163  		{"udevadm", "trigger", "--property-match=ID_INPUT_JOYSTICK=1"},
   164  	})
   165  }
   166  
   167  func (s *uDevSuite) TestReloadUDevRulesRunsUDevAdmWithTwoSubsystems(c *C) {
   168  	cmd := testutil.MockCommand(c, "udevadm", "")
   169  	defer cmd.Restore()
   170  	err := s.backend.ReloadRules([]string{"input", "tty"})
   171  	c.Assert(err, IsNil)
   172  	c.Assert(cmd.Calls(), DeepEquals, [][]string{
   173  		{"udevadm", "control", "--reload-rules"},
   174  		{"udevadm", "trigger", "--subsystem-nomatch=input"},
   175  		{"udevadm", "trigger", "--subsystem-match=input"},
   176  		{"udevadm", "trigger", "--subsystem-match=tty"},
   177  		{"udevadm", "settle", "--timeout=10"},
   178  	})
   179  }
   180  
   181  func (s *uDevSuite) TestNoReloadWhenPreseeding(c *C) {
   182  	cmd := testutil.MockCommand(c, "udevadm", "")
   183  	defer cmd.Restore()
   184  
   185  	b := udev.Backend{}
   186  	opts := &interfaces.SecurityBackendOptions{
   187  		Preseed: true,
   188  	}
   189  	c.Assert(b.Initialize(opts), IsNil)
   190  	c.Assert(b.ReloadRules(nil), IsNil)
   191  	c.Assert(cmd.Calls(), HasLen, 0)
   192  }