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 and /proc/net/if_inet6
63 # file content strings, which default to the provided ones.
65 my ($ifaces, $proc_net_dev, $proc_net_if_inet6) = @_;
66 $proc_net_dev //= load
('proc_net_dev');
67 $proc_net_if_inet6 //= load
('proc_net_if_inet6');
68 open my $fh1, '<', \
$ifaces;
69 open my $fh2, '<', \
$proc_net_dev;
70 open my $fh3, '<', \
$proc_net_if_inet6;
71 $config = PVE
::INotify
::__read_etc_network_interfaces
($fh1, $fh2, $fh3);
75 # Turn the current network config into a string.
77 return PVE
::INotify
::__write_etc_network_interfaces
($config);
81 ## Interface modification helpers
85 sub update_iface
($$%) {
86 my ($name, $families, %extra) = @_;
88 my $ifaces = $config->{ifaces
};
89 my $if = $ifaces->{$name};
91 die "no such interface: $name\n" if !$if;
95 # merge extra flags (like bridge_ports, ovs_*) directly
96 $if->{$_} = $extra{$_} foreach keys %extra;
100 my $if_families = $if->{families
} ||= [];
101 foreach my $family (@$families) {
102 my $type = delete $family->{family
};
103 @$if_families = ((grep { $_ ne $type } @$if_families), $type);
105 (my $suffix = $type) =~ s/^inet//;
106 $if->{"method$suffix"} = $family->{address
} ?
'static' : 'manual';
107 foreach(qw(address netmask gateway options)) {
108 if (my $value = delete $family->{$_}) {
109 $if->{"$_${suffix}"} = $value;
115 # Create an interface and error if it already exists.
116 sub new_iface
($$$%) {
117 my ($name, $type, $families, %extra) = @_;
118 my $ifaces = $config->{ifaces
};
119 croak
"interface already exists: $name" if $ifaces->{$name};
120 $ifaces->{$name} = { type
=> $type };
121 update_iface
($name, $families, %extra);
124 # Delete an interface and error if it did not exist.
125 sub delete_iface
($;$) {
126 my ($name, $family) = @_;
127 my $ifaces = $config->{ifaces
};
128 my $if = $ifaces->{$name} ||= {};
129 croak
"interface doesn't exist: $name" if !$if;
132 delete $ifaces->{$name};
136 my $families = $if->{families
};
137 @$families = grep {$_ ne $family} @$families;
138 (my $suffix = $family) =~ s/^inet//;
139 delete $if->{"$_$suffix"} foreach qw(address netmask gateway options);
146 # Compare two strings line by line and show a diff/error if they differ.
151 my ($ra, $wa) = POSIX
::pipe();
152 my ($rb, $wb) = POSIX
::pipe();
153 my $ha = IO
::Handle-
>new_from_fd($wa, 'w');
154 my $hb = IO
::Handle-
>new_from_fd($wb, 'w');
156 open my $diffproc, '-|', 'diff', '-up', "/dev/fd/$ra", "/dev/fd/$rb"
157 or die "failed to run program 'diff': $!";
161 open my $f1, '<', \
$a;
162 open my $f2, '<', \
$b;
165 $ha->print($line1) if defined($line1 = <$f1>);
166 $hb->print($line2) if defined($line2 = <$f2>);
167 } while (defined($line1 // $line2));
174 my $diff = <$diffproc>;
176 die "files differ:\n$diff";
179 # Write the current interface config and compare the result to a string.
183 diff
($expected, $got);
187 ## Main test execution:
189 # (sorted, it's not used right now but tests could pass on temporary files by
190 # prefixing the name with shared/ and thus you might want to split a larger
191 # test into t.01.first-part.pl, t.02.second-part.pl, etc.
194 for our $Test (sort <t
.*.pl
>) {
201 print "FAIL: $Test\n$@\n\n";
204 print "PASS: $Test\n";
208 die "$failed out of $total tests failed\n" if $failed;