26
26
from wol_client import (
27
27
send_request_to_wol_server ,
28
28
check_wakeup ,
29
- get_ip_mac ,
29
+ get_ip_address ,
30
+ get_mac_address ,
30
31
set_rtc_wake ,
31
32
s3_or_s5_system ,
32
33
bring_up_system ,
37
38
38
39
39
40
class TestSendRequestToWolServerFunction (unittest .TestCase ):
40
-
41
41
@patch ("urllib.request.urlopen" )
42
42
def test_send_request_success (self , mock_urlopen ):
43
43
mock_response = MagicMock ()
@@ -137,14 +137,13 @@ def test_unexpected_error(self, mock_file):
137
137
self .assertEqual (str (context .exception ), "Unexpected error" )
138
138
139
139
140
- class TestGetIPMac (unittest .TestCase ):
140
+ class TestGetIPAddress (unittest .TestCase ):
141
141
@patch ("socket.socket" )
142
142
@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 ):
144
144
# Mock data
145
145
interface = "eth0"
146
146
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
148
147
149
148
# Configure the mock objects
150
149
mock_socket_instance = MagicMock ()
@@ -153,60 +152,78 @@ def test_get_ip_mac_success(self, mock_ioctl, mock_socket):
153
152
def ioctl_side_effect (fd , request , arg ):
154
153
if request == 0x8915 :
155
154
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" )
159
156
160
157
mock_ioctl .side_effect = ioctl_side_effect
161
158
162
- ip_address , mac_address = get_ip_mac (interface )
159
+ ip_address = get_ip_address (interface )
163
160
164
161
self .assertEqual (ip_address , "192.168.0.1" )
165
- self .assertEqual (mac_address , "00:0c:29:85:ac:0e" )
166
162
167
163
@patch ("socket.socket" )
168
164
@patch ("fcntl.ioctl" )
169
165
def test_get_ip_address_failure (self , mock_ioctl , mock_socket ):
170
166
# Mock data
171
167
interface = "eth0"
172
- mock_mac = b"\x00 \x0c )\x85 \xac \x0e " # 00:0c:29:85:ac:0e
173
168
174
169
mock_socket_instance = MagicMock ()
175
170
mock_socket .return_value = mock_socket_instance
176
171
177
172
def ioctl_side_effect (fd , request , arg ):
178
173
if request == 0x8915 :
179
174
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 :
182
196
return b"\x00 " * 18 + mock_mac + b"\x00 " * (256 - 24 )
183
197
198
+ raise IOError ("Invalid request" )
199
+
184
200
mock_ioctl .side_effect = ioctl_side_effect
185
201
186
- ip_address , mac_address = get_ip_mac (interface )
202
+ mac_address = get_mac_address (interface )
187
203
188
- self .assertIsNone (ip_address )
189
204
self .assertEqual (mac_address , "00:0c:29:85:ac:0e" )
190
205
191
206
@patch ("socket.socket" )
192
207
@patch ("fcntl.ioctl" )
193
208
def test_get_mac_address_failure (self , mock_ioctl , mock_socket ):
209
+ # Mock data
194
210
interface = "eth0"
195
- mock_ip = b"\xc0 \xa8 \x00 \x01 " # 192.168.0.1
196
211
197
212
mock_socket_instance = MagicMock ()
198
213
mock_socket .return_value = mock_socket_instance
199
214
200
215
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 :
204
217
raise IOError ("MAC address retrieval failed" )
205
218
206
219
mock_ioctl .side_effect = ioctl_side_effect
207
220
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
+ )
210
227
211
228
212
229
class TestSetRTCWake (unittest .TestCase ):
@@ -371,26 +388,32 @@ class TestMainFunction(unittest.TestCase):
371
388
@patch ("wol_client.bring_up_system" )
372
389
@patch ("wol_client.send_request_to_wol_server" )
373
390
@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" )
375
393
@patch ("wol_client.parse_args" )
376
394
def test_main_success (
377
395
self ,
378
396
mock_parse_args ,
379
- mock_get_ip_mac ,
397
+ mock_get_mac_address ,
398
+ mock_get_ip_address ,
380
399
mock_check_wakeup ,
381
400
mock_send_request_to_wol_server ,
382
401
mock_bring_up_system ,
383
402
mock_write_timestamp ,
384
403
mock_s3_or_s5_system ,
385
404
):
386
405
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
+
388
410
mock_check_wakeup .return_value = True
389
411
mock_send_request_to_wol_server .return_value = create_mock_response ()
390
412
391
413
main ()
392
414
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" )
394
417
mock_send_request_to_wol_server .assert_called_once_with (
395
418
"http://192.168.1.1" ,
396
419
data = {
@@ -407,18 +430,25 @@ def test_main_success(
407
430
mock_s3_or_s5_system .assert_called_once_with ("s3" )
408
431
409
432
@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" )
411
436
@patch ("wol_client.check_wakeup" )
412
437
@patch ("wol_client.parse_args" )
413
438
def test_main_ip_none (
414
439
self ,
415
440
mock_parse_args ,
416
441
mock_check_wakeup ,
417
- mock_get_ip_mac ,
442
+ mock_bring_up_system ,
443
+ mock_get_ip_address ,
444
+ mock_get_mac_address ,
418
445
mock_send_request_to_wol_server ,
419
446
):
420
447
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
422
452
mock_check_wakeup .return_value = True
423
453
424
454
with self .assertRaises (SystemExit ) as cm :
@@ -428,19 +458,22 @@ def test_main_ip_none(
428
458
)
429
459
430
460
@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" )
432
463
@patch ("wol_client.check_wakeup" )
433
464
@patch ("wol_client.parse_args" )
434
465
def test_main_checkwakeup_disable (
435
466
self ,
436
467
mock_parse_args ,
437
468
mock_check_wakeup ,
438
- mock_get_ip_mac ,
469
+ mock_get_ip_address ,
470
+ mock_get_mac_address ,
439
471
mock_send_request_to_wol_server ,
440
472
):
441
473
mock_parse_args .return_value = create_mock_args ()
442
474
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"
444
477
mock_send_request_to_wol_server .return_value = create_mock_response ()
445
478
446
479
with self .assertRaises (SystemExit ) as cm :
0 commit comments