summaryrefslogtreecommitdiff
path: root/src/tests/client/test-client.py
blob: a3f094532be8c37e8dee662059964a69f166dd66 (plain)
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
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
#!/usr/bin/env python
# coding: utf-8

from __future__ import print_function

###############################################################################
#
# This test starts NetworkManager stub service in a user D-Bus session,
# and runs nmcli against it. The output is recorded and compared to a pre-generated
# expected output (src/tests/client/test-client.check-on-disk/*.expected) which
# is also committed to git.
#
###############################################################################
#
# HOWTO: Regenerate output
#
# When adjusting the tests, or when making changes to nmcli that intentionally
# change the output, the expected output must be regenerated.
#
# For that, you'd setup your system correctly (see SETUP below) and then simply:
#
#  $ NM_TEST_REGENERATE=1 make check-local-tests-client
#    # Or `NM_TEST_REGENERATE=1 make check -j 10`
#  $ git diff ... ; git add ...
#    # The previous step regenerated the expected output. Review the changes
#    # and consider whether they are correct. Then commit the changes to git.
#
#   With meson, you can do
#     $ meson -Ddocs=true --prefix=/tmp/nm1 build
#     $ ninja -C build
#     $ ninja -C build install
#     $ NM_TEST_REGENERATE=1 ninja -C build test
#
# Beware that you need to install the sources, and beware to choose a prefix that doesn't
# mess up your system (see SETUP below).
#
# SETUP: For regenerating the output, the translations must work. First
# test whether the following works:
#
#  1) LANG=pl_PL.UTF-8 /usr/bin/nmcli --version
#    # Ensure that Polish output works for the system-installed nmcli.
#    # If not, you should ensure that `locale -a` reports the Polish
#    # locale. If that is not the case, how to enable the locale depends on
#    # your distro.
#    #
#    # On Debian, you might do:
#    #   sed -i 's/^# \(pl_PL.UTF-8 .*\)$/\1/p' /etc/locale.gen
#    #   locale-gen pl_PL.UTF-8
#    # On Fedora, you might install `glibc-langpack-pl` package.
#
#  2) LANG=pl_PL.UTF-8 ./src/nmcli/nmcli --version
#    # Ensure that the built nmcli has Polish locale working. If not,
#    # you probably need to first `make install` the application at the
#    # correct prefix. Take care to configure the build with the desired
#    # prefix, like `./configure --prefix=/opt/tmp`. Usually, you want to avoid
#    # using /usr as prefix, because that might overwrite files from your
#    # package management system.
#
###############################################################################
#
# Environment variables to configure test:

# (optional) The build dir. Optional, mainly used to find the nmcli binary (in case
# ENV_NM_TEST_CLIENT_NMCLI_PATH is not set.
ENV_NM_TEST_CLIENT_BUILDDIR = "NM_TEST_CLIENT_BUILDDIR"

# (optional) Path to nmcli. By default, it looks for nmcli in build dir.
# In particular, you can test also a nmcli binary installed somewhere else.
ENV_NM_TEST_CLIENT_NMCLI_PATH = "NM_TEST_CLIENT_NMCLI_PATH"

# (optional) Path to nm-cloud-setup. By default, it looks for nm-cloud-setup
# in build dir.
ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH = "NM_TEST_CLIENT_CLOUD_SETUP_PATH"

# (optional) The test also compares tranlsated output (l10n). This requires,
# that you first install the translation in the right place. So, by default,
# if a test for a translation fails, it will mark the test as skipped, and not
# fail the tests. Under the assumption, that the test cannot succeed currently.
# By setting NM_TEST_CLIENT_CHECK_L10N=1, you can force a failure of the test.
ENV_NM_TEST_CLIENT_CHECK_L10N = "NM_TEST_CLIENT_CHECK_L10N"

# Regenerate the .expected files. Instead of asserting, rewrite the files
# on disk with the expected output.
ENV_NM_TEST_REGENERATE = "NM_TEST_REGENERATE"

# whether the file location should include the line number. That is useful
# only for debugging, to correlate the expected output with the test.
# Obviously, since the expected output is commited to git without line numbers,
# you'd have to first NM_TEST_REGENERATE the test expected data, with line
# numbers enabled.
ENV_NM_TEST_WITH_LINENO = "NM_TEST_WITH_LINENO"

ENV_NM_TEST_ASAN_OPTIONS = "NM_TEST_ASAN_OPTIONS"
ENV_NM_TEST_LSAN_OPTIONS = "NM_TEST_LSAN_OPTIONS"
ENV_NM_TEST_UBSAN_OPTIONS = "NM_TEST_UBSAN_OPTIONS"

# Run nmcli under valgrind. If unset, we honor NMTST_USE_VALGRIND instead.
# Valgrind is always disabled, if NM_TEST_REGENERATE is enabled.
ENV_NM_TEST_VALGRIND = "NM_TEST_VALGRIND"

ENV_LIBTOOL = "LIBTOOL"

###############################################################################

import collections
import dbus
import dbus.mainloop.glib
import dbus.service
import errno
import fcntl
import io
import itertools
import os
import random
import re
import shlex
import signal
import socket
import subprocess
import sys
import tempfile
import time
import unittest

import gi

try:
    from gi.repository import GLib
except ImportError:
    GLib = None

try:
    gi.require_version("NM", "1.0")
except ValueError:
    NM = None
else:
    try:
        from gi.repository import NM
    except ImportError:
        NM = None

try:
    import pexpect
except ImportError:
    pexpect = None

try:
    from http.server import HTTPServer
    from http.server import BaseHTTPRequestHandler
    from http.client import HTTPConnection, HTTPResponse
except ImportError:
    HTTPServer = None


###############################################################################


class PathConfiguration:
    @staticmethod
    def srcdir():
        # this is the directory where the test script itself lies.
        # Based on this directory, we find other parts that we expect
        # in the source repository.
        return os.path.dirname(os.path.abspath(__file__))

    @staticmethod
    def top_srcdir():
        return os.path.abspath(PathConfiguration.srcdir() + "/../../..")

    @staticmethod
    def test_networkmanager_service_path():
        v = os.path.abspath(
            PathConfiguration.top_srcdir() + "/tools/test-networkmanager-service.py"
        )
        assert os.path.exists(v), 'Cannot find test server at "%s"' % (v)
        return v

    @staticmethod
    def test_cloud_meta_mock_path():
        v = os.path.abspath(
            PathConfiguration.top_srcdir() + "/tools/test-cloud-meta-mock.py"
        )
        assert os.path.exists(v), 'Cannot find cloud metadata mock server at "%s"' % (v)
        return v

    @staticmethod
    def canonical_script_filename():
        p = "src/tests/client/test-client.py"
        assert (PathConfiguration.top_srcdir() + "/" + p) == os.path.abspath(__file__)
        return p


###############################################################################

dbus_session_inited = False

_DEFAULT_ARG = object()
_UNSTABLE_OUTPUT = object()

###############################################################################


