gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/lsdrivers/lsdrivers_linux_test.go (about)

     1  // Copyright 2012-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  )
    14  
    15  // Testdata describing a directory, file, or link.
    16  // type t is one of d, l, or -
    17  // n is the name, l the symlink if any.
    18  type dfl struct {
    19  	t string
    20  	n string
    21  	l string
    22  }
    23  
    24  func makeDFL(d []dfl) error {
    25  	for _, f := range d {
    26  		switch f.t {
    27  		case "d":
    28  			if err := os.MkdirAll(f.n, 0755); err != nil {
    29  				return err
    30  			}
    31  		case "l":
    32  			if err := os.Symlink(f.l, f.n); err != nil {
    33  				return err
    34  			}
    35  		case "-":
    36  			f, err := os.Create(f.n)
    37  			if err != nil {
    38  				return err
    39  			}
    40  			f.Close()
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  func allDone(dir string, t *testing.T) {
    47  	if err := os.RemoveAll(dir); err != nil {
    48  		t.Fatal(err)
    49  	}
    50  }
    51  
    52  // This tests one of the unused components, the alarm timer
    53  func TestUnusedAlarmTimer(t *testing.T) {
    54  	dir, err := ioutil.TempDir("", "lsdrivers")
    55  
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	// generated by: tar cf - /sys/bus | tar tvf -  | awk '{printf("{t:\"%s\", n: \"%s\", l: \"%s\"},\n", substr($1,1,1), $6, $8)}'
    60  	// Then filter what you need.
    61  	var alarmtimer = []dfl{
    62  		{t: "d", n: filepath.Join(dir, "sys/bus/platform/drivers/alarmtimer/"), l: ""},
    63  	}
    64  	if err := makeDFL(alarmtimer); err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	defer allDone(dir, t)
    68  
    69  	bus := filepath.Join(dir, "sys/bus")
    70  	out, err := lsdrivers(bus, false)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	o := strings.Join(out, "\n")
    75  	used := ""
    76  	if used != o {
    77  		t.Errorf("testing used: got %q, want %q", o, used)
    78  	}
    79  	out, err = lsdrivers(bus, true)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	unused := "platform.alarmtimer"
    84  	o = strings.Join(out, "\n")
    85  	if unused != o {
    86  		t.Errorf("testing unused: got %q, want %q", o, unused)
    87  	}
    88  }
    89  
    90  // This tests one of the unused components, acpi.button
    91  func TestUsedPWRButton(t *testing.T) {
    92  	dir, err := ioutil.TempDir("", "lsdrivers")
    93  
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	// generated by: tar cf - /sys/bus | tar tvf -  | awk '{printf("{t:\"%s\", n: \"%s\", l: \"%s\"},\n", substr($1,1,1), $6, $8)}'
    98  	var alarmtimer = []dfl{
    99  		{t: "d", n: filepath.Join(dir, "sys/bus/acpi/drivers/button/"), l: ""},
   100  		{t: "l", n: filepath.Join(dir, "sys/bus/acpi/drivers/button/LNXPWRBN:00"), l: "../../../../devices/LNXSYSTM:00/LNXPWRBN:00"},
   101  	}
   102  	if err := makeDFL(alarmtimer); err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	defer allDone(dir, t)
   106  
   107  	bus := filepath.Join(dir, "sys/bus")
   108  	out, err := lsdrivers(bus, false)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  	o := strings.Join(out, "\n")
   113  	used := "acpi.button"
   114  	if used != o {
   115  		t.Errorf("testing used: got %q, want %q", o, used)
   116  	}
   117  	out, err = lsdrivers(bus, true)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  	unused := ""
   122  	o = strings.Join(out, "\n")
   123  	if unused != o {
   124  		t.Errorf("testing unused: got %q, want %q", o, unused)
   125  	}
   126  }