[pve-devel] [RFC PATCH container] /etc/hosts handling rewritten + testsuite

Wolfgang Bumiller w.bumiller at proxmox.com
Fri Feb 5 08:53:40 CET 2016


---
 src/PVE/LXC/Setup/Base.pm                | 168 ++++++++++++++++++++----------
 src/test/Makefile                        |   2 +
 src/test/etc_hosts/Makefile              |   9 ++
 src/test/etc_hosts/run_tests.pl          | 138 ++++++++++++++++++++++++
 src/test/etc_hosts/test-template         |  96 +++++++++++++++++
 src/test/etc_hosts/test.1                | 143 +++++++++++++++++++++++++
 src/test/etc_hosts/test.2                | 173 +++++++++++++++++++++++++++++++
 src/test/etc_hosts/test.3                | 167 +++++++++++++++++++++++++++++
 src/test/etc_hosts/test.4                |  28 +++++
 src/test/test-centos6-001/etc/hosts.exp  |   1 +
 src/test/test-debian-001/etc/hosts.exp   |   1 +
 src/test/test-debian-002/etc/hosts.exp   |   1 +
 src/test/test-debian-003/etc/hosts.exp   |   1 +
 src/test/test-debian-004/etc/hosts.exp   |   1 +
 src/test/test-debian-005/etc/hosts.exp   |   1 +
 src/test/test-debian-006/etc/hosts.exp   |   1 +
 src/test/test-debian-007/etc/hosts.exp   |   1 +
 src/test/test-debian-008/etc/hosts.exp   |   1 +
 src/test/test-debian-009/etc/hosts.exp   |   1 +
 src/test/test-debian-010/etc/hosts.exp   |   1 +
 src/test/test-debian-013/etc/hosts.exp   |   1 +
 src/test/test-opensuse-001/etc/hosts.exp |   1 +
 22 files changed, 885 insertions(+), 52 deletions(-)
 create mode 100644 src/test/etc_hosts/Makefile
 create mode 100755 src/test/etc_hosts/run_tests.pl
 create mode 100644 src/test/etc_hosts/test-template
 create mode 100644 src/test/etc_hosts/test.1
 create mode 100644 src/test/etc_hosts/test.2
 create mode 100644 src/test/etc_hosts/test.3
 create mode 100644 src/test/etc_hosts/test.4

diff --git a/src/PVE/LXC/Setup/Base.pm b/src/PVE/LXC/Setup/Base.pm
index 8991b29..ba9ecce 100644
--- a/src/PVE/LXC/Setup/Base.pm
+++ b/src/PVE/LXC/Setup/Base.pm
@@ -53,87 +53,151 @@ sub lookup_dns_conf {
     return ($searchdomains, $nameserver);
 }
 
