Skip to content

Commit f0ffa6a

Browse files
Fix some issues per Pierre's comments
1 parent b591742 commit f0ffa6a

File tree

7 files changed

+91
-84
lines changed

7 files changed

+91
-84
lines changed

providers/base/bin/wol_check.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,16 @@ def get_system_boot_time():
6565
Read btime from /proc/stat and
6666
return the system boot timestamp (Unix timestamp, in seconds).
6767
"""
68-
try:
69-
with open("/proc/stat", "r") as f:
70-
for line in f:
71-
if line.startswith("btime"):
72-
btime = float(line.split()[1])
73-
back_time = datetime.datetime.fromtimestamp(btime)
74-
logging.debug("System back time: {}".format(back_time))
75-
return btime
76-
logging.error("cannot find btime")
77-
return None
78-
except FileNotFoundError:
79-
logging.error("cannot open /proc/stat.")
80-
return None
81-
except Exception as e:
82-
logging.error("error while read btime: {}".format(e))
83-
return None
68+
69+
with open("/proc/stat", "r") as f:
70+
for line in f:
71+
if line.startswith("btime"):
72+
btime = int(line.split()[1])
73+
back_time = datetime.datetime.fromtimestamp(btime)
74+
logging.debug("System back time: {}".format(back_time))
75+
return btime
76+
logging.error("cannot find btime")
77+
return None
8478

8579

8680
def parse_args(args=sys.argv[1:]):

providers/base/bin/wol_client.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def send_request_to_wol_server(url, data=None, retry=3):
5151
logging.debug("Status code: {}".format(status_code))
5252
if status_code == 200:
5353
logging.info(
54-
"Send request to Wake-on-lan server successful."
54+
"Request to Wake-on-LAN server sent successfully."
5555
)
5656
return
5757
else:
5858
logging.error(
59-
"Failded to send request to Wkae-on-lan server."
59+
"Failed to send request to Wake-on-LAN server."
6060
)
6161
except Exception as e:
6262
logging.error("An unexpected error occurred: {}".format(e))
@@ -96,7 +96,7 @@ def check_wakeup(interface):
9696
)
9797

9898

99-
def __get_ip_address(interface):
99+
def get_ip_address(interface):
100100
try:
101101
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
102102
ip_addr = fcntl.ioctl(
@@ -109,7 +109,7 @@ def __get_ip_address(interface):
109109
return None
110110

111111

112-
def __get_mac_address(interface):
112+
def get_mac_address(interface):
113113
try:
114114
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
115115
mac_addr = fcntl.ioctl(
@@ -122,13 +122,6 @@ def __get_mac_address(interface):
122122
raise SystemExit("Error: Unable to retrieve MAC address")
123123

124124

125-
def get_ip_mac(interface):
126-
ip_a = __get_ip_address(interface)
127-
mac_a = __get_mac_address(interface)
128-
129-
return ip_a, mac_a
130-
131-
132125
# set the rtc wake time to bring up system in case the wake-on-lan failed
133126
def set_rtc_wake(wake_time):
134127
"""
@@ -250,7 +243,8 @@ def main():
250243
delay = args.delay
251244
retry = args.retry
252245

253-
ip, mac = get_ip_mac(args.interface)
246+
ip = get_ip_address(args.interface)
247+
mac = get_mac_address(args.interface)
254248

255249
logging.info("IP: {}, MAC: {}".format(ip, mac))
256250

@@ -266,7 +260,7 @@ def main():
266260
"wake_type": args.waketype,
267261
}
268262

269-
send_request_to_wol_server(url, data=req, retry=3)
263+
send_request_to_wol_server(url, data=req, retry=retry)
270264

271265
bring_up_system("rtc", delay * retry * 2)
272266

providers/base/tests/test_wol_check.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,6 @@ def test_get_system_boot_time_no_btime(self, mock_file):
7878
self.assertIsNone(boot_time)
7979
self.assertIn("cannot find btime", log.output[0])
8080

81-
@patch("builtins.open", side_effect=FileNotFoundError)
82-
def test_get_system_boot_time_file_not_found(self, mock_file):
83-
with self.assertLogs(level="ERROR") as log:
84-
boot_time = get_system_boot_time()
85-
self.assertIsNone(boot_time)
86-
self.assertIn("cannot open /proc/stat.", log.output[0])
87-
88-
@patch("builtins.open", side_effect=Exception("some error"))
89-
def test_get_system_boot_time_exception(self, mock_file):
90-
with self.assertLogs(level="ERROR") as log:
91-
boot_time = get_system_boot_time()
92-
self.assertIsNone(boot_time)
93-
self.assertIn("error while read btime: some error", log.output[0])
94-
9581

