13 # Current config, r() parses a network interface string into this variable
19 # perl conveniently lets you open a string as filehandle so we allow tests
20 # to temporarily save interface files to virtual files:
23 # Load a temp-file and return it as a string, if it didn't exist, try loading
28 if (my $local = $saved_files{$from}) {
32 open my $fh, '<', $from or die "failed to open $from: $!";
39 # Save a temporary file.
41 my ($file, $data) = @_;
42 $saved_files{$file} = $data;
45 # Delete a temporary file
48 die "no such file: $file" if !delete $saved_files{$file};
51 # Delete all temporary files.
53 foreach (keys %saved_files) {
54 delete $saved_files{$_} if $_ !~ m
,^shared
/,;
62 # Read an interfaces file with optional /proc/net/dev file content string and
63 # the list of active interfaces, which otherwise default
65 my ($ifaces, $proc_net_dev, $active) = @_;
66 $proc_net_dev //= load
('proc_net_dev');
67 $active //= [split(/\s+/, load
('active_interfaces'))];
68 open my $fh1, '<', \
$ifaces;
69 open my $fh2, '<', \
$proc_net_dev;
70 $config = PVE
::INotify
::__read_etc_network_interfaces
($fh1, $fh2, $active);
74 # Turn the current network config into a string.
76 return PVE
::INotify
::__write_etc_network_interfaces
($config);
80 ## Interface modification helpers
84 sub update_iface
($$%) {
85 my ($name, $families, %extra) = @_;
87 my $ifaces = $config->{ifaces
};
88 my $if = $ifaces->{$name};
90 die "no such interface: $name\n" if !$if;
94 # merge extra flags (like bridge_ports, ovs_*) directly
95 $if->{$_} = $extra{$_} foreach keys %extra;
99 my $if_families = $if->{families
} ||= [];
100 foreach my $family (@$families) {
101 my $type = delete $family->{family
};
102 @$if_families = ((grep { $_ ne $type } @$if_families), $type);
104 (my $suffix = $type) =~ s/^inet//;
105 $if->{"method$suffix"} = $family->{address
} ?
'static' : 'manual';
106 foreach(qw(address netmask gateway options)) {
107 if (my $value = delete $family->{$_}) {
108 $if->{"$_${suffix}"} = $value;
114 # Create an interface and error if it already exists.
115 sub new_iface
($$$%) {
116 my ($name, $type, $families, %extra) = @_;
117 my $ifaces = $config->{ifaces
};
118 croak
"interface already exists: $name" if $ifaces->{$name};
119 $ifaces->{$name} = { type
=> $type };
120 update_iface
($name, $families, %extra);
123 # Delete an interface and error if it did not exist.
124 sub delete_iface
($;$) {
125 my ($name, $family) = @_;
126 my $ifaces = $config->{ifaces
};
127 my $if = $ifaces->{$name} ||= {};
128 croak
"interface doesn't exist: $name" if !$if;
131 delete $ifaces->{$name};
135 my $families = $if->{families
};
136 @$families = grep {$_ ne $family} @$families;
137 (my $suffix = $family) =~ s/^inet//;
138 delete $if->{"$_$suffix"} foreach qw(address netmask gateway options);
145 # Compare two strings line by line and show a diff/error if they differ.
150 my ($ra, $wa) = POSIX
::pipe();
151 my ($rb, $wb) = POSIX
::pipe();
152 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
153 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
155 open my $diffproc, '-|', 'diff', '-up', "/dev/fd/$ra", "/dev/fd/$rb"
156 or die "failed to run program 'diff': $!";
160 open my $f1, '<', \
$a;
161 open my $f2, '<', \
$b;
164 $ha->print($line1) if defined($line1 = <$f1>);
165 $hb->print($line2) if defined($line2 = <$f2>);
166 } while (defined($line1 // $line2));
173 my $diff = <$diffproc>;
175 die "files differ:\n$diff";
178 # Write the current interface config and compare the result to a string.
182 diff
($expected, $got);
186 ## Main test execution:
188 # (sorted, it's not used right now but tests could pass on temporary files by
189 # prefixing the name with shared/ and thus you might want to split a larger
190 # test into t.01.first-part.pl, t.02.second-part.pl, etc.
193 for our $Test (sort <t
.*.pl
>) {
200 print "FAIL: $Test\n$@\n\n";
203 print "PASS: $Test\n";
207 die "$failed out of $total tests failed\n" if $failed;