class Util:

    _signal_no_lookup = {
        1: "SIGHUP",
        2: "SIGINT",
        3: "SIGQUIT",
        4: "SIGILL",
        5: "SIGTRAP",
        6: "SIGABRT",
        8: "SIGFPE",
        9: "SIGKILL",
        11: "SIGSEGV",
        12: "SIGSYS",
        13: "SIGPIPE",
        14: "SIGALRM",
        15: "SIGTERM",
        16: "SIGURG",
        17: "SIGSTOP",
        18: "SIGTSTP",
        19: "SIGCONT",
        20: "SIGCHLD",
        21: "SIGTTIN",
        22: "SIGTTOU",
        23: "SIGPOLL",
        24: "SIGXCPU",
        25: "SIGXFSZ",
        26: "SIGVTALRM",
        27: "SIGPROF",
        30: "SIGUSR1",
        31: "SIGUSR2",
    }

    @classmethod
    def signal_no_to_str(cls, sig):
        s = cls._signal_no_lookup.get(sig, None)
        if s is None:
            return "<unknown %d>" % (sig)
        return s

    @staticmethod
    def python_has_version(major, minor=0):
        return sys.version_info[0] > major or (
            sys.version_info[0] == major and sys.version_info[1] >= minor
        )

    @staticmethod
    def is_string(s):
        if Util.python_has_version(3):
            t = str
        else:
            t = basestring
        return isinstance(s, t)

    @staticmethod
    def is_bool(s, defval=False):
        if s is None:
            return defval
        if isinstance(s, int):
            return s != 0
        if isinstance(s, str):
            if s.lower() in ["1", "y", "yes", "true", "on"]:
                return True
            if s.lower() in ["0", "n", "no", "false", "off"]:
                return False
        raise ValueError('Argument "%s" is not a boolean' % (s,))

    @staticmethod
    def as_bytes(s):
        if Util.is_string(s):
            return s.encode("utf-8")
        assert isinstance(s, bytes)
        return s

    @staticmethod
    def memoize_nullary(nullary_func):
        result = []

        def closure():
            if not result:
                result.append(nullary_func())
            return result[0]

        return closure

    _find_unsafe = re.compile(
        r"[^\w@%+=:,./-]", re.ASCII if sys.version_info[0] >= 3 else 0
    ).search

    @staticmethod
    def shlex_quote(s):
        # Reimplement shlex.quote().
        if Util.python_has_version(3, 3):
            return shlex.quote(s)
        if not s:
            return "''"
        if Util._find_unsafe(s) is None:
            return s
        return "'" + s.replace("'", "'\"'\"'") + "'"

    @staticmethod
    def shlex_join(args):
        # Reimplement shlex.join()
        return " ".join(Util.shlex_quote(s) for s in args)

    @staticmethod
    def popen_wait(p, timeout=0):
        (res, b_stdout, b_stderr) = Util.popen_wait_read(
            p, timeout=timeout, read_std_pipes=False
        )
        return res

    @staticmethod
    def popen_wait_read(p, timeout=0, read_std_pipes=True):
        start = NM.utils_get_timestamp_msec()
        delay = 0.0005
        b_stdout = b""
        b_stderr = b""
        res = None
        while True:
            if read_std_pipes:
                b_stdout += Util.buffer_read(p.stdout)
                b_stderr += Util.buffer_read(p.stderr)
            if p.poll() is not None:
                res = p.returncode
                break
            if timeout == 0:
                break
            assert timeout > 0
            remaining = timeout - ((NM.utils_get_timestamp_msec() - start) / 1000.0)
            if remaining <= 0:
                break
            delay = min(delay * 2, remaining, 0.05)
            time.sleep(delay)
        return (res, b_stdout, b_stderr)

    @staticmethod
    def buffer_read(buf):
        b = b""
        while True:
            try:
                b1 = buf.read()
            except io.BlockingIOError:
                b1 = b""
            except IOError:
                b1 = b""
            if not b1:
                return b
            b += b1

    @staticmethod
    def buffer_set_nonblock(buf):
        fd = buf.fileno()
        fl = fcntl.fcntl(fd, fcntl.F_GETFL)
        fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

    @staticmethod
    def random_job(jobs):
        jobs = list(jobs)
        l = len(jobs)
        t = l * (l + 1) / 2
        while True:
            # we return a random jobs from the list, but the indexes at the front of
            # the list are more likely. The idea is, that those jobs were started first,
            # and are expected to complete first. As we poll, we want to check more frequently
            # on the elements at the beginning of the list...
            #
            # Let's assign probabilities with an arithmetic series.
            # That is, if there are 16 jobs, then the first gets weighted
            # with 16, the second with 15, then 14, and so on, until the
            # last has weight 1. That means, the first element is 16 times
            # more probable than the last.
            # Element at idx (starting with 0) is picked with probability
            #    1 / (l*(l+1)/2) * (l - idx)
            r = random.random() * t
            idx = 0
            rx = 0
            while True:
                rx += l - idx
                if rx >= r or idx == l - 1:
                    yield jobs[idx]
                    break
                idx += 1

    @staticmethod
    def iter_single(itr, min_num=1, max_num=1):
        itr = list(itr)
        n = 0
        v = None
        for c in itr:
            n += 1
            if n > 1:
                break
            v = c
        if n < min_num:
            raise AssertionError(
                "Expected at least %s elements, but %s found" % (min_num, n)
            )
        if n > max_num:
            raise AssertionError(
                "Expected at most %s elements, but %s found" % (max_num, n)
            )
        return v

    @staticmethod
    def file_read(filename):
        try:
            with open(filename, "rb") as f:
                return f.read()
        except:
            return None

    @staticmethod
    def file_read_expected(filename):
        results_expect = []
        content_expect = Util.file_read(filename)
        try:
            base_idx = 0
            size_prefix = "size: ".encode("utf8")
            while True:
                if not content_expect[base_idx : base_idx + 10].startswith(size_prefix):
                    raise Exception("Unexpected token")
                j = base_idx + len(size_prefix)
                i = j
                if Util.python_has_version(3, 0):
                    eol = ord("\n")
                else:
                    eol = "\n"
                while content_expect[i] != eol:
                    i += 1
                i = i + 1 + int(content_expect[j:i])
                results_expect.append(content_expect[base_idx:i])
                if len(content_expect) == i:
                    break
                base_idx = i
        except Exception as e:
            results_expect = None

        return content_expect, results_expect

    @staticmethod
    def _replace_text_match_join(split_arr, replacement):
        yield split_arr[0]
        for t in split_arr[1:]:
            yield (replacement,)
            yield t

    @staticmethod
    def ReplaceTextSimple(search, replacement):
        # This gives a function that can be used by Util.replace_text().
        # The function replaces an input bytes string @t. It must either return
        # a bytes string, a list containing bytes strings and/or 1-tuples (the
        # latter containing one bytes string).
        # The 1-tuple acts as a placeholder for atomic text, that cannot be replaced
        # a second time.
        #
        # Search for replace_text_fcn in Util.replace_text() where this is called.
        replacement = Util.as_bytes(replacement)

        if callable(search):
            search_fcn = search
        else:
            search_fcn = lambda: search

        def replace_fcn(t):
            assert isinstance(t, bytes)
            search_txt = search_fcn()
            if search_txt is None:
                return t
            search_txt = Util.as_bytes(search_txt)
            return Util._replace_text_match_join(t.split(search_txt), replacement)

        return replace_fcn

    @staticmethod
    def ReplaceTextRegex(pattern, replacement):
        # See ReplaceTextSimple.
        pattern = Util.as_bytes(pattern)
        replacement = Util.as_bytes(replacement)
        p = re.compile(pattern)
        return lambda t: Util._replace_text_match_join(p.split(t), replacement)

    @staticmethod
    def replace_text(text, replace_arr):
        if not replace_arr:
            return text
        needs_encode = Util.python_has_version(3) and Util.is_string(text)
        if needs_encode:
            text = text.encode("utf-8")
        text = [text]
        for replace_text_fcn in replace_arr:
            text2 = []
            for t in text:
                # tuples are markers for atomic strings. They won't be replaced a second
                # time.
                if not isinstance(t, tuple):
                    t = replace_text_fcn(t)
                if isinstance(t, bytes) or isinstance(t, tuple):
                    text2.append(t)
                else:
                    text2.extend(t)
            text = text2
        bb = b"".join([(t[0] if isinstance(t, tuple) else t) for t in text])
        if needs_encode:
            bb = bb.decode("utf-8")
        return bb

    @staticmethod
    def replace_text_sort_list(lst, replace_arr):
        lst = [(Util.replace_text(elem, replace_arr), elem) for elem in lst]
        lst = sorted(lst)
        lst = [tup[1] for tup in lst]
        return list(lst)

    @staticmethod
    def debug_dbus_interface():
        # this is for printf debugging, not used in actual code.
        os.system(
            "busctl --user --verbose call org.freedesktop.NetworkManager /org/freedesktop org.freedesktop.DBus.ObjectManager GetManagedObjects | cat"
        )

    @staticmethod
    def iter_nmcli_output_modes():
        for mode in [[], ["--mode", "tabular"], ["--mode", "multiline"]]:
            for fmt in [[], ["--pretty"], ["--terse"]]:
                for color in [[], ["--color", "yes"]]:
                    yield mode + fmt + color

    @staticmethod
    def valgrind_check_log(valgrind_log, logname):
        if valgrind_log is None:
            return

        fd, name = valgrind_log

        os.close(fd)

        if not os.path.isfile(name):
            raise Exception("valgrind log %s unexpectedly does not exist" % (name,))

        if os.path.getsize(name) != 0:
            out = subprocess.run(
                [
                    "sed",
                    "-e",
                    "/^--[0-9]\+-- WARNING: unhandled .* syscall: /,/^--[0-9]\+-- it at http.*\.$/d",
                    name,
                ],
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
            )
            if out.returncode != 0:
                raise Exception('Calling "sed" to search valgrind log failed')
            if out.stdout:
                print("valgrind log %s for %s is not empty:" % (name, logname))
                print("\n%s\n" % (out.stdout.decode("utf-8", errors="replace"),))
                raise Exception("valgrind log %s unexpectedly is not empty" % (name,))

        os.remove(name)

    @staticmethod
    def pexpect_expect_all(pexp, *pattern_list):
        # This will call "pexpect.expect()" on pattern_list,
        # expecting all entries to match exactly once, in any
        # order.
        pattern_list = list(pattern_list)
        while pattern_list:
            idx = pexp.expect(pattern_list)
            del pattern_list[idx]

    @staticmethod
    def skip_without_pexpect(_func=None):
        if _func is None:
            if pexpect is None:
                raise unittest.SkipTest("pexpect not available")
            return

        def f(*a, **kw):
            Util.skip_without_pexpect()
            _func(*a, **kw)

        return f

    @staticmethod
    def skip_without_dbus_session(_func=None):
        if _func is None:
            if not dbus_session_inited:
                raise unittest.SkipTest(
                    "Own D-Bus session for testing is not initialized. Do you have dbus-run-session available?"
                )
            return

        def f(*a, **kw):
            Util.skip_without_dbus_session()
            _func(*a, **kw)

        return f

    @staticmethod
    def skip_without_NM(_func=None):
        if _func is None:
            if NM is None:
                raise unittest.SkipTest(
                    "gi.NM is not available. Did you build with introspection?"
                )
            return

        def f(*a, **kw):
            Util.skip_without_NM()
            _func(*a, **kw)

        return f


###############################################################################


class Configuration:
    def __init__(self):
        self._values = {}

    def get(self, name):
        v = self._values.get(name, None)
        if name in self._values:
            return v
        if name == ENV_NM_TEST_CLIENT_BUILDDIR:
            v = os.environ.get(
                ENV_NM_TEST_CLIENT_BUILDDIR, PathConfiguration.top_srcdir()
            )
            if not os.path.isdir(v):
                raise Exception("Missing builddir. Set NM_TEST_CLIENT_BUILDDIR?")
        elif name == ENV_NM_TEST_CLIENT_NMCLI_PATH:
            v = os.environ.get(ENV_NM_TEST_CLIENT_NMCLI_PATH, None)
            if v is None:
                try:
                    v = os.path.abspath(
                        self.get(ENV_NM_TEST_CLIENT_BUILDDIR) + "/src/nmcli/nmcli"
                    )
                except:
                    pass
            if not os.path.exists(v):
                raise Exception("Missing nmcli binary. Set NM_TEST_CLIENT_NMCLI_PATH?")
        elif name == ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH:
            v = os.environ.get(ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH, None)
            if v is None:
                try:
                    v = os.path.abspath(
                        self.get(ENV_NM_TEST_CLIENT_BUILDDIR)
                        + "/src/nm-cloud-setup/nm-cloud-setup"
                    )
                except:
                    pass
            if not os.path.exists(v):
                raise Exception(
                    "Missing nm-cloud-setup binary. Set NM_TEST_CLIENT_CLOUD_SETUP_PATH?"
                )
        elif name == ENV_NM_TEST_CLIENT_CHECK_L10N:
            # if we test locales other than 'C', the output of nmcli depends on whether
            # nmcli can load the translations. Unfortunately, I cannot find a way to
            # make gettext use the po/*.gmo files from the build-dir.
            #
            # hence, such tests only work, if you also issue `make-install`
            #
            # Only by setting NM_TEST_CLIENT_CHECK_L10N=1, these tests are included
            # as well.
            v = Util.is_bool(os.environ.get(ENV_NM_TEST_CLIENT_CHECK_L10N, None))
        elif name == ENV_NM_TEST_REGENERATE:
            # in the "regenerate" mode, the tests will rewrite the files on disk against
            # which we assert. That is useful, if there are intentional changes and
            # we want to regenerate the expected output.
            v = Util.is_bool(os.environ.get(ENV_NM_TEST_REGENERATE, None))
        elif name == ENV_NM_TEST_WITH_LINENO:
            v = Util.is_bool(os.environ.get(ENV_NM_TEST_WITH_LINENO, None))
        elif name == ENV_NM_TEST_VALGRIND:
            if self.get(ENV_NM_TEST_REGENERATE):
                v = False
            else:
                v = os.environ.get(ENV_NM_TEST_VALGRIND, None)
                if v:
                    v = Util.is_bool(v)
                else:
                    v = Util.is_bool(os.environ.get("NMTST_USE_VALGRIND", None))
        elif name in [
            ENV_NM_TEST_ASAN_OPTIONS,
            ENV_NM_TEST_LSAN_OPTIONS,
            ENV_NM_TEST_UBSAN_OPTIONS,
        ]:
            v = os.environ.get(name, None)
            if v is None:
                if name == ENV_NM_TEST_ASAN_OPTIONS:
                    v = "detect_leaks=1"
                    # v += ' fast_unwind_on_malloc=false'
                elif name == ENV_NM_TEST_LSAN_OPTIONS:
                    v = ""
                elif name == ENV_NM_TEST_UBSAN_OPTIONS:
                    v = "print_stacktrace=1:halt_on_error=1"
                else:
                    assert False
        elif name == ENV_LIBTOOL:
            v = os.environ.get(name, None)
            if v is None:
                v = os.path.abspath(
                    os.path.dirname(self.get(ENV_NM_TEST_CLIENT_NMCLI_PATH))
                    + "/../../libtool"
                )
                if not os.path.isfile(v):
                    v = None
                else:
                    v = [v]
            elif not v:
                v = None
            else:
                v = shlex.split(v)
        else:
            raise Exception()
        self._values[name] = v
        return v


