This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
176 lines (138 loc) · 6.11 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import subprocess as sp
from dotenv import load_dotenv
from pytarallo.Tarallo import Tarallo, Item
from tarallo_interface import TaralloInterface
from pytarallo.Errors import *
import turbofresa
from smartctl_parser import parse_disks, SMART
from nose.plugins.skip import SkipTest
class Test_Tarallo:
"""Verify functioning of TaralloInterface"""
# !!!WARNING!!!
# Only run this on a test version of the TARALLO!!!
@classmethod
def setup_class(cls):
# Dummy disk used throughout all the tests
cls.dummy_disk = {
'brand': 'PYTHON_TEST',
'capacity-byte': 1,
'hdd-form-factor': '2.5-7mm',
'model': 'TEST',
'sata-ports-n': 1,
'smart-data': SMART.working.value,
'sn': 'USB123456',
'type': 'ssd',
}
# Load url and token from env
load_dotenv()
tarallo_url = os.getenv("TARALLO_URL")
tarallo_token = os.getenv("TARALLO_TOKEN")
try:
cls.tarallo_instance = Tarallo(tarallo_url, tarallo_token)
status = cls.tarallo_instance.status()
except:
cls.connected = False
else:
if status == 200:
cls.connected = True
else:
cls.connected = False
finally:
if cls.connected is False:
print("Couldn't connect to tarallo server")
else:
cls.tarallo_interface = TaralloInterface(cls.tarallo_instance)
def setup(self):
if not self.connected:
# Skip the test if we're not connected to TARALLO
raise SkipTest("Not connected to TARALLO")
else:
# Remove all possible duplicates of the dummy
codes = self.tarallo_instance.get_codes_by_feature('sn', self.dummy_disk['sn'])
for code in codes:
self.tarallo_instance.remove_item(code)
def test_add_disk(self):
"""Try adding disk to TARALLO"""
disk = self.dummy_disk
# Verify that we don't have any problem adding the disk
assert self.tarallo_interface.add_disk(disk) is True
# Verify that one and only one disk is being added
disk_code = self.tarallo_instance.get_codes_by_feature('sn', disk['sn'])
assert len(disk_code) == 1
def test_add_duplicate_no_conflict(self):
"""Try to add a non-conflicting duplicate"""
disk = self.dummy_disk
# Check that there are no duplicates
assert self.tarallo_interface.check_duplicate(disk) == 0, "TaralloInterface is detecting a duplicate"
# Manually add the dummy through pytarallo
item = Item()
item.features = disk
item.location = 'Polito'
try:
self.tarallo_instance.add_item(item=item)
print("Item inserted successfully")
except ValidationError:
print("Item not inserted")
response = self.tarallo_instance.response
print("HTTP status code:", response.status_code, "\n" + response.json()['message'])
raise AssertionError("Failed to manually add disk through pytarallo")
# Verify that check_duplicate doesn't detect a conflicting entry
assert self.tarallo_interface.check_duplicate(disk) == 1, "TaralloInterface is detecting a conflicting duplicate"
# Try to add the duplicate through TaralloInterface
assert self.tarallo_interface.add_disk(disk) is True, "Failed to add disk through TaralloInterface"
# Verify there's only one copy of the disk on TARALLO
disk_code = self.tarallo_instance.get_codes_by_feature('sn', disk['sn'])
assert len(disk_code) == 1, "Disk added by error"
def test_add_duplicate_conflict(self):
"""Try to add a conflicting duplicate"""
disk = self.dummy_disk
conflicting = dict(disk)
conflicting['model'] = "UNEXPECTED"
item = Item()
item.features = conflicting
item.location = 'Polito'
try:
self.tarallo_instance.add_item(item=item)
print("Item inserted successfully")
except ValidationError:
print("Item not inserted")
response = self.tarallo_instance.response
print("HTTP status code:", response.status_code, "\n" + response.json()['message'])
raise AssertionError("Failed to manually add disk to TARALLO")
# Check if conflict is detected
assert self.tarallo_interface.check_duplicate(disk) == -1, "TaralloInterface didn't detect the conflict"
# Check if TaralloInterface refuses to add the disk
assert self.tarallo_interface.add_disk(disk) is False, "TaralloInterface added the disk anyway"
def test_update_broken(self):
"""Update TARALLO with broken disk"""
disk = dict(self.dummy_disk)
self.tarallo_interface.add_disk(disk)
disk['smart-data'] = SMART.fail.value
# Verify the disk has been added even if duplicate
assert self.tarallo_interface.add_disk(disk) is True
class Test_Turbofresa:
"""Verify functioning of disk parser and TURBOFRESA"""
def test_smartctl_filegen(self):
import subprocess as sp
print('')
smartctl_path = os.path.join(os.getcwd(), "smartctl_test")
filegen = os.path.join(os.getcwd(), "smartctl_filegen.sh")
return_code = sp.run(["sudo", "-S", filegen, smartctl_path]).returncode
assert (return_code == 0), 'Error during disk detection'
sp.run(['sudo', '-S', 'rm', '-rf', smartctl_path])
def test_parser(self):
import sys
connected = set()
output = sp.check_output(["lsblk", "-nd", "-o", "NAME"]).decode(sys.stdout.encoding)
for line in output.splitlines():
connected.add(line)
disks = parse_disks(interactive=False, usbdebug=True)
assert len(connected) == len(disks)
for disk in disks:
assert disk["mount_point"] in connected
def test_ignore_sys_disks(self):
ignored = turbofresa.ignore_sys_disks()
assert len(ignored) > 0
for disk in ignored:
assert "sd" in disk