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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
|
#!/usr/bin/env python
# vim: noet ts=8 sw=8 sts=8
from __future__ import absolute_import, division, print_function, unicode_literals
from builtins import (str, open, range, zip, int, input)
import argparse
import glob
import os
import subprocess
import time
import tempfile
import shutil
import re
import stat
import signal
import atexit
import sys
import linecache
import random
import string
import fcntl
import errno
import datetime
import yaml
import struct
import mmap
import pycriu as crpc
os.chdir(os.path.dirname(os.path.abspath(__file__)))
prev_line = None
def alarm(*args):
print("==== ALARM ====")
signal.signal(signal.SIGALRM, alarm)
def traceit(f, e, a):
if e == "line":
lineno = f.f_lineno
fil = f.f_globals["__file__"]
if fil.endswith("zdtm.py"):
global prev_line
line = linecache.getline(fil, lineno)
if line == prev_line:
print(" ...")
else:
prev_line = line
print("+%4d: %s" % (lineno, line.rstrip()))
return traceit
# Root dir for ns and uns flavors. All tests
# sit in the same dir
tests_root = None
def clean_tests_root():
global tests_root
if tests_root and tests_root[0] == os.getpid():
os.rmdir(tests_root[1])
def make_tests_root():
global tests_root
if not tests_root:
tests_root = (os.getpid(), tempfile.mkdtemp("", "criu-root-", "/tmp"))
atexit.register(clean_tests_root)
return tests_root[1]
# Report generation
report_dir = None
def init_report(path):
global report_dir
report_dir = path
if not os.access(report_dir, os.F_OK):
os.makedirs(report_dir)
def add_to_report(path, tgt_name):
global report_dir
if report_dir:
tgt_path = os.path.join(report_dir, tgt_name)
att = 0
while os.access(tgt_path, os.F_OK):
tgt_path = os.path.join(report_dir, tgt_name + ".%d" % att)
att += 1
ignore = shutil.ignore_patterns('*.socket')
if os.path.isdir(path):
shutil.copytree(path, tgt_path, ignore=ignore)
else:
if not os.path.exists(os.path.dirname(tgt_path)):
os.mkdir(os.path.dirname(tgt_path))
shutil.copy2(path, tgt_path)
def add_to_output(path):
global report_dir
if not report_dir:
return
output_path = os.path.join(report_dir, "output")
with open(path, "r") as fdi, open(output_path, "a") as fdo:
for line in fdi:
fdo.write(line)
prev_crash_reports = set(glob.glob("/tmp/zdtm-core-*.txt"))
def check_core_files():
reports = set(glob.glob("/tmp/zdtm-core-*.txt")) - prev_crash_reports
if not reports:
return False
while subprocess.Popen(r"ps axf | grep 'abrt\.sh'",
shell=True).wait() == 0:
time.sleep(1)
for i in reports:
add_to_report(i, os.path.basename(i))
print_sep(i)
with open(i, "r") as report:
print(report.read())
print_sep(i)
return True
# Arch we run on
arch = os.uname()[4]
#
# Flavors
# h -- host, test is run in the same set of namespaces as criu
# ns -- namespaces, test is run in itw own set of namespaces
# uns -- user namespace, the same as above plus user namespace
#
class host_flavor:
def __init__(self, opts):
self.name = "host"
self.ns = False
self.root = None
def init(self, l_bins, x_bins):
pass
def fini(self):
pass
@staticmethod
def clean():
pass
class ns_flavor:
__root_dirs = [
"/bin", "/sbin", "/etc", "/lib", "/lib64", "/dev", "/dev/pts",
"/dev/net", "/tmp", "/usr", "/proc", "/run"
]
def __init__(self, opts):
self.name = "ns"
self.ns = True
self.uns = False
self.root = make_tests_root()
self.root_mounted = False
def __copy_one(self, fname):
tfname = self.root + fname
if not os.access(tfname, os.F_OK):
# Copying should be atomic as tests can be
# run in parallel
try:
os.makedirs(self.root + os.path.dirname(fname))
except OSError as e:
if e.errno != errno.EEXIST:
raise
dst = tempfile.mktemp(".tso", "",
self.root + os.path.dirname(fname))
shutil.copy2(fname, dst)
os.rename(dst, tfname)
def __copy_libs(self, binary):
ldd = subprocess.Popen(["ldd", binary], stdout=subprocess.PIPE)
xl = re.compile(
r'^(linux-gate.so|linux-vdso(64)?.so|not a dynamic|.*\s*ldd\s)')
# This Mayakovsky-style code gets list of libraries a binary
# needs minus vdso and gate .so-s
libs = map(
lambda x: x[1] == '=>' and x[2] or x[0],
map(
lambda x: str(x).split(),
filter(
lambda x: not xl.match(x),
map(
lambda x: str(x).strip(),
filter(lambda x: str(x).startswith('\t'),
ldd.stdout.read().decode(
'ascii').splitlines())))))
ldd.wait()
for lib in libs:
if not os.access(lib, os.F_OK):
raise test_fail_exc("Can't find lib %s required by %s" %
(lib, binary))
self.__copy_one(lib)
def __mknod(self, name, rdev=None):
name = "/dev/" + name
if not rdev:
if not os.access(name, os.F_OK):
print("Skipping %s at root" % name)
return
else:
rdev = os.stat(name).st_rdev
name = self.root + name
os.mknod(name, stat.S_IFCHR, rdev)
os.chmod(name, 0o666)
def __construct_root(self):
for dir in self.__root_dirs:
os.mkdir(self.root + dir)
os.chmod(self.root + dir, 0o777)
for ldir in ["/bin", "/sbin", "/lib", "/lib64"]:
os.symlink(".." + ldir, self.root + "/usr" + ldir)
self.__mknod("tty", os.makedev(5, 0))
self.__mknod("null", os.makedev(1, 3))
self.__mknod("net/tun")
self.__mknod("rtc")
self.__mknod("autofs", os.makedev(10, 235))
def __copy_deps(self, deps):
for d in deps.split('|'):
if os.access(d, os.F_OK):
self.__copy_one(d)
self.__copy_libs(d)
return
raise test_fail_exc("Deps check %s failed" % deps)
def init(self, l_bins, x_bins):
subprocess.check_call(
["mount", "--make-slave", "--bind", ".", self.root])
self.root_mounted = True
if not os.access(self.root + "/.constructed", os.F_OK):
with open(os.path.abspath(__file__)) as o:
fcntl.flock(o, fcntl.LOCK_EX)
if not os.access(self.root + "/.constructed", os.F_OK):
print("Construct root for %s" % l_bins[0])
self.__construct_root()
os.mknod(self.root + "/.constructed", stat.S_IFREG | 0o600)
for b in l_bins:
self.__copy_libs(b)
for b in x_bins:
self.__copy_deps(b)
def fini(self):
if self.root_mounted:
subprocess.check_call(["./umount2", self.root])
self.root_mounted = False
@staticmethod
def clean():
for d in ns_flavor.__root_dirs:
p = './' + d
print('Remove %s' % p)
if os.access(p, os.F_OK):
shutil.rmtree('./' + d)
if os.access('./.constructed', os.F_OK):
os.unlink('./.constructed')
class userns_flavor(ns_flavor):
def __init__(self, opts):
ns_flavor.__init__(self, opts)
self.name = "userns"
self.uns = True
def init(self, l_bins, x_bins):
# To be able to create roots_yard in CRIU
os.chmod(".", os.stat(".").st_mode | 0o077)
ns_flavor.init(self, l_bins, x_bins)
@staticmethod
def clean():
pass
flavors = {'h': host_flavor, 'ns': ns_flavor, 'uns': userns_flavor}
flavors_codes = dict(zip(range(len(flavors)), sorted(flavors.keys())))
#
# Helpers
#
def encode_flav(f):
return sorted(flavors.keys()).index(f) + 128
def decode_flav(i):
return flavors_codes.get(i - 128, "unknown")
def tail(path):
p = subprocess.Popen(['tail', '-n1', path], stdout=subprocess.PIPE)
out = p.stdout.readline()
p.wait()
return out.decode()
def rpidfile(path):
with open(path) as fd:
return fd.readline().strip()
def wait_pid_die(pid, who, tmo=30):
stime = 0.1
while stime < tmo:
try:
os.kill(int(pid), 0)
except OSError as e:
if e.errno != errno.ESRCH:
print(e)
break
print("Wait for %s(%d) to die for %f" % (who, pid, stime))
time.sleep(stime)
stime *= 2
else:
subprocess.Popen(["ps", "-p", str(pid)]).wait()
subprocess.Popen(["ps", "axf", str(pid)]).wait()
raise test_fail_exc("%s die" % who)
def test_flag(tdesc, flag):
return flag in tdesc.get('flags', '').split()
#
# Exception thrown when something inside the test goes wrong,
# e.g. test doesn't start, criu returns with non zero code or
# test checks fail
#
class test_fail_exc(Exception):
def __init__(self, step):
self.step = step
def __str__(self):
return str(self.step)
class test_fail_expected_exc(Exception):
def __init__(self, cr_action):
self.cr_action = cr_action
#
# A test from zdtm/ directory.
#
class zdtm_test:
def __init__(self, name, desc, flavor, freezer):
self.__name = name
self.__desc = desc
self.__freezer = None
self.__make_action('cleanout')
self.__pid = 0
self.__flavor = flavor
self.__freezer = freezer
self._bins = [name]
self._env = {}
self._deps = desc.get('deps', [])
self.auto_reap = True
self.__timeout = int(self.__desc.get('timeout') or 30)
def __make_action(self, act, env=None, root=None):
sys.stdout.flush() # Not to let make's messages appear before ours
tpath = self.__name + '.' + act
s_args = [
'make', '--no-print-directory', '-C',
os.path.dirname(tpath),
os.path.basename(tpath)
]
if env:
env = dict(os.environ, **env)
s = subprocess.Popen(
s_args,
env=env,
cwd=root,
close_fds=True,
preexec_fn=self.__freezer and self.__freezer.attach or None)
if act == "pid":
try_run_hook(self, ["--post-start"])
if s.wait():
raise test_fail_exc(str(s_args))
if self.__freezer:
self.__freezer.freeze()
def __pidfile(self):
return self.__name + '.pid'
def __wait_task_die(self):
wait_pid_die(int(self.__pid), self.__name, self.__timeout)
def __add_wperms(self):
# Add write perms for .out and .pid files
for b in self._bins:
p = os.path.dirname(b)
os.chmod(p, os.stat(p).st_mode | 0o222)
def start(self):
self.__flavor.init(self._bins, self._deps)
print("Start test")
env = self._env
if not self.__freezer.kernel:
env['ZDTM_THREAD_BOMB'] = "5"
if test_flag(self.__desc, 'pre-dump-notify'):
env['ZDTM_NOTIFY_FDIN'] = "100"
env['ZDTM_NOTIFY_FDOUT'] = "101"
if not test_flag(self.__desc, 'suid'):
# Numbers should match those in criu
env['ZDTM_UID'] = "18943"
env['ZDTM_GID'] = "58467"
env['ZDTM_GROUPS'] = "27495 48244"
self.__add_wperms()
else:
print("Test is SUID")
if self.__flavor.ns:
env['ZDTM_NEWNS'] = "1"
env['ZDTM_ROOT'] = self.__flavor.root
env['PATH'] = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
if self.__flavor.uns:
env['ZDTM_USERNS'] = "1"
self.__add_wperms()
if os.getenv("GCOV"):
criu_dir = os.path.dirname(os.getcwd())
criu_dir_r = "%s%s" % (self.__flavor.root, criu_dir)
env['ZDTM_CRIU'] = os.path.dirname(os.getcwd())
subprocess.check_call(["mkdir", "-p", criu_dir_r])
self.__make_action('pid', env, self.__flavor.root)
try:
os.kill(int(self.getpid()), 0)
except Exception as e:
raise test_fail_exc("start: %s" % e)
if not self.static():
# Wait less than a second to give the test chance to
# move into some semi-random state
time.sleep(random.random())
def kill(self, sig=signal.SIGKILL):
self.__freezer.thaw()
if self.__pid:
print("Send the %d signal to %s" % (sig, self.__pid))
os.kill(int(self.__pid), sig)
self.gone(sig == signal.SIGKILL)
self.__flavor.fini()
def pre_dump_notify(self):
env = self._env
if 'ZDTM_NOTIFY_FDIN' not in env:
return
if self.__pid == 0:
self.getpid()
notify_fdout_path = "/proc/%s/fd/%s" % (self.__pid,
env['ZDTM_NOTIFY_FDOUT'])
notify_fdin_path = "/proc/%s/fd/%s" % (self.__pid,
env['ZDTM_NOTIFY_FDIN'])
print("Send pre-dump notify to %s" % (self.__pid))
with open(notify_fdout_path, "rb") as fdout:
with open(notify_fdin_path, "wb") as fdin:
fdin.write(struct.pack("i", 0))
fdin.flush()
print("Wait pre-dump notify reply")
ret = struct.unpack('i', fdout.read(4))
print("Completed pre-dump notify with %d" % (ret))
def stop(self):
self.__freezer.thaw()
self.getpid() # Read the pid from pidfile back
self.kill(signal.SIGTERM)
res = tail(self.__name + '.out')
if 'PASS' not in list(map(lambda s: s.strip(), res.split())):
if os.access(self.__name + '.out.inprogress', os.F_OK):
print_sep(self.__name + '.out.inprogress')
with open(self.__name + '.out.inprogress') as fd:
print(fd.read())
print_sep(self.__name + '.out.inprogress')
raise test_fail_exc("result check")
def getpid(self):
if self.__pid == 0:
self.__pid = rpidfile(self.__pidfile())
return self.__pid
def getname(self):
return self.__name
def __getcropts(self):
opts = self.__desc.get('opts', '').split() + [
"--pidfile", os.path.realpath(self.__pidfile())
]
if self.__flavor.ns:
opts += ["--root", self.__flavor.root]
if test_flag(self.__desc, 'crlib'):
opts += [
"-L",
os.path.dirname(os.path.realpath(self.__name)) + '/lib'
]
return opts
def getdopts(self):
return self.__getcropts() + self.__freezer.getdopts(
) + self.__desc.get('dopts', '').split()
def getropts(self):
return self.__getcropts() + self.__freezer.getropts(
) + self.__desc.get('ropts', '').split()
def unlink_pidfile(self):
self.__pid = 0
os.unlink(self.__pidfile())
def gone(self, force=True):
if not self.auto_reap:
pid, status = os.waitpid(int(self.__pid), 0)
if pid != int(self.__pid):
raise test_fail_exc("kill pid mess")
self.__wait_task_die()
self.__pid = 0
if force:
os.unlink(self.__pidfile())
def print_output(self):
if os.access(self.__name + '.out', os.R_OK):
print("Test output: " + "=" * 32)
with open(self.__name + '.out') as output:
print(output.read())
print(" <<< " + "=" * 32)
def static(self):
return self.__name.split('/')[1] == 'static'
def ns(self):
return self.__flavor.ns
def blocking(self):
return test_flag(self.__desc, 'crfail')
@staticmethod
def available():
if not os.access("umount2", os.X_OK):
subprocess.check_call(["make", "umount2"])
if not os.access("zdtm_ct", os.X_OK):
subprocess.check_call(["make", "zdtm_ct"])
if not os.access("zdtm/lib/libzdtmtst.a", os.F_OK):
subprocess.check_call(["make", "-C", "zdtm/"])
subprocess.check_call(
["flock", "zdtm_mount_cgroups.lock", "./zdtm_mount_cgroups"])
@staticmethod
def cleanup():
subprocess.check_call(
["flock", "zdtm_mount_cgroups.lock", "./zdtm_umount_cgroups"])
def load_module_from_file(name, path):
if sys.version_info[0] == 3 and sys.version_info[1] >= 5:
import importlib.util
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
else:
import imp
mod = imp.load_source(name, path)
return mod
class inhfd_test:
def __init__(self, name, desc, flavor, freezer):
self.__name = os.path.basename(name)
print("Load %s" % name)
self.__fdtyp = load_module_from_file(self.__name, name)
self.__peer_pid = 0
self.__files = None
self.__peer_file_names = []
self.__dump_opts = []
self.__messages = {}
def __get_message(self, i):
m = self.__messages.get(i, None)
if not m:
m = b"".join([
random.choice(string.ascii_letters).encode() for _ in range(10)
]) + b"%06d" % i
self.__messages[i] = m
return m
def start(self):
self.__files = self.__fdtyp.create_fds()
# Check FDs returned for inter-connection
i = 0
for my_file, peer_file in self.__files:
msg = self.__get_message(i)
my_file.write(msg)
my_file.flush()
data = peer_file.read(len(msg))
if data != msg:
raise test_fail_exc("FDs screwup: %r %r" % (msg, data))
i += 1
start_pipe = os.pipe()
self.__peer_pid = os.fork()
if self.__peer_pid == 0:
os.setsid()
for _, peer_file in self.__files:
getattr(self.__fdtyp, "child_prep", lambda fd: None)(peer_file)
try:
os.unlink(self.__name + ".out")
except Exception as e:
print(e)
fd = os.open(self.__name + ".out",
os.O_WRONLY | os.O_APPEND | os.O_CREAT)
os.dup2(fd, 1)
os.dup2(fd, 2)
os.close(fd)
fd = os.open("/dev/null", os.O_RDONLY)
os.dup2(fd, 0)
for my_file, _ in self.__files:
my_file.close()
os.close(start_pipe[0])
os.close(start_pipe[1])
i = 0
for _, peer_file in self.__files:
msg = self.__get_message(i)
my_file.close()
try:
data = peer_file.read(16)
except Exception as e:
print("Unable to read a peer file: %s" % e)
sys.exit(1)
if data != msg:
print("%r %r" % (data, msg))
i += 1
sys.exit(data == msg and 42 or 2)
os.close(start_pipe[1])
os.read(start_pipe[0], 12)
os.close(start_pipe[0])
for _, peer_file in self.__files:
self.__peer_file_names.append(self.__fdtyp.filename(peer_file))
self.__dump_opts += self.__fdtyp.dump_opts(peer_file)
self.__fds = set(os.listdir("/proc/%s/fd" % self.__peer_pid))
def stop(self):
fds = set(os.listdir("/proc/%s/fd" % self.__peer_pid))
if fds != self.__fds:
raise test_fail_exc("File descriptors mismatch: %s %s" %
(fds, self.__fds))
i = 0
for my_file, _ in self.__files:
msg = self.__get_message(i)
my_file.write(msg)
my_file.flush()
i += 1
pid, status = os.waitpid(self.__peer_pid, 0)
with open(self.__name + ".out") as output:
print(output.read())
self.__peer_pid = 0
if not os.WIFEXITED(status) or os.WEXITSTATUS(status) != 42:
raise test_fail_exc("test failed with %d" % status)
def kill(self):
if self.__peer_pid:
os.kill(self.__peer_pid, signal.SIGKILL)
def getname(self):
return self.__name
def getpid(self):
return "%s" % self.__peer_pid
def gone(self, force=True):
os.waitpid(self.__peer_pid, 0)
wait_pid_die(self.__peer_pid, self.__name)
self.__files = None
def getdopts(self):
return self.__dump_opts
def getropts(self):
self.__files = self.__fdtyp.create_fds()
ropts = ["--restore-sibling"]
for i in range(len(self.__files)):
my_file, peer_file = self.__files[i]
fd = peer_file.fileno()
fdflags = fcntl.fcntl(fd, fcntl.F_GETFD) & ~fcntl.FD_CLOEXEC
fcntl.fcntl(fd, fcntl.F_SETFD, fdflags)
peer_file_name = self.__peer_file_names[i]
ropts.extend(["--inherit-fd", "fd[%d]:%s" % (fd, peer_file_name)])
return ropts
def print_output(self):
pass
def static(self):
return True
def blocking(self):
return False
@staticmethod
def available():
pass
@staticmethod
def cleanup():
pass
class groups_test(zdtm_test):
def __init__(self, name, desc, flavor, freezer):
zdtm_test.__init__(self, 'zdtm/lib/groups', desc, flavor, freezer)
if flavor.ns:
self.__real_name = name
with open(name) as fd:
self.__subs = map(lambda x: x.strip(), fd.readlines())
print("Subs:\n%s" % '\n'.join(self.__subs))
else:
self.__real_name = ''
self.__subs = []
self._bins += self.__subs
self._deps += get_test_desc('zdtm/lib/groups')['deps']
self._env = {'ZDTM_TESTS': self.__real_name}
def __get_start_cmd(self, name):
tdir = os.path.dirname(name)
tname = os.path.basename(name)
s_args = ['make', '--no-print-directory', '-C', tdir]
subprocess.check_call(s_args + [tname + '.cleanout'])
s = subprocess.Popen(s_args + ['--dry-run', tname + '.pid'],
stdout=subprocess.PIPE)
cmd = s.stdout.readlines().pop().strip()
s.wait()
return 'cd /' + tdir + ' && ' + cmd
def start(self):
if (self.__subs):
with open(self.__real_name + '.start', 'w') as f:
for test in self.__subs:
cmd = self.__get_start_cmd(test)
f.write(cmd + '\n')
with open(self.__real_name + '.stop', 'w') as f:
for test in self.__subs:
f.write('kill -TERM `cat /%s.pid`\n' % test)
zdtm_test.start(self)
def stop(self):
zdtm_test.stop(self)
for test in self.__subs:
res = tail(test + '.out')
if 'PASS' not in res.split():
raise test_fail_exc("sub %s result check" % test)
test_classes = {'zdtm': zdtm_test, 'inhfd': inhfd_test, 'groups': groups_test}
#
# CRIU when launched using CLI
#
join_ns_file = '/run/netns/zdtm_netns'
class criu_cli:
@staticmethod
def run(action,
args,
criu_bin,
fault=None,
strace=[],
preexec=None,
nowait=False):
env = dict(
os.environ,
ASAN_OPTIONS="log_path=asan.log:disable_coredump=0:detect_leaks=0")
if fault:
print("Forcing %s fault" % fault)
env['CRIU_FAULT'] = fault
cr = subprocess.Popen(strace +
[criu_bin, action, "--no-default-config"] + args,
env=env,
close_fds=False,
preexec_fn=preexec)
if nowait:
return cr
return cr.wait()
class criu_rpc_process:
def wait(self):
return self.criu.wait_pid(self.pid)
def terminate(self):
os.kill(self.pid, signal.SIGTERM)
class criu_rpc:
@staticmethod
def __set_opts(criu, args, ctx):
while len(args) != 0:
arg = args.pop(0)
if arg == '-v4':
criu.opts.log_level = 4
continue
if arg == '-o':
criu.opts.log_file = args.pop(0)
continue
if arg == '-D':
criu.opts.images_dir_fd = os.open(args.pop(0), os.O_DIRECTORY)
ctx['imgd'] = criu.opts.images_dir_fd
continue
if arg == '-t':
criu.opts.pid = int(args.pop(0))
continue
if arg == '--pidfile':
ctx['pidf'] = args.pop(0)
continue
if arg == '--timeout':
criu.opts.timeout = int(args.pop(0))
continue
if arg == '--restore-detached':
# Set by service by default
ctx['rd'] = True
continue
if arg == '--root':
criu.opts.root = args.pop(0)
continue
if arg == '--external':
criu.opts.external.append(args.pop(0))
continue
if arg == '--status-fd':
fd = int(args.pop(0))
os.write(fd, b"\0")
fcntl.fcntl(fd, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
continue
if arg == '--port':
criu.opts.ps.port = int(args.pop(0))
continue
if arg == '--address':
criu.opts.ps.address = args.pop(0)
continue
if arg == '--page-server':
continue
if arg == '--prev-images-dir':
criu.opts.parent_img = args.pop(0)
continue
if arg == '--track-mem':
criu.opts.track_mem = True
continue
if arg == '--tcp-established':
criu.opts.tcp_established = True
continue
if arg == '--restore-sibling':
criu.opts.rst_sibling = True
continue
if arg == "--inherit-fd":
inhfd = criu.opts.inherit_fd.add()
key = args.pop(0)
fd, key = key.split(":", 1)
inhfd.fd = int(fd[3:-1])
inhfd.key = key
continue
raise test_fail_exc('RPC for %s required' % arg)
@staticmethod
def run(action,
args,
criu_bin,
fault=None,
strace=[],
preexec=None,
nowait=False):
if fault:
raise test_fail_exc('RPC and FAULT not supported')
if strace:
raise test_fail_exc('RPC and SAT not supported')
if preexec:
raise test_fail_exc('RPC and PREEXEC not supported')
ctx = {} # Object used to keep info untill action is done
criu = crpc.criu()
criu.use_binary(criu_bin)
criu_rpc.__set_opts(criu, args, ctx)
p = None
try:
if action == 'dump':
criu.dump()
elif action == 'pre-dump':
criu.pre_dump()
elif action == 'restore':
if 'rd' not in ctx:
raise test_fail_exc(
'RPC Non-detached restore is impossible')
res = criu.restore()
pidf = ctx.get('pidf')
if pidf:
with open(pidf, 'w') as fd:
fd.write('%d\n' % res.pid)
elif action == "page-server":
res = criu.page_server_chld()
p = criu_rpc_process()
p.pid = res.pid
p.criu = criu
else:
raise test_fail_exc('RPC for %s required' % action)
except crpc.CRIUExceptionExternal as e:
print("Fail", e)
ret = -1
else:
ret = 0
imgd = ctx.get('imgd')
if imgd:
os.close(imgd)
if nowait and ret == 0:
return p
return ret
class criu:
def __init__(self, opts):
self.__test = None
self.__dump_path = None
self.__iter = 0
self.__prev_dump_iter = None
self.__page_server = bool(opts['page_server'])
self.__remote_lazy_pages = bool(opts['remote_lazy_pages'])
self.__lazy_pages = (self.__remote_lazy_pages or
bool(opts['lazy_pages']))
self.__lazy_migrate = bool(opts['lazy_migrate'])
self.__restore_sibling = bool(opts['sibling'])
self.__join_ns = bool(opts['join_ns'])
self.__empty_ns = bool(opts['empty_ns'])
self.__fault = opts['fault']
self.__script = opts['script']
self.__sat = bool(opts['sat'])
self.__dedup = bool(opts['dedup'])
self.__mdedup = bool(opts['noauto_dedup'])
self.__user = bool(opts['user'])
self.__leave_stopped = bool(opts['stop'])
self.__criu = (opts['rpc'] and criu_rpc or criu_cli)
self.__show_stats = bool(opts['show_stats'])
self.__lazy_pages_p = None
self.__page_server_p = None
self.__dump_process = None
self.__tls = self.__tls_options() if opts['tls'] else []
self.__criu_bin = opts['criu_bin']
self.__crit_bin = opts['crit_bin']
def fini(self):
if self.__lazy_migrate:
ret = self.__dump_process.wait()
if self.__lazy_pages_p:
ret = self.__lazy_pages_p.wait()
grep_errors(os.path.join(self.__ddir(), "lazy-pages.log"))
self.__lazy_pages_p = None
if ret:
raise test_fail_exc("criu lazy-pages exited with %s" % ret)
if self.__page_server_p:
ret = self.__page_server_p.wait()
grep_errors(os.path.join(self.__ddir(), "page-server.log"))
self.__page_server_p = None
if ret:
raise test_fail_exc("criu page-server exited with %s" % ret)
if self.__dump_process:
ret = self.__dump_process.wait()
grep_errors(os.path.join(self.__ddir(), "dump.log"))
self.__dump_process = None
if ret:
raise test_fail_exc("criu dump exited with %s" % ret)
return
def logs(self):
return self.__dump_path
def set_test(self, test):
self.__test = test
self.__dump_path = "dump/" + test.getname() + "/" + test.getpid()
if os.path.exists(self.__dump_path):
for i in range(100):
newpath = self.__dump_path + "." + str(i)
if not os.path.exists(newpath):
os.rename(self.__dump_path, newpath)
break
else:
raise test_fail_exc("couldn't find dump dir %s" %
self.__dump_path)
os.makedirs(self.__dump_path)
def cleanup(self):
if self.__dump_path:
print("Removing %s" % self.__dump_path)
shutil.rmtree(self.__dump_path)
def __tls_options(self):
pki_dir = os.path.dirname(os.path.abspath(__file__)) + "/pki"
return [
"--tls", "--tls-no-cn-verify", "--tls-key", pki_dir + "/key.pem",
"--tls-cert", pki_dir + "/cert.pem", "--tls-cacert",
pki_dir + "/cacert.pem"
]
def __ddir(self):
return os.path.join(self.__dump_path, "%d" % self.__iter)
def set_user_id(self):
# Numbers should match those in zdtm_test
os.setresgid(58467, 58467, 58467)
os.setresuid(18943, 18943, 18943)
def __criu_act(self, action, opts=[], log=None, nowait=False):
if not log:
log = action + ".log"
s_args = ["-o", log, "-D", self.__ddir(), "-v4"] + opts
with open(os.path.join(self.__ddir(), action + '.cropt'), 'w') as f:
f.write(' '.join(s_args) + '\n')
print("Run criu " + action)
strace = []
if self.__sat:
fname = os.path.join(self.__ddir(), action + '.strace')
print_fname(fname, 'strace')
strace = ["strace", "-o", fname, '-T']
if action == 'restore':
strace += ['-f']
s_args += [
'--action-script',
os.getcwd() + '/../scripts/fake-restore.sh'
]
if self.__script:
s_args += ['--action-script', self.__script]
if action == "restore":
preexec = None
else:
preexec = self.__user and self.set_user_id or None
__ddir = self.__ddir()
status_fds = None
if nowait:
status_fds = os.pipe()
fd = status_fds[1]
fdflags = fcntl.fcntl(fd, fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, fdflags & ~fcntl.FD_CLOEXEC)
s_args += ["--status-fd", str(fd)]
with open("/proc/sys/kernel/ns_last_pid") as ns_last_pid_fd:
ns_last_pid = ns_last_pid_fd.read()
ret = self.__criu.run(action, s_args, self.__criu_bin, self.__fault,
strace, preexec, nowait)
if nowait:
os.close(status_fds[1])
if os.read(status_fds[0], 1) != b'\0':
ret = ret.wait()
if self.__test.blocking():
raise test_fail_expected_exc(action)
else:
raise test_fail_exc("criu %s exited with %s" %
(action, ret))
os.close(status_fds[0])
return ret
grep_errors(os.path.join(__ddir, log))
if ret != 0:
if self.__fault and int(self.__fault) < 128:
try_run_hook(self.__test, ["--fault", action])
if action == "dump":
# create a clean directory for images
os.rename(__ddir, __ddir + ".fail")
os.mkdir(__ddir)
os.chmod(__ddir, 0o777)
else:
# on restore we move only a log file, because we need images
os.rename(os.path.join(__ddir, log),
os.path.join(__ddir, log + ".fail"))
# restore ns_last_pid to avoid a case when criu gets
# PID of one of restored processes.
with open("/proc/sys/kernel/ns_last_pid", "w+") as fd:
fd.write(ns_last_pid)
# try again without faults
print("Run criu " + action)
ret = self.__criu.run(action, s_args, self.__criu_bin, False,
strace, preexec)
grep_errors(os.path.join(__ddir, log))
if ret == 0:
return
rst_succeeded = os.access(
os.path.join(__ddir, "restore-succeeded"), os.F_OK)
if self.__test.blocking() or (self.__sat and action == 'restore' and
rst_succeeded):
raise test_fail_expected_exc(action)
else:
raise test_fail_exc("CRIU %s" % action)
def __stats_file(self, action):
return os.path.join(self.__ddir(), "stats-%s" % action)
def show_stats(self, action):
if not self.__show_stats:
return
subprocess.Popen([self.__crit_bin, "show",
self.__stats_file(action)]).wait()
def check_pages_counts(self):
if not os.access(self.__stats_file("dump"), os.R_OK):
return
stats_written = -1
with open(self.__stats_file("dump"), 'rb') as stfile:
stats = crpc.images.load(stfile)
stent = stats['entries'][0]['dump']
stats_written = int(stent['shpages_written']) + int(
stent['pages_written'])
real_written = 0
for f in os.listdir(self.__ddir()):
if f.startswith('pages-'):
real_written += os.path.getsize(os.path.join(self.__ddir(), f))
r_pages = real_written / mmap.PAGESIZE
r_off = real_written % mmap.PAGESIZE
if (stats_written != r_pages) or (r_off != 0):
print("ERROR: bad page counts, stats = %d real = %d(%d)" %
(stats_written, r_pages, r_off))
raise test_fail_exc("page counts mismatch")
def dump(self, action, opts=[]):
self.__iter += 1
os.mkdir(self.__ddir())
os.chmod(self.__ddir(), 0o777)
a_opts = ["-t", self.__test.getpid()]
if self.__prev_dump_iter:
a_opts += [
"--prev-images-dir",
"../%d" % self.__prev_dump_iter, "--track-mem"
]
self.__prev_dump_iter = self.__iter
if self.__page_server:
print("Adding page server")
ps_opts = ["--port", "12345"] + self.__tls
if self.__dedup:
ps_opts += ["--auto-dedup"]
self.__page_server_p = self.__criu_act("page-server",
opts=ps_opts,
nowait=True)
a_opts += [
"--page-server", "--address", "127.0.0.1", "--port", "12345"
] + self.__tls
a_opts += self.__test.getdopts()
if self.__dedup:
a_opts += ["--auto-dedup"]
a_opts += ["--timeout", "10"]
criu_dir = os.path.dirname(os.getcwd())
if os.getenv("GCOV"):
a_opts.append('--external')
a_opts.append('mnt[%s]:zdtm' % criu_dir)
if self.__leave_stopped:
a_opts += ['--leave-stopped']
if self.__empty_ns:
a_opts += ['--empty-ns', 'net']
nowait = False
if self.__lazy_migrate and action == "dump":
a_opts += ["--lazy-pages", "--port", "12345"] + self.__tls
nowait = True
self.__dump_process = self.__criu_act(action,
opts=a_opts + opts,
nowait=nowait)
if self.__mdedup and self.__iter > 1:
self.__criu_act("dedup", opts=[])
self.show_stats("dump")
self.check_pages_counts()
if self.__leave_stopped:
pstree_check_stopped(self.__test.getpid())
pstree_signal(self.__test.getpid(), signal.SIGKILL)
if self.__page_server_p:
ret = self.__page_server_p.wait()
grep_errors(os.path.join(self.__ddir(), "page-server.log"))
self.__page_server_p = None
if ret:
raise test_fail_exc("criu page-server exited with %d" % ret)
def restore(self):
r_opts = []
if self.__restore_sibling:
r_opts = ["--restore-sibling"]
self.__test.auto_reap = False
r_opts += self.__test.getropts()
if self.__join_ns:
r_opts.append("--join-ns")
r_opts.append("net:%s" % join_ns_file)
if self.__empty_ns:
r_opts += ['--empty-ns', 'net']
r_opts += ['--action-script', os.getcwd() + '/empty-netns-prep.sh']
if self.__dedup:
r_opts += ["--auto-dedup"]
self.__prev_dump_iter = None
criu_dir = os.path.dirname(os.getcwd())
if os.getenv("GCOV"):
r_opts.append('--external')
r_opts.append('mnt[zdtm]:%s' % criu_dir)
if self.__lazy_pages or self.__lazy_migrate:
lp_opts = []
if self.__remote_lazy_pages or self.__lazy_migrate:
lp_opts += [
"--page-server", "--port", "12345", "--address",
"127.0.0.1"
] + self.__tls
if self.__remote_lazy_pages:
ps_opts = [
"--pidfile", "ps.pid", "--port", "12345", "--lazy-pages"
] + self.__tls
self.__page_server_p = self.__criu_act("page-server",
opts=ps_opts,
nowait=True)
self.__lazy_pages_p = self.__criu_act("lazy-pages",
opts=lp_opts,
nowait=True)
r_opts += ["--lazy-pages"]
if self.__leave_stopped:
r_opts += ['--leave-stopped']
self.__criu_act("restore", opts=r_opts + ["--restore-detached"])
self.show_stats("restore")
if self.__leave_stopped:
pstree_check_stopped(self.__test.getpid())
pstree_signal(self.__test.getpid(), signal.SIGCONT)
@staticmethod
def check(feature):
return criu_cli.run(
"check", ["--no-default-config", "-v0", "--feature", feature],
opts['criu_bin']) == 0
@staticmethod
def available():
if not os.access(opts['criu_bin'], os.X_OK):
print("CRIU binary not found at %s" % opts['criu_bin'])
sys.exit(1)
def kill(self):
if self.__lazy_pages_p:
self.__lazy_pages_p.terminate()
print("criu lazy-pages exited with %s" %
self.__lazy_pages_p.wait())
grep_errors(os.path.join(self.__ddir(), "lazy-pages.log"))
self.__lazy_pages_p = None
if self.__page_server_p:
self.__page_server_p.terminate()
print("criu page-server exited with %s" %
self.__page_server_p.wait())
grep_errors(os.path.join(self.__ddir(), "page-server.log"))
self.__page_server_p = None
if self.__dump_process:
self.__dump_process.terminate()
print("criu dump exited with %s" % self.__dump_process.wait())
grep_errors(os.path.join(self.__ddir(), "dump.log"))
self.__dump_process = None
def try_run_hook(test, args):
hname = test.getname() + '.hook'
if os.access(hname, os.X_OK):
print("Running %s(%s)" % (hname, ', '.join(args)))
hook = subprocess.Popen([hname] + args)
if hook.wait() != 0:
raise test_fail_exc("hook " + " ".join(args))
#
# Step by step execution
#
do_sbs = False
def init_sbs():
if sys.stdout.isatty():
global do_sbs
do_sbs = True
else:
print("Can't do step-by-step in this runtime")
def sbs(what):
if do_sbs:
input("Pause at %s. Press Enter to continue." % what)
#
# Main testing entity -- dump (probably with pre-dumps) and restore
#
def iter_parm(opt, dflt):
x = ((opt or str(dflt)) + ":0").split(':')
return (range(0, int(x[0])), float(x[1]))
def cr(cr_api, test, opts):
if opts['nocr']:
return
cr_api.set_test(test)
iters = iter_parm(opts['iters'], 1)
for i in iters[0]:
pres = iter_parm(opts['pre'], 0)
for p in pres[0]:
if opts['snaps']:
cr_api.dump("dump", opts=["--leave-running", "--track-mem"])
else:
cr_api.dump("pre-dump")
try_run_hook(test, ["--post-pre-dump"])
test.pre_dump_notify()
time.sleep(pres[1])
sbs('pre-dump')
os.environ["ZDTM_TEST_PID"] = str(test.getpid())
if opts['norst']:
try_run_hook(test, ["--pre-dump"])
cr_api.dump("dump", opts=["--leave-running"])
else:
try_run_hook(test, ["--pre-dump"])
cr_api.dump("dump")
if not opts['lazy_migrate']:
test.gone()
else:
test.unlink_pidfile()
sbs('pre-restore')
try_run_hook(test, ["--pre-restore"])
cr_api.restore()
os.environ["ZDTM_TEST_PID"] = str(test.getpid())
os.environ["ZDTM_IMG_DIR"] = cr_api.logs()
try_run_hook(test, ["--post-restore"])
sbs('post-restore')
time.sleep(iters[1])
# Additional checks that can be done outside of test process
def get_visible_state(test):
maps = {}
files = {}
mounts = {}
if not getattr(test, "static", lambda: False)() or \
not getattr(test, "ns", lambda: False)():
return ({}, {}, {})
r = re.compile('^[0-9]+$')
pids = filter(lambda p: r.match(p),
os.listdir("/proc/%s/root/proc/" % test.getpid()))
for pid in pids:
files[pid] = set(
os.listdir("/proc/%s/root/proc/%s/fd" % (test.getpid(), pid)))
cmaps = [[0, 0, ""]]
last = 0
mapsfd = open("/proc/%s/root/proc/%s/maps" % (test.getpid(), pid))
for mp in mapsfd:
m = list(map(lambda x: int('0x' + x, 0), mp.split()[0].split('-')))
m.append(mp.split()[1])
f = "/proc/%s/root/proc/%s/map_files/%s" % (test.getpid(), pid,
mp.split()[0])
if os.access(f, os.F_OK):
st = os.lstat(f)
m.append(oct(st.st_mode))
if cmaps[last][1] == m[0] and cmaps[last][2] == m[2]:
cmaps[last][1] = m[1]
else:
cmaps.append(m)
last += 1
mapsfd.close()
maps[pid] = set(
map(lambda x: '%x-%x %s' % (x[0], x[1], " ".join(x[2:])), cmaps))
cmounts = []
try:
r = re.compile(
r"^\S+\s\S+\s\S+\s(\S+)\s(\S+)\s(\S+)\s[^-]*?(shared)?[^-]*?(master)?[^-]*?-"
)
with open("/proc/%s/root/proc/%s/mountinfo" %
(test.getpid(), pid)) as mountinfo:
for m in mountinfo:
cmounts.append(r.match(m).groups())
except IOError as e:
if e.errno != errno.EINVAL:
raise e
mounts[pid] = cmounts
return files, maps, mounts
def check_visible_state(test, state, opts):
new = get_visible_state(test)
for pid in state[0].keys():
fnew = new[0][pid]
fold = state[0][pid]
if fnew != fold:
print("%s: Old files lost: %s" % (pid, fold - fnew))
print("%s: New files appeared: %s" % (pid, fnew - fold))
raise test_fail_exc("fds compare")
old_maps = state[1][pid]
new_maps = new[1][pid]
if os.getenv("COMPAT_TEST"):
# the vsyscall vma isn't unmapped from x32 processes
vsyscall = u"ffffffffff600000-ffffffffff601000 r-xp"
if vsyscall in new_maps and vsyscall not in old_maps:
new_maps.remove(vsyscall)
if old_maps != new_maps:
print("%s: Old maps lost: %s" % (pid, old_maps - new_maps))
print("%s: New maps appeared: %s" % (pid, new_maps - old_maps))
if not opts['fault']: # skip parasite blob
raise test_fail_exc("maps compare")
old_mounts = state[2][pid]
new_mounts = new[2][pid]
for i in range(len(old_mounts)):
m = old_mounts.pop(0)
if m in new_mounts:
new_mounts.remove(m)
else:
old_mounts.append(m)
if old_mounts or new_mounts:
print("%s: Old mounts lost: %s" % (pid, old_mounts))
print("%s: New mounts appeared: %s" % (pid, new_mounts))
raise test_fail_exc("mounts compare")
if '--link-remap' in test.getdopts():
import glob
link_remap_list = glob.glob(
os.path.dirname(test.getname()) + '/link_remap*')
if link_remap_list:
print("%s: link-remap files left: %s" %
(test.getname(), link_remap_list))
raise test_fail_exc("link remaps left")
class noop_freezer:
def __init__(self):
self.kernel = False
def attach(self):
pass
def freeze(self):
pass
def thaw(self):
pass
def getdopts(self):
return []
def getropts(self):
return []
class cg_freezer:
def __init__(self, path, state):
self.__path = '/sys/fs/cgroup/freezer/' + path
self.__state = state
self.kernel = True
def attach(self):
if not os.access(self.__path, os.F_OK):
os.makedirs(self.__path)
with open(self.__path + '/tasks', 'w') as f:
f.write('0')
def __set_state(self, state):
with open(self.__path + '/freezer.state', 'w') as f:
f.write(state)
def freeze(self):
if self.__state.startswith('f'):
self.__set_state('FROZEN')
def thaw(self):
if self.__state.startswith('f'):
self.__set_state('THAWED')
def getdopts(self):
return ['--freeze-cgroup', self.__path, '--manage-cgroups']
def getropts(self):
return ['--manage-cgroups']
def get_freezer(desc):
if not desc:
return noop_freezer()
fd = desc.split(':')
fr = cg_freezer(path=fd[0], state=fd[1])
return fr
def cmp_ns(ns1, match, ns2, msg):
ns1_ino = os.stat(ns1).st_ino
ns2_ino = os.stat(ns2).st_ino
if eval("%r %s %r" % (ns1_ino, match, ns2_ino)):
print("%s match (%r %s %r) fail" % (msg, ns1_ino, match, ns2_ino))
raise test_fail_exc("%s compare" % msg)
def check_joinns_state(t):
cmp_ns("/proc/%s/ns/net" % t.getpid(), "!=", join_ns_file, "join-ns")
def pstree_each_pid(root_pid):
f_children_path = "/proc/{0}/task/{0}/children".format(root_pid)
child_pids = []
try:
with open(f_children_path, "r") as f_children:
pid_line = f_children.readline().strip(" \n")
if pid_line:
child_pids += pid_line.split(" ")
except Exception as e:
print("Unable to read /proc/*/children: %s" % e)
return # process is dead
yield root_pid
for child_pid in child_pids:
for pid in pstree_each_pid(child_pid):
yield pid
def is_proc_stopped(pid):
def get_thread_status(thread_dir):
try:
with open(os.path.join(thread_dir, "status")) as f_status:
for line in f_status.readlines():
if line.startswith("State:"):
return line.split(":", 1)[1].strip().split(" ")[0]
except Exception as e:
print("Unable to read a thread status: %s" % e)
pass # process is dead
return None
def is_thread_stopped(status):
return (status is None) or (status == "T") or (status == "Z")
tasks_dir = "/proc/%s/task" % pid
thread_dirs = []
try:
thread_dirs = os.listdir(tasks_dir)
except Exception as e:
print("Unable to read threads: %s" % e)
pass # process is dead
for thread_dir in thread_dirs:
thread_status = get_thread_status(os.path.join(tasks_dir, thread_dir))
if not is_thread_stopped(thread_status):
return False
if not is_thread_stopped(get_thread_status("/proc/%s" % pid)):
return False
return True
def pstree_check_stopped(root_pid):
for pid in pstree_each_pid(root_pid):
if not is_proc_stopped(pid):
raise test_fail_exc("CRIU --leave-stopped %s" % pid)
def pstree_signal(root_pid, signal):
for pid in pstree_each_pid(root_pid):
try:
os.kill(int(pid), signal)
except Exception as e:
print("Unable to kill %d: %s" % (pid, e))
pass # process is dead
def do_run_test(tname, tdesc, flavs, opts):
tcname = tname.split('/')[0]
tclass = test_classes.get(tcname, None)
if not tclass:
print("Unknown test class %s" % tcname)
return
if opts['report']:
init_report(opts['report'])
if opts['sbs']:
init_sbs()
fcg = get_freezer(opts['freezecg'])
for f in flavs:
print_sep("Run %s in %s" % (tname, f))
if opts['dry_run']:
continue
flav = flavors[f](opts)
t = tclass(tname, tdesc, flav, fcg)
cr_api = criu(opts)
try:
t.start()
s = get_visible_state(t)
try:
cr(cr_api, t, opts)
except test_fail_expected_exc as e:
if e.cr_action == "dump":
t.stop()
else:
check_visible_state(t, s, opts)
if opts['join_ns']:
check_joinns_state(t)
t.stop()
cr_api.fini()
try_run_hook(t, ["--clean"])
except test_fail_exc as e:
print_sep("Test %s FAIL at %s" % (tname, e.step), '#')
t.print_output()
t.kill()
cr_api.kill()
try_run_hook(t, ["--clean"])
if cr_api.logs():
add_to_report(cr_api.logs(),
tname.replace('/', '_') + "_" + f + "/images")
if opts['keep_img'] == 'never':
cr_api.cleanup()
# When option --keep-going not specified this exit
# does two things: exits from subprocess and aborts the
# main script execution on the 1st error met
sys.exit(encode_flav(f))
else:
if opts['keep_img'] != 'always':
cr_api.cleanup()
print_sep("Test %s PASS" % tname)
class Launcher:
def __init__(self, opts, nr_tests):
self.__opts = opts
self.__total = nr_tests
self.__runtest = 0
self.__nr = 0
self.__max = int(opts['parallel'] or 1)
self.__subs = {}
self.__fail = False
self.__file_report = None
self.__junit_file = None
self.__junit_test_cases = None
self.__failed = []
self.__nr_skip = 0
if self.__max > 1 and self.__total > 1:
self.__use_log = True
elif opts['report']:
self.__use_log = True
else:
self.__use_log = False
if opts['report'] and (opts['keep_going'] or self.__total == 1):
global TestSuite, TestCase
from junit_xml import TestSuite, TestCase
now = datetime.datetime.now()
att = 0
reportname = os.path.join(report_dir, "criu-testreport.tap")
junitreport = os.path.join(report_dir, "criu-testreport.xml")
while os.access(reportname, os.F_OK) or os.access(
junitreport, os.F_OK):
reportname = os.path.join(report_dir,
"criu-testreport" + ".%d.tap" % att)
junitreport = os.path.join(report_dir,
"criu-testreport" + ".%d.xml" % att)
att += 1
self.__junit_file = open(junitreport, 'a')
self.__junit_test_cases = []
self.__file_report = open(reportname, 'a')
print(u"TAP version 13", file=self.__file_report)
print(u"# Hardware architecture: " + arch, file=self.__file_report)
print(u"# Timestamp: " + now.strftime("%Y-%m-%d %H:%M") +
" (GMT+1)",
file=self.__file_report)
print(u"# ", file=self.__file_report)
print(u"1.." + str(nr_tests), file=self.__file_report)
with open("/proc/sys/kernel/tainted") as taintfd:
self.__taint = taintfd.read()
if int(self.__taint, 0) != 0:
print("The kernel is tainted: %r" % self.__taint)
if not opts["ignore_taint"]:
raise Exception("The kernel is tainted: %r" % self.__taint)
def __show_progress(self, msg):
perc = int(self.__nr * 16 / self.__total)
print("=== Run %d/%d %s %s" %
(self.__nr, self.__total, '=' * perc + '-' * (16 - perc), msg))
def skip(self, name, reason):
print("Skipping %s (%s)" % (name, reason))
self.__nr += 1
self.__runtest += 1
self.__nr_skip += 1
if self.__junit_test_cases is not None:
tc = TestCase(name)
tc.add_skipped_info(reason)
self.__junit_test_cases.append(tc)
if self.__file_report:
testline = u"ok %d - %s # SKIP %s" % (self.__runtest, name, reason)
print(testline, file=self.__file_report)
def run_test(self, name, desc, flavor):
if len(self.__subs) >= self.__max:
self.wait()
with open("/proc/sys/kernel/tainted") as taintfd:
taint = taintfd.read()
if self.__taint != taint:
raise Exception("The kernel is tainted: %r (%r)" %
(taint, self.__taint))
if test_flag(desc, 'excl'):
self.wait_all()
self.__nr += 1
self.__show_progress(name)
nd = ('nocr', 'norst', 'pre', 'iters', 'page_server', 'sibling',
'stop', 'empty_ns', 'fault', 'keep_img', 'report', 'snaps',
'sat', 'script', 'rpc', 'lazy_pages', 'join_ns', 'dedup', 'sbs',
'freezecg', 'user', 'dry_run', 'noauto_dedup',
'remote_lazy_pages', 'show_stats', 'lazy_migrate',
'tls', 'criu_bin', 'crit_bin')
arg = repr((name, desc, flavor, {d: self.__opts[d] for d in nd}))
if self.__use_log:
logf = name.replace('/', '_') + ".log"
log = open(logf, "w")
else:
logf = None
log = None
sub = subprocess.Popen(["./zdtm_ct", "zdtm.py"],
env=dict(os.environ, CR_CT_TEST_INFO=arg),
stdout=log,
stderr=subprocess.STDOUT,
close_fds=True)
self.__subs[sub.pid] = {
'sub': sub,
'log': logf,
'name': name,
"start": time.time()
}
if test_flag(desc, 'excl'):
self.wait()
def __wait_one(self, flags):
pid = -1
status = -1
signal.alarm(10)
while True:
try:
pid, status = os.waitpid(0, flags)
except OSError as e:
if e.errno == errno.EINTR:
subprocess.Popen(["ps", "axf"]).wait()
continue
signal.alarm(0)
raise e
else:
break
signal.alarm(0)
self.__runtest += 1
if pid != 0:
sub = self.__subs.pop(pid)
tc = None
if self.__junit_test_cases is not None:
tc = TestCase(sub['name'],
elapsed_sec=time.time() - sub['start'])
self.__junit_test_cases.append(tc)
if status != 0:
self.__fail = True
failed_flavor = decode_flav(os.WEXITSTATUS(status))
self.__failed.append([sub['name'], failed_flavor])
if self.__file_report:
testline = u"not ok %d - %s # flavor %s" % (
self.__runtest, sub['name'], failed_flavor)
with open(sub['log']) as sublog:
output = sublog.read()
details = {'output': output}
tc.add_error_info(output=output)
print(testline, file=self.__file_report)
print("%s" % yaml.safe_dump(details,
explicit_start=True,
explicit_end=True,
default_style='|'),
file=self.__file_report)
if sub['log']:
add_to_output(sub['log'])
else:
if self.__file_report:
testline = u"ok %d - %s" % (self.__runtest, sub['name'])
print(testline, file=self.__file_report)
if sub['log']:
with open(sub['log']) as sublog:
print("%s" % sublog.read().encode(
'ascii', 'ignore').decode('utf-8'))
os.unlink(sub['log'])
return True
return False
def __wait_all(self):
while self.__subs:
self.__wait_one(0)
def wait(self):
self.__wait_one(0)
while self.__subs:
if not self.__wait_one(os.WNOHANG):
break
if self.__fail and not opts['keep_going']:
raise test_fail_exc('')
def wait_all(self):
self.__wait_all()
if self.__fail and not opts['keep_going']:
raise test_fail_exc('')
def finish(self):
self.__wait_all()
if not opts['fault'] and check_core_files():
self.__fail = True
if self.__file_report:
ts = TestSuite(opts['title'], self.__junit_test_cases,
os.getenv("NODE_NAME"))
self.__junit_file.write(TestSuite.to_xml_string([ts]))
self.__junit_file.close()
self.__file_report.close()
if opts['keep_going']:
if self.__fail:
print_sep(
"%d TEST(S) FAILED (TOTAL %d/SKIPPED %d)" %
(len(self.__failed), self.__total, self.__nr_skip), "#")
for failed in self.__failed:
print(" * %s(%s)" % (failed[0], failed[1]))
else:
print_sep(
"ALL TEST(S) PASSED (TOTAL %d/SKIPPED %d)" %
(self.__total, self.__nr_skip), "#")
if self.__fail:
print_sep("FAIL", "#")
sys.exit(1)
def all_tests(opts):
with open(opts['set'] + '.desc') as fd:
desc = eval(fd.read())
files = []
mask = stat.S_IFREG | stat.S_IXUSR
for d in os.walk(desc['dir']):
for f in d[2]:
fp = os.path.join(d[0], f)
st = os.lstat(fp)
if (st.st_mode & mask) != mask:
continue
if stat.S_IFMT(st.st_mode) in [stat.S_IFLNK, stat.S_IFSOCK]:
continue
files.append(fp)
excl = list(map(lambda x: os.path.join(desc['dir'], x), desc['exclude']))
tlist = filter(
lambda x: not x.endswith('.checkskip') and not x.endswith('.hook') and
x not in excl, map(lambda x: x.strip(), files))
return tlist
# Descriptor for abstract test not in list
default_test = {}
def get_test_desc(tname):
d_path = tname + '.desc'
if os.access(d_path, os.F_OK) and os.path.getsize(d_path) > 0:
with open(d_path) as fd:
return eval(fd.read())
return default_test
def self_checkskip(tname):
chs = tname + '.checkskip'
if os.access(chs, os.X_OK):
ch = subprocess.Popen([chs])
return not ch.wait() == 0
return False
def print_fname(fname, typ):
print("=[%s]=> %s" % (typ, fname))
def print_sep(title, sep="=", width=80):
print((" " + title + " ").center(width, sep))
def print_error(line):
line = line.rstrip()
print(line.encode('utf-8'))
if line.endswith('>'): # combine pie output
return True
return False
def grep_errors(fname):
first = True
print_next = False
before = []
with open(fname, errors='replace') as fd:
for l in fd:
before.append(l)
if len(before) > 5:
before.pop(0)
if "Error" in l or "Warn" in l:
if first:
print_fname(fname, 'log')
print_sep("grep Error", "-", 60)
first = False
for i in before:
print_next = print_error(i)
before = []
else:
if print_next:
print_next = print_error(l)
before = []
if not first:
print_sep("ERROR OVER", "-", 60)
def run_tests(opts):
excl = None
features = {}
if opts['pre'] or opts['snaps']:
if not criu.check("mem_dirty_track"):
print("Tracking memory is not available")
return
if opts['all']:
torun = all_tests(opts)
run_all = True
elif opts['tests']:
r = re.compile(opts['tests'])
torun = filter(lambda x: r.match(x), all_tests(opts))
run_all = True
elif opts['test']:
torun = opts['test']
run_all = False
elif opts['from']:
if not os.access(opts['from'], os.R_OK):
print("No such file")
return
with open(opts['from']) as fd:
torun = map(lambda x: x.strip(), fd)
opts['keep_going'] = False
run_all = True
else:
print("Specify test with -t <name> or -a")
return
torun = list(torun)
if opts['keep_going'] and len(torun) < 2:
print(
"[WARNING] Option --keep-going is more useful when running multiple tests"
)
opts['keep_going'] = False
if opts['exclude']:
excl = re.compile(".*(" + "|".join(opts['exclude']) + ")")
print("Compiled exclusion list")
if opts['report']:
init_report(opts['report'])
if opts['parallel'] and opts['freezecg']:
print("Parallel launch with freezer not supported")
opts['parallel'] = None
if opts['join_ns']:
if subprocess.Popen(["ip", "netns", "add", "zdtm_netns"]).wait():
raise Exception("Unable to create a network namespace")
if subprocess.Popen([
"ip", "netns", "exec", "zdtm_netns", "ip", "link", "set", "up",
"dev", "lo"
]).wait():
raise Exception("ip link set up dev lo")
if opts['lazy_pages'] or opts['remote_lazy_pages'] or opts['lazy_migrate']:
uffd = criu.check("uffd")
uffd_noncoop = criu.check("uffd-noncoop")
if not uffd:
raise Exception(
"UFFD is not supported, cannot run with --lazy-pages")
if not uffd_noncoop:
# Most tests will work with 4.3 - 4.11
print(
"[WARNING] Non-cooperative UFFD is missing, some tests might spuriously fail"
)
launcher = Launcher(opts, len(torun))
try:
for t in torun:
global arch
if excl and excl.match(t):
launcher.skip(t, "exclude")
continue
tdesc = get_test_desc(t)
if tdesc.get('arch', arch) != arch:
launcher.skip(t, "arch %s" % tdesc['arch'])
continue
if test_flag(tdesc, 'reqrst') and opts['norst']:
launcher.skip(t, "restore stage is required")
continue
if run_all and test_flag(tdesc, 'noauto'):
launcher.skip(t, "manual run only")
continue
feat_list = tdesc.get('feature', "")
for feat in feat_list.split():
if feat not in features:
print("Checking feature %s" % feat)
features[feat] = criu.check(feat)
if not features[feat]:
launcher.skip(t, "no %s feature" % feat)
feat_list = None
break
if feat_list is None:
continue
if self_checkskip(t):
launcher.skip(t, "checkskip failed")
continue
if opts['user']:
if test_flag(tdesc, 'suid'):
launcher.skip(t, "suid test in user mode")
continue
if test_flag(tdesc, 'nouser'):
launcher.skip(t, "criu root prio needed")
continue
if opts['join_ns']:
if test_flag(tdesc, 'samens'):
launcher.skip(t, "samens test in the same namespace")
continue
if opts['lazy_pages'] or opts['remote_lazy_pages'] or opts[
'lazy_migrate']:
if test_flag(tdesc, 'nolazy'):
launcher.skip(t, "lazy pages are not supported")
continue
if opts['remote_lazy_pages']:
if test_flag(tdesc, 'noremotelazy'):
launcher.skip(t, "remote lazy pages are not supported")
continue
test_flavs = tdesc.get('flavor', 'h ns uns').split()
opts_flavs = (opts['flavor'] or 'h,ns,uns').split(',')
if opts_flavs != ['best']:
run_flavs = set(test_flavs) & set(opts_flavs)
else:
run_flavs = set([test_flavs.pop()])
if not criu.check("userns"):
run_flavs -= set(['uns'])
if opts['user']:
# FIXME -- probably uns will make sense
run_flavs -= set(['ns', 'uns'])
# remove ns and uns flavor in join_ns
if opts['join_ns']:
run_flavs -= set(['ns', 'uns'])
if opts['empty_ns']:
run_flavs -= set(['h'])
if run_flavs:
launcher.run_test(t, tdesc, run_flavs)
else:
launcher.skip(t, "no flavors")
finally:
launcher.finish()
if opts['join_ns']:
subprocess.Popen(["ip", "netns", "delete", "zdtm_netns"]).wait()
sti_fmt = "%-40s%-10s%s"
def show_test_info(t):
tdesc = get_test_desc(t)
flavs = tdesc.get('flavor', '')
return sti_fmt % (t, flavs, tdesc.get('flags', ''))
def list_tests(opts):
tlist = all_tests(opts)
if opts['info']:
print(sti_fmt % ('Name', 'Flavors', 'Flags'))
tlist = map(lambda x: show_test_info(x), tlist)
print('\n'.join(tlist))
class group:
def __init__(self, tname, tdesc):
self.__tests = [tname]
self.__desc = tdesc
self.__deps = set()
def __is_mergeable_desc(self, desc):
# For now make it full match
if self.__desc.get('flags') != desc.get('flags'):
return False
if self.__desc.get('flavor') != desc.get('flavor'):
return False
if self.__desc.get('arch') != desc.get('arch'):
return False
if self.__desc.get('opts') != desc.get('opts'):
return False
if self.__desc.get('feature') != desc.get('feature'):
return False
return True
def merge(self, tname, tdesc):
if not self.__is_mergeable_desc(tdesc):
return False
self.__deps |= set(tdesc.get('deps', []))
self.__tests.append(tname)
return True
def size(self):
return len(self.__tests)
# common method to write a "meta" auxiliary script (hook/checkskip)
# which will call all tests' scripts in turn
def __dump_meta(self, fname, ext):
scripts = filter(lambda names: os.access(names[1], os.X_OK),
map(lambda test: (test, test + ext), self.__tests))
if scripts:
f = open(fname + ext, "w")
f.write("#!/bin/sh -e\n")
for test, script in scripts:
f.write("echo 'Running %s for %s'\n" % (ext, test))
f.write('%s "$@"\n' % script)
f.write("echo 'All %s scripts OK'\n" % ext)
f.close()
os.chmod(fname + ext, 0o700)
def dump(self, fname):
f = open(fname, "w")
for t in self.__tests:
f.write(t + '\n')
f.close()
os.chmod(fname, 0o700)
if len(self.__desc) or len(self.__deps):
f = open(fname + '.desc', "w")
if len(self.__deps):
self.__desc['deps'] = list(self.__deps)
f.write(repr(self.__desc))
f.close()
# write "meta" .checkskip and .hook scripts
self.__dump_meta(fname, '.checkskip')
self.__dump_meta(fname, '.hook')
def group_tests(opts):
excl = None
groups = []
pend_groups = []
maxs = int(opts['max_size'])
if not os.access("groups", os.F_OK):
os.mkdir("groups")
tlist = all_tests(opts)
random.shuffle(tlist)
if opts['exclude']:
excl = re.compile(".*(" + "|".join(opts['exclude']) + ")")
print("Compiled exclusion list")
for t in tlist:
if excl and excl.match(t):
continue
td = get_test_desc(t)
for g in pend_groups:
if g.merge(t, td):
if g.size() == maxs:
pend_groups.remove(g)
groups.append(g)
break
else:
g = group(t, td)
pend_groups.append(g)
groups += pend_groups
nr = 0
suf = opts['name'] or 'group'
for g in groups:
if maxs > 1 and g.size() == 1: # Not much point in group test for this
continue
fn = os.path.join("groups", "%s.%d" % (suf, nr))
g.dump(fn)
nr += 1
print("Generated %d group(s)" % nr)
def clean_stuff(opts):
print("Cleaning %s" % opts['what'])
if opts['what'] == 'nsroot':
for f in flavors:
f = flavors[f]
f.clean()
#
# main() starts here
#
if 'CR_CT_TEST_INFO' in os.environ:
# Fork here, since we're new pidns init and are supposed to
# collect this namespace's zombies
status = 0
pid = os.fork()
if pid == 0:
tinfo = eval(os.environ['CR_CT_TEST_INFO'])
do_run_test(tinfo[0], tinfo[1], tinfo[2], tinfo[3])
else:
while True:
wpid, status = os.wait()
if wpid == pid:
if os.WIFEXITED(status):
status = os.WEXITSTATUS(status)
else:
status = 1
break
sys.exit(status)
p = argparse.ArgumentParser("CRIU test suite")
p.add_argument("--debug",
help="Print what's being executed",
action='store_true')
p.add_argument("--set", help="Which set of tests to use", default='zdtm')
sp = p.add_subparsers(help="Use --help for list of actions")
rp = sp.add_parser("run", help="Run test(s)")
rp.set_defaults(action=run_tests)
rp.add_argument("-a", "--all", action='store_true')
rp.add_argument("-t", "--test", help="Test name", action='append')
rp.add_argument("-T", "--tests", help="Regexp")
rp.add_argument("-F", "--from", help="From file")
rp.add_argument("-f", "--flavor", help="Flavor to run")
rp.add_argument("-x",
"--exclude",
help="Exclude tests from --all run",
action='append')
rp.add_argument("--sibling",
help="Restore tests as siblings",
action='store_true')
rp.add_argument("--join-ns",
help="Restore tests and join existing namespace",
action='store_true')
rp.add_argument("--empty-ns",
help="Restore tests in empty net namespace",
action='store_true')
rp.add_argument("--pre", help="Do some pre-dumps before dump (n[:pause])")
rp.add_argument("--snaps",
help="Instead of pre-dumps do full dumps",
action='store_true')
rp.add_argument("--dedup",
help="Auto-deduplicate images on iterations",
action='store_true')
rp.add_argument("--noauto-dedup",
help="Manual deduplicate images on iterations",
action='store_true')
rp.add_argument("--nocr",
help="Do not CR anything, just check test works",
action='store_true')
rp.add_argument("--norst",
help="Don't restore tasks, leave them running after dump",
action='store_true')
rp.add_argument("--stop",
help="Check that --leave-stopped option stops ps tree.",
action='store_true')
rp.add_argument("--iters",
help="Do CR cycle several times before check (n[:pause])")
rp.add_argument("--fault", help="Test fault injection")
rp.add_argument(
"--sat",
help="Generate criu strace-s for sat tool (restore is fake, images are kept)",
action='store_true')
rp.add_argument(
"--sbs",
help="Do step-by-step execution, asking user for keypress to continue",
action='store_true')
rp.add_argument("--freezecg", help="Use freeze cgroup (path:state)")
rp.add_argument("--user", help="Run CRIU as regular user", action='store_true')
rp.add_argument("--rpc",
help="Run CRIU via RPC rather than CLI",
action='store_true')
rp.add_argument("--page-server",
help="Use page server dump",
action='store_true')
rp.add_argument("--remote",
help="Use remote option for diskless C/R",
action='store_true')
rp.add_argument("-p", "--parallel", help="Run test in parallel")
rp.add_argument("--dry-run",
help="Don't run tests, just pretend to",
action='store_true')
rp.add_argument("--script", help="Add script to get notified by criu")
rp.add_argument("-k",
"--keep-img",
help="Whether or not to keep images after test",
choices=['always', 'never', 'failed'],
default='failed')
rp.add_argument("--report", help="Generate summary report in directory")
rp.add_argument("--keep-going",
help="Keep running tests in spite of failures",
action='store_true')
rp.add_argument("--ignore-taint",
help="Don't care about a non-zero kernel taint flag",
action='store_true')
rp.add_argument("--lazy-pages",
help="restore pages on demand",
action='store_true')
rp.add_argument("--lazy-migrate",
help="restore pages on demand",
action='store_true')
rp.add_argument("--remote-lazy-pages",
help="simulate lazy migration",
action='store_true')
rp.add_argument("--tls", help="use TLS for migration", action='store_true')
rp.add_argument("--title", help="A test suite title", default="criu")
rp.add_argument("--show-stats",
help="Show criu statistics",
action='store_true')
rp.add_argument("--criu-bin",
help="Path to criu binary",
default='../criu/criu')
rp.add_argument("--crit-bin",
help="Path to crit binary",
default='../crit/crit')
lp = sp.add_parser("list", help="List tests")
lp.set_defaults(action=list_tests)
lp.add_argument('-i',
'--info',
help="Show more info about tests",
action='store_true')
gp = sp.add_parser("group", help="Generate groups")
gp.set_defaults(action=group_tests)
gp.add_argument("-m", "--max-size", help="Maximum number of tests in group")
gp.add_argument("-n", "--name", help="Common name for group tests")
gp.add_argument("-x",
"--exclude",
help="Exclude tests from --all run",
action='append')
cp = sp.add_parser("clean", help="Clean something")
cp.set_defaults(action=clean_stuff)
cp.add_argument("what", choices=['nsroot'])
opts = vars(p.parse_args())
if opts.get('sat', False):
opts['keep_img'] = 'always'
if opts['debug']:
sys.settrace(traceit)
if opts['action'] == 'run':
criu.available()
for tst in test_classes.values():
tst.available()
opts['action'](opts)
for tst in test_classes.values():
tst.cleanup()
|