2022-11-21 02:22:26 +08:00
|
|
|
package system
|
2017-04-21 15:41:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2017-04-21 15:41:11 +08:00
|
|
|
)
|
|
|
|
|
2022-12-06 01:28:57 +08:00
|
|
|
var _ gobot.PWMPinner = (*pwmPinSysFs)(nil)
|
|
|
|
|
|
|
|
const (
|
|
|
|
normal = "normal"
|
2023-12-12 02:09:00 +08:00
|
|
|
inverted = "inversed"
|
2022-12-06 01:28:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func initTestPWMPinSysFsWithMockedFilesystem(mockPaths []string) (*pwmPinSysFs, *MockFilesystem) {
|
|
|
|
fs := newMockFilesystem(mockPaths)
|
2023-11-27 02:47:28 +08:00
|
|
|
sfa := &sysfsFileAccess{fs: fs, readBufLen: 200}
|
|
|
|
pin := newPWMPinSysfs(sfa, "/sys/class/pwm/pwmchip0", 10, normal, inverted)
|
2022-12-06 01:28:57 +08:00
|
|
|
return pin, fs
|
|
|
|
}
|
2017-04-25 16:43:58 +08:00
|
|
|
|
2017-04-21 15:41:11 +08:00
|
|
|
func TestPwmPin(t *testing.T) {
|
2022-11-05 14:42:28 +08:00
|
|
|
mockedPaths := []string{
|
2017-04-21 15:41:11 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
2017-04-22 05:06:45 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/polarity",
|
2022-11-05 14:42:28 +08:00
|
|
|
}
|
2022-12-06 01:28:57 +08:00
|
|
|
pin, fs := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths)
|
2017-04-21 15:41:11 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "10", pin.pin)
|
2017-04-21 15:41:11 +08:00
|
|
|
|
|
|
|
err := pin.Unexport()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "10", fs.Files["/sys/class/pwm/pwmchip0/unexport"].Contents)
|
2017-04-21 15:41:11 +08:00
|
|
|
|
|
|
|
err = pin.Export()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "10", fs.Files["/sys/class/pwm/pwmchip0/export"].Contents)
|
2017-04-21 15:41:11 +08:00
|
|
|
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, pin.SetPolarity(false))
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, inverted, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents)
|
2017-06-04 06:13:49 +08:00
|
|
|
pol, _ := pin.Polarity()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.False(t, pol)
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, pin.SetPolarity(true))
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, normal, fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].Contents)
|
2017-06-04 06:13:49 +08:00
|
|
|
pol, _ = pin.Polarity()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, pol)
|
2017-06-04 06:13:49 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotEqual(t, "1", fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents)
|
2022-12-06 01:28:57 +08:00
|
|
|
err = pin.SetEnabled(true)
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "1", fs.Files["/sys/class/pwm/pwmchip0/pwm10/enable"].Contents)
|
2022-12-06 01:28:57 +08:00
|
|
|
err = pin.SetPolarity(true)
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "Cannot set PWM polarity when enabled")
|
2017-04-21 15:41:11 +08:00
|
|
|
|
|
|
|
fs.Files["/sys/class/pwm/pwmchip0/pwm10/period"].Contents = "6"
|
|
|
|
data, _ := pin.Period()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, uint32(6), data)
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, pin.SetPeriod(100000))
|
2017-04-22 18:30:59 +08:00
|
|
|
data, _ = pin.Period()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, uint32(100000), data)
|
2017-04-21 16:05:59 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotEqual(t, "1", fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents)
|
2017-04-22 05:06:45 +08:00
|
|
|
err = pin.SetDutyCycle(100)
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "100", fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].Contents)
|
2017-04-22 18:30:59 +08:00
|
|
|
data, _ = pin.DutyCycle()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, uint32(100), data)
|
2017-04-21 15:41:11 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 16:35:03 +08:00
|
|
|
func TestPwmPinAlreadyExported(t *testing.T) {
|
2023-11-27 02:47:28 +08:00
|
|
|
// arrange
|
2022-11-05 14:42:28 +08:00
|
|
|
mockedPaths := []string{
|
2017-04-21 15:41:11 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
2022-11-05 14:42:28 +08:00
|
|
|
}
|
2023-11-27 02:47:28 +08:00
|
|
|
pin, fs := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths)
|
|
|
|
fs.Files["/sys/class/pwm/pwmchip0/export"].simulateWriteError = &os.PathError{Err: Syscall_EBUSY}
|
|
|
|
// act & assert: no error indicates that the pin was already exported
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, pin.Export())
|
2017-05-03 16:35:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPwmPinExportError(t *testing.T) {
|
2023-11-27 02:47:28 +08:00
|
|
|
// arrange
|
2022-11-05 14:42:28 +08:00
|
|
|
mockedPaths := []string{
|
2017-05-03 16:35:03 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
2022-11-05 14:42:28 +08:00
|
|
|
}
|
2023-11-27 02:47:28 +08:00
|
|
|
pin, fs := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths)
|
|
|
|
fs.Files["/sys/class/pwm/pwmchip0/export"].simulateWriteError = &os.PathError{Err: Syscall_EFAULT}
|
|
|
|
// act
|
2022-11-05 14:42:28 +08:00
|
|
|
err := pin.Export()
|
2023-11-27 02:47:28 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "Export() failed for id 10 with : bad address")
|
2017-04-21 15:41:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPwmPinUnxportError(t *testing.T) {
|
2023-11-27 02:47:28 +08:00
|
|
|
// arrange
|
2022-11-05 14:42:28 +08:00
|
|
|
mockedPaths := []string{
|
2017-04-21 15:41:11 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
2022-11-05 14:42:28 +08:00
|
|
|
}
|
2023-11-27 02:47:28 +08:00
|
|
|
pin, fs := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths)
|
|
|
|
fs.Files["/sys/class/pwm/pwmchip0/unexport"].simulateWriteError = &os.PathError{Err: Syscall_EBUSY}
|
|
|
|
// act
|
2022-11-05 14:42:28 +08:00
|
|
|
err := pin.Unexport()
|
2023-11-27 02:47:28 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "Unexport() failed for id 10 with : device or resource busy")
|
2017-04-21 15:41:11 +08:00
|
|
|
}
|
2017-04-21 16:05:59 +08:00
|
|
|
|
|
|
|
func TestPwmPinPeriodError(t *testing.T) {
|
2023-11-27 02:47:28 +08:00
|
|
|
// arrange
|
2022-11-05 14:42:28 +08:00
|
|
|
mockedPaths := []string{
|
2017-04-21 16:05:59 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
2022-11-05 14:42:28 +08:00
|
|
|
}
|
2023-11-27 02:47:28 +08:00
|
|
|
pin, fs := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths)
|
|
|
|
fs.Files["/sys/class/pwm/pwmchip0/pwm10/period"].simulateReadError = &os.PathError{Err: Syscall_EBUSY}
|
|
|
|
// act
|
2017-04-21 16:05:59 +08:00
|
|
|
_, err := pin.Period()
|
2023-11-27 02:47:28 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "Period() failed for id 10 with : device or resource busy")
|
2017-04-21 16:05:59 +08:00
|
|
|
}
|
2017-04-25 18:41:57 +08:00
|
|
|
|
|
|
|
func TestPwmPinPolarityError(t *testing.T) {
|
2023-11-27 02:47:28 +08:00
|
|
|
// arrange
|
2022-11-05 14:42:28 +08:00
|
|
|
mockedPaths := []string{
|
2017-04-25 18:41:57 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
2023-11-27 02:47:28 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/polarity",
|
2022-11-05 14:42:28 +08:00
|
|
|
}
|
2023-11-27 02:47:28 +08:00
|
|
|
pin, fs := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths)
|
|
|
|
fs.Files["/sys/class/pwm/pwmchip0/pwm10/polarity"].simulateReadError = &os.PathError{Err: Syscall_EBUSY}
|
|
|
|
// act
|
2017-04-25 18:41:57 +08:00
|
|
|
_, err := pin.Polarity()
|
2023-11-27 02:47:28 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "Polarity() failed for id 10 with : device or resource busy")
|
2017-04-25 18:41:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPwmPinDutyCycleError(t *testing.T) {
|
2023-11-27 02:47:28 +08:00
|
|
|
// arrange
|
2022-11-05 14:42:28 +08:00
|
|
|
mockedPaths := []string{
|
2017-04-25 18:41:57 +08:00
|
|
|
"/sys/class/pwm/pwmchip0/export",
|
|
|
|
"/sys/class/pwm/pwmchip0/unexport",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/enable",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/period",
|
|
|
|
"/sys/class/pwm/pwmchip0/pwm10/duty_cycle",
|
2022-11-05 14:42:28 +08:00
|
|
|
}
|
2023-11-27 02:47:28 +08:00
|
|
|
pin, fs := initTestPWMPinSysFsWithMockedFilesystem(mockedPaths)
|
|
|
|
fs.Files["/sys/class/pwm/pwmchip0/pwm10/duty_cycle"].simulateReadError = &os.PathError{Err: Syscall_EBUSY}
|
|
|
|
// act
|
2017-04-25 18:41:57 +08:00
|
|
|
_, err := pin.DutyCycle()
|
2023-11-27 02:47:28 +08:00
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, err, "DutyCycle() failed for id 10 with : device or resource busy")
|
2017-04-25 18:41:57 +08:00
|
|
|
}
|