+my $remove_elements = sub {
+    my ($needle_array, @haystack) = @_;
+
+    return grep {
+	my $hay = $_;
+	!grep { $hay eq $_ } @$needle_array
+    } @haystack;
+};
+
 sub update_etc_hosts {
     my ($etc_hosts_data, $hostip, $oldname, $newname, $searchdomains) = @_;
 
     my $done = 0;
 
-    my @lines;
-
     my $namepart = ($newname =~ s/\..*$//r);
 
-    my $all_names = '';
+    # First prepare all the new hostnames we want to use:
+    my @all_names = ();
     if ($newname =~ /\./) {
-	$all_names .= "$newname $namepart";
+	push @all_names, $newname, $namepart;
     } else {
 	foreach my $domain (PVE::Tools::split_list($searchdomains)) {
-	    $all_names .= ' ' if $all_names;
-	    $all_names .= "$newname.$domain";
+	    push @all_names, "$newname.$domain";
 	}
-	$all_names .= ' ' if $all_names;
-	$all_names .= $newname;
+	push @all_names, $newname;
     }
-    
-    foreach my $line (split(/\n/, $etc_hosts_data)) {
-	if ($line =~ m/^#/ || $line =~ m/^\s*$/) {
-	    push @lines, $line;
+
+    # And remember the default string (used multiple times)
+    my $entry_line;
+    if (defined($hostip)) {
+	$entry_line = "$hostip " . join(' ', @all_names);
+    } else {
+	$entry_line = "127.0.1.1 $namepart";
+    }
+
+    # Now analyze the existing data
+    my $lines = [split(/\n/, $etc_hosts_data)];
+
+    my $ip_line;
+    my $hostname_line;
+    my $both_line;
+    my ($found_lo4, $found_lo6);
+
+    my $addline = sub {
+	my ($after, $line) = @_;
+	splice @$lines, $after, 0, $line;
+	$found_lo4++ if defined($found_lo4) && $found_lo4 >= $after;
+	$found_lo6++ if defined($found_lo6) && $found_lo6 >= $after;
+    };
+
+    foreach my $i (0..(@$lines-1)) {
+	my $line = $lines->[$i];
+	next if $line =~ /^#/ || $line =~ /^\s*$/;
+
+	my ($ip, @names) = split(/\s+/, $line);
+
+	if ($ip eq '127.0.0.1') {
+	    $found_lo4 = $i;
 	    next;
 	}
 
-	my ($ip, @names) = split(/\s+/, $line);
-	if (($ip eq '127.0.0.1') || ($ip eq '::1')) {
-	    push @lines, $line;
+	if ($ip eq '::1') {
+	    $found_lo6 = $i;
 	    next;
 	}
 
-	my $found = 0;
-	foreach my $name (@names) {
-	    if ($name eq $oldname || $name eq $newname) {
-		$found = 1;
-	    } else {
-		# fixme: record extra names?
-	    }
+	if (!defined($ip_line) && defined($hostip) && $ip eq $hostip) {
+	    $ip_line = $i;
+	    # also check hostname
 	}
-	$found = 1 if defined($hostip) && ($ip eq $hostip);
-	
-	if ($found) {
-	    if (!$done) {
-		if (defined($hostip)) {
-		    push @lines, "$hostip $all_names";
-		} else {
-		    push @lines, "127.0.1.1 $namepart";
-		}
-		$done = 1;
-	    }
-	    next;
-	} else {
-	    push @lines, $line;
+
+	if (!defined($hostname_line) && grep { $_ eq $oldname ||
+	                                       $_ eq $newname ||
+	                                       $_ eq $namepart } @names) {
+	    $hostname_line = $i;
+	    $both_line = $i if defined($ip_line) && $ip_line == $i;
 	}
     }
 
-    if (!$done) {
+    # We we have a few cases to distinguish between:
+    my $add_local_hostname = 1;
+
+    if (defined($both_line)) {
+	# We have a line where both IP and hostname match, so we only replace
+	# this one.
+	$lines->[$both_line] = "$hostip " . join(' ', @all_names);
+    } elsif (defined($hostname_line) && defined($ip_line)) {
+	# We matched two lines: one by IP and one by hostname
+	# The ip line does *not* contain our hostname
+	# The host line does *not* contain our ip
+	# (otherwise we would be in the $both_line case)
+	# So we consider the ip-line "extra" names and the host-line needs
+	# to be stripped of our host names, then we add our entry.
+	my ($ip, @names) = split(/\s+/, $lines->[$hostname_line]);
+	@names = &$remove_elements([$oldname, @all_names], @names);
+	# The hostname line can be empty now:
+	if (!@names) {
+	    $lines->[$hostname_line] = $entry_line;
+	} else {
+	    $lines->[$hostname_line] = "$ip " . join(' ', @names);
+	    my $at = $ip_line < $hostname_line ? $ip_line : $hostname_line;
+	    &$addline($at, $entry_line);
+	}
+    } elsif (defined($hostname_line)) {
+	# There's a line containing our hostname but not our ip address, and no
+	# other line contains our ip address either, so clear out the name to
+	# replace it, but only if we actually have a configured IP address,
+	# otherwise it's up to the user to deal with this:
 	if (defined($hostip)) {
-	    push @lines, "$hostip $all_names";
+	    my ($ip, @names) = split(/\s+/, $lines->[$hostname_line]);
+	    @names = &$remove_elements([$oldname, @all_names], @names);
+	    if (@names) {
+		$lines->[$hostname_line] = "$ip " . join(' ', @names);
+		&$addline($hostname_line, $entry_line);
+	    } else {
+		splice @$lines, $hostname_line, 1, $entry_line;
+	    }
 	} else {
-	    push @lines, "127.0.1.1 $namepart";
-	}	
+	    # If we don't know our IP leave the entry as it ias and don't add
+	    # an 127.0.1.1 either.
+	    $add_local_hostname = 0;
+	}
+    } elsif (defined($ip_line)) {
+	# We found a line containing our IP address, we simply replace the first
+	# such occurrance with our data:
+	$lines->[$ip_line] = $entry_line;
+	$add_local_hostname = 0;
+    } else {
+	# Neither hostname nor IP existed before, append:
+	push @$lines, $entry_line;
+	$add_local_hostname = 0;
     }
 
-    my $found_localhost = 0;
-    foreach my $line (@lines) {
-	if ($line =~ m/^127.0.0.1\s/) {
-	    $found_localhost = 1;
-	    last;
-	}
+    if ($add_local_hostname && !defined($hostip)) {
+	push @$lines, "127.0.1.1 $namepart";
     }
 
-    if (!$found_localhost) {
-	unshift @lines, "127.0.0.1 localhost.localnet localhost";
+    if (!defined($found_lo6)) {
+	&$addline(($found_lo4//-1) + 1, "::1 localhost.localnet localhost");
     }
-    
-    $etc_hosts_data = join("\n", @lines) . "\n";
-    
-    return $etc_hosts_data;
+    if (!defined($found_lo4)) {
+	unshift @$lines, "127.0.0.1 localhost.localnet localhost";
+    }
+
+    return join("\n", @$lines) . "\n";
 }
 
 sub template_fixup {
diff --git a/src/test/Makefile b/src/test/Makefile
index 8105b7d..1530e3f 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -5,7 +5,9 @@ all:
 
 test:
 	./run_tests.pl
+	$(MAKE) -C etc_hosts test
 
 
 clean:
 	rm -rf tmprootfs
+	$(MAKE) -C etc_hosts clean
diff --git a/src/test/etc_hosts/Makefile b/src/test/etc_hosts/Makefile
new file mode 100644
index 0000000..b075d33
--- /dev/null
+++ b/src/test/etc_hosts/Makefile
@@ -0,0 +1,9 @@
+all:
+
+
+test:
+	./run_tests.pl
+
+
+clean:
+	rm -f tmphosts*
diff --git a/src/test/etc_hosts/run_tests.pl b/src/test/etc_hosts/run_tests.pl
new file mode 100755
index 0000000..9274194
--- /dev/null
+++ b/src/test/etc_hosts/run_tests.pl
@@ -0,0 +1,138 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use lib qw(../..);
+
+use PVE::LXC::Setup::Base;
+
+# these are in order of PVE::LXC::Setup::Base::update_etc_hosts() parameters
+my @CONF_PARAMS = qw(hostip oldname newname searchdomains);
+
+sub test_file {
+    my ($exp_fn, $real_fn, $msg, $cfg) = @_;
+
+    return if system("diff -u '$exp_fn' '$real_fn'") == 0;
+
+    die "files do not match: $msg\n" .
+	join("\n", map { "   $_: " . ($cfg->{$_}//'') } @CONF_PARAMS) . "\n";
+}
+
+sub parse_test($) {
+    my ($file) = @_;
+
+    my $tests = [];
+    my $testdesc = {};
+
+    open my $fh, '<', $file or die "failed to open $file: $!\n";
+
+    my $line;
+    my $data = '';
+    while ($line = <$fh>) {
+	last if $line =~ /^test:/;
+	$data .= $line;
+    }
+    $testdesc->{input} = $data;
+
+    die "missing tests\n" if !defined($line);
+    while (defined($line) && $line =~ /^test:/) {
+	chomp $line;
+	$line =~ s/^test:\s*//;
+	my $test = { id => $line };
+
+	# Read test configuration
+	while ($line = <$fh>) {
+	    chomp $line;
+	    last if $line =~ /^expect:/;
+	    next if $line =~ /^\s*$/ || $line =~ /^\s*#/;
+	    if ($line =~ /^\s*(\w+)\s*=\s*(.*)$/) {
+		die "duplicate entry: $1\n" if defined($test->{$1});
+		$test->{$1} = $2;
+		die "unknown config key: $1\n" if !grep { $_ eq $1 } @CONF_PARAMS;
+	    } else {
+		die "bad test input: $line";
+	    }
+	}
+	die "missing expected output\n" if !defined($line);
+
+	$test->{newname} //= 'localhost';
+	$test->{oldname} //= 'localhost';
+
+	# Read expected output
+	$data = '';
+	while ($line = <$fh>) {
+	    last if $line =~ /^test:/ || $line =~ /^end\s*$/;
+	    $data .= $line;
+	}
+	$test->{expected} = $data;
+	push @$tests, $test;
+
+	if (defined($line) && $line =~ /^end\s*$/) {
+	    # skip to next 'test:'
+	    while ($line = <$fh>) {
+		last if $line =~ /^test:/;
+	    }
+	}
+    }
+    die "garbage after test description: $line\n" if defined($line);
+
+    close $fh;
+
+    $testdesc->{tests} = $tests;
+    return $testdesc;
+}
+
+sub run_test {
+    my ($testfile) = @_;
+
+    my $testdesc = parse_test($testfile);
+
+    my $input = $testdesc->{input};
+
+    my $id = 0;
+    foreach my $test (@{$testdesc->{tests}}) {
+	my @cfgargs = map { $test->{$_} } @CONF_PARAMS;
+
+	my $got = PVE::LXC::Setup::Base::update_etc_hosts($input, @cfgargs);
+
+	open my $expfd, '>', 'tmphosts.exp'
+	    or die "failed to open tmphosts.exp: $!\n";
+	print {$expfd} $test->{expected};
+	close $expfd;
+
+	open my $gotfd, '>', 'tmphosts.got'
+	    or die "failed to open tmphosts.got: $!\n";
+	print {$gotfd} $got;
+	close $gotfd;
+
+	my $subtestname = $test->{id} || $id;
+	test_file('tmphosts.exp', 'tmphosts.got', "first pass, subtest $subtestname", $test);
+
+	# second run:
+	$got = PVE::LXC::Setup::Base::update_etc_hosts($got, @cfgargs);
+	open $gotfd, '>', 'tmphosts.got'
+	    or die "failed to open tmphosts.got: $!\n";
+	print {$gotfd} $got;
+	close $gotfd;
+
+	test_file('tmphosts.exp', 'tmphosts.got', "second pass, subtest $subtestname", $test);
+
+	print "TEST $testfile [$subtestname] => OK\n";
+	++$id;
+    }
+}
+
+if (scalar(@ARGV)) {
+
+    foreach my $testdir (@ARGV) {
+	run_test($testdir);  
+    }
+
+} else {
+
+    foreach my $testdir (<test.*>) {#
+	next if ! -f $testdir; 
+	run_test($testdir);
+    }
+}
diff --git a/src/test/etc_hosts/test-template b/src/test/etc_hosts/test-template
new file mode 100644
index 0000000..ab51769
--- /dev/null
+++ b/src/test/etc_hosts/test-template
@@ -0,0 +1,96 @@
+WRITE INPUT HERE
+
+test: 0
+# Every test file should at least have all of these cases:
+#  0) Empty config
+#  1) hostip
+#  2)          hostname
+#  3) hostip + hostname
+#  4)                     searchdomain
+#  5) hostip       +      searchdomain
+#  6)          hostname + searchdomain
+#  7) hostip + hostname + searchdomain
+#  8)          fqdn
+#  9) hostip + fqdn
+# 10)          fqdn + searchdomain
+# 11) hostip + fqdn + searchdomain
+
+expect:
+COPY INPUT HERE
+127.0.1.1 localhost
+end
+
+test: 1
+  hostip = 192.168.0.1
+expect:
+TODO
+end
+
+test: 2
+  newname = myhost
+expect:
+TODO
+end
+
+test: 3
+  hostip = 192.168.0.1
+  newname = myhost
+expect:
+TODO
+end
+
+test: 4
+  searchdomains = proxmox.com
+expect:
+TODO
+end
+
+test: 5
+  hostip = 192.168.0.1
+  searchdomains = proxmox.com
+expect:
+TODO
+end
+
+test: 6
+  newname = myhost
+  searchdomains = proxmox.com
+expect:
+TODO
+end
+
+test: 7
+  hostip = 192.168.0.1
+  newname = myhost
+  searchdomains = proxmox.com
+expect:
+TODO
+end
+
+test: 8
+  newname = myhost.fqdn.com
+expect:
+TODO
+end
+
+test: 9
+  hostip = 192.168.0.1
+  newname = myhost.fqdn.com
+expect:
+TODO
+end
+
+test: 10
+  newname = myhost.fqdn.com
+  searchdomains = proxmox.com
+expect:
+TODO
+end
+
+test: 11
+  hostip = 2001::1
+  newname = myhost
+  searchdomains = proxmox.com
+expect:
+TODO
+end
diff --git a/src/test/etc_hosts/test.1 b/src/test/etc_hosts/test.1
new file mode 100644
index 0000000..1778566
--- /dev/null
+++ b/src/test/etc_hosts/test.1
@@ -0,0 +1,143 @@
+# empty input file
+
+test: 0
+# Every test file should at least have all of these cases:
+#  0) Empty config
+#  1) hostip
+#  2)          hostname
+#  3) hostip + hostname
+#  4)                     searchdomain
+#  5) hostip       +      searchdomain
+#  6)          hostname + searchdomain
+#  7) hostip + hostname + searchdomain
+#  8)          fqdn
+#  9) hostip + fqdn
+# 10)          fqdn + searchdomain
+# 11) hostip + fqdn + searchdomain
+
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+127.0.1.1 localhost
+end
+
+test: 1
+  hostip = 192.168.0.1
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+192.168.0.1 localhost
+end
+
+test: 2
+  newname = myhost
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+127.0.1.1 myhost
+end
+
+test: 3
+  hostip = 192.168.0.1
+  newname = myhost
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+192.168.0.1 myhost
+end
+
+test: 4
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+127.0.1.1 localhost
+end
+
+test: 5
+  hostip = 192.168.0.1
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+192.168.0.1 localhost.proxmox.com localhost
+end
+
+test: 6
+  newname = myhost
+  searchdomains = proxmox.com
+  # here we expect no fqdn for the 127.0.1.1 entry
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+127.0.1.1 myhost
+end
+
+test: 7
+  hostip = 192.168.0.1
+  newname = myhost
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+192.168.0.1 myhost.proxmox.com myhost
+end
+
+test: 8
+  newname = myhost.fqdn.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+127.0.1.1 myhost
+end
+
+test: 9
+  hostip = 192.168.0.1
+  newname = myhost.fqdn.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+192.168.0.1 myhost.fqdn.com myhost
+end
+
+test: 10
+  newname = myhost.fqdn.com
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+127.0.1.1 myhost
+end
+
+test: 11
+  hostip = 192.168.0.1
+  newname = myhost.fqdn.com
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+192.168.0.1 myhost.fqdn.com myhost
+end
+
+test: ipv6
+  hostip = 2001::1
+  newname = myhost
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+# empty input file
+2001::1 myhost.proxmox.com myhost
+end
diff --git a/src/test/etc_hosts/test.2 b/src/test/etc_hosts/test.2
new file mode 100644
index 0000000..5222b78
--- /dev/null
+++ b/src/test/etc_hosts/test.2
@@ -0,0 +1,173 @@
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+
+test: 0
+# This test seris should not touch any of the predefined host lines
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+127.0.1.1 localhost
+end
+
+test: 1
+  hostip = 192.168.0.1
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+192.168.0.1 localhost
+end
+
+test: 2
+  newname = myhost
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+127.0.1.1 myhost
+end
+
+test: 3
+  hostip = 192.168.0.1
+  newname = myhost
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+192.168.0.1 myhost
+end
+
+test: 4
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+127.0.1.1 localhost
+end
+
+test: 5
+  hostip = 192.168.0.1
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+192.168.0.1 localhost.proxmox.com localhost
+end
+
+test: 6
+  newname = myhost
+  searchdomains = proxmox.com
+  # here we expect no fqdn for the 127.0.1.1 entry
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+127.0.1.1 myhost
+end
+
+test: 7
+  hostip = 192.168.0.1
+  newname = myhost
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+192.168.0.1 myhost.proxmox.com myhost
+end
+
+test: 8
+  newname = myhost.fqdn.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+127.0.1.1 myhost
+end
+
+test: 9
+  hostip = 192.168.0.1
+  newname = myhost.fqdn.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+192.168.0.1 myhost.fqdn.com myhost
+end
+
+test: 10
+  newname = myhost.fqdn.com
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+127.0.1.1 myhost
+end
+
+test: 11
+  hostip = 192.168.0.1
+  newname = myhost.fqdn.com
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+192.168.0.1 myhost.fqdn.com myhost
+end
diff --git a/src/test/etc_hosts/test.3 b/src/test/etc_hosts/test.3
new file mode 100644
index 0000000..b00b159
--- /dev/null
+++ b/src/test/etc_hosts/test.3
@@ -0,0 +1,167 @@
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+
+test: 1
+# This file tests against the existing hostname entries
+  hostip = 10.0.0.1
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 localhost
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 2
+  newname = atestname
+# A hostname was configured and found in /etc/hosts, no IP was configured
+# so we expect the user to manage /etc/hosts for their custom settings.
+# We'd only add the hostname if it didn't exist before (test.2.2).
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 3
+  hostip = 10.0.0.1
+  newname = atestname
+# In this case we have a line matching both hostname and IP address, so we
+# assume that it's the one line we're managing and replace it completely,
+# but we must not touch the second such line
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 atestname
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 5
+  hostip = 10.0.0.1
+  searchdomains = proxmox.com
+# Like in test 3 we assume the first line matching our IP is "our" line.
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 localhost.proxmox.com localhost
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 6
+  newname = atestname
+  searchdomains = proxmox.com
+# Same as test 2 above
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 7
+  hostip = 10.0.0.1
+  newname = atestname
+  searchdomains = proxmox.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 atestname.proxmox.com atestname
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 8
+  newname = atest.fqdn
+# Same as test 2 above
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 9
+# We already had a test matching both in one line
+# so now we match on different lines
+  hostip = 10.0.0.1
+  newname = more.com
+# Note that we only remove more.com from 11.0.0.1, which is fine because our
+# new managed line is now on top and will be matching both IP+hostname, so now
+# if the user adds it back in it'll *stay* there for good. (test.4.1)
+# We simply assumed here that the 11.0.0.1 line was our previous IP and our
+# previously "managed" line.
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 more.com more
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 10
+  newname = atest.fqdn
+  searchdomains = proxmox.com
+# Same as test 2 above
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
+
+test: 11
+  hostip = 10.0.0.1
+  newname = atest.fqdn
+  searchdomains = proxmox.com
+# Same as test 7 above
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 atest.fqdn atest
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+end
diff --git a/src/test/etc_hosts/test.4 b/src/test/etc_hosts/test.4
new file mode 100644
index 0000000..7d2f7a6
--- /dev/null
+++ b/src/test/etc_hosts/test.4
@@ -0,0 +1,28 @@
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 more.com more
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
+
+test: 1
+# See test.3.9, this file's input is its output plus 'more.com' added back
+# to the 11.0.0.1 address with the container's hostname being 'more.com'
+# In this case we should not remove more.com from 11.0.0.1 unlike in test 3.9
+# where it was considered our main entry to meddle with beause the 10.0.0.1
+# entry did not exist initially.
+  hostip = 10.0.0.1
+  newname = more.com
+expect:
+127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
+
+10.0.0.1 more.com more
+10.0.0.1 keep.this.here and.this.too atestname atest.fqdn
+10.0.0.1 second-occurrance.must.stay atestname
+11.0.0.1 atestname more.com
+11.0.0.2 atest.fqdn more.com
+11.0.0.3 atestname atest.fqdn more.com
diff --git a/src/test/test-centos6-001/etc/hosts.exp b/src/test/test-centos6-001/etc/hosts.exp
index 0791e77..e4b15b9 100644
--- a/src/test/test-centos6-001/etc/hosts.exp
+++ b/src/test/test-centos6-001/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 test1.proxmox.com test1
diff --git a/src/test/test-debian-001/etc/hosts.exp b/src/test/test-debian-001/etc/hosts.exp
index 7f3910c..28a76ca 100644
--- a/src/test/test-debian-001/etc/hosts.exp
+++ b/src/test/test-debian-001/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
diff --git a/src/test/test-debian-002/etc/hosts.exp b/src/test/test-debian-002/etc/hosts.exp
index 0791e77..e4b15b9 100644
--- a/src/test/test-debian-002/etc/hosts.exp
+++ b/src/test/test-debian-002/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 test1.proxmox.com test1
diff --git a/src/test/test-debian-003/etc/hosts.exp b/src/test/test-debian-003/etc/hosts.exp
index 9e3fb59..6eea856 100644
--- a/src/test/test-debian-003/etc/hosts.exp
+++ b/src/test/test-debian-003/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test3
diff --git a/src/test/test-debian-004/etc/hosts.exp b/src/test/test-debian-004/etc/hosts.exp
index cd65bdf..0415bcd 100644
--- a/src/test/test-debian-004/etc/hosts.exp
+++ b/src/test/test-debian-004/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.2 test3.use-fqdn.com test3
diff --git a/src/test/test-debian-005/etc/hosts.exp b/src/test/test-debian-005/etc/hosts.exp
index 7f3910c..28a76ca 100644
--- a/src/test/test-debian-005/etc/hosts.exp
+++ b/src/test/test-debian-005/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
diff --git a/src/test/test-debian-006/etc/hosts.exp b/src/test/test-debian-006/etc/hosts.exp
index 7f3910c..28a76ca 100644
--- a/src/test/test-debian-006/etc/hosts.exp
+++ b/src/test/test-debian-006/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
diff --git a/src/test/test-debian-007/etc/hosts.exp b/src/test/test-debian-007/etc/hosts.exp
index 7f3910c..28a76ca 100644
--- a/src/test/test-debian-007/etc/hosts.exp
+++ b/src/test/test-debian-007/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test1
diff --git a/src/test/test-debian-008/etc/hosts.exp b/src/test/test-debian-008/etc/hosts.exp
index 58c7bba..706f199 100644
--- a/src/test/test-debian-008/etc/hosts.exp
+++ b/src/test/test-debian-008/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localdomain localhost
+::1 localhost.localnet localhost
 192.168.3.102 CT102.proxmox.com CT102
diff --git a/src/test/test-debian-009/etc/hosts.exp b/src/test/test-debian-009/etc/hosts.exp
index 41eed1f..b7bb266 100644
--- a/src/test/test-debian-009/etc/hosts.exp
+++ b/src/test/test-debian-009/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test9
diff --git a/src/test/test-debian-010/etc/hosts.exp b/src/test/test-debian-010/etc/hosts.exp
index 00a9546..6d0b824 100644
--- a/src/test/test-debian-010/etc/hosts.exp
+++ b/src/test/test-debian-010/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 test10
diff --git a/src/test/test-debian-013/etc/hosts.exp b/src/test/test-debian-013/etc/hosts.exp
index bd9bcd7..9e54fb6 100644
--- a/src/test/test-debian-013/etc/hosts.exp
+++ b/src/test/test-debian-013/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 127.0.1.1 localhost
diff --git a/src/test/test-opensuse-001/etc/hosts.exp b/src/test/test-opensuse-001/etc/hosts.exp
index c0113b8..aad9166 100644
--- a/src/test/test-opensuse-001/etc/hosts.exp
+++ b/src/test/test-opensuse-001/etc/hosts.exp
@@ -1,2 +1,3 @@
 127.0.0.1 localhost.localnet localhost
+::1 localhost.localnet localhost
 1.2.3.4 pvesuse1.proxmox.com pvesuse1
-- 
2.1.4





More information about the pve-devel mailing list