conf = Configuration()

###############################################################################


class NMStubServer:
    @staticmethod
    def _conn_get_main_object(conn):
        try:
            return conn.get_object(
                "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager"
            )
        except:
            return None

    def __init__(self, seed):
        service_path = PathConfiguration.test_networkmanager_service_path()
        self._conn = dbus.SessionBus()
        env = os.environ.copy()
        env["NM_TEST_NETWORKMANAGER_SERVICE_SEED"] = seed
        p = subprocess.Popen(
            [sys.executable, service_path], stdin=subprocess.PIPE, env=env
        )

        start = NM.utils_get_timestamp_msec()
        while True:
            if p.poll() is not None:
                p.stdin.close()
                if p.returncode == 77:
                    raise unittest.SkipTest(
                        "the stub service %s exited with status 77" % (service_path)
                    )
                raise Exception(
                    "the stub service %s exited unexpectedly" % (service_path)
                )
            nmobj = self._conn_get_main_object(self._conn)
            if nmobj is not None:
                break
            if (NM.utils_get_timestamp_msec() - start) >= 4000:
                p.stdin.close()
                p.kill()
                Util.popen_wait(p, 1)
                raise Exception(
                    "after starting stub service the D-Bus name was not claimed in time"
                )

        self._nmobj = nmobj
        self._nmiface = dbus.Interface(
            nmobj, "org.freedesktop.NetworkManager.LibnmGlibTest"
        )
        self._p = p

    def shutdown(self, kill_mode="random"):
        conn = self._conn
        p = self._p
        self._nmobj = None
        self._nmiface = None
        self._conn = None
        self._p = None

        # The test stub service watches stdin and will do a proper
        # shutdown when it closes. That means, to send signals about
        # going away.
        # On the other hand, just killing it will cause the process
        # from dropping off the bus.
        if kill_mode == "kill":
            p.kill()
        elif kill_mode == "stdin-close":
            p.stdin.close()
        else:
            assert kill_mode == "random"
            ops = [p.stdin.close, p.kill]
            random.shuffle(ops)
            ops[0]()
            r = random.random()
            if r < 0.75:
                if r < 0.5:
                    time.sleep(r * 0.2)
                ops[1]()

        if Util.popen_wait(p, 1) is None:
            raise Exception("Stub service did not exit in time")
        p.stdin.close()
        if self._conn_get_main_object(conn) is not None:
            raise Exception(
                "Stub service is not still here although it should shut down"
            )

    class _MethodProxy:
        def __init__(self, parent, method_name):
            self._parent = parent
            self._method_name = method_name

        def __call__(self, *args, **kwargs):
            dbus_iface = kwargs.pop("dbus_iface", None)
            if dbus_iface is None:
                dbus_iface = self._parent._nmiface
            method = dbus_iface.get_dbus_method(self._method_name)
            if kwargs:
                # for convenience, we allow the caller to specify arguments
                # as kwargs. In this case, we construct a a{sv} array as last argument.
                args = list(args)
                args.append(kwargs)
            return method(*args)

    def __getattr__(self, member):
        if not member.startswith("op_"):
            raise AttributeError(member)
        return self._MethodProxy(self, member[3:])

    def addConnection(self, connection, do_verify_strict=True):
        return self.op_AddConnection(connection, do_verify_strict)

    def findConnections(self, **kwargs):
        if kwargs:
            lst = self.op_FindConnections(**kwargs)
        else:
            lst = self.op_FindConnections({})
        return list([(str(elem[0]), str(elem[1]), str(elem[2])) for elem in lst])

    def findConnectionUuid(self, con_id, required=True):
        try:
            u = Util.iter_single(self.findConnections(con_id=con_id))[1]
            assert u, "Invalid uuid %s" % (u)
        except Exception as e:
            if not required:
                return None
            raise AssertionError(
                "Unexpectedly not found connection %s: %s" % (con_id, str(e))
            )
        return u

    def setProperty(self, path, propname, value, iface_name=None):
        if iface_name is None:
            iface_name = ""
        self.op_SetProperties([(path, [(iface_name, [(propname, value)])])])

    def addAndActivateConnection(
        self, connection, device, specific_object="", delay=None
    ):
        if delay is not None:
            self.op_SetActiveConnectionStateChangedDelay(device, delay)
        nm_iface = self._conn_get_main_object(self._conn)
        self.op_AddAndActivateConnection(
            connection, device, specific_object, dbus_iface=nm_iface
        )


###############################################################################


class AsyncProcess:
    def __init__(self, args, env, complete_cb, max_waittime_msec=20000):
        self._args = list(args)
        self._env = env
        self._complete_cb = complete_cb
        self._max_waittime_msec = max_waittime_msec

    def start(self):
        if not hasattr(self, "_p"):
            self._p_start_timestamp = NM.utils_get_timestamp_msec()
            self._p_stdout_buf = b""
            self._p_stderr_buf = b""
            self._p = subprocess.Popen(
                self._args,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                env=self._env,
            )
            Util.buffer_set_nonblock(self._p.stdout)
            Util.buffer_set_nonblock(self._p.stderr)

    def _timeout_remaining_time(self):
        # note that we call this during poll() and wait_and_complete().
        # we don't know the exact time when the process terminated,
        # so this is only approximately correct, if we call poll/wait
        # frequently.
        # Worst case, we will think that the process did not time out,
        # when in fact it was running longer than max-waittime.
        return self._max_waittime_msec - (
            NM.utils_get_timestamp_msec() - self._p_start_timestamp
        )

    def poll(self, timeout=0):
        self.start()

        (return_code, b_stdout, b_stderr) = Util.popen_wait_read(self._p, timeout)

        self._p_stdout_buf += b_stdout
        self._p_stderr_buf += b_stderr

        if return_code is None and self._timeout_remaining_time() <= 0:
            raise Exception(
                "process is still running after timeout: %s" % (" ".join(self._args))
            )
        return return_code

    def wait_and_complete(self):
        self.start()

        p = self._p
        self._p = None

        (return_code, b_stdout, b_stderr) = Util.popen_wait_read(
            p, max(0, self._timeout_remaining_time()) / 1000
        )
        (stdout, stderr) = (p.stdout.read(), p.stderr.read())
        p.stdout.close()
        p.stderr.close()

        stdout = self._p_stdout_buf + b_stdout + stdout
        stderr = self._p_stderr_buf + b_stderr + stderr
        del self._p_stdout_buf
        del self._p_stderr_buf

        if return_code is None:
            print(stdout)
            print(stderr)
            raise Exception(
                "process did not complete in time: %s" % (" ".join(self._args))
            )

        self._complete_cb(self, return_code, stdout, stderr)


###############################################################################


MAX_JOBS = 15


