gobot.io/x/gobot/v2@v2.1.0/system/pwmpin_sysfs_test.go (about) 1 package system 2 3 import ( 4 "os" 5 "syscall" 6 "testing" 7 8 "gobot.io/x/gobot/v2" 9 "gobot.io/x/gobot/v2/gobottest" 10 ) 11 12 var _ gobot.PWMPinner = (*pwmPinSysFs)(nil) 13 14 const ( 15 normal = "normal" 16 inverted = "inverted" 17 ) 18 19 func initTestPWMPinSysFsWithMockedFilesystem(mockPaths []string) (*pwmPinSysFs, *MockFilesystem) { 20 fs := newMockFilesystem(mockPaths) 21 pin := newPWMPinSysfs(fs, "/sys/class/pwm/pwmchip0", 10, normal, inverted) 22 return pin, fs 23 } 24 25 func TestPwmPin(t *testing.T) { 26 mockedPaths := []string{ 27 "/sys/class/pwm/pwmchip0/export", 28 "/sys/class/pwm/pwmchip0/unexport", 29 "/sys/class/pwm/pwmchip0/pwm10/enable", 30 "/sys/class/pwm/pwmchip0/pwm10/period", 31 "/sys/class/pwm/pwmchip0/pwm10/duty_cycle", 32 "/sys/class/pwm/pwmchip0/pwm10/polarity", 33 } 34 pin, fs := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths) 35 36 gobottest.Assert(t, pin.pin, "10") 37 38 err := pin.Unexport() 39 gobottest.Assert(t, err, nil) 40 gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/unexport"].Contents, "10") 41 42 err = pin.Export() 43 gobottest.Assert(t, err, nil) 44 gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/export"].Contents, "10") 45 46 gobottest.Assert(t, pin.SetPolarity(false), nil) 47 gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, inverted) 48 pol, _ := pin.Polarity() 49 gobottest.Assert(t, pol, false) 50 gobottest.Assert(t, pin.SetPolarity(true), nil) 51 gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents, normal) 52 pol, _ = pin.Polarity() 53 gobottest.Assert(t, pol, true) 54 55 gobottest.Refute(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1") 56 err = pin.SetEnabled(true) 57 gobottest.Assert(t, err, nil) 58 gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents, "1") 59 err = pin.SetPolarity(true) 60 gobottest.Assert(t, err.Error(), "Cannot set PWM polarity when enabled") 61 62 fs.Files["/sys/class/pwm/pwmchip0/pwm10/period"].Contents = "6" 63 data, _ := pin.Period() 64 gobottest.Assert(t, data, uint32(6)) 65 gobottest.Assert(t, pin.SetPeriod(100000), nil) 66 data, _ = pin.Period() 67 gobottest.Assert(t, data, uint32(100000)) 68 69 gobottest.Refute(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "1") 70 err = pin.SetDutyCycle(100) 71 gobottest.Assert(t, err, nil) 72 gobottest.Assert(t, fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents, "100") 73 data, _ = pin.DutyCycle() 74 gobottest.Assert(t, data, uint32(100)) 75 } 76 77 func TestPwmPinAlreadyExported(t *testing.T) { 78 mockedPaths := []string{ 79 "/sys/class/pwm/pwmchip0/export", 80 "/sys/class/pwm/pwmchip0/unexport", 81 "/sys/class/pwm/pwmchip0/pwm10/enable", 82 "/sys/class/pwm/pwmchip0/pwm10/period", 83 "/sys/class/pwm/pwmchip0/pwm10/duty_cycle", 84 } 85 pin, _ := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths) 86 87 pin.write = func(filesystem, string, []byte) (int, error) { 88 return 0, &os.PathError{Err: syscall.EBUSY} 89 } 90 91 // no error indicates that the pin was already exported 92 gobottest.Assert(t, pin.Export(), nil) 93 } 94 95 func TestPwmPinExportError(t *testing.T) { 96 mockedPaths := []string{ 97 "/sys/class/pwm/pwmchip0/export", 98 "/sys/class/pwm/pwmchip0/unexport", 99 "/sys/class/pwm/pwmchip0/pwm10/enable", 100 "/sys/class/pwm/pwmchip0/pwm10/period", 101 "/sys/class/pwm/pwmchip0/pwm10/duty_cycle", 102 } 103 pin, _ := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths) 104 105 pin.write = func(filesystem, string, []byte) (int, error) { 106 return 0, &os.PathError{Err: syscall.EFAULT} 107 } 108 109 // no error indicates that the pin was already exported 110 err := pin.Export() 111 gobottest.Assert(t, err.Error(), "Export() failed for id 10 with : bad address") 112 } 113 114 func TestPwmPinUnxportError(t *testing.T) { 115 mockedPaths := []string{ 116 "/sys/class/pwm/pwmchip0/export", 117 "/sys/class/pwm/pwmchip0/unexport", 118 "/sys/class/pwm/pwmchip0/pwm10/enable", 119 "/sys/class/pwm/pwmchip0/pwm10/period", 120 "/sys/class/pwm/pwmchip0/pwm10/duty_cycle", 121 } 122 pin, _ := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths) 123 124 pin.write = func(filesystem, string, []byte) (int, error) { 125 return 0, &os.PathError{Err: syscall.EBUSY} 126 } 127 128 err := pin.Unexport() 129 gobottest.Assert(t, err.Error(), "Unexport() failed for id 10 with : device or resource busy") 130 } 131 132 func TestPwmPinPeriodError(t *testing.T) { 133 mockedPaths := []string{ 134 "/sys/class/pwm/pwmchip0/export", 135 "/sys/class/pwm/pwmchip0/unexport", 136 "/sys/class/pwm/pwmchip0/pwm10/enable", 137 "/sys/class/pwm/pwmchip0/pwm10/period", 138 "/sys/class/pwm/pwmchip0/pwm10/duty_cycle", 139 } 140 pin, _ := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths) 141 142 pin.read = func(filesystem, string) ([]byte, error) { 143 return nil, &os.PathError{Err: syscall.EBUSY} 144 } 145 146 _, err := pin.Period() 147 gobottest.Assert(t, err.Error(), "Period() failed for id 10 with : device or resource busy") 148 } 149 150 func TestPwmPinPolarityError(t *testing.T) { 151 mockedPaths := []string{ 152 "/sys/class/pwm/pwmchip0/export", 153 "/sys/class/pwm/pwmchip0/unexport", 154 "/sys/class/pwm/pwmchip0/pwm10/enable", 155 "/sys/class/pwm/pwmchip0/pwm10/period", 156 "/sys/class/pwm/pwmchip0/pwm10/duty_cycle", 157 } 158 pin, _ := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths) 159 160 pin.read = func(filesystem, string) ([]byte, error) { 161 return nil, &os.PathError{Err: syscall.EBUSY} 162 } 163 164 _, err := pin.Polarity() 165 gobottest.Assert(t, err.Error(), "Polarity() failed for id 10 with : device or resource busy") 166 } 167 168 func TestPwmPinDutyCycleError(t *testing.T) { 169 mockedPaths := []string{ 170 "/sys/class/pwm/pwmchip0/export", 171 "/sys/class/pwm/pwmchip0/unexport", 172 "/sys/class/pwm/pwmchip0/pwm10/enable", 173 "/sys/class/pwm/pwmchip0/pwm10/period", 174 "/sys/class/pwm/pwmchip0/pwm10/duty_cycle", 175 } 176 pin, _ := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths) 177 178 pin.read = func(filesystem, string) ([]byte, error) { 179 return nil, &os.PathError{Err: syscall.EBUSY} 180 } 181 182 _, err := pin.DutyCycle() 183 gobottest.Assert(t, err.Error(), "DutyCycle() failed for id 10 with : device or resource busy") 184 }