[pve-devel] [PATCH common 1/2] Document current handling of reading and updating resolv.conf

Emmanuel Kasper e.kasper at proxmox.com
Wed Mar 8 13:08:17 CET 2017


also add unit tests for the two subroutines
---
should the unit tests be added to make check target ?
 src/PVE/INotify.pm                     |  3 ++
 test/inotify/resolv.conf_single_domain |  5 ++++
 test/inotify/resolv.conf_single_search |  5 ++++
 test/inotify/run_inotify_tests.pl      | 50 ++++++++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+)
 create mode 100644 test/inotify/resolv.conf_single_domain
 create mode 100644 test/inotify/resolv.conf_single_search
 create mode 100755 test/inotify/run_inotify_tests.pl

diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm
index c2055b6..7d2001f 100644
--- a/src/PVE/INotify.pm
+++ b/src/PVE/INotify.pm
@@ -539,6 +539,8 @@ sub read_etc_resolv_conf {
     my $nscount = 0;
     while (my $line = <$fh>) {
 	chomp $line;
+	# resolv.conf should *either* have a search or domain, else behaviour is undefined
+	# see res_init.c in libc, havesearch is set to 0 when a domain entry is found
 	if ($line =~ m/^(search|domain)\s+(\S+)\s*/) {
 	    $res->{search} = $2;
 	} elsif ($line =~ m/^\s*nameserver\s+($PVE::Tools::IPRE)\s*/) {
@@ -557,6 +559,7 @@ sub update_etc_resolv_conf {
 
     my $data = "";
 
+    # when writing search domains always write to search, as this allows multiple entries
     $data = "search $resolv->{search}\n"
 	if $resolv->{search};
 
diff --git a/test/inotify/resolv.conf_single_domain b/test/inotify/resolv.conf_single_domain
new file mode 100644
index 0000000..aaa2b64
--- /dev/null
+++ b/test/inotify/resolv.conf_single_domain
@@ -0,0 +1,5 @@
+# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
+#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
+nameserver 8.8.4.4
+nameserver 8.8.8.8
+domain proxmox.com
diff --git a/test/inotify/resolv.conf_single_search b/test/inotify/resolv.conf_single_search
new file mode 100644
index 0000000..1900906
--- /dev/null
+++ b/test/inotify/resolv.conf_single_search
@@ -0,0 +1,5 @@
+# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
+#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
+nameserver 8.8.4.4
+nameserver 8.8.8.8
+search proxmox.com
diff --git a/test/inotify/run_inotify_tests.pl b/test/inotify/run_inotify_tests.pl
new file mode 100755
index 0000000..5ecfc5e
--- /dev/null
+++ b/test/inotify/run_inotify_tests.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use FindBin '$Bin'; # $Bin is path to current script. Yes, name is strange.
+use Test::More;
+use IO::Handle;
+use Data::Dumper;
+
+use PVE::INotify;
+use PVE::Tools;
+
+# tests for PVE::INotify::read_etc_resolv_conf and PVE::INotify::update_etc_resolv_conf
+# reading a resolv.conf with a multiple domain entry
+my $single_domain = join('/', $Bin, 'resolv.conf_single_domain');
+my $fh = IO::File->new("< $single_domain");
+my $data = PVE::INotify::read_etc_resolv_conf($single_domain, $fh);
+
+is($data->{search}, 'proxmox.com', 'single domain parameter was correctly read');
+is($data->{dns1}, '8.8.4.4', 'ns1 parameter was correctly read');
+is($data->{dns2}, '8.8.8.8', 'ns2 parameter was correctly read');
+
+$fh->seek(0,0);
+my @args = ();
+
+my $data_to_write = PVE::INotify::update_etc_resolv_conf($single_domain, $fh, $data, @args);
+
+is(($data_to_write =~ m/^search proxmox.com\n/), 1, 'single entry was correctly added as search');
+is(($data_to_write =~ m/\nnameserver 8.8.8.8\n/), 1, 'ns1 parameter was correctly added');
+is(($data_to_write =~ m/\nnameserver 8.8.4.4\n/), 1, 'ns2 parameter was correctly added');
+
+$fh->close();
+
+# reading a resolv.conf with a single search entry
+# we don't retest for nameservers as the test data is the same for those
+my $single_search = join('/', $Bin, 'resolv.conf_single_search');
+$fh = IO::File->new("< $single_search");
+$data = PVE::INotify::read_etc_resolv_conf($single_search, $fh);
+
+is($data->{search}, 'proxmox.com', 'single entry search was correctly read');
+
+$fh->seek(0,0);
+$data_to_write = PVE::INotify::update_etc_resolv_conf($single_search, $fh, $data, @args);
+
+is(($data_to_write =~ m/^search proxmox.com\n/), 1, 'single entry search was correctly added');
+
+$fh->close();
+
+done_testing();
\ No newline at end of file
-- 
2.1.4





More information about the pve-devel mailing list