class TestNmClient(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        self._calling_num = {}
        self._skip_test_for_l10n_diff = []
        self._async_jobs = []
        self._results = []
        self.srv = None
        unittest.TestCase.__init__(self, *args, **kwargs)

    def srv_start(self):
        self.srv_shutdown()
        self.srv = NMStubServer(self._testMethodName)

    def srv_shutdown(self):
        if self.srv is not None:
            srv = self.srv
            self.srv = None
            srv.shutdown()

    def ReplaceTextConUuid(self, con_name, replacement):
        return Util.ReplaceTextSimple(
            Util.memoize_nullary(lambda: self.srv.findConnectionUuid(con_name)),
            replacement,
        )

    def _env(
        self, lang="C", calling_num=None, fatal_warnings=_DEFAULT_ARG, extra_env=None
    ):
        if lang == "C":
            language = ""
        elif lang == "de_DE.utf8":
            language = "de"
        elif lang == "pl_PL.UTF-8":
            language = "pl"
        else:
            self.fail("invalid language %s" % (lang))

        env = {}
        for k in [
            "LD_LIBRARY_PATH",
            "DBUS_SESSION_BUS_ADDRESS",
            "LIBNM_CLIENT_DEBUG",
            "LIBNM_CLIENT_DEBUG_FILE",
        ]:
            val = os.environ.get(k, None)
            if val is not None:
                env[k] = val
        env["LANG"] = lang
        env["LANGUAGE"] = language
        env["LIBNM_USE_SESSION_BUS"] = "1"
        env["LIBNM_USE_NO_UDEV"] = "1"
        env["TERM"] = "linux"
        env["ASAN_OPTIONS"] = conf.get(ENV_NM_TEST_ASAN_OPTIONS)
        env["LSAN_OPTIONS"] = conf.get(ENV_NM_TEST_LSAN_OPTIONS)
        env["LBSAN_OPTIONS"] = conf.get(ENV_NM_TEST_UBSAN_OPTIONS)
        env["XDG_CONFIG_HOME"] = PathConfiguration.srcdir()
        if calling_num is not None:
            env["NM_TEST_CALLING_NUM"] = str(calling_num)
        if fatal_warnings is _DEFAULT_ARG or fatal_warnings:
            env["G_DEBUG"] = "fatal-warnings"
        if extra_env is not None:
            for k, v in extra_env.items():
                env[k] = v
        return env

    def cmd_construct_argv(self, cmd_path, args, with_valgrind=None):

        if with_valgrind is None:
            with_valgrind = conf.get(ENV_NM_TEST_VALGRIND)

        valgrind_log = None
        cmd = conf.get(cmd_path)
        if with_valgrind:
            valgrind_log = tempfile.mkstemp(prefix="nm-test-client-valgrind.")
            argv = [
                "valgrind",
                "--quiet",
                "--error-exitcode=37",
                "--leak-check=full",
                "--gen-suppressions=all",
                (
                    "--suppressions="
                    + PathConfiguration.top_srcdir()
                    + "/valgrind.suppressions"
                ),
                "--num-callers=100",
                "--log-file=" + valgrind_log[1],
                cmd,
            ]
            libtool = conf.get(ENV_LIBTOOL)
            if libtool:
                argv = list(libtool) + ["--mode=execute"] + argv
        else:
            argv = [cmd]

        argv.extend(args)
        return argv, valgrind_log

    def call_pexpect(self, cmd_path, args, extra_env):
        argv, valgrind_log = self.cmd_construct_argv(cmd_path, args)
        env = self._env(extra_env=extra_env)

        pexp = pexpect.spawn(argv[0], argv[1:], timeout=10, env=env)

        pexp.str_last_chars = 100000

        typ = collections.namedtuple("CallPexpect", ["pexp", "valgrind_log"])
        return typ(pexp, valgrind_log)

    def async_start(self, wait_all=False):

        while True:

            while True:
                for async_job in list(self._async_jobs[0:MAX_JOBS]):
                    async_job.start()
                # start up to MAX_JOBS jobs, but poll() and complete those
                # that are already exited. Retry, until there are no more
                # jobs to start, or until MAX_JOBS are running.
                jobs_running = []
                for async_job in list(self._async_jobs[0:MAX_JOBS]):
                    if async_job.poll() is not None:
                        self._async_jobs.remove(async_job)
                        async_job.wait_and_complete()
                        continue
                    jobs_running.append(async_job)
                if len(jobs_running) >= len(self._async_jobs):
                    break
                if len(jobs_running) >= MAX_JOBS:
                    break

            if not jobs_running:
                return
            if not wait_all:
                return

            # in a loop, indefinitely poll the running jobs until we find one that
            # completes. Note that poll() itself will raise an exception if a
            # jobs times out.
            for async_job in Util.random_job(jobs_running):
                if async_job.poll(timeout=0.03) is not None:
                    self._async_jobs.remove(async_job)
                    async_job.wait_and_complete()
                    break

    def async_wait(self):
        return self.async_start(wait_all=True)

    def _nm_test_post(self):

        self.async_wait()

        self.srv_shutdown()

        self._calling_num = None

        results = self._results
        self._results = None

        if len(results) == 0:
            return

        skip_test_for_l10n_diff = self._skip_test_for_l10n_diff
        self._skip_test_for_l10n_diff = None

        test_name = self._testMethodName

        filename = os.path.abspath(
            PathConfiguration.srcdir()
            + "/test-client.check-on-disk/"
            + test_name
            + ".expected"
        )

        regenerate = conf.get(ENV_NM_TEST_REGENERATE)

        content_expect, results_expect = Util.file_read_expected(filename)

        if results_expect is None:
            if not regenerate:
                self.fail(
                    "Failed to parse expected file '%s'. Let the test write the file by rerunning with NM_TEST_REGENERATE=1"
                    % (filename)
                )
        else:
            for i in range(0, min(len(results_expect), len(results))):
                n = results[i]
                if results_expect[i] == n["content"]:
                    continue
                if regenerate:
                    continue
                if n["ignore_l10n_diff"]:
                    skip_test_for_l10n_diff.append(n["test_name"])
                    continue
                print(
                    "\n\n\nThe file '%s' does not have the expected content:"
                    % (filename)
                )
                print("ACTUAL OUTPUT:\n[[%s]]\n" % (n["content"]))
                print("EXPECT OUTPUT:\n[[%s]]\n" % (results_expect[i]))
                print(
                    "Let the test write the file by rerunning with NM_TEST_REGENERATE=1"
                )
                print(
                    "See howto in %s for details.\n"
                    % (PathConfiguration.canonical_script_filename())
                )
                sys.stdout.flush()
                self.fail(
                    "Unexpected output of command, expected %s. Rerun test with NM_TEST_REGENERATE=1 to regenerate files"
                    % (filename)
                )
            if len(results_expect) != len(results):
                if not regenerate:
                    print(
                        "\n\n\nThe number of tests in %s does not match the expected content (%s vs %s):"
                        % (filename, len(results_expect), len(results))
                    )
                    if len(results_expect) < len(results):
                        print(
                            "ACTUAL OUTPUT:\n[[%s]]\n"
                            % (results[len(results_expect)]["content"])
                        )
                    else:
                        print(
                            "EXPECT OUTPUT:\n[[%s]]\n" % (results_expect[len(results)])
                        )
                    print(
                        "Let the test write the file by rerunning with NM_TEST_REGENERATE=1"
                    )
                    print(
                        "See howto in %s for details.\n"
                        % (PathConfiguration.canonical_script_filename())
                    )
                    sys.stdout.flush()
                    self.fail(
                        "Unexpected output of command, expected %s. Rerun test with NM_TEST_REGENERATE=1 to regenerate files"
                        % (filename)
                    )

        if regenerate:
            content_new = b"".join([r["content"] for r in results])
            if content_new != content_expect:
                try:
                    with open(filename, "wb") as content_file:
                        content_file.write(content_new)
                except Exception as e:
                    self.fail("Failure to write '%s': %s" % (filename, e))

        if skip_test_for_l10n_diff:
            # nmcli loads translations from the installation path. This failure commonly
            # happens because you did not install the binary in the --prefix, before
            # running the test. Hence, translations are not available or differ.
            raise unittest.SkipTest(
                "Skipped asserting for localized tests %s. Set NM_TEST_CLIENT_CHECK_L10N=1 to force fail."
                % (",".join(skip_test_for_l10n_diff))
            )

    def setUp(self):
        Util.skip_without_dbus_session()
        Util.skip_without_NM()


class TestNmcli(TestNmClient):
    def call_nmcli_l(
        self,
        args,
        check_on_disk=_DEFAULT_ARG,
        fatal_warnings=_DEFAULT_ARG,
        expected_returncode=_DEFAULT_ARG,
        expected_stdout=_DEFAULT_ARG,
        expected_stderr=_DEFAULT_ARG,
        replace_stdout=None,
        replace_stderr=None,
        replace_cmd=None,
        sort_lines_stdout=False,
        extra_env=None,
        sync_barrier=False,
    ):
        frame = sys._getframe(1)
        for lang in ["C", "pl"]:
            self._call_nmcli(
                args,
                lang,
                check_on_disk,
                fatal_warnings,
                expected_returncode,
                expected_stdout,
                expected_stderr,
                replace_stdout,
                replace_stderr,
                replace_cmd,
                sort_lines_stdout,
                extra_env,
                sync_barrier,
                frame,
            )

    def call_nmcli(
        self,
        args,
        langs=None,
        lang=None,
        check_on_disk=_DEFAULT_ARG,
        fatal_warnings=_DEFAULT_ARG,
        expected_returncode=_DEFAULT_ARG,
        expected_stdout=_DEFAULT_ARG,
        expected_stderr=_DEFAULT_ARG,
        replace_stdout=None,
        replace_stderr=None,
        replace_cmd=None,
        sort_lines_stdout=False,
        extra_env=None,
        sync_barrier=None,
    ):

        frame = sys._getframe(1)

        if langs is not None:
            assert lang is None
        else:
            if lang is None:
                lang = "C"
            langs = [lang]

        if sync_barrier is None:
            sync_barrier = len(langs) == 1

        for lang in langs:
            self._call_nmcli(
                args,
                lang,
                check_on_disk,
                fatal_warnings,
                expected_returncode,
                expected_stdout,
                expected_stderr,
                replace_stdout,
                replace_stderr,
                replace_cmd,
                sort_lines_stdout,
                extra_env,
                sync_barrier,
                frame,
            )

    def call_nmcli_pexpect(self, args):
        return self.call_pexpect(ENV_NM_TEST_CLIENT_NMCLI_PATH, args, {"NO_COLOR": "1"})

    def _call_nmcli(
        self,
        args,
        lang,
        check_on_disk,
        fatal_warnings,
        expected_returncode,
        expected_stdout,
        expected_stderr,
        replace_stdout,
        replace_stderr,
        replace_cmd,
        sort_lines_stdout,
        extra_env,
        sync_barrier,
        frame,
    ):

        if sync_barrier:
            self.async_wait()

        calling_fcn = frame.f_code.co_name
        calling_num = self._calling_num.get(calling_fcn, 0) + 1
        self._calling_num[calling_fcn] = calling_num

        test_name = "%s-%03d" % (calling_fcn, calling_num)

        # we cannot use frame.f_code.co_filename directly, because it might be different depending
        # on where the file lies and which is CWD. We still want to give the location of
        # the file, so that the user can easier find the source (when looking at the .expected files)
        self.assertTrue(
            os.path.abspath(frame.f_code.co_filename).endswith(
                "/" + PathConfiguration.canonical_script_filename()
            )
        )

        if conf.get(ENV_NM_TEST_WITH_LINENO):
            calling_location = "%s:%d:%s()/%d" % (
                PathConfiguration.canonical_script_filename(),
                frame.f_lineno,
                frame.f_code.co_name,
                calling_num,
            )
        else:
            calling_location = "%s:%s()/%d" % (
                PathConfiguration.canonical_script_filename(),
                frame.f_code.co_name,
                calling_num,
            )

        if lang is None or lang == "C":
            lang = "C"
        elif lang == "de":
            lang = "de_DE.utf8"
        elif lang == "pl":
            lang = "pl_PL.UTF-8"
        else:
            self.fail("invalid language %s" % (lang))

        # Running under valgrind is not yet supported for those tests.
        args, valgrind_log = self.cmd_construct_argv(
            ENV_NM_TEST_CLIENT_NMCLI_PATH, args, with_valgrind=False
        )

        assert valgrind_log is None

        if replace_stdout is not None:
            replace_stdout = list(replace_stdout)
        if replace_stderr is not None:
            replace_stderr = list(replace_stderr)
        if replace_cmd is not None:
            replace_cmd = list(replace_cmd)

        if check_on_disk is _DEFAULT_ARG:
            check_on_disk = (
                expected_returncode is _DEFAULT_ARG
                and (
                    expected_stdout is _DEFAULT_ARG
                    or expected_stdout is _UNSTABLE_OUTPUT
                )
                and (
                    expected_stderr is _DEFAULT_ARG
                    or expected_stderr is _UNSTABLE_OUTPUT
                )
            )
        if expected_returncode is _DEFAULT_ARG:
            expected_returncode = None
        if expected_stdout is _DEFAULT_ARG:
            expected_stdout = None
        if expected_stderr is _DEFAULT_ARG:
            expected_stderr = None

        results_idx = len(self._results)
        self._results.append(None)

        def complete_cb(async_job, returncode, stdout, stderr):

            if expected_stdout is _UNSTABLE_OUTPUT:
                stdout = "<UNSTABLE OUTPUT>".encode("utf-8")
            else:
                stdout = Util.replace_text(stdout, replace_stdout)

            if expected_stderr is _UNSTABLE_OUTPUT:
                stderr = "<UNSTABLE OUTPUT>".encode("utf-8")
            else:
                stderr = Util.replace_text(stderr, replace_stderr)

            if sort_lines_stdout:
                stdout = b"\n".join(sorted(stdout.split(b"\n")))

            ignore_l10n_diff = lang != "C" and not conf.get(
                ENV_NM_TEST_CLIENT_CHECK_L10N
            )

            if expected_stderr is not None and expected_stderr is not _UNSTABLE_OUTPUT:
                if expected_stderr != stderr:
                    if ignore_l10n_diff:
                        self._skip_test_for_l10n_diff.append(test_name)
                    else:
                        self.assertEqual(expected_stderr, stderr)
            if expected_stdout is not None and expected_stdout is not _UNSTABLE_OUTPUT:
                if expected_stdout != stdout:
                    if ignore_l10n_diff:
                        self._skip_test_for_l10n_diff.append(test_name)
                    else:
                        self.assertEqual(expected_stdout, stdout)
            if expected_returncode is not None:
                self.assertEqual(expected_returncode, returncode)

            if fatal_warnings is _DEFAULT_ARG:
                if expected_returncode != -5:
                    self.assertNotEqual(returncode, -5)
            elif fatal_warnings:
                if expected_returncode is None:
                    self.assertEqual(returncode, -5)

            if check_on_disk:
                cmd = "$NMCLI %s" % (Util.shlex_join(args[1:]),)
                cmd = Util.replace_text(cmd, replace_cmd)

                if returncode < 0:
                    returncode_str = "%d (SIGNAL %s)" % (
                        returncode,
                        Util.signal_no_to_str(-returncode),
                    )
                else:
                    returncode_str = "%d" % (returncode)

                content = (
                    ("location: %s\n" % (calling_location)).encode("utf8")
                    + ("cmd: %s\n" % (cmd)).encode("utf8")
                    + ("lang: %s\n" % (lang)).encode("utf8")
                    + ("returncode: %s\n" % (returncode_str)).encode("utf8")
                )
                if len(stdout) > 0:
                    content += (
                        ("stdout: %d bytes\n>>>\n" % (len(stdout))).encode("utf8")
                        + stdout
                        + "\n<<<\n".encode("utf8")
                    )
                if len(stderr) > 0:
                    content += (
                        ("stderr: %d bytes\n>>>\n" % (len(stderr))).encode("utf8")
                        + stderr
                        + "\n<<<\n".encode("utf8")
                    )
                content = ("size: %s\n" % (len(content))).encode("utf8") + content

                self._results[results_idx] = {
                    "test_name": test_name,
                    "ignore_l10n_diff": ignore_l10n_diff,
                    "content": content,
                }

        env = self._env(lang, calling_num, fatal_warnings, extra_env)
        async_job = AsyncProcess(args=args, env=env, complete_cb=complete_cb)

        self._async_jobs.append(async_job)

        self.async_start(wait_all=sync_barrier)

    def nm_test(func):
        def f(self):
            self.srv_start()
            func(self)
            self._nm_test_post()

        return f

    def nm_test_no_dbus(func):
        def f(self):
            func(self)
            self._nm_test_post()

        return f

    def init_001(self):
        self.srv.op_AddObj("WiredDevice", iface="eth0")
        self.srv.op_AddObj("WiredDevice", iface="eth1")
        self.srv.op_AddObj("WifiDevice", iface="wlan0")
        self.srv.op_AddObj("WifiDevice", iface="wlan1")

        # add another device with an identical ifname. The D-Bus API itself
        # does not enforce the ifnames are unique.
        self.srv.op_AddObj("WifiDevice", ident="wlan1/x", iface="wlan1")

        self.srv.op_AddObj("WifiAp", device="wlan0", rsnf=0x0)

        self.srv.op_AddObj("WifiAp", device="wlan0")

        NM_AP_FLAGS = getattr(NM, "80211ApSecurityFlags")
        rsnf = 0x0
        rsnf = rsnf | NM_AP_FLAGS.PAIR_TKIP
        rsnf = rsnf | NM_AP_FLAGS.PAIR_CCMP
        rsnf = rsnf | NM_AP_FLAGS.GROUP_TKIP
        rsnf = rsnf | NM_AP_FLAGS.GROUP_CCMP
        rsnf = rsnf | NM_AP_FLAGS.KEY_MGMT_SAE
        self.srv.op_AddObj("WifiAp", device="wlan0", wpaf=0x0, rsnf=rsnf)

        self.srv.op_AddObj("WifiAp", device="wlan1")

        self.srv.addConnection(
            {"connection": {"type": "802-3-ethernet", "id": "con-1"}}
        )

    @nm_test
    def test_001(self):

        self.call_nmcli_l([])

        self.call_nmcli_l(
            ["-f", "AP", "-mode", "multiline", "-p", "d", "show", "wlan0"]
        )

        self.call_nmcli_l(["c", "s"])

        self.call_nmcli_l(["bogus", "s"])

        for mode in Util.iter_nmcli_output_modes():
            self.call_nmcli_l(mode + ["general", "permissions"])

    @nm_test
    def test_002(self):
        self.init_001()

        self.call_nmcli_l(["d"])

        self.call_nmcli_l(["-f", "all", "d"])

        self.call_nmcli_l([])

        self.call_nmcli_l(["-f", "AP", "-mode", "multiline", "d", "show", "wlan0"])
        self.call_nmcli_l(
            ["-f", "AP", "-mode", "multiline", "-p", "d", "show", "wlan0"]
        )
        self.call_nmcli_l(
            ["-f", "AP", "-mode", "multiline", "-t", "d", "show", "wlan0"]
        )
        self.call_nmcli_l(["-f", "AP", "-mode", "tabular", "d", "show", "wlan0"])
        self.call_nmcli_l(["-f", "AP", "-mode", "tabular", "-p", "d", "show", "wlan0"])
        self.call_nmcli_l(["-f", "AP", "-mode", "tabular", "-t", "d", "show", "wlan0"])

        self.call_nmcli_l(["-f", "ALL", "d", "wifi"])

        self.call_nmcli_l(["c"])

        self.call_nmcli_l(["c", "s", "con-1"])

    @nm_test
    def test_003(self):
        con_gsm_list = [
            ("con-gsm1", "xyz.con-gsm1"),
            ("con-gsm2", ""),
            ("con-gsm3", " "),
        ]

        self.init_001()

        replace_uuids = []

        replace_uuids.append(
            self.ReplaceTextConUuid("con-xx1", "UUID-con-xx1-REPLACED-REPLACED-REPLA")
        )

        self.call_nmcli(
            ["c", "add", "type", "ethernet", "ifname", "*", "con-name", "con-xx1"],
            replace_stdout=replace_uuids,
        )

        self.call_nmcli_l(["c", "s"], replace_stdout=replace_uuids)

        for con_name, apn in con_gsm_list:

            replace_uuids.append(
                self.ReplaceTextConUuid(
                    con_name, "UUID-" + con_name + "-REPLACED-REPLACED-REPL"
                )
            )

            self.call_nmcli(
                [
                    "connection",
                    "add",
                    "type",
                    "gsm",
                    "autoconnect",
                    "no",
                    "con-name",
                    con_name,
                    "ifname",
                    "*",
                    "apn",
                    apn,
                    "serial.baud",
                    "5",
                    "serial.send-delay",
                    "100",
                    "serial.pari",
                    "1",
                    "ipv4.dns-options",
                    " ",
                ],
                replace_stdout=replace_uuids,
            )

        replace_uuids.append(
            self.ReplaceTextConUuid("ethernet", "UUID-ethernet-REPLACED-REPLACED-REPL")
        )

        self.call_nmcli(
            ["c", "add", "type", "ethernet", "ifname", "*"],
            replace_stdout=replace_uuids,
        )

        self.call_nmcli_l(["c", "s"], replace_stdout=replace_uuids)

        self.call_nmcli_l(["-f", "ALL", "c", "s"], replace_stdout=replace_uuids)

        self.call_nmcli_l(
            ["--complete-args", "-f", "ALL", "c", "s", ""],
            replace_stdout=replace_uuids,
            sort_lines_stdout=True,
        )

        for con_name, apn in con_gsm_list:
            self.call_nmcli_l(["con", "s", con_name], replace_stdout=replace_uuids)
            self.call_nmcli_l(
                ["-g", "all", "con", "s", con_name], replace_stdout=replace_uuids
            )

        # activate the same profile on multiple devices. Our stub-implmentation
        # is fine with that... although NetworkManager service would reject
        # such a configuration by deactivating the profile first. But note that
        # that is only an internal behavior of NetworkManager service. The D-Bus
        # API perfectly allows for one profile to be active multiple times. Also
        # note, that there is always a short time where one profile goes down,
        # while another is activating. Hence, while real NetworkManager commonly
        # does not allow that multiple profiles *stay* connected at the same
        # time, there is always the possibility that a profile is activating/active
        # on a device, while also activating/deactivating in parallel.
        for dev in ["eth0", "eth1"]:
            self.call_nmcli(["con", "up", "ethernet", "ifname", dev])

            self.call_nmcli_l(["con"], replace_stdout=replace_uuids)

            self.call_nmcli_l(["-f", "ALL", "con"], replace_stdout=replace_uuids)

            self.call_nmcli_l(
                ["-f", "ALL", "con", "s", "-a"], replace_stdout=replace_uuids
            )

            self.call_nmcli_l(
                ["-f", "ACTIVE-PATH,DEVICE,UUID", "con", "s", "-act"],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                ["-f", "UUID,NAME", "con", "s", "--active"],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                ["-f", "ALL", "con", "s", "ethernet"], replace_stdout=replace_uuids
            )

            self.call_nmcli_l(
                ["-f", "GENERAL.STATE", "con", "s", "ethernet"],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(["con", "s", "ethernet"], replace_stdout=replace_uuids)

            self.call_nmcli_l(
                ["-f", "ALL", "dev", "status"], replace_stdout=replace_uuids
            )

            # test invalid call ('s' abbrevates 'status' and not 'show'
            self.call_nmcli_l(
                ["-f", "ALL", "dev", "s", "eth0"], replace_stdout=replace_uuids
            )

            self.call_nmcli_l(
                ["-f", "ALL", "dev", "show", "eth0"], replace_stdout=replace_uuids
            )

            self.call_nmcli_l(
                ["-f", "ALL", "-t", "dev", "show", "eth0"], replace_stdout=replace_uuids
            )

        self.async_wait()

        self.srv.setProperty(
            "/org/freedesktop/NetworkManager/ActiveConnection/1",
            "State",
            dbus.UInt32(NM.ActiveConnectionState.DEACTIVATING),
        )

        self.call_nmcli_l([], replace_stdout=replace_uuids)

        for i in [0, 1]:
            if i == 1:
                self.async_wait()
                self.srv.op_ConnectionSetVisible(False, con_id="ethernet")

            for mode in Util.iter_nmcli_output_modes():
                self.call_nmcli_l(
                    mode + ["-f", "ALL", "con"], replace_stdout=replace_uuids
                )

                self.call_nmcli_l(
                    mode + ["-f", "UUID,TYPE", "con"], replace_stdout=replace_uuids
                )

                self.call_nmcli_l(
                    mode + ["con", "s", "ethernet"], replace_stdout=replace_uuids
                )

                self.call_nmcli_l(
                    mode
                    + ["c", "s", "/org/freedesktop/NetworkManager/ActiveConnection/1"],
                    replace_stdout=replace_uuids,
                )

                self.call_nmcli_l(
                    mode + ["-f", "all", "dev", "show", "eth0"],
                    replace_stdout=replace_uuids,
                )

    @nm_test
    def test_004(self):
        self.init_001()

        replace_uuids = []

        replace_uuids.append(
            self.ReplaceTextConUuid("con-xx1", "UUID-con-xx1-REPLACED-REPLACED-REPLA")
        )

        self.call_nmcli(
            [
                "c",
                "add",
                "type",
                "wifi",
                "ifname",
                "*",
                "ssid",
                "foobar",
                "con-name",
                "con-xx1",
            ],
            replace_stdout=replace_uuids,
        )

        self.call_nmcli(["connection", "mod", "con-xx1", "ip.gateway", ""])
        self.call_nmcli(
            ["connection", "mod", "con-xx1", "ipv4.gateway", "172.16.0.1"], lang="pl"
        )
        self.call_nmcli(["connection", "mod", "con-xx1", "ipv6.gateway", "::99"])
        self.call_nmcli(["connection", "mod", "con-xx1", "802.abc", ""])
        self.call_nmcli(["connection", "mod", "con-xx1", "802-11-wireless.band", "a"])
        self.call_nmcli(
            [
                "connection",
                "mod",
                "con-xx1",
                "ipv4.addresses",
                "192.168.77.5/24",
                "ipv4.routes",
                "2.3.4.5/32 192.168.77.1",
                "ipv6.addresses",
                "1:2:3:4::6/64",
                "ipv6.routes",
                "1:2:3:4:5:6::5/128",
            ]
        )
        self.call_nmcli_l(["con", "s", "con-xx1"], replace_stdout=replace_uuids)

        self.async_wait()

        replace_uuids.append(
            self.ReplaceTextConUuid("con-vpn-1", "UUID-con-vpn-1-REPLACED-REPLACED-REP")
        )

        self.call_nmcli(
            [
                "connection",
                "add",
                "type",
                "vpn",
                "con-name",
                "con-vpn-1",
                "ifname",
                "*",
                "vpn-type",
                "openvpn",
                "vpn.data",
                "key1 = val1,   key2  = val2, key3=val3",
            ],
            replace_stdout=replace_uuids,
        )

        self.call_nmcli_l(["con", "s"], replace_stdout=replace_uuids)
        self.call_nmcli_l(["con", "s", "con-vpn-1"], replace_stdout=replace_uuids)

        self.call_nmcli(["con", "up", "con-xx1"])
        self.call_nmcli_l(["con", "s"], replace_stdout=replace_uuids)

        self.call_nmcli(["con", "up", "con-vpn-1"])
        self.call_nmcli_l(["con", "s"], replace_stdout=replace_uuids)
        self.call_nmcli_l(["con", "s", "con-vpn-1"], replace_stdout=replace_uuids)

        self.async_wait()

        self.srv.setProperty(
            "/org/freedesktop/NetworkManager/ActiveConnection/2",
            "VpnState",
            dbus.UInt32(NM.VpnConnectionState.ACTIVATED),
        )

        uuids = Util.replace_text_sort_list(
            [c[1] for c in self.srv.findConnections()], replace_uuids
        )

        self.call_nmcli_l([], replace_stdout=replace_uuids)

        for mode in Util.iter_nmcli_output_modes():

            self.call_nmcli_l(
                mode + ["con", "s", "con-vpn-1"], replace_stdout=replace_uuids
            )
            self.call_nmcli_l(
                mode + ["con", "s", "con-vpn-1"], replace_stdout=replace_uuids
            )

            self.call_nmcli_l(
                mode + ["-f", "ALL", "con", "s", "con-vpn-1"],
                replace_stdout=replace_uuids,
            )

            # This only filters 'vpn' settings from the connection profile.
            # Contrary to '-f GENERAL' below, it does not show the properties of
            # the activated VPN connection. This is a nmcli bug.
            self.call_nmcli_l(
                mode + ["-f", "VPN", "con", "s", "con-vpn-1"],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                mode + ["-f", "GENERAL", "con", "s", "con-vpn-1"],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(mode + ["dev", "s"], replace_stdout=replace_uuids)

            self.call_nmcli_l(
                mode + ["-f", "all", "dev", "status"], replace_stdout=replace_uuids
            )

            self.call_nmcli_l(mode + ["dev", "show"], replace_stdout=replace_uuids)

            self.call_nmcli_l(
                mode + ["-f", "all", "dev", "show"], replace_stdout=replace_uuids
            )

            self.call_nmcli_l(
                mode + ["dev", "show", "wlan0"], replace_stdout=replace_uuids
            )

            self.call_nmcli_l(
                mode + ["-f", "all", "dev", "show", "wlan0"],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                mode
                + [
                    "-f",
                    "GENERAL,GENERAL.HWADDR,WIFI-PROPERTIES",
                    "dev",
                    "show",
                    "wlan0",
                ],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                mode
                + [
                    "-f",
                    "GENERAL,GENERAL.HWADDR,WIFI-PROPERTIES",
                    "dev",
                    "show",
                    "wlan0",
                ],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                mode + ["-f", "DEVICE,TYPE,DBUS-PATH", "dev"],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                mode + ["-f", "ALL", "device", "wifi", "list"],
                replace_stdout=replace_uuids,
            )
            self.call_nmcli_l(
                mode + ["-f", "COMMON", "device", "wifi", "list"],
                replace_stdout=replace_uuids,
            )
            self.call_nmcli_l(
                mode
                + [
                    "-f",
                    "NAME,SSID,SSID-HEX,BSSID,MODE,CHAN,FREQ,RATE,SIGNAL,BARS,SECURITY,WPA-FLAGS,RSN-FLAGS,DEVICE,ACTIVE,IN-USE,DBUS-PATH",
                    "device",
                    "wifi",
                    "list",
                ],
                replace_stdout=replace_uuids,
            )
            self.call_nmcli_l(
                mode
                + ["-f", "ALL", "device", "wifi", "list", "bssid", "C0:E2:BE:E8:EF:B6"],
                replace_stdout=replace_uuids,
            )
            self.call_nmcli_l(
                mode
                + [
                    "-f",
                    "COMMON",
                    "device",
                    "wifi",
                    "list",
                    "bssid",
                    "C0:E2:BE:E8:EF:B6",
                ],
                replace_stdout=replace_uuids,
            )
            self.call_nmcli_l(
                mode
                + [
                    "-f",
                    "NAME,SSID,SSID-HEX,BSSID,MODE,CHAN,FREQ,RATE,SIGNAL,BARS,SECURITY,WPA-FLAGS,RSN-FLAGS,DEVICE,ACTIVE,IN-USE,DBUS-PATH",
                    "device",
                    "wifi",
                    "list",
                    "bssid",
                    "C0:E2:BE:E8:EF:B6",
                ],
                replace_stdout=replace_uuids,
            )
            self.call_nmcli_l(
                mode + ["-f", "ALL", "device", "show", "wlan0"],
                replace_stdout=replace_uuids,
            )
            self.call_nmcli_l(
                mode + ["-f", "COMMON", "device", "show", "wlan0"],
                replace_stdout=replace_uuids,
            )
            self.call_nmcli_l(
                mode
                + [
                    "-f",
                    "GENERAL,CAPABILITIES,WIFI-PROPERTIES,AP,WIRED-PROPERTIES,WIMAX-PROPERTIES,NSP,IP4,DHCP4,IP6,DHCP6,BOND,TEAM,BRIDGE,VLAN,BLUETOOTH,CONNECTIONS",
                    "device",
                    "show",
                    "wlan0",
                ],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                mode + ["dev", "lldp", "list", "ifname", "eth0"],
                replace_stdout=replace_uuids,
            )

            self.call_nmcli_l(
                mode
                + [
                    "-f",
                    "connection.id,connection.uuid,connection.type,connection.interface-name,802-3-ethernet.mac-address,vpn.user-name",
                    "connection",
                    "show",
                ]
                + uuids,
                replace_stdout=replace_uuids,
                replace_cmd=replace_uuids,
            )

    @nm_test_no_dbus
    def test_offline(self):

        # Make sure we're not using D-Bus
        no_dbus_env = {
            "DBUS_SYSTEM_BUS_ADDRESS": "very:invalid",
            "DBUS_SESSION_BUS_ADDRESS": "very:invalid",
        }

        # This check just makes sure the above works and the
        # "nmcli g" command indeed fails talking to D-Bus
        self.call_nmcli(
            ["g"],
            extra_env=no_dbus_env,
            replace_stderr=[
                Util.ReplaceTextRegex(
                    # depending on glib version, it prints `%s', '%s', or ā€œ%sā€.
                    # depending on libc version, it converts unicode to ? or *.
                    r"Key/Value pair 0, [`*?']invalid[*?'], in address element [`*?']very:invalid[*?'] does not contain an equal sign",
                    "Key/Value pair 0, 'invalid', in address element 'very:invalid' does not contain an equal sign",
                )
            ],
        )

        replace_uuids = [
            Util.ReplaceTextRegex(
                r"\buuid=[-a-f0-9]+\b", "uuid=UUID-WAS-HERE-BUT-IS-NO-MORE-SADLY"
            )
        ]

        self.call_nmcli(
            ["--offline", "c", "add", "type", "ethernet"],
            extra_env=no_dbus_env,
            replace_stdout=replace_uuids,
        )

        self.call_nmcli(
            ["--offline", "c", "show"],
            extra_env=no_dbus_env,
        )

        self.call_nmcli(
            ["--offline", "g"],
            extra_env=no_dbus_env,
        )

        self.call_nmcli(
            ["--offline"],
            extra_env=no_dbus_env,
        )

        self.call_nmcli(
            [
                "--offline",
                "c",
                "add",
                "type",
                "wifi",
                "ssid",
                "lala",
                "802-1x.eap",
                "pwd",
                "802-1x.identity",
                "foo",
                "802-1x.password",
                "bar",
            ],
            extra_env=no_dbus_env,
            replace_stdout=replace_uuids,
        )

        self.call_nmcli(
            [
                "--offline",
                "c",
                "add",
                "type",
                "wifi",
                "ssid",
                "lala",
                "802-1x.eap",
                "pwd",
                "802-1x.identity",
                "foo",
                "802-1x.password",
                "bar",
                "802-1x.password-flags",
                "agent-owned",
            ],
            extra_env=no_dbus_env,
            replace_stdout=replace_uuids,
        )

        self.call_nmcli(
            ["--complete-args", "--offline", "conn", "modify", "ipv6.ad"],
            extra_env=no_dbus_env,
        )

    @Util.skip_without_pexpect
    @nm_test
    def test_ask_mode(self):
        nmc = self.call_nmcli_pexpect(["--ask", "c", "add"])
        nmc.pexp.expect("Connection type:")
        nmc.pexp.sendline("ethernet")
        nmc.pexp.expect("Interface name:")
        nmc.pexp.sendline("eth0")
        nmc.pexp.expect("There are 3 optional settings for Wired Ethernet.")
        nmc.pexp.expect("Do you want to provide them\? \(yes/no\) \[yes]")
        nmc.pexp.sendline("no")
        nmc.pexp.expect("There are 2 optional settings for IPv4 protocol.")
        nmc.pexp.expect("Do you want to provide them\? \(yes/no\) \[yes]")
        nmc.pexp.sendline("no")
        nmc.pexp.expect("There are 2 optional settings for IPv6 protocol.")
        nmc.pexp.expect("Do you want to provide them\? \(yes/no\) \[yes]")
        nmc.pexp.sendline("no")
        nmc.pexp.expect("There are 4 optional settings for Proxy.")
        nmc.pexp.expect("Do you want to provide them\? \(yes/no\) \[yes]")
        nmc.pexp.sendline("no")
        nmc.pexp.expect("Connection 'ethernet' \(.*\) successfully added.")
        nmc.pexp.expect(pexpect.EOF)
        Util.valgrind_check_log(nmc.valgrind_log, "test_ask_mode")

    @Util.skip_without_pexpect
    @nm_test
    def test_monitor(self):
        def start_mon(self):
            nmc = self.call_nmcli_pexpect(["monitor"])
            nmc.pexp.expect("NetworkManager is running")
            return nmc

        def end_mon(self, nmc):
            nmc.pexp.kill(signal.SIGINT)
            nmc.pexp.expect(pexpect.EOF)
            Util.valgrind_check_log(nmc.valgrind_log, "test_monitor")

        nmc = start_mon(self)

        self.srv.op_AddObj("WiredDevice", iface="eth0")
        nmc.pexp.expect("eth0: device created\r\n")

        self.srv.addConnection(
            {"connection": {"type": "802-3-ethernet", "id": "con-1"}}
        )
        nmc.pexp.expect("con-1: connection profile created\r\n")

        end_mon(self, nmc)

        nmc = start_mon(self)
        self.srv_shutdown()
        Util.pexpect_expect_all(
            nmc.pexp,
            "con-1: connection profile removed",
            "eth0: device removed",
        )
        nmc.pexp.expect("NetworkManager is stopped")
        end_mon(self, nmc)


###############################################################################


class TestNmCloudSetup(TestNmClient):

    _mac1 = "9e:c0:3e:92:24:2d"
    _mac2 = "53:e9:7e:52:8d:a8"

    _ip1 = "172.31.26.249"
    _ip2 = "172.31.176.249"

    def cloud_setup_test(func):
        """
        Runs the mock NetworkManager along with a mock cloud metadata service.
        """

        def f(self):
            Util.skip_without_pexpect()

            if tuple(sys.version_info[0:2]) < (3, 2):
                # subprocess.Popen()'s "pass_fd" argument requires at least Python 3.2.
                raise unittest.SkipTest("This test requires at least Python 3.2")

            s = socket.socket()
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
            s.bind(("localhost", 0))

            # The same value as Python's TCPServer uses.
            # Chosen by summoning the sprit of TCP under influence of
            # hallucinogenic substances.
            s.listen(5)

            def pass_socket():
                os.dup2(s.fileno(), 3)

            service_path = PathConfiguration.test_cloud_meta_mock_path()
            env = os.environ.copy()
            env["LISTEN_FDS"] = "1"
            p = subprocess.Popen(
                [sys.executable, service_path, "--empty"],
                stdin=subprocess.PIPE,
                env=env,
                pass_fds=(3,),
                preexec_fn=pass_socket,
            )

            (hostaddr, port) = s.getsockname()
            self.md_conn = HTTPConnection(hostaddr, port=port)
            self.md_url = "http://%s:%d" % (hostaddr, port)
            s.close()

            error = None

            self.srv_start()
            try:
                func(self)
            except Exception as e:
                error = e
            self._nm_test_post()

            self.md_conn.close()
            p.stdin.close()
            p.terminate()
            p.wait()

            if error:
                raise error

        return f

    def _mock_devices(self):
        # Add a device with an active connection that has IPv4 configured
        self.srv.op_AddObj("WiredDevice", iface="eth0", mac="9e:c0:3e:92:24:2d")
        self.srv.addAndActivateConnection(
            {
                "connection": {"type": "802-3-ethernet", "id": "con-eth0"},
                "ipv4": {"method": "auto"},
            },
            "/org/freedesktop/NetworkManager/Devices/1",
            delay=0,
        )

        # The second connection has no IPv4
        self.srv.op_AddObj("WiredDevice", iface="eth1", mac="53:e9:7e:52:8d:a8")
        self.srv.addAndActivateConnection(
            {"connection": {"type": "802-3-ethernet", "id": "con-eth1"}},
            "/org/freedesktop/NetworkManager/Devices/2",
            "",
            delay=0,
        )

    def _mock_path(self, path, body):
        self.md_conn.request("PUT", path, body=body)
        self.md_conn.getresponse().read()

    @cloud_setup_test
    def test_aliyun(self):
        self._mock_devices()

        _aliyun_meta = "/2016-01-01/meta-data/"
        _aliyun_macs = _aliyun_meta + "network/interfaces/macs/"
        self._mock_path(_aliyun_meta, "ami-id\n")
        self._mock_path(
            _aliyun_macs, TestNmCloudSetup._mac2 + "\n" + TestNmCloudSetup._mac1
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac2 + "/vpc-cidr-block", "172.31.16.0/20"
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac2 + "/private-ipv4s",
            TestNmCloudSetup._ip1,
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac2 + "/primary-ip-address",
            TestNmCloudSetup._ip1,
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac2 + "/netmask", "255.255.255.0"
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac2 + "/gateway", "172.31.26.2"
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac1 + "/vpc-cidr-block", "172.31.166.0/20"
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac1 + "/private-ipv4s",
            TestNmCloudSetup._ip2,
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac1 + "/primary-ip-address",
            TestNmCloudSetup._ip2,
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac1 + "/netmask", "255.255.255.0"
        )
        self._mock_path(
            _aliyun_macs + TestNmCloudSetup._mac1 + "/gateway", "172.31.176.2"
        )

        # Run nm-cloud-setup for the first time
        nmc = self.call_pexpect(
            ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH,
            [],
            {
                "NM_CLOUD_SETUP_ALIYUN_HOST": self.md_url,
                "NM_CLOUD_SETUP_LOG": "trace",
                "NM_CLOUD_SETUP_ALIYUN": "yes",
            },
        )

        nmc.pexp.expect("provider aliyun detected")
        nmc.pexp.expect("found interfaces: 9E:C0:3E:92:24:2D, 53:E9:7E:52:8D:A8")
        nmc.pexp.expect("get-config: start fetching meta data")
        nmc.pexp.expect("get-config: success")
        nmc.pexp.expect("meta data received")
        # One of the devices has no IPv4 configuration to be modified
        nmc.pexp.expect("device has no suitable applied connection. Skip")
        # The other one was lacking an address set it up.
        nmc.pexp.expect("some changes were applied for provider aliyun")
        nmc.pexp.expect(pexpect.EOF)

        # Run nm-cloud-setup for the second time
        nmc = self.call_pexpect(
            ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH,
            [],
            {
                "NM_CLOUD_SETUP_ALIYUN_HOST": self.md_url,
                "NM_CLOUD_SETUP_LOG": "trace",
                "NM_CLOUD_SETUP_ALIYUN": "yes",
            },
        )

        nmc.pexp.expect("provider aliyun detected")
        nmc.pexp.expect("found interfaces: 9E:C0:3E:92:24:2D, 53:E9:7E:52:8D:A8")
        nmc.pexp.expect("get-config: starting")
        nmc.pexp.expect("get-config: success")
        nmc.pexp.expect("meta data received")
        # No changes this time
        nmc.pexp.expect('device needs no update to applied connection "con-eth0"')
        nmc.pexp.expect("no changes were applied for provider aliyun")
        nmc.pexp.expect(pexpect.EOF)

        Util.valgrind_check_log(nmc.valgrind_log, "test_aliyun")

    @cloud_setup_test
    def test_azure(self):
        self._mock_devices()

        _azure_meta = "/metadata/instance"
        _azure_iface = _azure_meta + "/network/interface/"
        _azure_query = "?format=text&api-version=2017-04-02"
        self._mock_path(_azure_meta + _azure_query, "")
        self._mock_path(_azure_iface + _azure_query, "0\n1\n")
        self._mock_path(
            _azure_iface + "0/macAddress" + _azure_query, TestNmCloudSetup._mac1
        )
        self._mock_path(
            _azure_iface + "1/macAddress" + _azure_query, TestNmCloudSetup._mac2
        )
        self._mock_path(_azure_iface + "0/ipv4/ipAddress/" + _azure_query, "0\n")
        self._mock_path(_azure_iface + "1/ipv4/ipAddress/" + _azure_query, "0\n")
        self._mock_path(
            _azure_iface + "0/ipv4/ipAddress/0/privateIpAddress" + _azure_query,
            TestNmCloudSetup._ip1,
        )
        self._mock_path(
            _azure_iface + "1/ipv4/ipAddress/0/privateIpAddress" + _azure_query,
            TestNmCloudSetup._ip2,
        )
        self._mock_path(
            _azure_iface + "0/ipv4/subnet/0/address/" + _azure_query, "172.31.16.0"
        )
        self._mock_path(
            _azure_iface + "1/ipv4/subnet/0/address/" + _azure_query, "172.31.166.0"
        )
        self._mock_path(_azure_iface + "0/ipv4/subnet/0/prefix/" + _azure_query, "20")
        self._mock_path(_azure_iface + "1/ipv4/subnet/0/prefix/" + _azure_query, "20")

        # Run nm-cloud-setup for the first time
        nmc = self.call_pexpect(
            ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH,
            [],
            {
                "NM_CLOUD_SETUP_AZURE_HOST": self.md_url,
                "NM_CLOUD_SETUP_LOG": "trace",
                "NM_CLOUD_SETUP_AZURE": "yes",
            },
        )

        nmc.pexp.expect("provider azure detected")
        nmc.pexp.expect("found interfaces: 9E:C0:3E:92:24:2D, 53:E9:7E:52:8D:A8")
        nmc.pexp.expect("found azure interfaces: 2")
        nmc.pexp.expect("interface\[0]: found a matching device with hwaddr")
        nmc.pexp.expect(
            "interface\[0]: (received subnet address|received subnet prefix 20)"
        )
        nmc.pexp.expect(
            "interface\[0]: (received subnet address|received subnet prefix 20)"
        )
        nmc.pexp.expect("get-config: success")
        nmc.pexp.expect("meta data received")
        # One of the devices has no IPv4 configuration to be modified
        nmc.pexp.expect("device has no suitable applied connection. Skip")
        # The other one was lacking an address set it up.
        nmc.pexp.expect("some changes were applied for provider azure")
        nmc.pexp.expect(pexpect.EOF)

        # Run nm-cloud-setup for the second time
        nmc = self.call_pexpect(
            ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH,
            [],
            {
                "NM_CLOUD_SETUP_AZURE_HOST": self.md_url,
                "NM_CLOUD_SETUP_LOG": "trace",
                "NM_CLOUD_SETUP_AZURE": "yes",
            },
        )

        nmc.pexp.expect("provider azure detected")
        nmc.pexp.expect("found interfaces: 9E:C0:3E:92:24:2D, 53:E9:7E:52:8D:A8")
        nmc.pexp.expect("get-config: starting")
        nmc.pexp.expect("get-config: success")
        nmc.pexp.expect("meta data received")
        # No changes this time
        nmc.pexp.expect('device needs no update to applied connection "con-eth0"')
        nmc.pexp.expect("no changes were applied for provider azure")
        nmc.pexp.expect(pexpect.EOF)

        Util.valgrind_check_log(nmc.valgrind_log, "test_azure")

    @cloud_setup_test
    def test_ec2(self):
        self._mock_devices()

        _ec2_macs = "/2018-09-24/meta-data/network/interfaces/macs/"
        self._mock_path("/latest/meta-data/", "ami-id\n")
        self._mock_path(
            _ec2_macs, TestNmCloudSetup._mac2 + "\n" + TestNmCloudSetup._mac1
        )
        self._mock_path(
            _ec2_macs + TestNmCloudSetup._mac2 + "/subnet-ipv4-cidr-block",
            "172.31.16.0/20",
        )
        self._mock_path(
            _ec2_macs + TestNmCloudSetup._mac2 + "/local-ipv4s", TestNmCloudSetup._ip1
        )
        self._mock_path(
            _ec2_macs + TestNmCloudSetup._mac1 + "/subnet-ipv4-cidr-block",
            "172.31.166.0/20",
        )
        self._mock_path(
            _ec2_macs + TestNmCloudSetup._mac1 + "/local-ipv4s", TestNmCloudSetup._ip2
        )

        # Run nm-cloud-setup for the first time
        nmc = self.call_pexpect(
            ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH,
            [],
            {
                "NM_CLOUD_SETUP_EC2_HOST": self.md_url,
                "NM_CLOUD_SETUP_LOG": "trace",
                "NM_CLOUD_SETUP_EC2": "yes",
            },
        )

        nmc.pexp.expect("provider ec2 detected")
        nmc.pexp.expect("found interfaces: 9E:C0:3E:92:24:2D, 53:E9:7E:52:8D:A8")
        nmc.pexp.expect("get-config: starting")
        nmc.pexp.expect("get-config: success")
        nmc.pexp.expect("meta data received")
        # One of the devices has no IPv4 configuration to be modified
        nmc.pexp.expect("device has no suitable applied connection. Skip")
        # The other one was lacking an address set it up.
        nmc.pexp.expect("some changes were applied for provider ec2")
        nmc.pexp.expect(pexpect.EOF)

        # Run nm-cloud-setup for the second time
        nmc = self.call_pexpect(
            ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH,
            [],
            {
                "NM_CLOUD_SETUP_EC2_HOST": self.md_url,
                "NM_CLOUD_SETUP_LOG": "trace",
                "NM_CLOUD_SETUP_EC2": "yes",
            },
        )

        nmc.pexp.expect("provider ec2 detected")
        nmc.pexp.expect("found interfaces: 9E:C0:3E:92:24:2D, 53:E9:7E:52:8D:A8")
        nmc.pexp.expect("get-config: starting")
        nmc.pexp.expect("get-config: success")
        nmc.pexp.expect("meta data received")
        # No changes this time
        nmc.pexp.expect('device needs no update to applied connection "con-eth0"')
        nmc.pexp.expect("no changes were applied for provider ec2")
        nmc.pexp.expect(pexpect.EOF)

        Util.valgrind_check_log(nmc.valgrind_log, "test_ec2")

    @cloud_setup_test
    def test_gcp(self):
        self._mock_devices()

        gcp_meta = "/computeMetadata/v1/instance/"
        gcp_iface = gcp_meta + "network-interfaces/"
        self._mock_path(gcp_meta + "id", "")
        self._mock_path(gcp_iface, "0\n1\n")
        self._mock_path(gcp_iface + "0/mac", TestNmCloudSetup._mac1)
        self._mock_path(gcp_iface + "1/mac", TestNmCloudSetup._mac2)
        self._mock_path(gcp_iface + "0/forwarded-ips/", "0\n")
        self._mock_path(gcp_iface + "0/forwarded-ips/0", TestNmCloudSetup._ip1)
        self._mock_path(gcp_iface + "1/forwarded-ips/", "0\n")
        self._mock_path(gcp_iface + "1/forwarded-ips/0", TestNmCloudSetup._ip2)

        # Run nm-cloud-setup for the first time
        nmc = self.call_pexpect(
            ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH,
            [],
            {
                "NM_CLOUD_SETUP_GCP_HOST": self.md_url,
                "NM_CLOUD_SETUP_LOG": "trace",
                "NM_CLOUD_SETUP_GCP": "yes",
            },
        )

        nmc.pexp.expect("provider GCP detected")
        nmc.pexp.expect("found interfaces: 9E:C0:3E:92:24:2D, 53:E9:7E:52:8D:A8")
        nmc.pexp.expect("found GCP interfaces: 2")
        nmc.pexp.expect("GCP interface\[0]: found a requested device with hwaddr")
        nmc.pexp.expect("get-config: success")
        nmc.pexp.expect("meta data received")
        # One of the devices has no IPv4 configuration to be modified
        nmc.pexp.expect("device has no suitable applied connection. Skip")
        # The other one was lacking an address set it up.
        nmc.pexp.expect("some changes were applied for provider GCP")
        nmc.pexp.expect(pexpect.EOF)

        # Run nm-cloud-setup for the second time
        nmc = self.call_pexpect(
            ENV_NM_TEST_CLIENT_CLOUD_SETUP_PATH,
            [],
            {
                "NM_CLOUD_SETUP_GCP_HOST": self.md_url,
                "NM_CLOUD_SETUP_LOG": "trace",
                "NM_CLOUD_SETUP_GCP": "yes",
            },
        )

        nmc.pexp.expect("provider GCP detected")
        nmc.pexp.expect("found interfaces: 9E:C0:3E:92:24:2D, 53:E9:7E:52:8D:A8")
        nmc.pexp.expect("get-config: starting")
        nmc.pexp.expect("get-config: success")
        nmc.pexp.expect("meta data received")
        # No changes this time
        nmc.pexp.expect('device needs no update to applied connection "con-eth0"')
        nmc.pexp.expect("no changes were applied for provider GCP")
        nmc.pexp.expect(pexpect.EOF)

        Util.valgrind_check_log(nmc.valgrind_log, "test_gcp")


###############################################################################


def main():
    global dbus_session_inited

    if len(sys.argv) >= 2 and sys.argv[1] == "--started-with-dbus-session":
        dbus_session_inited = True
        del sys.argv[1]

    if not dbus_session_inited:
        # we don't have yet our own dbus-session. Reexec ourself with
        # a new dbus-session.
        try:
            try:
                os.execlp(
                    "dbus-run-session",
                    "dbus-run-session",
                    "--",
                    sys.executable,
                    __file__,
                    "--started-with-dbus-session",
                    *sys.argv[1:]
                )
            except OSError as e:
                if e.errno != errno.ENOENT:
                    raise
                # we have no dbus-run-session in path? Fall-through
                # to skip tests gracefully
            else:
                raise Exception("unknown error during exec")
        except Exception as e:
            assert False, "Failure to re-exec dbus-run-session: %s" % (str(e))

    if not dbus_session_inited:
        # we still don't have a D-Bus session. Probably dbus-run-session is not available.
        # retry with dbus-launch
        if os.system("type dbus-launch 1>/dev/null") == 0:
            try:
                os.execlp(
                    "bash",
                    "bash",
                    "-e",
                    "-c",
                    "eval `dbus-launch --sh-syntax`;\n"
                    + 'trap "kill $DBUS_SESSION_BUS_PID" EXIT;\n'
                    + "\n"
                    + Util.shlex_join(
                        [
                            sys.executable,
                            __file__,
                            "--started-with-dbus-session",
                        ]
                        + sys.argv[1:]
                    )
                    + " \n"
                    + "",
                )
            except Exception as e:
                m = str(e)
            else:
                m = "unknown error"
            assert False, "Failure to re-exec to start script with dbus-launch: %s" % (
                m
            )

    r = unittest.main(exit=False)

    sys.exit(not r.result.wasSuccessful())


if __name__ == "__main__":
    main()