github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/interfaces/hotplug/deviceinfo_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  	"testing"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/dirs"
    28  	"github.com/snapcore/snapd/osutil"
    29  	"github.com/snapcore/snapd/testutil"
    30  )
    31  
    32  func Test(t *testing.T) { TestingT(t) }
    33  
    34  type hotplugSuite struct {
    35  	testutil.BaseTest
    36  }
    37  
    38  var _ = Suite(&hotplugSuite{})
    39  
    40  func (s *hotplugSuite) SetUpTest(c *C) {
    41  	s.BaseTest.SetUpTest(c)
    42  	dirs.SetRootDir("/")
    43  
    44  	restore := osutil.MockMountInfo("")
    45  	s.AddCleanup(restore)
    46  }
    47  
    48  func (s *hotplugSuite) TearDownTest(c *C) {
    49  	s.BaseTest.TearDownTest(c)
    50  	dirs.SetRootDir("")
    51  }
    52  
    53  func (s *hotplugSuite) TestBasicProperties(c *C) {
    54  	env := map[string]string{
    55  		"DEVPATH": "/devices/pci0000:00/0000:00:14.0/usb2/2-3", "DEVNAME": "bus/usb/002/003",
    56  		"DEVTYPE": "usb_device",
    57  		"PRODUCT": "1d50/6108/0", "DEVNUM": "003",
    58  		"SEQNUM": "4053",
    59  		"ACTION": "add", "SUBSYSTEM": "usb",
    60  		"MAJOR": "189", "MINOR": "130",
    61  		"TYPE": "0/0/0", "BUSNUM": "002",
    62  	}
    63  
    64  	di, err := NewHotplugDeviceInfo(env)
    65  	c.Assert(err, IsNil)
    66  
    67  	c.Assert(di.DeviceName(), Equals, "bus/usb/002/003")
    68  	c.Assert(di.DeviceType(), Equals, "usb_device")
    69  	c.Assert(di.DevicePath(), Equals, "/sys/devices/pci0000:00/0000:00:14.0/usb2/2-3")
    70  	c.Assert(di.Subsystem(), Equals, "usb")
    71  	c.Assert(di.Major(), Equals, "189")
    72  	c.Assert(di.Minor(), Equals, "130")
    73  
    74  	minor, ok := di.Attribute("MINOR")
    75  	c.Assert(ok, Equals, true)
    76  	c.Assert(minor, Equals, "130")
    77  
    78  	_, ok = di.Attribute("FOO")
    79  	c.Assert(ok, Equals, false)
    80  }
    81  
    82  func (s *hotplugSuite) TestPropertiesMissing(c *C) {
    83  	env := map[string]string{
    84  		"DEVPATH": "/devices/pci0000:00/0000:00:14.0/usb2/2-3",
    85  		"ACTION":  "add", "SUBSYSTEM": "usb",
    86  	}
    87  
    88  	_, err := NewHotplugDeviceInfo(map[string]string{})
    89  	c.Assert(err, NotNil)
    90  	c.Assert(err, ErrorMatches, `missing device path attribute`)
    91  
    92  	di, err := NewHotplugDeviceInfo(env)
    93  	c.Assert(err, IsNil)
    94  
    95  	c.Assert(di.DeviceName(), Equals, "")
    96  	c.Assert(di.DeviceType(), Equals, "")
    97  	c.Assert(di.DevicePath(), Equals, "/sys/devices/pci0000:00/0000:00:14.0/usb2/2-3")
    98  	c.Assert(di.Subsystem(), Equals, "usb")
    99  	c.Assert(di.Major(), Equals, "")
   100  	c.Assert(di.Minor(), Equals, "")
   101  }
   102  
   103  func (s *hotplugSuite) TestStringFormat(c *C) {
   104  	tests := []struct {
   105  		env map[string]string
   106  		out string
   107  	}{
   108  		{
   109  			env: map[string]string{
   110  				"DEVPATH":                 "/devices/a/b/c",
   111  				"DEVNAME":                 "/dev/xyz",
   112  				"ID_VENDOR_FROM_DATABASE": "foo",
   113  				"ID_MODEL_FROM_DATABASE":  "bar",
   114  				"ID_SERIAL":               "999000",
   115  				"ACTION":                  "add",
   116  				"SUBSYSTEM":               "usb",
   117  			},
   118  			out: "/dev/xyz (bar; serial: 999000)",
   119  		},
   120  		{
   121  			env: map[string]string{
   122  				"DEVPATH":         "/devices/a/b/c",
   123  				"ID_SERIAL":       "Foo 999000",
   124  				"ID_SERIAL_SHORT": "999000",
   125  				"ACTION":          "add",
   126  				"SUBSYSTEM":       "usb",
   127  			},
   128  			out: "/sys/devices/a/b/c (serial: Foo 999000)",
   129  		},
   130  		{
   131  			env: map[string]string{
   132  				"DEVPATH":      "/devices/a/b/c",
   133  				"ID_VENDOR_ID": "foo",
   134  				"ID_MODEL_ID":  "bar",
   135  				"ID_SERIAL":    "noserial",
   136  				"ACTION":       "add",
   137  			},
   138  			out: "/sys/devices/a/b/c (bar)",
   139  		},
   140  		{
   141  			env: map[string]string{
   142  				"DEVPATH":                 "/devices/a/b/c",
   143  				"ID_VENDOR_FROM_DATABASE": "very long vendor name abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
   144  				"ID_SERIAL_SHORT":         "123",
   145  				"ACTION":                  "add",
   146  			},
   147  			out: "/sys/devices/a/b/c (very long vendor name abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV…; serial: 123)",
   148  		},
   149  		{
   150  			env: map[string]string{
   151  				"DEVPATH":                "/devices/a/b/c",
   152  				"ID_MODEL_FROM_DATABASE": "very long model name abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
   153  				"ACTION":                 "add",
   154  				"MAJOR":                  "189", "MINOR": "1",
   155  			},
   156  			out: "/sys/devices/a/b/c (very long model name abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW…)",
   157  		},
   158  		{
   159  			env: map[string]string{
   160  				"DEVPATH": "/devices/a/b/c",
   161  				"ACTION":  "add",
   162  			},
   163  			out: "/sys/devices/a/b/c",
   164  		},
   165  		{
   166  			env: map[string]string{
   167  				"DEVNAME": "/dev/a",
   168  				"DEVPATH": "/devices/a/b/c",
   169  				"ACTION":  "add",
   170  			},
   171  			out: "/dev/a",
   172  		},
   173  	}
   174  
   175  	for _, tst := range tests {
   176  		di, err := NewHotplugDeviceInfo(tst.env)
   177  		c.Assert(err, IsNil)
   178  
   179  		c.Check(di.String(), Equals, tst.out)
   180  	}
   181  }
   182  
   183  func (s *hotplugSuite) TestShortStringFormat(c *C) {
   184  	di, err := NewHotplugDeviceInfo(map[string]string{
   185  		"DEVPATH":                 "/devices/a",
   186  		"ID_VENDOR_FROM_DATABASE": "very long vendor name",
   187  		"ACTION":                  "add",
   188  	})
   189  	c.Assert(err, IsNil)
   190  	c.Check(di.ShortString(), Equals, "/sys/devices/a (very long vendor…)")
   191  }