9682
class TestGetWakeupTimestamp(unittest.TestCase):
9783

providers/base/tests/test_wol_client.py

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
from wol_client import (
2727
send_request_to_wol_server,
2828
check_wakeup,
29-
get_ip_mac,
29+
get_ip_address,
30+
get_mac_address,
3031
set_rtc_wake,
3132
s3_or_s5_system,
3233
bring_up_system,
@@ -37,7 +38,6 @@
3738

3839

3940
class TestSendRequestToWolServerFunction(unittest.TestCase):
40-
4141
@patch("urllib.request.urlopen")
4242
def test_send_request_success(self, mock_urlopen):
4343
mock_response = MagicMock()
@@ -137,14 +137,13 @@ def test_unexpected_error(self, mock_file):
137137
self.assertEqual(str(context.exception), "Unexpected error")
138138

139139

140-
class TestGetIPMac(unittest.TestCase):
140+
class TestGetIPAddress(unittest.TestCase):
141141
@patch("socket.socket")
142142
@patch("fcntl.ioctl")
143-
def test_get_ip_mac_success(self, mock_ioctl, mock_socket):
143+
def test_get_ip_success(self, mock_ioctl, mock_socket):
144144
# Mock data
145145
interface = "eth0"
146146
mock_ip = b"\xc0\xa8\x00\x01" # 192.168.0.1
147-
mock_mac = b"\x00\x0c)\x85\xac\x0e" # 00:0c:29:85:ac:0e
148147

149148
# Configure the mock objects
150149
mock_socket_instance = MagicMock()
@@ -153,60 +152,78 @@ def test_get_ip_mac_success(self, mock_ioctl, mock_socket):
153152
def ioctl_side_effect(fd, request, arg):
154153
if request == 0x8915:
155154
return b"\x00" * 20 + mock_ip + b"\x00" * (256 - 24)
156-
elif request == 0x8927:
157-
return b"\x00" * 18 + mock_mac + b"\x00" * (256 - 24)
158-
# raise IOError("Invalid request")
155+
raise IOError("Invalid request")
159156

160157
mock_ioctl.side_effect = ioctl_side_effect
161158

162-
ip_address, mac_address = get_ip_mac(interface)
159+
ip_address = get_ip_address(interface)
163160

164161
self.assertEqual(ip_address, "192.168.0.1")
165-
self.assertEqual(mac_address, "00:0c:29:85:ac:0e")
166162

167163
@patch("socket.socket")
168164
@patch("fcntl.ioctl")
169165
def test_get_ip_address_failure(self, mock_ioctl, mock_socket):
170166
# Mock data
171167
interface = "eth0"
172-
mock_mac = b"\x00\x0c)\x85\xac\x0e" # 00:0c:29:85:ac:0e
173168

174169
mock_socket_instance = MagicMock()
175170
mock_socket.return_value = mock_socket_instance
176171

177172
def ioctl_side_effect(fd, request, arg):
178173
if request == 0x8915:
179174
raise IOError("IP address retrieval failed")
180-
elif request == 0x8927:
181-
# return struct.pack('256s', b'\x00' * 18) + mock_mac
175+
176+
mock_ioctl.side_effect = ioctl_side_effect
177+
178+
ip_address = get_ip_address(interface)
179+
self.assertIsNone(ip_address)
180+
181+
182+
class TestGetMACAddress(unittest.TestCase):
183+
@patch("socket.socket")
184+
@patch("fcntl.ioctl")
185+
def test_get_mac_success(self, mock_ioctl, mock_socket):
186+
# Mock data
187+
interface = "eth0"
188+
mock_mac = b"\x00\x0c\x29\x85\xac\x0e" # 00:0c:29:85:ac:0e
189+
190+
# Configure the mock objects
191+
mock_socket_instance = MagicMock()
192+
mock_socket.return_value = mock_socket_instance
193+
194+
def ioctl_side_effect(fd, request, arg):
195+
if request == 0x8927:
182196
return b"\x00" * 18 + mock_mac + b"\x00" * (256 - 24)
183197

198+
raise IOError("Invalid request")
199+
184200
mock_ioctl.side_effect = ioctl_side_effect
185201

186-
ip_address, mac_address = get_ip_mac(interface)
202+
mac_address = get_mac_address(interface)
187203

188-
self.assertIsNone(ip_address)
189204
self.assertEqual(mac_address, "00:0c:29:85:ac:0e")
190205

191206
@patch("socket.socket")
192207
@patch("fcntl.ioctl")
193208
def test_get_mac_address_failure(self, mock_ioctl, mock_socket):
209+
# Mock data
194210
interface = "eth0"
195-
mock_ip = b"\xc0\xa8\x00\x01" # 192.168.0.1
196211

197212
mock_socket_instance = MagicMock()
198213
mock_socket.return_value = mock_socket_instance
199214

200215
def ioctl_side_effect(fd, request, arg):
201-
if request == 0x8915:
202-
return struct.pack("256s", b"\x00" * 16) + mock_ip
203-
elif request == 0x8927:
216+
if request == 0x8927:
204217
raise IOError("MAC address retrieval failed")
205218

206219
mock_ioctl.side_effect = ioctl_side_effect
207220

208-
with self.assertRaises(SystemExit):
209-
get_ip_mac(interface)
221+
with self.assertRaises(SystemExit) as cm:
222+
get_mac_address(interface)
223+
224+
self.assertEqual(
225+
cm.exception.code, "Error: Unable to retrieve MAC address"
226+
)
210227

211228

212229
class TestSetRTCWake(unittest.TestCase):
@@ -371,26 +388,32 @@ class TestMainFunction(unittest.TestCase):
371388
@patch("wol_client.bring_up_system")
372389
@patch("wol_client.send_request_to_wol_server")
373390
@patch("wol_client.check_wakeup")
374-
@patch("wol_client.get_ip_mac")
391+
@patch("wol_client.get_ip_address")
392+
@patch("wol_client.get_mac_address")
375393
@patch("wol_client.parse_args")
376394
def test_main_success(
377395
self,
378396
mock_parse_args,
379-
mock_get_ip_mac,
397+
mock_get_mac_address,
398+
mock_get_ip_address,
380399
mock_check_wakeup,
381400
mock_send_request_to_wol_server,
382401
mock_bring_up_system,
383402
mock_write_timestamp,
384403
mock_s3_or_s5_system,
385404
):
386405
mock_parse_args.return_value = create_mock_args()
387-
mock_get_ip_mac.return_value = ("192.168.1.100", "00:11:22:33:44:55")
406+
mock_get_ip_address.return_value = "192.168.1.100"
407+
mock_get_mac_address.return_value = "00:11:22:33:44:55"
408+
mock_bring_up_system.return_value = None
409+
388410
mock_check_wakeup.return_value = True
389411
mock_send_request_to_wol_server.return_value = create_mock_response()
390412

391413
main()
392414

393-
mock_get_ip_mac.assert_called_once_with("eth0")
415+
mock_get_ip_address.assert_called_once_with("eth0")
416+
mock_get_mac_address.assert_called_once_with("eth0")
394417
mock_send_request_to_wol_server.assert_called_once_with(
395418
"http://192.168.1.1",
396419
data={
@@ -407,18 +430,25 @@ def test_main_success(
407430
mock_s3_or_s5_system.assert_called_once_with("s3")
408431

409432
@patch("wol_client.send_request_to_wol_server")
410-
@patch("wol_client.get_ip_mac")
433+
@patch("wol_client.bring_up_system")
434+
@patch("wol_client.get_ip_address")
435+
@patch("wol_client.get_mac_address")
411436
@patch("wol_client.check_wakeup")
412437
@patch("wol_client.parse_args")
413438
def test_main_ip_none(
414439
self,
415440
mock_parse_args,
416441
mock_check_wakeup,
417-
mock_get_ip_mac,
442+
mock_bring_up_system,
443+
mock_get_ip_address,
444+
mock_get_mac_address,
418445
mock_send_request_to_wol_server,
419446
):
420447
mock_parse_args.return_value = create_mock_args()
421-
mock_get_ip_mac.return_value = (None, "00:11:22:33:44:55")
448+
449+
mock_get_ip_address.return_value = None
450+
mock_get_mac_address.return_value = "00:11:22:33:44:55"
451+
mock_bring_up_system.return_value = None
422452
mock_check_wakeup.return_value = True
423453

424454
with self.assertRaises(SystemExit) as cm:
@@ -428,19 +458,22 @@ def test_main_ip_none(
428458
)
429459

430460
@patch("wol_client.send_request_to_wol_server")
431-
@patch("wol_client.get_ip_mac")
461+
@patch("wol_client.get_ip_address")
462+
@patch("wol_client.get_mac_address")
432463
@patch("wol_client.check_wakeup")
433464
@patch("wol_client.parse_args")
434465
def test_main_checkwakeup_disable(
435466
self,
436467
mock_parse_args,
437468
mock_check_wakeup,
438-
mock_get_ip_mac,
469+
mock_get_ip_address,
470+
mock_get_mac_address,
439471
mock_send_request_to_wol_server,
440472
):
441473
mock_parse_args.return_value = create_mock_args()
442474
mock_check_wakeup.return_value = False
443-
mock_get_ip_mac.return_value = ("192.168.1.100", "00:11:22:33:44:55")
475+
mock_get_ip_address.return_value = "192.168.1.100"
476+
mock_get_mac_address.return_value = "00:11:22:33:44:55"
444477
mock_send_request_to_wol_server.return_value = create_mock_response()
445478

446479
with self.assertRaises(SystemExit) as cm:

providers/base/units/ethernet/jobs.pxu

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,26 +440,25 @@ _summary: Network reconnect resume test (wired)
440440
_purpose:
441441
Checks the length of time it takes to reconnect an existing wired connection after a suspend/resume cycle.
442442

443-
id: ethernet/wol_auto_S3_{{ interface }}
443+
id: ethernet/wol_auto_S3_{ interface }
444444
template-id: ethernet/wol_auto_S3_interface
445445
category_id: com.canonical.plainbox::ethernet
446446
unit: template
447447
template-resource: device
448-
template-engine: jinja2
449448
template-filter: device.category == 'NETWORK' and device.mac != 'UNKNOWN'
450-
_summary: Wake on LAN (WOL) automatic test from S3 - {{ interface }}
449+
_summary: Wake on LAN (WOL) automatic test from S3 - { interface }
451450
plugin: shell
452451
environ: SERVER_WAKE_ON_LAN WAKE_ON_LAN_DELAY WAKE_ON_LAN_RETRY PLAINBOX_SESSION_SHARE
453452
imports: from com.canonical.plainbox import manifest
454453
requires:
455454
manifest.has_ethernet_adapter == 'True'
456455
manifest.has_ethernet_wake_on_lan_support == 'True'
457-
manifest.has_wake_on_lan_server == 'True'
456+
manifest.wake_on_lan_server_running == 'True'
458457
command:
459458
set -e
460-
wol_client.py --interface {{ interface }} --target "$SERVER_WAKE_ON_LAN" --delay "$WAKE_ON_LAN_DELAY" --retry "$WAKE_ON_LAN_RETRY" --waketype g --powertype s3 --timestamp_file "$PLAINBOX_SESSION_SHARE"/{{ interface }}_s3_timestamp
459+
wol_client.py --interface { interface } --target "$SERVER_WAKE_ON_LAN" --delay "$WAKE_ON_LAN_DELAY" --retry "$WAKE_ON_LAN_RETRY" --waketype g --powertype s3 --timestamp_file "$PLAINBOX_SESSION_SHARE"/{ interface }_s3_timestamp
461460
sleep 30
462-
wol_check.py --delay "$WAKE_ON_LAN_DELAY" --retry "$WAKE_ON_LAN_RETRY" --powertype s3 --timestamp_file "$PLAINBOX_SESSION_SHARE"/{{ interface }}_s3_timestamp
461+
wol_check.py --delay "$WAKE_ON_LAN_DELAY" --retry "$WAKE_ON_LAN_RETRY" --powertype s3 --timestamp_file "$PLAINBOX_SESSION_SHARE"/{ interface }_s3_timestamp
463462
user: root
464463
estimated_duration: 600
465464
flags: preserve-locale

providers/base/units/ethernet/manifest.pxu

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ _name: Wake-on-LAN support through Ethernet port
99
value-type: bool
1010

1111
unit: manifest entry
12-
id: has_wake_on_lan_server
13-
_name: Has wake-on-Lan server for automation setup and running
12+
id: wake_on_lan_server_running
13+
_prompt: Is the following server setup and running?:
14+
_name: A Wake-on-LAN server for automation
1415
value-type: bool
1516

1617
unit: manifest entry

0 commit comments

Comments
 (0)