github.com/rigado/snapd@v2.42.5-go-mod+incompatible/interfaces/hotplug/udevadm_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 hotplug
    21  
    22  import (
    23  	"github.com/snapcore/snapd/testutil"
    24  
    25  	. "gopkg.in/check.v1"
    26  )
    27  
    28  type udevadmSuite struct {
    29  	testutil.BaseTest
    30  }
    31  
    32  var _ = Suite(&udevadmSuite{})
    33  
    34  func (s *udevadmSuite) SetUpTest(c *C) {
    35  	s.BaseTest.SetUpTest(c)
    36  }
    37  
    38  func (s *udevadmSuite) TearDownTest(c *C) {
    39  	s.BaseTest.TearDownTest(c)
    40  }
    41  
    42  func (s *udevadmSuite) TestParsingHappy(c *C) {
    43  	cmd := testutil.MockCommand(c, udevadmBin, `#!/bin/sh
    44  cat << __END__
    45  P: /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0
    46  E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0
    47  E: DRIVER=ftdi_sio
    48  E: SUBSYSTEM=usb-serial
    49  
    50  P: /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0/tty/ttyUSB0
    51  N: ttyUSB0
    52  S: serial/by-id/usb-FTDI_FT232R_USB_UART_AH06W0EQ-if00-port0
    53  S: serial/by-path/pci-0000:00:14.0-usb-0:2:1.0-port0
    54  E: DEVLINKS=/dev/serial/by-path/pci-0000:00:14.0-usb-0:2:1.0-port0 /dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AH06W0EQ-if00-port0
    55  E: DEVNAME=/dev/ttyUSB0
    56  E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0/tty/ttyUSB0
    57  E: ID_BUS=usb
    58  E: ID_MODEL=FT232R_USB_UART
    59  E: ID_MODEL_FROM_DATABASE=FT232 Serial (UART) IC
    60  E: ID_MODEL_ID=6001
    61  E: ID_PATH=pci-0000:00:14.0-usb-0:2:1.0
    62  E: ID_PATH_TAG=pci-0000_00_14_0-usb-0_2_1_0
    63  E: ID_PCI_CLASS_FROM_DATABASE=Serial bus controller
    64  E: ID_PCI_INTERFACE_FROM_DATABASE=XHCI
    65  E: ID_PCI_SUBCLASS_FROM_DATABASE=USB controller
    66  E: ID_REVISION=0600
    67  E: ID_SERIAL=FTDI_FT232R_USB_UART_AH06W0EQ
    68  E: ID_SERIAL_SHORT=AH06W0EQ
    69  E: ID_TYPE=generic
    70  E: ID_USB_DRIVER=ftdi_sio
    71  E: ID_USB_INTERFACES=:ffffff:
    72  E: ID_USB_INTERFACE_NUM=00
    73  E: ID_VENDOR=FTDI
    74  E: ID_VENDOR_FROM_DATABASE=Future Technology Devices International, Ltd
    75  E: ID_VENDOR_ID=0403
    76  E: MAJOR=188
    77  E: MINOR=0
    78  E: SUBSYSTEM=tty
    79  E: TAGS=:systemd:
    80  __END__
    81  `)
    82  	defer cmd.Restore()
    83  
    84  	devices, perrs, err := EnumerateExistingDevices()
    85  	c.Assert(err, IsNil)
    86  	c.Assert(perrs, HasLen, 0)
    87  	c.Assert(devices, HasLen, 2)
    88  
    89  	v, _ := devices[0].Attribute("DEVPATH")
    90  	c.Assert(v, Equals, "/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0")
    91  
    92  	v, _ = devices[1].Attribute("DEVPATH")
    93  	c.Assert(v, Equals, "/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0/tty/ttyUSB0")
    94  
    95  	v, _ = devices[1].Attribute("MAJOR")
    96  	c.Assert(v, Equals, "188")
    97  
    98  	v, _ = devices[1].Attribute("TAGS")
    99  	c.Assert(v, Equals, ":systemd:")
   100  }
   101  
   102  func (s *udevadmSuite) TestParsingError(c *C) {
   103  	cmd := testutil.MockCommand(c, udevadmBin, `#!/bin/sh
   104  cat << __END__
   105  P: /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0
   106  E: DEVPATH
   107  
   108  E: K=V
   109  
   110  P: /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0
   111  E: DEVPATH=foo
   112  __END__
   113  `)
   114  	defer cmd.Restore()
   115  
   116  	devices, perrs, err := EnumerateExistingDevices()
   117  	c.Assert(err, IsNil)
   118  	c.Assert(perrs, HasLen, 2)
   119  	c.Assert(perrs[0], ErrorMatches, `cannot parse udevadm output "E: DEVPATH"`)
   120  	c.Assert(perrs[1], ErrorMatches, `no device block marker found before "E: K=V"`)
   121  
   122  	// successfully parsed devices are still reported
   123  	c.Assert(devices, HasLen, 1)
   124  	v, _ := devices[0].Attribute("DEVPATH")
   125  	c.Assert(v, Equals, "foo")
   126  }