| 1 |
import pytest |
| 2 |
|
| 3 |
import os |
| 4 |
from labgrid.util.agents.sysfsgpio import GpioDigitalOutput |
| 5 |
from tempfile import TemporaryDirectory |
| 6 |
|
| 7 |
class TestGpioAgent: |
| 8 |
|
| 9 |
class GpioDigitalOutputMock(GpioDigitalOutput): |
| 10 |
def __init__(self, **kwargs): |
| 11 |
index = kwargs['index'] |
| 12 |
self.sysfs_mock_directory = TemporaryDirectory() |
| 13 |
GpioDigitalOutput._gpio_sysfs_path_prefix = self.sysfs_mock_directory.name |
| 14 |
GpioDigitalOutput._buffered_file_access = True |
| 15 |
export_file_path = os.path.join(self.sysfs_mock_directory.name, 'export') |
| 16 |
os.mknod(export_file_path) |
| 17 |
|
| 18 |
|
| 19 |
with pytest.raises(ValueError, match='Device not found'): |
| 20 |
super().__init__(**kwargs) |
| 21 |
with open(export_file_path, mode='r') as export_file: |
| 22 |
export_content = export_file.readline() |
| 23 |
assert export_content == str(index) |
| 24 |
self.gpio_line_directory = os.path.join(self.sysfs_mock_directory.name, f'gpio{index}') |
| 25 |
os.mkdir(self.gpio_line_directory) |
| 26 |
for control_file in ['direction', 'value']: |
| 27 |
control_file_path = os.path.join(self.gpio_line_directory, control_file) |
| 28 |
print(f'creating control file `{control_file_path}`') |
| 29 |
os.mknod(control_file_path) |
| 30 |
super().__init__(**kwargs) |
| 31 |
|
| 32 |
def set(self, val): |
| 33 |
|
| 34 |
|
| 35 |
os.lseek(self.gpio_sysfs_value_fd, 0, os.SEEK_SET) |
| 36 |
super().set(val) |
| 37 |
|
| 38 |
def test_instantiation(self): |
| 39 |
gpio_line = TestGpioAgent.GpioDigitalOutputMock(index=13) |
| 40 |
assert isinstance(gpio_line, GpioDigitalOutput) |
| 41 |
|
| 42 |
def test_set(self): |
| 43 |
gpio_line = TestGpioAgent.GpioDigitalOutputMock(index=13) |
| 44 |
for val in [True, False, True, False]: |
| 45 |
gpio_line.set(val) |
| 46 |
assert gpio_line.get() == val |