-
Notifications
You must be signed in to change notification settings - Fork 16
/
ister_test.py
5953 lines (4998 loc) · 202 KB
/
ister_test.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: ts=4 sw=4 tw=80 et ai si
"""Linux installation template system test suite"""
#
# This file is part of ister.
#
# Copyright (C) 2014 Intel Corporation
#
# ister is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3 of the License, or (at your
# option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#
# If we see an exception it is always fatal so the broad exception
# warning isn't helpful.
# pylint: disable=broad-except
# Using global is fine for us
# pylint: disable=global-statement
# Warning for too many lines in the file isn't an issue
# pylint: disable=too-many-lines
# We often don't use self when mocking so this warning isn't helpful
# pylint: disable=no-self-use
# Length of function names aren't particularly important for tests
# pylint: disable=invalid-name
# Classes are generally for mocking, don't need public methods
# pylint: disable=too-few-public-methods
# Mock functions almost by definition do not make use of their inputs.
# Fine if they don't use them
# pylint: disable=unused-argument
# We don't need docstrings for many of these methods, especially when they are
# just mock methods anyways
# pylint: disable=missing-docstring
# Don't worry about protected access warnings since we want our unit tests to
# test those methods
# pylint: disable=protected-access
import functools
import json
import os
import shutil
import socket
import stat
import sys
import tempfile
import urllib.request as request
import pycurl
import netifaces
import traceback
import types
try:
import pycryptsetup
except:
pycrypts = types.ModuleType("pycryptsetup")
pycrypts.CryptSetup = types.new_class("CryptSetup")
sys.modules["pycryptsetup"] = pycrypts
import pycryptsetup
import ister
import ister_gui
COMMAND_RESULTS = []
def good_virtual_disk_template():
"""Return string representation of good_virtual_disk_template"""
return u'{"DestinationType" : "virtual", "PartitionLayout" : \
[{"disk" : "gvdt", "partition" : 1, "size" : "512M", "type" : "EFI"}, \
{"disk" : "gvdt", "partition" : 2, \
"size" : "512M", "type" : "swap"}, {"disk" : "gvdt", "partition" : 3, \
"size" : "rest", "type" : "linux"}], \
"FilesystemTypes" : \
[{"disk" : "gvdt", "partition" : 1, "type" : "vfat"}, {"disk" : "gvdt", \
"partition" : 2, "type" : "swap"}, \
{"disk" : "gvdt", "partition" : 3, "type" : "ext4"}], \
"PartitionMountPoints" : \
[{"disk" : "gvdt", "partition" : 1, "mount" : "/boot"}, {"disk" : "gvdt", \
"partition" : 3, "mount" : "/"}], \
"Version" : 800, "Bundles" : ["linux-kvm"], \
"HTTPSProxy" : "https://proxy.clear.com", \
"SoftwareManager": "swupd"}'
def good_latest_template():
"""Return string representation of good_latest_template"""
return u'{"DestinationType" : "virtual", "PartitionLayout" : \
[{"disk" : "gvdt", "partition" : 1, "size" : "512M", "type" : "EFI"}, \
{"disk" : "gvdt", "partition" : 2, \
"size" : "512M", "type" : "swap"}, {"disk" : "gvdt", "partition" : 3, \
"size" : "rest", "type" : "linux"}], \
"FilesystemTypes" : \
[{"disk" : "gvdt", "partition" : 1, "type" : "vfat"}, {"disk" : "gvdt", \
"partition" : 2, "type" : "swap"}, \
{"disk" : "gvdt", "partition" : 3, "type" : "ext4"}], \
"PartitionMountPoints" : \
[{"disk" : "gvdt", "partition" : 1, "mount" : "/boot"}, {"disk" : "gvdt", \
"partition" : 3, "mount" : "/"}], \
"Version" : "latest", "Bundles" : ["linux-kvm"], \
"HTTPSProxy" : "https://proxy.clear.com", \
"SoftwareManager": "swupd"}'
def full_user_install_template():
"""Return string representation of full_user_install_template"""
return u'{"DestinationType" : "virtual", "PartitionLayout": \
[{"disk": "fuit", "partition": 1, "size": "512M", "type": "EFI"}, \
{"disk": "fuit", "partition": 2, "size": "512M", "type": "swap"}, \
{"disk": "fuit", "partition": 3, "size": "rest", "type": "linux"}], \
"FilesystemTypes": \
[{"disk": "fuit", "partition": 1, "type": "vfat"}, {"disk": "fuit", \
"partition": 2, "type": "swap"}, \
{"disk": "fuit", "partition": 3, "type": "ext4"}], \
"PartitionMountPoints": \
[{"disk": "fuit", "partition": 1, "mount": "/boot"}, {"disk": "fuit", \
"partition": 3, "mount": "/"}], \
"Users": [{"username": "user", "key": "key.pub", \
"uid": 1001, "sudo": "password"}]}'
def good_template_string_partitions():
"""Return string representation of good_template_string_partitions"""
return u'{"DestinationType" : "virtual", "PartitionLayout" : \
[{"disk" : "gvdt", "partition" : "1", "size" : "512M", "type" : "EFI"}, \
{"disk" : "gvdt", "partition" : "2", \
"size" : "512M", "type" : "swap"}, {"disk" : "gvdt", "partition" : "3", \
"size" : "rest", "type" : "linux"}], \
"FilesystemTypes" : \
[{"disk" : "gvdt", "partition" : "1", "type" : "vfat"}, {"disk" : "gvdt", \
"partition" : "2", "type" : "swap"}, \
{"disk" : "gvdt", "partition" : "3", "type" : "ext4"}], \
"PartitionMountPoints" : \
[{"disk" : "gvdt", "partition" : "1", "mount" : "/boot"}, \
{"disk" : "gvdt", "partition" : "3", "mount" : "/"}], \
"Version" : 800, "Bundles" : ["linux-kvm"], \
"HTTPSProxy" : "https://proxy.clear.com", \
"SoftwareManager": "swupd"}'
def cryptsetup_wrapper(func):
"""Wrapprt for test whose function use pycryptsetup"""
@functools.wraps(func)
def wrapper():
"""CryptSetup Mock Class"""
class mock_CryptSetup:
def __init__(self, device=None, name=None):
if device:
COMMAND_RESULTS.append(device)
if name:
COMMAND_RESULTS.append(name)
def luksFormat(self, cipher=None, cipherMode=None,
keysize=None, hashMode=None):
if cipher:
COMMAND_RESULTS.append(cipher)
if cipherMode:
COMMAND_RESULTS.append(cipherMode)
if keysize:
COMMAND_RESULTS.append(keysize)
if hashMode:
COMMAND_RESULTS.append(hashMode)
def addKeyByPassphrase(self, passphrase1, passphrase2):
COMMAND_RESULTS.append(
"{0} - {1}".format(passphrase1, passphrase2))
def activate(self, name='', passphrase=''):
COMMAND_RESULTS.append("activating")
def deactivate(self):
COMMAND_RESULTS.append("deactivating")
pycrypt = pycryptsetup.CryptSetup
pycryptsetup.CryptSetup = mock_CryptSetup
try:
func()
except Exception as e:
raise e
finally:
pycryptsetup.CryptSetup = pycrypt
return wrapper
def run_command_wrapper(func):
"""Wrapper for tests whose functions use run_command"""
@functools.wraps(func)
def wrapper():
"""run_command_wrapper"""
def mock_run_command(cmd, _=None, raise_exception=True,
log_output=True, environ=None, show_output=False,
shell=False):
"""mock_run_command wrapper"""
COMMAND_RESULTS.append(cmd)
if not raise_exception:
COMMAND_RESULTS.append(False)
if not log_output:
COMMAND_RESULTS.append(False)
if environ:
COMMAND_RESULTS.append(True)
if show_output:
COMMAND_RESULTS.append(True)
if shell:
COMMAND_RESULTS.append(True)
return [], [], 0
global COMMAND_RESULTS
COMMAND_RESULTS = []
run_command = ister.run_command
ister.run_command = mock_run_command
try:
func()
except Exception as exep:
raise exep
finally:
ister.run_command = run_command
return wrapper
def makedirs_wrapper(test_type):
"""Wrapper for makedirs mocking"""
def makedirs_type(func):
"""makedirs_type wrapper"""
@functools.wraps(func)
def wrapper():
"""makedirs_wrapper"""
backup_makedirs = os.makedirs
def mock_makedirs_good(dname, mode=0, exist_ok=False):
"""mock_makedirs_good wrapper"""
COMMAND_RESULTS.append(dname)
COMMAND_RESULTS.append(mode)
COMMAND_RESULTS.append(exist_ok)
return
def mock_makedirs_bad(dname, mode=0, exist_ok=False):
"""mock_makedirs_bad wrapper"""
COMMAND_RESULTS.append(dname)
COMMAND_RESULTS.append(mode)
COMMAND_RESULTS.append(exist_ok)
raise Exception("mock makedirs bad")
if test_type == "good":
os.makedirs = mock_makedirs_good
else:
os.makedirs = mock_makedirs_bad
try:
func()
except Exception as exep:
raise exep
finally:
os.makedirs = backup_makedirs
return wrapper
return makedirs_type
def chroot_open_wrapper(test_type):
"""Wrapper for chroot mocking"""
def chroot_type(func):
"""chroot_type wrapper"""
@functools.wraps(func)
def wrapper():
"""chroot_open_wrapper"""
backup_open = os.open
backup_chroot = os.chroot
backup_chdir = os.chdir
backup_close = os.close
def mock_open_good(dest, perm):
"""mock_open_good wrapper"""
COMMAND_RESULTS.append(dest)
COMMAND_RESULTS.append(perm)
return dest
def mock_open_bad(dest, perm):
"""mock_open_bad wrapper"""
del dest
del perm
raise Exception("open")
def mock_open_silent(dest, perm):
"""mock_open_silent wrapper"""
del perm
return dest
def mock_chroot_chdir_close_good(dest):
"""mock_chroot_chrdir_close_good wrapper"""
COMMAND_RESULTS.append(dest)
def mock_chroot_bad(dest):
"""mock_chroot_bad wrapper"""
del dest
raise Exception("chroot")
def mock_chdir_bad(dest):
"""mock_chdir_bad wrapper"""
del dest
raise Exception("chdir")
def mock_close_bad(dest):
"""mock_close_bad wrapper"""
del dest
raise Exception("close")
def mock_chroot_chdir_close_silent(dest):
"""mock_chroot_chdir_close_silent wrapper"""
del dest
return
os.open = mock_open_silent
os.chroot = mock_chroot_chdir_close_silent
os.chdir = mock_chroot_chdir_close_silent
os.close = mock_chroot_chdir_close_silent
if test_type == "good":
os.open = mock_open_good
os.chroot = mock_chroot_chdir_close_good
os.chdir = mock_chroot_chdir_close_good
os.close = mock_chroot_chdir_close_good
elif test_type == "bad open":
os.open = mock_open_bad
elif test_type == "bad chroot":
os.chroot = mock_chroot_bad
elif test_type == "bad chdir":
os.chdir = mock_chdir_bad
elif test_type == "bad close":
os.close = mock_close_bad
try:
func()
except Exception as exep:
raise exep
finally:
os.open = backup_open
os.chroot = backup_chroot
os.chdir = backup_chdir
os.close = backup_close
return wrapper
return chroot_type
def urlopen_wrapper(test_type, read_str):
"""Wrapper for urlopen"""
def urlopen_type(func):
"""urlopen_type wrapper"""
@functools.wraps(func)
def wrapper():
"""open_wrapper"""
backup_open = __builtins__.open
class MockOpen():
"""MockOpen wrapper class"""
def read(self):
"""read wrapper"""
COMMAND_RESULTS.append("read")
return read_str
def close(self):
"""close wrapper"""
COMMAND_RESULTS.append("close")
return
def __exit__(self, *args):
return
def __enter__(self, *args):
return self
def mock_open_good(url):
"""mock_open_good wrapper"""
COMMAND_RESULTS.append(url)
return MockOpen()
def mock_open_bad(url):
"""mock_open_bad wrapper"""
del url
raise Exception("urlopen")
if test_type == "good":
request.urlopen = mock_open_good
elif test_type == "bad":
request.urlopen = mock_open_bad
try:
func()
except Exception as exep:
raise exep
finally:
request.urlopen = backup_open
return wrapper
return urlopen_type
def open_wrapper(test_type, read_str):
"""Wrapper for open"""
def open_type(func):
"""open_type wrapper"""
@functools.wraps(func)
def wrapper():
"""open_wrapper"""
backup_open = __builtins__.open
class MockOpen():
"""MockOpen wrapper class"""
def write(self, data):
"""write wrapper"""
COMMAND_RESULTS.append(data)
def read(self):
"""read wrapper"""
COMMAND_RESULTS.append("read")
return read_str
def close(self):
"""close wrapper"""
COMMAND_RESULTS.append("close")
return
def writelines(self, data):
"""writelines wrapper"""
COMMAND_RESULTS.append(data)
return
def readlines(self):
"""readlines wrapper"""
COMMAND_RESULTS.append("readlines")
return read_str
def __exit__(self, *args):
return
def __enter__(self, *args):
return self
def mock_open_good(dest, perm):
"""mock_open_good wrapper"""
COMMAND_RESULTS.append(dest)
COMMAND_RESULTS.append(perm)
return MockOpen()
def mock_open_bad(dest, perm):
"""mock_open_bad wrapper"""
del dest
del perm
raise Exception("open")
if test_type == "good":
__builtins__.open = mock_open_good
elif test_type == "bad":
__builtins__.open = mock_open_bad
try:
func()
except Exception as exep:
raise exep
finally:
__builtins__.open = backup_open
return wrapper
return open_type
def fdopen_wrapper(test_type, read_str):
"""Wrapper for fdopen"""
def fd_open_type(func):
"""fdopen_type wrapper"""
@functools.wraps(func)
def fd_wrapper():
"""fdopen_wrapper"""
backup_open = os.fdopen
class MockOpen():
"""MockOpen wrapper class"""
def write(self, data):
"""write wrapper"""
COMMAND_RESULTS.append(data)
def read(self):
"""read wrapper"""
COMMAND_RESULTS.append("read")
return read_str
def close(self):
"""close wrapper"""
COMMAND_RESULTS.append("close")
return
def writelines(self, data):
"""writelines wrapper"""
global COMMAND_RESULTS
COMMAND_RESULTS += data
return
def mock_open_good(dest, perm):
"""mock_open_good wrapper"""
COMMAND_RESULTS.append(dest)
COMMAND_RESULTS.append(perm)
return MockOpen()
def mock_open_bad(dest, perm):
"""mock_open_bad wrapper"""
del dest
del perm
raise Exception("open")
if test_type == "good":
os.fdopen = mock_open_good
elif test_type == "bad":
os.fdopen = mock_open_bad
try:
func()
except Exception as exep:
raise exep
finally:
os.fdopen = backup_open
return fd_wrapper
return fd_open_type
def add_user_key_wrapper(func):
"""Wrapper for functions in add_user_key"""
@functools.wraps(func)
@makedirs_wrapper("good")
def wrapper():
"""add_user_key_wrapper"""
import pwd
backup_chown = os.chown
backup_getpwnam = pwd.getpwnam
def mock_chown(dest, uid, gid):
"""mock_chown wrapper"""
COMMAND_RESULTS.append(dest)
COMMAND_RESULTS.append(uid)
COMMAND_RESULTS.append(gid)
def mock_getpwnam(dest):
"""mock_getpwnam wrapper"""
COMMAND_RESULTS.append(dest)
return [0, 0, 1000, 1000]
os.chown = mock_chown
pwd.getpwnam = mock_getpwnam
try:
func()
except Exception as exep:
raise exep
finally:
os.chown = backup_chown
pwd.getpwnam = backup_getpwnam
return wrapper
def run_command_good():
"""Good run_command test"""
ister.run_command("true")
def run_command_bad():
"""Bad run_command test"""
exception_flag = False
try:
ister.run_command("not-a-binary", False)
except Exception:
raise Exception("Command raised exception with surpression enabled")
try:
ister.run_command("not-a-binary")
except Exception:
exception_flag = True
if not exception_flag:
raise Exception("Bad command did not fail")
@run_command_wrapper
def create_virtual_disk_good_meg():
"""Create disk with size specified in megabytes"""
template = {"PartitionLayout": [{"size": "20000M", "disk": "vdisk_tmp"},
{"size": "50M"}]}
command = "dd if=/dev/zero of=vdisk_tmp bs=1024 count=0 seek=20532224"
ister.create_virtual_disk(template)
if command != COMMAND_RESULTS[0] or len(COMMAND_RESULTS) != 1:
raise Exception("command to create image doesn't match expected "
"result: {0}".format(COMMAND_RESULTS))
@run_command_wrapper
def create_virtual_disk_good_gig():
"""Create disk with size specified in gigabytes"""
template = {"PartitionLayout": [{"size": "20G", "disk": "vdisk_tmp"},
{"size": "1G"}]}
command = "dd if=/dev/zero of=vdisk_tmp bs=1024 count=0 seek=22021120"
ister.create_virtual_disk(template)
if command != COMMAND_RESULTS[0] or len(COMMAND_RESULTS) != 1:
raise Exception("command to create image doesn't match expected "
"result: {0}".format(COMMAND_RESULTS))
@run_command_wrapper
def create_virtual_disk_good_tera():
"""Create disk with size specified in terabytes"""
template = {"PartitionLayout": [{"size": "1T", "disk": "vdisk_tmp"}]}
command = "dd if=/dev/zero of=vdisk_tmp bs=1024 count=0 seek=1073742848"
ister.create_virtual_disk(template)
if command != COMMAND_RESULTS[0] or len(COMMAND_RESULTS) != 1:
raise Exception("command to create image doesn't match expected "
"result: {0}".format(COMMAND_RESULTS))
def commands_compare_helper(commands):
"""Helper function to verify expected commands vs results"""
if len(commands) != len(COMMAND_RESULTS):
raise Exception("results don't match expectations:\nresults -> {0}\nexpected -> {1}"
.format(COMMAND_RESULTS, commands))
for idx, item in enumerate(commands):
del item
if commands[idx] != COMMAND_RESULTS[idx]:
raise Exception("command at position {0} doesn't match expected "
"result: \n{1}\n{2}".format(idx, commands[idx],
COMMAND_RESULTS[idx]))
@run_command_wrapper
def create_partitions_good_physical_min():
"""Setup minimal partition table on disk"""
commands = ["parted -sa optimal /dev/sda unit MiB mklabel gpt",
"parted -sa optimal -- /dev/sda unit MiB mkpart primary fat32 "
"0% 512",
"parted -s /dev/sda set 1 boot on",
"parted -sa optimal -- /dev/sda unit MiB mkpart primary ext2 "
"512 -1M"]
template = {"PartitionLayout": [{"partition": 1, "disk": "sda",
"size": "512M", "type": "EFI"},
{"partition": 2, "disk": "sda",
"size": "rest", "type": "linux"}],
"DestinationType": "physical"}
ister.create_partitions(template, 0)
commands_compare_helper(commands)
@run_command_wrapper
def create_partitions_good_physical_swap():
"""Setup with swap partition table on multidisk"""
commands = ["parted -sa optimal /dev/sda unit MiB mklabel gpt",
"parted -sa optimal /dev/sdb unit MiB mklabel gpt",
"parted -sa optimal -- /dev/sda unit MiB mkpart primary fat32 "
"0% 512",
"parted -s /dev/sda set 1 boot on",
"parted -sa optimal -- /dev/sda unit MiB mkpart primary "
"linux-swap 512 4608",
"parted -sa optimal -- /dev/sda unit MiB mkpart primary ext2 "
"4608 -1M",
"parted -sa optimal -- /dev/sdb unit MiB mkpart primary ext2 "
"0% -1M"]
template = {"PartitionLayout": [{"partition": 1, "disk": "sda",
"size": "512M", "type": "EFI"},
{"partition": 2, "disk": "sda",
"size": "4096M", "type": "swap"},
{"partition": 3, "disk": "sda",
"size": "rest", "type": "linux"},
{"partition": 1, "disk": "sdb",
"size": "rest", "type": "linux"}],
"DestinationType": "physical"}
ister.create_partitions(template, 0)
commands_compare_helper(commands)
@run_command_wrapper
def create_partitions_good_physical_specific():
"""Setup with partition table on multidisk"""
commands = ["parted -sa optimal /dev/sda unit MiB mklabel gpt",
"parted -sa optimal /dev/sdb unit MiB mklabel gpt",
"parted -sa optimal -- /dev/sda unit MiB mkpart primary fat32 "
"0% 512",
"parted -s /dev/sda set 1 boot on",
"parted -sa optimal -- /dev/sda unit MiB mkpart primary ext2 "
"512 4608",
"parted -sa optimal -- /dev/sdb unit MiB mkpart primary ext2 "
"0% -1M"]
template = {"PartitionLayout": [{"partition": 1, "disk": "sda",
"size": "512M", "type": "EFI"},
{"partition": 2, "disk": "sda",
"size": "4096M", "type": "linux"},
{"partition": 1, "disk": "sdb",
"size": "rest", "type": "linux"}],
"DestinationType": "physical"}
ister.create_partitions(template, 0)
commands_compare_helper(commands)
@run_command_wrapper
def create_partitions_good_virtual_swap():
"""Setup with swap partition table on virtual image"""
commands = ["parted -sa optimal image unit MiB mklabel gpt",
"parted -sa optimal -- image unit MiB mkpart primary fat32 "
"0% 512",
"parted -s image set 1 boot on",
"parted -sa optimal -- image unit MiB mkpart primary "
"linux-swap 512 4608",
"parted -sa optimal -- image unit MiB mkpart primary ext2 "
"4608 -1M"]
template = {"PartitionLayout": [{"partition": 1, "disk": "image",
"size": "512M", "type": "EFI"},
{"partition": 2, "disk": "image",
"size": "4096M", "type": "swap"},
{"partition": 3, "disk": "image",
"size": "rest", "type": "linux"}],
"DestinationType": "virtual"}
ister.create_partitions(template, 0)
commands_compare_helper(commands)
@run_command_wrapper
def map_loop_device_good():
"""Create loop device for virtual image"""
import subprocess
check_output_backup = subprocess.check_output
def mock_check_output(cmd):
"""mock_check_output wrapper"""
global COMMAND_RESULTS
COMMAND_RESULTS = cmd
return b"/dev/loop0"
subprocess.check_output = mock_check_output
template = {"PartitionLayout": [{"disk": "image"}]}
commands = ["losetup", "--partscan", "--find", "--show",
"image", "partprobe /dev/loop0"]
try:
ister.map_loop_device(template, 0)
finally:
subprocess.check_output = check_output_backup
dev = template.get("dev")
if not dev:
raise Exception("Didn't set dev in template")
if dev != "/dev/loop0":
raise Exception("Incorrect dev set: {0}".format(dev))
commands_compare_helper(commands)
def map_loop_device_bad_check_output():
"""Handle losetup check_output Exception"""
import subprocess
check_output_backup = subprocess.check_output
def mock_check_output(cmd):
"""mock_check_output wrapper"""
del cmd
raise Exception("bad")
subprocess.check_output = mock_check_output
exception_flag = False
template = {"PartitionLayout": [{"disk": "image"}]}
try:
ister.map_loop_device(template, 0)
except Exception:
exception_flag = True
finally:
subprocess.check_output = check_output_backup
if not exception_flag:
raise Exception("Failed to manage check_output Exception")
def map_loop_device_bad_losetup():
"""Handle losetup failure"""
import subprocess
check_output_backup = subprocess.check_output
def mock_check_output(cmd):
"""mock_check_output wrapper"""
del cmd
return b""
subprocess.check_output = mock_check_output
exception_flag = False
template = {"PartitionLayout": [{"disk": "image"}]}
try:
ister.map_loop_device(template, 0)
except Exception:
exception_flag = True
finally:
subprocess.check_output = check_output_backup
if not exception_flag:
raise Exception("Did not detect losetup failure")
def get_device_name_good_virtual():
"""Get virtual device name"""
template = {"dev": "/dev/loop0"}
dev = ister.get_device_name(template, None)
if dev != ("/dev/loop0p", "p"):
raise Exception("Bad device name returned {0}".format(dev))
def get_device_name_good_physical():
"""Get physical device name"""
listdir_backup = os.listdir
def mock_listdir(directory):
"""mock_listdir wrapper"""
del directory
return ["sda", "sda1", "sda2"]
os.listdir = mock_listdir
dev = ister.get_device_name({}, "sda")
os.listdir = listdir_backup
if dev != ("/dev/sda", ""):
raise Exception("Bad device name returned {0}".format(dev))
def get_device_name_good_mmcblk_physical():
"""Get physical device name"""
listdir_backup = os.listdir
def mock_listdir(directory):
"""mock_listdir wrapper"""
del directory
return ["mmcblk1", "mmcblk1p1", "mmcblk1p2"]
os.listdir = mock_listdir
dev = ister.get_device_name({}, "mmcblk1")
os.listdir = listdir_backup
if dev != ("/dev/mmcblk1p", "p"):
raise Exception("Bad device name returned {0}".format(dev))
def get_device_name_good_nvme0n1():
"""Get physical device name"""
listdir_backup = os.listdir
def mock_listdir(directory):
"""mock_listdir wrapper"""
del directory
return ["nvme0n1", "nvme0n1p1", "nvme0n1p2"]
os.listdir = mock_listdir
dev = ister.get_device_name({}, "nvme0n1")
os.listdir = listdir_backup
if dev != ("/dev/nvme0n1p", "p"):
raise Exception("Bad device name returned {0}".format(dev))
@run_command_wrapper
def get_part_devname_good():
"""Get partition device name"""
def mock_check_output(cmd):
COMMAND_RESULTS.extend(cmd)
return b"sda\n"
check_output_backup = ister_gui.subprocess.check_output
ister_gui.subprocess.check_output = mock_check_output
expected = ["/usr/bin/lsblk",
"-no", "pkname",
"/dev/sda1"]
res = ister_gui.get_part_devname("sda1")
ister_gui.subprocess.check_output = check_output_backup
if res != "sda":
raise Exception("Result was {}, expected sda".format(res))
commands_compare_helper(expected)
@run_command_wrapper
def get_part_devname_with_devname():
"""Get partition device name when passing device name"""
def mock_check_output(cmd):
COMMAND_RESULTS.extend(cmd)
return b"\nsda\nsda\nsda\n"
check_output_backup = ister_gui.subprocess.check_output
ister_gui.subprocess.check_output = mock_check_output
expected = ["/usr/bin/lsblk",
"-no", "pkname",
"/dev/sda"]
res = ister_gui.get_part_devname("/dev/sda")
ister_gui.subprocess.check_output = check_output_backup
if res != "sda":
raise Exception("Result was {}, expected sda".format(res))
commands_compare_helper(expected)
@run_command_wrapper
def get_part_devname_with_no_input():
"""Get partition device name when passing an empty string"""
def mock_check_output(cmd):
COMMAND_RESULTS.extend(cmd)
return b"\nsda\nsda\nsda\n"
check_output_backup = ister_gui.subprocess.check_output
ister_gui.subprocess.check_output = mock_check_output
expected = ["/usr/bin/lsblk",
"-no", "pkname",
"/dev/"]
res = ister_gui.get_part_devname("")
ister_gui.subprocess.check_output = check_output_backup
if res != "sda":
raise Exception("Result was {}, expected sda".format(res))
commands_compare_helper(expected)
def get_part_devname_exception():
"""Get partition device name with exception raised"""
def mock_check_output(cmd):
raise ister_gui.subprocess.CalledProcessError(1, cmd, "No such device")
check_output_backup = ister_gui.subprocess.check_output
ister_gui.subprocess.check_output = mock_check_output
res = ister_gui.get_part_devname("/dev/sda1")
ister_gui.subprocess.check_output = check_output_backup
if res:
raise Exception("Result was {}, expected None".format(res))
@cryptsetup_wrapper
@run_command_wrapper
def create_filesystems_encrypted_good():
"""Create filesystems without options"""
listdir_backup = os.listdir
def mock_listdir(directory):
"""mock_listdir wrapper"""
del directory
return ["sda", "sda1", "sda2", "sda3", "sda4",
"sdb", "sdb1", "sdb2", "sdb3"]
template = {"FilesystemTypes": [{"disk": "sda", "type": "ext2",
"partition": 1},
{"disk": "sda", "type": "ext3",
"partition": 2},
{"disk": "sda", "type": "ext4",
"partition": 3},
{"disk": "sda", "type": "btrfs",
"partition": 4},
{"disk": "sdb", "type": "vfat",
"partition": 1},
{"disk": "sdb", "type": "swap",
"partition": 2},
{"disk": "sdb", "type": "xfs",
"partition": 3, "encryption": {
"passphrase":"abc@123",
"name" : "mapper_name"}}]}
commands = ["mkfs.ext2 -F /dev/sda1",
"mkfs.ext3 -F /dev/sda2",
"mkfs.ext4 -F /dev/sda3",
"mkfs.btrfs -f /dev/sda4",
"mkfs.vfat /dev/sdb1",
"sgdisk /dev/sdb "
"--typecode=2:0657fd6d-a4ab-43c4-84e5-0933c84b4f4f",
"mkswap /dev/sdb2",
"swapon /dev/sdb2",
False,
'/dev/sdb3', 'aes',
'xts-plain64', 512, 'sha256',
'abc@123 - abc@123',
'activating',
'mkfs.xfs -f /dev/mapper/mapper_name'
]
os.listdir = mock_listdir
ister.create_filesystems(template)
os.listdir = listdir_backup
commands_compare_helper(commands)
@run_command_wrapper
def create_filesystems_good():
"""Create filesystems without options"""
listdir_backup = os.listdir
def mock_listdir(directory):
"""mock_listdir wrapper"""
del directory
return ["sda", "sda1", "sda2", "sda3", "sda4",
"sdb", "sdb1", "sdb2", "sdb3"]
template = {"FilesystemTypes": [{"disk": "sda", "type": "ext2",
"partition": 1},
{"disk": "sda", "type": "ext3",
"partition": 2},
{"disk": "sda", "type": "ext4",
"partition": 3},
{"disk": "sda", "type": "btrfs",
"partition": 4},
{"disk": "sdb", "type": "vfat",
"partition": 1},
{"disk": "sdb", "type": "swap",
"partition": 2},
{"disk": "sdb", "type": "xfs",
"partition": 3}]}
commands = ["mkfs.ext2 -F /dev/sda1",
"mkfs.ext3 -F /dev/sda2",
"mkfs.ext4 -F /dev/sda3",
"mkfs.btrfs -f /dev/sda4",
"mkfs.vfat /dev/sdb1",
"sgdisk /dev/sdb "
"--typecode=2:0657fd6d-a4ab-43c4-84e5-0933c84b4f4f",
"mkswap /dev/sdb2",
"swapon /dev/sdb2",
False,
"mkfs.xfs -f /dev/sdb3"]