pkg.re/essentialkaos/ek@v12.36.0+incompatible/system/sensors/sensors_test.go (about)

     1  // +build linux
     2  
     3  package sensors
     4  
     5  // ////////////////////////////////////////////////////////////////////////////////// //
     6  //                                                                                    //
     7  //                         Copyright (c) 2021 ESSENTIAL KAOS                          //
     8  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     9  //                                                                                    //
    10  // ////////////////////////////////////////////////////////////////////////////////// //
    11  
    12  import (
    13  	"io/ioutil"
    14  	"os"
    15  	"testing"
    16  
    17  	. "pkg.re/essentialkaos/check.v1"
    18  )
    19  
    20  // ////////////////////////////////////////////////////////////////////////////////// //
    21  
    22  func Test(t *testing.T) { TestingT(t) }
    23  
    24  type SensorsSuite struct{}
    25  
    26  // ////////////////////////////////////////////////////////////////////////////////// //
    27  
    28  var _ = Suite(&SensorsSuite{})
    29  
    30  // ////////////////////////////////////////////////////////////////////////////////// //
    31  
    32  func (s *SensorsSuite) TestParsing(c *C) {
    33  	tmpDir := c.MkDir()
    34  	createFakeFS(tmpDir)
    35  
    36  	hwmonDir = tmpDir
    37  
    38  	devices, err := Collect()
    39  
    40  	c.Assert(err, IsNil)
    41  	c.Assert(devices, NotNil)
    42  	c.Assert(devices, HasLen, 2)
    43  	c.Assert(devices[0].Name, Equals, "acpitz")
    44  	c.Assert(devices[0].TempSensors[0].Name, Equals, "")
    45  	c.Assert(devices[0].TempSensors[0].Cur, Equals, 8.2)
    46  	c.Assert(devices[0].TempSensors[0].Min, Equals, 0.0)
    47  	c.Assert(devices[0].TempSensors[0].Max, Equals, 0.0)
    48  	c.Assert(devices[0].TempSensors[0].Crit, Equals, 25.6)
    49  	c.Assert(devices[1].Name, Equals, "coretemp")
    50  	c.Assert(devices[1].TempSensors[0].Name, Equals, "Core 0")
    51  	c.Assert(devices[1].TempSensors[0].Cur, Equals, 38.0)
    52  	c.Assert(devices[1].TempSensors[0].Min, Equals, 12.0)
    53  	c.Assert(devices[1].TempSensors[0].Max, Equals, 81.0)
    54  	c.Assert(devices[1].TempSensors[0].Crit, Equals, 91.0)
    55  
    56  	t1, t2, t3 := devices[1].Temperature()
    57  	c.Assert(t1, Equals, 38.0)
    58  	c.Assert(t2, Equals, 41.0)
    59  	c.Assert(t3, Equals, 39.5)
    60  
    61  	c.Assert(devices[1].TempSensors[0].String(), Equals, "{Name:Core 0 Cur:+38°C Min:+12°C Max:+81°C Crit:+91°C}")
    62  }
    63  
    64  func (s *SensorsSuite) TestParsingErrors(c *C) {
    65  	hwmonDir = "/_unknown_"
    66  
    67  	_, err := Collect()
    68  	c.Assert(err, NotNil)
    69  
    70  	d := &Device{}
    71  	t1, t2, t3 := d.Temperature()
    72  	c.Assert(t1, Equals, 0.0)
    73  	c.Assert(t2, Equals, 0.0)
    74  	c.Assert(t3, Equals, 0.0)
    75  
    76  	tmpDir := c.MkDir()
    77  	createFakeFS(tmpDir)
    78  
    79  	hwmonDir = tmpDir
    80  
    81  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_crit", []byte("AAA"), 0644)
    82  	_, err = Collect()
    83  	c.Assert(err, NotNil)
    84  
    85  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_max", []byte("AAA"), 0644)
    86  	_, err = Collect()
    87  	c.Assert(err, NotNil)
    88  
    89  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_min", []byte("AAA"), 0644)
    90  	_, err = Collect()
    91  	c.Assert(err, NotNil)
    92  
    93  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_input", []byte("AAA"), 0644)
    94  	_, err = Collect()
    95  	c.Assert(err, NotNil)
    96  
    97  	os.Remove(tmpDir + "/hwmon1/temp1_input")
    98  	os.Mkdir(tmpDir+"/hwmon1/temp1_input", 0644)
    99  	_, err = Collect()
   100  	c.Assert(err, NotNil)
   101  
   102  	os.Remove(tmpDir + "/hwmon1/temp1_label")
   103  	os.Mkdir(tmpDir+"/hwmon1/temp1_label", 0644)
   104  	_, err = Collect()
   105  	c.Assert(err, NotNil)
   106  
   107  	os.Remove(tmpDir + "/hwmon1/name")
   108  	os.Mkdir(tmpDir+"/hwmon1/name", 0644)
   109  	_, err = Collect()
   110  	c.Assert(err, NotNil)
   111  }
   112  
   113  // ////////////////////////////////////////////////////////////////////////////////// //
   114  
   115  func createFakeFS(tmpDir string) {
   116  	os.Mkdir(tmpDir+"/hwmon0", 0755)
   117  	os.Mkdir(tmpDir+"/hwmon1", 0755)
   118  	os.Mkdir(tmpDir+"/hwmon2", 0755)
   119  
   120  	ioutil.WriteFile(tmpDir+"/hwmon0/name", []byte("acpitz"), 0644)
   121  	ioutil.WriteFile(tmpDir+"/hwmon0/temp1_crit", []byte("25600"), 0644)
   122  	ioutil.WriteFile(tmpDir+"/hwmon0/temp1_input", []byte("8200"), 0644)
   123  
   124  	ioutil.WriteFile(tmpDir+"/hwmon1/name", []byte("coretemp"), 0644)
   125  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_label", []byte("Core 0"), 0644)
   126  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_crit", []byte("91000"), 0644)
   127  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_input", []byte("38000"), 0644)
   128  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_max", []byte("81000"), 0644)
   129  	ioutil.WriteFile(tmpDir+"/hwmon1/temp1_min", []byte("12000"), 0644)
   130  	ioutil.WriteFile(tmpDir+"/hwmon1/temp2_label", []byte("Core 0"), 0644)
   131  	ioutil.WriteFile(tmpDir+"/hwmon1/temp2_crit", []byte("91000"), 0644)
   132  	ioutil.WriteFile(tmpDir+"/hwmon1/temp2_input", []byte("41000"), 0644)
   133  	ioutil.WriteFile(tmpDir+"/hwmon1/temp2_max", []byte("81000"), 0644)
   134  	ioutil.WriteFile(tmpDir+"/hwmon1/temp2_min", []byte("12000"), 0644)
   135  }