forked from os-autoinst/os-autoinst-distri-opensuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wickedbase.pm
1327 lines (1062 loc) · 45.3 KB
/
wickedbase.pm
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
# SUSE's openQA tests
#
# Copyright 2017-2022 SUSE LLC
# SPDX-License-Identifier: FSFAP
# Summary: Base module for all wicked scenarios
# Maintainer: Anton Smorodskyi <[email protected]>
package wickedbase;
use base 'opensusebasetest';
use utils qw(systemctl file_content_replace zypper_call random_string);
use Encode qw(encode_utf8);
use network_utils;
use lockapi;
use testapi qw(is_serial_terminal :DEFAULT);
use serial_terminal 'select_serial_terminal';
use version_utils 'is_sle';
use bmwqemu;
use serial_terminal;
use Carp;
use Mojo::File 'path';
use Mojo::Util qw(b64_encode b64_decode trim);
use Regexp::Common 'net';
use File::Basename;
use version_utils 'check_version';
use List::MoreUtils qw(uniq);
use containers::common qw(install_podman_when_needed install_docker_when_needed);
use strict;
use warnings;
use Utils::Architectures;
use constant WICKED_DATA_DIR => '/root/wicked/data';
=head2 wicked_command
wicked_command($action => [ifup|ifdown|ifreaload], $iface)
Executes wicked command given the action on the corresponding interface.
The mandatory parameter C<action> specifies the action [ifup|ifdown|ifreaload].
The mandatory parameter C<iface> specifies the interface which action will be executed on.
This function saves the command and the stdout and stderr to a file to be uploaded later.
=cut
sub wicked_command {
my ($self, $action, $iface) = @_;
my $serial_log = '/tmp/wicked_serial.log';
$self->add_post_log_file($serial_log);
my $cmd = '/usr/sbin/wicked --log-target syslog --debug all ' . $action . ' ' . $iface;
assert_script_run('echo -e "\n# $(date -Isecond)\n# "' . $cmd . ' >> ' . $serial_log);
$cmd = $self->valgrind_cmd('wicked') . " $cmd" if (grep { /^wicked$/ } $self->valgrind_get_services());
record_info('wicked cmd', $cmd);
assert_script_run('time ' . $cmd . ' 2>&1 | tee -a ' . $serial_log);
assert_script_run(q(echo -e "\n# ip addr" >> ) . $serial_log);
assert_script_run('ip addr 2>&1 | tee -a ' . $serial_log);
}
=head2 get_wicked_version
get_wicked_version()
Return the current installed wicked version
=cut
sub get_wicked_version {
my $v = script_output(q(rpm -qa 'wicked' --qf '%{VERSION}\n'));
die("Unable to get wicked version '$v'") unless $v =~ /^\d+\.\d+\.\d+$/;
return $v;
}
=head2 check_wicked_version
check_wicked_version('>=0.6.66')
=cut
sub check_wicked_version {
my ($self, $query) = @_;
return 1 if get_var('WICKED_SKIP_VERSION_CHECK', 0);
return check_version($query, $self->get_wicked_version());
}
sub skip_by_wicked_version
{
my ($self, $v) = @_;
$v //= $self->wicked_version;
if ($v && !$self->check_wicked_version($v)) {
$self->result('skip');
return 1;
} else {
return 0;
}
}
=head2 assert_wicked_state
assert_wicked_state([wicked_client_down => 0, interfaces_down => 0,
wicked_daemon_down => 0, $ping_ip, $iface])
Check that wicked processes are as expected by input arguments. 'Up' by default.
The optional parameters C<wicked_client_down> is given normally with C<interfaces_down> => 1
to verify that wicked.service process is down.
The optional argument C<wicked_daemon_down> is given to verify that wickedd.service is down.
The optional argument C<ping_ip> checks that the IP is reachable.
The optinal argument C<iface> allows to print the output of the command 'ip address show'.
With no arguments, it will check that wicked.service and wickedd.service are up.
=cut
sub assert_wicked_state {
my ($self, %args) = @_;
systemctl('is-active wicked.service', expect_false => $args{wicked_client_down});
systemctl('is-active wickedd.service', expect_false => $args{wicked_daemon_down});
assert_script_run(sprintf("grep -q \"%s\" /sys/class/net/%s/operstate", $args{interfaces_down} ? 'down' : 'up', $args{iface}));
$self->ping_with_timeout(ip => $args{ping_ip}) if $args{ping_ip};
}
=head2 valgrind_get_services
valgrind_get_services()
Retrieves the list of wicked services where valgrind is enabled for. It read the
`WICKED_VALGRIND` variable.
=cut
sub valgrind_get_services {
my $self = shift;
my $val = get_var('WICKED_VALGRIND');
return if (!defined($val) || !$val);
my @all = qw(wickedd-auto4
wickedd-dhcp6
wickedd
wickedd-dhcp4
wickedd-nanny
wicked);
my @tmp;
my @enable;
if (index($val, ",")) {
@tmp = split(/,/, $val);
} else {
@tmp = $val;
}
foreach my $v (@tmp) {
$v =~ s/\.service$//;
if (grep { /^$v$/ } @all) {
push @enable, $v;
} elsif ($v =~ /^1|all$/i) {
push @enable, @all;
} else {
record_info('WARNING', "Unknown value for WICKED_VALGRIND $v", result => 'softfail');
}
}
@enable = uniq @enable;
return @enable;
}
=head2 valgrind_cmd
valgrind_cmd([service => undef, systemd => 0])
Retrieves the valgrind command. If the C<service> is given, the log file will contain
the name of it. It used `WICKED_VALGRIND_CMD` variable to build the command.
When the command is needed to rewrite the systemd service file, specify C<<systemd=>1>>.
=cut
sub valgrind_cmd {
my ($self, %args) = @_;
$args{service} //= undef;
$args{systemd} //= 0;
my $valgrind_cmd = '/usr/bin/valgrind --verbose --tool=memcheck --leak-check=yes';
# Don't add environment variable to disable debuginfod when we retrive the
# command for the systemd service file.
# XXX We could use '--enable-debuginfod=no' but it comes with valgrind 3.20.0
# which isn't in sle-12-SP5 yet.
if ($args{systemd} == 0) {
$valgrind_cmd = "DEBUGINFOD_URLS='' $valgrind_cmd";
}
$valgrind_cmd = get_var('WICKED_VALGRIND_CMD', $valgrind_cmd);
if ($args{service}) {
my $cnt = $self->{valgrind_log_file_counter}->{$args{service}} += 1;
my $logfile = "/var/log/valgrind_" . $args{service} . "_$cnt.log";
$self->add_post_log_file($logfile);
$valgrind_cmd = "$valgrind_cmd --log-file=$logfile";
}
return $valgrind_cmd;
}
=head2 valgrind_enable
valgrind_enable()
Modify all systemd service units, to enable valgrind for all binarys which where
specified via WICKED_VALGRIND.
=cut
sub valgrind_enable {
my $self = shift;
my @services = $self->valgrind_get_services();
return 0 if (!@services);
record_info("valgrind enable", "services: @services\ncommand: " . $self->valgrind_cmd);
assert_script_run(q(echo 'DEBUGINFOD_URLS=""' >> /etc/sysconfig/network/config));
foreach my $service (@services) {
my $service_file = "/etc/systemd/system/$service.service";
assert_script_run("systemctl cat $service > $service_file");
# Add valgrind command prefix to `ExecStart=` in the custom service file
assert_script_run(sprintf(q(sed -i -E 's@^(ExecStart=)(.*)$@\1%s \2@' '%s'),
$self->valgrind_cmd(service => $service, systemd => 1), $service_file));
record_info("$service.service", script_output("cat $service_file"));
}
assert_script_run('systemctl daemon-reload');
return 1;
}
=head2
valgrind_prerun()
Run before test-module. Simple cleanup of left-overs.
=cut
sub valgrind_prerun {
my $self = shift;
my @services = $self->valgrind_get_services();
foreach my $service (@services) {
my $logfile = "/var/log/valgrind_${service}_*.log";
assert_script_run("rm -f $logfile");
}
}
=head2 valgrind_postrun
valgrind_postrun()
Check for valgrind errors in one of the valgrind enabled binaries valgrind-logs.
=cut
sub valgrind_postrun {
my $self = shift;
my @services = $self->valgrind_get_services();
foreach my $service (@services) {
my @files = split(/\r?\n/, script_output("find /var/log -name 'valgrind_${service}_*.log' -print"));
foreach my $f (@files) {
if ((my $out = script_output("cat '$f'")) =~ /ERROR\s+SUMMARY:\s+(\d+)/) {
if ($1 > 0) {
record_info("valgrind $service", "service: $service\nfile: $f\n\n" . $out, result => 'fail');
$self->result('fail');
}
}
}
}
}
sub reset_wicked {
my $self = @_;
# Remove any config file and leave the system clean to start tests
assert_script_run('find /etc/sysconfig/network/ \( -name "ifcfg-*" -not -name "ifcfg-lo" \) -exec rm {} \;');
assert_script_run('find /etc/sysconfig/network/ \( -name "routes" -o -name "ifroute-*" \) -exec rm {} \;');
# Remove any previous manual ip configuration
my $iface = iface();
assert_script_run("ip a flush dev $iface");
assert_script_run('ip r flush all');
assert_script_run("ip link set dev $iface down");
file_content_replace("/etc/sysconfig/network/config", "^NETCONFIG_DNS_STATIC_SERVERS=.*" => " ");
assert_script_run("netconfig -f update");
# Restart services
assert_script_run('rcwickedd restart');
assert_script_run('rcwicked restart');
}
=head2 get_remote_ip
get_remote_ip(type => $type [, netmask => 0])
Calls internally C<get_ip()> and retrieves the corresponding IP of the remote site.
=cut
sub get_remote_ip {
my ($self, %args) = @_;
$args{is_wicked_ref} = !check_var('IS_WICKED_REF', '1');
return $self->get_ip(%args);
}
=head2 get_ip
get_ip(type => [host|gre1|sit1|tunl1|tun1|br0|vlan|vlan_changed] [, is_wicked_ref => check_var('IS_WICKED_REF', '1'), netmask => 0])
Retrives IP address as C<string> in IPv4 or IPv6 format and netmask prefix if C<netmask> is set.
The mandatory parameter C<type> specify the interfaces type.
If parameter C<netmask> is set, the IP address contains the C</xx> netmask prefix, if specified.
With C<is_wicked_ref> you can specify which IP address you like to retrives. If C<is_wicked_ref> isn't
set the job variable C<IS_WICKED_REF> will be used. See also C<get_remote_ip()>.
=cut
sub get_ip {
my ($self, %args) = @_;
die '$args{type} is required' unless $args{type};
$args{is_wicked_ref} //= check_var('IS_WICKED_REF', '1');
$args{netmask} //= 0;
my $ips_hash =
{
# SUT REF
host => ['10.0.2.11/15', '10.0.2.10/15'],
host6 => ['fd00:deca:fbad:50::11/64', 'fd00:deca:fbad:50::10/64'],
gre1 => ['192.168.1.2', '192.168.1.1'],
sit1 => ['2001:0db8:1234::000f', '2001:0db8:1234::000e'],
tunl1 => ['3.3.3.11', '3.3.3.10'],
tun1 => ['192.168.2.11', '192.168.2.10'],
tap1 => ['192.168.2.11', '192.168.2.10'],
br0 => ['10.0.2.11', '10.0.2.10'],
vlan => ['192.0.2.11/24', '192.0.2.10/24'],
vlan_changed => ['192.0.2.111/24', '192.0.2.110/24'],
macvtap => ['10.0.2.18/15', '10.0.2.17/15'],
bond => ['10.0.2.18', '10.0.2.17'],
dhcp_2nic => ['10.20.30.', '10.20.30.12'], # dhcp_2nic in SUT, we don't know the last octect
second_card => ['10.0.3.11', '10.0.3.12'],
gateway => ['10.0.2.2', '10.0.2.2'],
wlan => ['10.6.6.2/24', '10.6.6.1/24'],
wlan_dhcp => ['10.6.6.10/24', '10.6.6.1/24'],
wlan_bss1 => ['10.6.7.2/24', '10.6.7.1/24'],
wlan_dhcp_bss1 => ['10.6.7.10/24', '10.6.7.1/24'],
wlan_bss2 => ['10.6.8.2/24', '10.6.8.1/24'],
wlan_dhcp_bss2 => ['10.6.8.10/24', '10.6.8.1/24'],
wlan_bss3 => ['10.6.9.2/24', '10.6.9.1/24'],
wlan_dhcp_bss3 => ['10.6.9.10/24', '10.6.9.1/24'],
ipv6 => ['fd00:dead:beef:', 'fd00:dead:beef:'],
dhcp6 => ['fd00:dead:beef:6021:d::11', 'fd00:dead:beef:6021:d::10'],
dns_advice => ['fd00:dead:beef:6021::42', 'fd00:dead:beef:6021::42'],
vxlan => ['10.100.0.11/24', '10.100.0.10/24'],
};
my $ip = $ips_hash->{$args{type}}->[$args{is_wicked_ref}];
die "$args{type} not exists" unless $ip;
if (!$args{netmask}) {
$ip =~ s'/\d+$'';
}
return $ip;
}
=head2 get_current_ip
get_current_ip(ifc => $interface [, ip_version => 'v4'])
Gets the IP of a given interface by C<ifc>.
The parameter C<ip_version> chould be one of the values 'v4' or 'v6'.
=cut
sub get_current_ip {
my ($self, $ifc, %args) = @_;
$args{ip_version} //= 'v4';
die("Missing mandatory parameter ifc") unless ($ifc);
my $out = script_output('ip -o ' . ($args{ip_version} eq 'v6' ? '-6' : '-4') . ' addr list ' . $ifc . ' | awk \'{print $4}\' | cut -d/ -f1');
my @ips = split('\r?\n', $out);
return $ips[0] if (@ips);
return;
}
=head2 download_data_dir
Download all files from data/wicked into WICKED_DATA_DIR.
This method is used by before_test.pm.
=cut
sub download_data_dir {
assert_script_run("mkdir -p '" . WICKED_DATA_DIR . "'");
assert_script_run("(cd '" . WICKED_DATA_DIR . "'; curl -L -v " . autoinst_url . "/data/wicked > wicked.data && cpio -id < wicked.data && mv data wicked && rm wicked.data)");
}
=head2 get_from_data
get_from_data($source, $target [, executable => 0, add_suffix => 0])
Downloads to the current VM a file from the data directory given by C<source> and stores it in C<target>.
If the parameter C<add_suffix> is set to 1, it will append 'ref' or 'sut' at the end of the filename.
If the parameter C<executable> is set to 1, it will grant execution permissions to the file.
=cut
sub get_from_data {
my ($self, $source, $target, %args) = @_;
$source .= check_var('IS_WICKED_REF', '1') ? 'ref' : 'sut' if $args{add_suffix};
# we know we fail on other directories than data/wicked
assert_script_run("cp -r '" . WICKED_DATA_DIR . '/' . $source . "' '$target'");
assert_script_run("chmod +x '$target'") if $args{executable};
}
=head2 ping_with_timeout
ping_with_timeout(type => $type[, ip => $ip, timeout => 60, ip_version => 'v4', proceed_on_failure => 0])
Pings a given IP with a given C<timeout>.
C<ip_version> defines the ping command to be used, 'ping' by default and 'ping6' for 'v6'.
IP could be specified directly via C<ip> or using C<type> variable. In case of C<type> variable
it will be bypassed to C<get_remote_ip> function to get IP by label.
If ping fails, command die unless you specify C<proceed_on_failure>.
=cut
sub ping_with_timeout {
my ($self, %args) = @_;
$args{ip_version} //= 'v4';
$args{timeout} //= '60';
$args{proceed_on_failure} //= 0;
$args{count_success} //= 1;
$args{ip} = $self->get_remote_ip(%args) if $args{type};
$args{threshold} //= 50;
my $timeout = $args{timeout};
my $ping_command = ($args{ip_version} eq "v6") ? "ping6" : "ping";
$ping_command .= " -c 1 $args{ip}";
$ping_command .= " -I $args{interface}" if $args{interface};
while ($timeout > 0) {
if (script_run($ping_command) == 0) {
if ($args{count_success} > 1) {
my $cnt = $args{count_success} - 1;
$ping_command =~ s/\s-c\s1\s+/ -c $cnt /;
my $ping_out = script_output($ping_command, proceed_on_failure => 1);
$ping_out =~ /, (\d{1,3})% packet loss/;
#we treat interface in workable state if it manage to echo more than half packets
if ($1 > $args{threshold}) {
die('PING EXCEED THRESHOLD ' . $args{threshold} . '%\n' . $ping_out);
}
elsif ($1) {
record_info('WARNING', sprintf('PING with %d%% packet loss. Threshold is %d%% \n %s', $1, $args{threshold}, $ping_out));
}
}
return 1;
}
$timeout -= 1;
sleep 5;
}
if (!$args{proceed_on_failure}) {
die('PING failed: ' . $ping_command);
}
return 0;
}
=head2 setup_tuntap
setup_tuntap($config, $type => [tun1|tap1])
Setups a TUN or TAP interface from a C<config> file with the keywords 'local_ip' and 'remote_ip' which
will be replaced with the corresponding IPs.
The mandatory parameter C<type> determines if it will configure a TUN device or a TAP device.
The interface will be brought up using a wicked command.
=cut
sub setup_tuntap {
my ($self, $config, $type, $iface) = @_;
my $local_ip = $self->get_ip(type => $type);
my $remote_ip = $self->get_remote_ip(type => $type);
my $host_ip = $self->get_ip(type => 'host');
file_content_replace($config, local_ip => $local_ip, remote_ip => $remote_ip, host_ip => $host_ip, iface => $iface);
$self->wicked_command('ifup', 'all');
}
=head2 setup_tunnel
setup_tunnel($config, $type => [gre1|sit1|tunl1|tun1])
Setups a tunnel interface from a C<config> file with the keywords 'local_ip', 'remote_ip' and 'tunnel_ip' which
will be replaced with the corresponding IPs. The mandatory parameter C<type> should determine the interface to be configured.
The interface will be brought up using a wicked command.
=cut
sub setup_tunnel {
my ($self, $config, $type, $iface) = @_;
my $local_ip = $self->get_ip(type => 'host');
my $remote_ip = $self->get_remote_ip(type => 'host');
my $tunnel_ip = $self->get_ip(type => $type);
file_content_replace($config, local_ip => $local_ip, remote_ip => $remote_ip, tunnel_ip => $tunnel_ip, iface => $iface);
$self->wicked_command('ifup', 'all');
}
=head2 create_tunnel_with_commands
create_tunnel_with_commands($type => [gre1|sit1|tunl1|tun1], $mode => [gre|sit|ipip|tun], $sub_mask)
Setups a TUNL interface with IP commands (no wicked commands!).
The parameter C<type> determines the interface to be configured and C<mode> the type of tunnel.
Supported tunnels in this function are GRE, SIT, IPIP, TUN.
=cut
sub create_tunnel_with_commands {
my ($self, $type, $mode, $sub_mask) = @_;
my $local_ip = $self->get_ip(type => 'host');
my $remote_ip = $self->get_remote_ip(type => 'host');
my $tunnel_ip = $self->get_ip(type => $type);
assert_script_run("ip tunnel add $type mode $mode remote $remote_ip local $local_ip");
assert_script_run("ip link set $type up");
assert_script_run("ip addr add $tunnel_ip/$sub_mask dev $type");
assert_script_run("ip addr");
}
sub unique_macaddr {
my ($self, %args) = @_;
my $prefix = $args{prefix} // 'BA:00';
$prefix =~ s/:/_/;
$prefix = hex($prefix);
$self->{unique_macaddr_cnt} //= 0;
$prefix += $self->{unique_macaddr_cnt}++;
my $w_id = get_required_var('WORKER_ID');
die("WORKER_ID too big!") if ($w_id > 0xffffffff);
die("No unique mac address left!") if ($self->{unique_macaddr_cnt} > 0xff);
return sprintf('%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx',
($prefix >> 8) | 0x02, $prefix,
$w_id >> 24, $w_id >> 16,
$w_id >> 8, $w_id);
}
=head2 setup_bridge
setup_bridge($config, $command => [ifup, ifdown, ifreload] [, $dummy])
Setups a bridge interface from a C<config> file with the keywords 'local_ip' which
will be replaced with the corresponding IP. If C<dummy> is given, it will also configure the
dummy interface using the config file given by this parameter.
C<command> determines the wicked command to bring up/down the interface
=cut
sub setup_bridge {
my ($self, $config, $dummy, $command) = @_;
my $local_ip = $self->get_ip(type => 'host');
my $iface = iface();
file_content_replace($dummy, __macaddr__ => $self->unique_macaddr()) if ($dummy ne '');
file_content_replace($config, ip_address => $local_ip, iface => $iface, __macaddr__ => $self->unique_macaddr());
$self->wicked_command($command, 'all');
if ($dummy ne '') {
assert_script_run("cat $dummy");
$self->wicked_command($command, 'dummy0');
}
}
=head2 setup_openvpn_client
setup_openvpn_client($device => [tun1, tap1])
Setups the openvpn client using the interface given by C<device>
=cut
sub setup_openvpn_client {
my ($self, $device) = @_;
my $openvpn_client = '/etc/openvpn/client.conf';
my $remote_ip = $self->get_remote_ip(type => 'host');
$self->get_from_data('wicked/openvpn/client.conf', $openvpn_client);
file_content_replace($openvpn_client, remote_ip => $remote_ip, device => $device);
}
=head2 get_test_result
get_test_result($type, $ip_version => v4)
It returns FAILED or PASSED if the ping to the remote IP of a certain interface type given by C<type> is reachable or not.
The parameter C<ip_version> chould be one of the values 'v4' or 'v6'.
=cut
sub get_test_result {
my ($self, $type, $ip_version) = @_;
my $timeout = "60";
my $ip = $self->get_remote_ip(type => $type);
my $ret = $self->ping_with_timeout(ip => "$ip", timeout => "$timeout", ip_version => $ip_version);
if (!$ret) {
record_info("PING FAILED", "Can't ping IP $ip", result => 'fail');
return "FAILED";
}
else {
return "PASSED";
}
}
=head2 upload_log_file
The wicked way of uploading a file using the serial console. This method does
not throw and error. On failing we only put a C<<record_info(result => fail)>>
$self->upload_log_file($src [, $dst]);
=cut
sub upload_log_file {
my ($self, $src, $dst) = @_;
$dst //= basename($src);
$dst = $self->{name} . '_' . $dst if (index($dst, $self->{name}) == -1);
eval {
select_console('root-virtio-terminal1') if (get_var('VIRTIO_CONSOLE_NUM', 1) > 1);
upload_file($src, $dst);
};
record_info('Failed to upload file', $@, result => 'fail') if ($@);
select_serial_terminal;
}
sub add_post_log_file {
my ($self, $filename) = @_;
$self->{post_log_files} //= [];
my $arr = $self->{post_log_files};
push(@$arr, $filename) unless grep { $_ eq $filename } @$arr;
}
=head2 upload_wicked_logs
upload_wicked_logs($prefix => [pre|post])
Gathers all the needed wicked information and compiles a compressed file that get's uploaded to the openqa instance.
This function is normally called before and after a test is executed, the parameter C<prefix> is used to to be appended
to the file name to be uploaded. Normally 'pre' or 'post', but could be any string.
=cut
sub upload_wicked_logs {
my ($self, $prefix) = @_;
my $dir_name = $self->{name} . '_' . $prefix;
my $logs_dir = "/tmp/$dir_name";
# because all later commands ignoring any error we need to prove
# that there is sense to do something at all
assert_script_run('echo "CHECK CONSOLE"', fail_message => 'Console not usable. Failed to collect logs');
record_info('Logs', "Collecting logs in $logs_dir");
script_run("mkdir -p $logs_dir/etc/sysconfig");
script_run("cp -r /etc/sysconfig/network $logs_dir/etc/sysconfig/");
script_run("cp -r /etc/wicked $logs_dir/etc/");
script_run("date +'%Y-%m-%d %T.%6N' > $logs_dir/date");
script_run('journalctl --sync');
script_run("journalctl -b -o short-precise > $logs_dir/journalctl.log");
script_run("wicked ifstatus --verbose all > $logs_dir/wicked_ifstatus.log 2>&1");
script_run("wicked show-config > $logs_dir/wicked_config.log 2>&1");
script_run("wicked show-xml > $logs_dir/wicked_xml.log 2>&1");
script_run("ip addr show > $logs_dir/ip_addr.log 2>&1");
script_run("ip route show table all > $logs_dir/ip_route.log 2>&1");
script_run("cat /etc/resolv.conf > $logs_dir/resolv.conf 2>&1");
if ($prefix eq 'post') {
for my $lfile (@{$self->{post_log_files} // []}) {
script_run("cp $lfile $logs_dir/");
}
}
script_run("tar -C /tmp/ -cvzf /tmp/$dir_name.tar.gz $dir_name");
$self->upload_log_file("/tmp/$dir_name.tar.gz");
}
=head2 do_barrier_create
do_barrier_create(<barrier_postfix> [, <test_name>] )
Create a barier which can be later used to syncronize the wicked tests for SUT and REF.
This function can be called statically. In this case the C<test_name> parameter is
mandatory.
=cut
sub do_barrier_create {
my ($self, $type, $test_name) = ref $_[0] ? @_ : (undef, @_);
$test_name //= $self ? $self->{name} : die("test_name parameter is mandatory");
my $barrier_name = 'test_' . $test_name . '_' . $type;
record_info('barrier create', $barrier_name . ' num_children: 2');
barrier_create($barrier_name, 2);
}
=head2 do_barrier
do_barrier(<barrier_postfix>)
Used to syncronize the wicked tests for SUT and REF creating the corresponding mutex locks.
=cut
sub do_barrier {
my ($self, $type) = @_;
my $barrier_name = 'test_' . $self->{name} . '_' . $type;
barrier_wait({name => $barrier_name, check_dead_job => 1});
# This is to mitigate the problem, that if a parallel job is running in the
# barrier_wait() poll loop, while this job finished. This would lead to a
# failure on the other side.
$self->{last_barrier_wait_call} = time;
}
=head2 setup_vlan
setup_vlan($ip_type)
Creating VLAN using only ip commands. Getting ip alias name for wickedbase::get_ip
function
=cut
sub setup_vlan {
my ($self, $ip_type) = @_;
my $iface = iface();
my $local_ip = $self->get_ip(type => $ip_type, netmask => 1);
assert_script_run("ip link add link $iface name $iface.42 type vlan id 42");
assert_script_run('ip link');
assert_script_run("ip -d link show $iface.42");
assert_script_run("ip addr add $local_ip dev $iface.42");
assert_script_run("ip link set dev $iface.42 up");
}
sub prepare_check_macvtap {
my ($self, $config, $iface, $ip_address, $macaddr) = @_;
$self->get_from_data('wicked/check_macvtap.c', 'check_macvtap.c', executable => 1);
assert_script_run('gcc ./check_macvtap.c -o check_macvtap');
script_run('chmod +x ./check_macvtap');
file_content_replace($config, iface => $iface, ip_address => $ip_address, __macaddr__ => $macaddr);
}
sub validate_macvtap {
my ($self) = @_;
my $macvtap_log = '/tmp/' . $self->{name} . '_check_macvtap_output.txt';
$self->add_post_log_file($macvtap_log);
my $ref_ip = $self->get_ip(type => 'host', netmask => 0, is_wicked_ref => 1);
my $ip_address = $self->get_ip(type => 'macvtap', netmask => 0);
script_run("./check_macvtap $ref_ip $ip_address > $macvtap_log 2>&1 & export CHECK_MACVTAP_PID=\$!");
sleep(30); # OVS on a worker is slow sometimes to change and we haven't found better way how to handle it
# arping not getting packet back it is expected because check_macvtap
# executable is consume it from tap device before it actually reaches arping
script_run("arping -c 1 -I macvtap1 $ref_ip");
assert_script_run('time wait ${CHECK_MACVTAP_PID}', timeout => 90);
validate_script_output("cat $macvtap_log", sub { m/Success listening to tap device/ });
}
sub setup_bond {
my ($self, $mode, $iface0, $iface1) = @_;
my $cfg_bond0 = '/etc/sysconfig/network/ifcfg-bond0';
my $cfg_ifc0 = '/etc/sysconfig/network/ifcfg-' . $iface0;
my $cfg_ifc1 = '/etc/sysconfig/network/ifcfg-' . $iface1;
$self->get_from_data('wicked/ifcfg/ifcfg-eth0-hotplug', $cfg_ifc0);
$self->get_from_data('wicked/ifcfg/ifcfg-eth0-hotplug', $cfg_ifc1);
$self->get_from_data('wicked/bonding/ifcfg-bond0-' . $mode, $cfg_bond0);
my $ipaddr4 = $self->get_ip(type => 'host', netmask => 1);
my $ipaddr6 = $self->get_ip(type => 'host6', netmask => 1);
my $ping_ip_1 = $self->get_ip(type => 'host', is_wicked_ref => 1);
my $ping_ip_2 = $self->get_ip(type => 'second_card', is_wicked_ref => 1);
file_content_replace($cfg_bond0, ipaddr4 => $ipaddr4, ipaddr6 => $ipaddr6, iface0 => $iface0, iface1 => $iface1, ping_ip_1 => $ping_ip_1, ping_ip_2 => $ping_ip_2, '--sed-modifier' => 'g');
$self->wicked_command('ifup', 'all');
}
sub setup_team {
my ($self, $mode, $iface0, $iface1) = @_;
my $cfg_team0 = '/etc/sysconfig/network/ifcfg-team0';
my $cfg_ifc0 = '/etc/sysconfig/network/ifcfg-' . $iface0;
my $cfg_ifc1 = '/etc/sysconfig/network/ifcfg-' . $iface1;
my $data_ifcfg = 'wicked/ifcfg/ifcfg-eth0-hotplug';
$data_ifcfg .= '-static' if ($mode eq 'ab-nsna_ping');
$self->get_from_data($data_ifcfg, $cfg_ifc0);
$self->get_from_data($data_ifcfg, $cfg_ifc1);
$self->get_from_data('wicked/teaming/ifcfg-team0-' . $mode, $cfg_team0);
my $ipaddr4 = $self->get_ip(type => 'host', netmask => 1);
my $ipaddr6 = $self->get_ip(type => 'host6', netmask => 1);
my $ping_ip4 = $self->get_ip(type => 'host', is_wicked_ref => 1);
my $ping_ip6 = $self->get_ip(type => 'host6', is_wicked_ref => 1);
file_content_replace($cfg_team0, ipaddr4 => $ipaddr4, ipaddr6 => $ipaddr6, iface0 => $iface0, iface1 => $iface1, ping_ip4 => $ping_ip4, ping_ip6 => $ping_ip6);
$self->wicked_command('ifup', 'all');
}
sub setup_vxlan {
my ($self, $ctx) = @_;
my $remote_ip = $self->get_remote_ip(type => 'host');
my $local_ip = $self->get_ip(type => 'host', netmask => 1);
my $tunl_ip = $self->get_ip(type => 'vxlan', netmask => 1);
$self->write_cfg('/etc/sysconfig/network/ifcfg-vxlan1', <<EOT);
STARTMODE=auto
BOOTPROTO=static
LLADDR={{unique_macaddr}}
IPADDR=$tunl_ip
VXLAN=yes
VXLAN_ID=100
VXLAN_REMOTE_IP=$remote_ip
EOT
$self->write_cfg('/etc/sysconfig/network/ifcfg-' . $ctx->iface(), <<EOT);
STARTMODE=auto
BOOTPROTO=static
IPADDR=$local_ip
EOT
$self->wicked_command('ifup', 'all');
$self->ping_with_timeout(type => 'vxlan', interface => 'vxlan1', count_success => 30, timeout => 4);
}
sub get_active_link {
my ($self, $link) = @_;
if ($link =~ /bond/) {
return $1 if (script_output("cat /proc/net/bonding/$link") =~ m/Active Slave: (\w+)/);
}
elsif ($link =~ /team/) {
return $1 if (script_output("teamdctl $link state view") =~ m/active port: (\w+)/);
}
return;
}
sub validate_interfaces {
my ($self, $interface, $iface0, $iface1, $ping) = @_;
$ping //= 1;
if (!ifc_is_up($interface)) {
record_info('ip addr', script_output('ip addr'));
die("Interface $interface does not exist or is not UP");
}
validate_script_output('ip a s dev ' . $iface0, sub { /master $interface/ }) if $iface0;
validate_script_output('ip a s dev ' . $iface1, sub { /master $interface/ }) if $iface1;
$self->ping_with_timeout(type => 'host', interface => $interface, count_success => 30, timeout => 4) if ($ping);
}
sub check_fail_over {
my ($self, $interface, $timeout) = @_;
$timeout //= get_var('FAILOVER_TIMEOUT', 60);
my $active_link = undef;
$active_link = $self->get_active_link($interface);
$self->ifbind('unbind', $active_link);
while ($timeout >= 0) {
return 1 if ($self->get_active_link($interface) ne $active_link);
$timeout -= 1;
sleep 1;
}
die('Active Link is the same after interface cut');
}
=head2 sync_start_of
sync_start_of($service, $mutex, [,$timeout])
Start C<$service> within defined $timeout ( default is 60).
After succesfully service start will create mutex with C<$mutex>
which can be used by parallel test to catch this event
=cut
sub sync_start_of {
my ($self, $service, $mutex, $tries) = @_;
$tries //= 12;
if (script_run("systemctl start $service.service") == 0) {
while ($tries > 0) {
if (script_run("systemctl is-active $service.service") == 0) {
record_info($service, $service . ' is active');
die("Create mutex failed") unless mutex_create($mutex);
return 0;
}
$tries -= 1;
sleep 5;
}
}
# if we get here means that service failed to start
script_run("journalctl -u $service -o short-precise");
# we creating this mutex anyway even so we know that we fail to start service
# to not cause dead-lock
die("Create mutex failed") unless mutex_create($mutex);
die("Failed to start $service");
}
sub ifbind {
my ($self, $action, $interface) = @_;
my $cmd = 'bash ' . WICKED_DATA_DIR . "/wicked/ifbind.sh $action $interface";
record_info('ifbind', $cmd . "\n" . script_output($cmd));
}
sub check_ipv6 {
my ($self, $ctx) = @_;
my $gateway = $self->get_ip(type => 'gateway');
my $ipv6_network_prefix = $self->get_ip(type => 'ipv6');
my $ipv6_dns = $self->get_ip(type => 'dns_advice');
$self->get_from_data('wicked/ifcfg/ipv6', '/etc/sysconfig/network/ifcfg-' . $ctx->iface());
$self->wicked_command('ifup', $ctx->iface());
assert_script_run('rdisc6 ' . $ctx->iface());
$self->wicked_command('ifdown', $ctx->iface());
$self->wicked_command('ifup', $ctx->iface());
my $errors = 0;
my $tries = 12;
my $no_ip = 1;
my $output = '';
while ($tries > 0 && $no_ip) {
$no_ip = 0;
$output = script_output('ip a s dev ' . $ctx->iface());
unless ($output =~ /inet $RE{net}{IPv4}/) {
record_info('Waiting for IPv4');
$no_ip = 1;
}
unless ($output =~ /inet6 $RE{net}{IPv6}/) {
record_info('Waiting for IPv6');
$no_ip = 1;
}
$tries -= 1;
sleep(5);
}
$errors = $no_ip;
unless ($output !~ /tentative/) {
record_info('FAIL', 'tentative word presented', result => 'fail');
$errors = 1;
}
unless ($output =~ /inet6 $ipv6_network_prefix/m) {
record_info('FAIL', 'no prefix for local network route', result => 'fail');
$errors = 1;
}
$output = script_output('ip -6 r s');
unless ($output =~ /^default/m) {
record_info('FAIL', 'no default route presented', result => 'fail');
$errors = 1;
}
$output = script_output('ip -6 -o r s');
unless ($output =~ /^default.*proto ra/m) {
record_info('FAIL', 'IPv6 default route comes not from RA', result => 'fail');
$errors = 1;
}
$tries = 12;
my $dns_failure = 1;
while ($tries > 0 && $dns_failure) {
$dns_failure = 0;
$output = script_output('cat /etc/resolv.conf');
unless ($output =~ /^nameserver $gateway/m) {
record_info('IPv4 DNS', 'IPv4 DNS is missing in resolv.conf');
$dns_failure = 1;
}
unless ($output =~ /^nameserver $ipv6_dns/m) {
record_info('IPv6 DNS', 'IPv6 DNS is missing in resolv.conf');
$dns_failure = 1;
}
$tries -= 1;
sleep(5);
}
die "There were errors during test" if $errors || $dns_failure;
}
sub lookup {
my ($self, $name, $env) = @_;
if (exists $env->{$name}) {
return $env->{$name};
} elsif (my $v = eval { return $self->$name }) {