[PATCH] Portability: use `uname -n` instead of `hostname`

Sergey Kandaurov pluknet at nginx.com
Mon Oct 12 17:15:42 UTC 2020


> On 11 Oct 2020, at 09:04, Geert Hendrickx <geert at hendrickx.be> wrote:
> 
> # HG changeset patch
> # User Geert Hendrickx <geert at hendrickx.be>
> # Date 1602402793 -7200
> #      Sun Oct 11 09:53:13 2020 +0200
> # Node ID 53f3eae1dc9a4f901f3d4da67daa0181d13dc222
> # Parent  4e06441193417456bf24b213d15da12a90322f4c
> Portability: use `uname -n` instead of `hostname`.
> 
> As `hostname` is not defined by POSIX and not available
> by default on some modern Linux distro's (Arch, Fedora),
> making some tests fail.  Using `uname -n` instead fixes
> this in a portable way.

Whilst `uname -n` is certainly more portable than 4.2BSD'ish hostname(1),
the former will barely work on win32 which is also ought to be supported.
How about this change instead? It should work across all supported hosts.

# HG changeset patch
# User Sergey Kandaurov <pluknet at nginx.com>
# Date 1602522787 -3600
#      Mon Oct 12 18:13:07 2020 +0100
# Node ID b61e820caa837c412bb6cfeb33a5dccb05115701
# Parent  4e06441193417456bf24b213d15da12a90322f4c
Tests: using Sys::Hostname to get hostname in a portable way.

The hostname utility is not defined by POSIX and not available
by default on some modern Linux distro's (Arch, Fedora).

Reported by Geert Hendrickx.

diff --git a/mail_error_log.t b/mail_error_log.t
--- a/mail_error_log.t
+++ b/mail_error_log.t
@@ -13,6 +13,7 @@ use strict;
 use Test::More;
 
 use IO::Select;
+use Sys::Hostname;
 
 BEGIN { use FindBin; chdir($FindBin::Bin); }
 
@@ -233,8 +234,7 @@ SKIP: {
 	ok($sec < 60, "$desc valid seconds");
 
 	ok(defined($host), "$desc has host");
-	chomp(my $hostname = lc `hostname`);
-	is($host , $hostname, "$desc valid host");
+	is($host, lc(hostname()), "$desc valid host");
 
 	ok(defined($tag), "$desc has tag");
 	like($tag, qr'\w+', "$desc valid tag");
diff --git a/stream_access_log.t b/stream_access_log.t
--- a/stream_access_log.t
+++ b/stream_access_log.t
@@ -12,6 +12,8 @@ use strict;
 
 use Test::More;
 
+use Sys::Hostname;
+
 BEGIN { use FindBin; chdir($FindBin::Bin); }
 
 use lib 'lib';
@@ -158,7 +160,7 @@ is($t->read_file('filtered.log'), "127.0
 ok($t->read_file('complex.log'), 'if with complex value');
 ok($t->read_file('varlog_3.log'), 'variable in file');
 
-chomp(my $hostname = lc `hostname`);
+my $hostname = lc hostname();
 like($t->read_file('vars.log'), qr/^\d+:[\d.]+:$hostname:\d+$/, 'log vars');
 is($t->read_file('addr.log'),
 	"$escaped:$lhost:$lport:127.0.0.1:$dport:127.0.0.1:$uport\n",
diff --git a/stream_error_log.t b/stream_error_log.t
--- a/stream_error_log.t
+++ b/stream_error_log.t
@@ -13,6 +13,7 @@ use strict;
 use Test::More;
 
 use IO::Select;
+use Sys::Hostname;
 
 BEGIN { use FindBin; chdir($FindBin::Bin); }
 
@@ -241,8 +242,7 @@ SKIP: {
 	ok($sec < 60, "$desc valid seconds");
 
 	ok(defined($host), "$desc has host");
-	chomp(my $hostname = lc `hostname`);
-	is($host , $hostname, "$desc valid host");
+	is($host, lc(hostname()), "$desc valid host");
 
 	ok(defined($tag), "$desc has tag");
 	like($tag, qr'\w+', "$desc valid tag");
diff --git a/stream_variables.t b/stream_variables.t
--- a/stream_variables.t
+++ b/stream_variables.t
@@ -12,6 +12,8 @@ use strict;
 
 use Test::More;
 
+use Sys::Hostname;
+
 BEGIN { use FindBin; chdir($FindBin::Bin); }
 
 use lib 'lib';
@@ -80,7 +82,7 @@ EOF
 
 ###############################################################################
 
-chomp(my $hostname = lc `hostname`);
+my $hostname = lc hostname();
 like(stream('127.0.0.1:' . port(8080))->read(),
 	qr/^\d+:[\d.]+:$hostname:\d+:0$/, 'vars');
 
diff --git a/syslog.t b/syslog.t
--- a/syslog.t
+++ b/syslog.t
@@ -13,6 +13,7 @@ use strict;
 use Test::More;
 
 use IO::Select;
+use Sys::Hostname;
 
 BEGIN { use FindBin; chdir($FindBin::Bin); }
 
@@ -321,8 +322,7 @@ sub parse_syslog_message {
 	ok($sec < 60, "$desc valid seconds");
 
 	ok(defined($host), "$desc has host");
-	chomp(my $hostname = lc `hostname`);
-	is($host , $hostname, "$desc valid host");
+	is($host, lc(hostname()), "$desc valid host");
 
 	ok(defined($tag), "$desc has tag");
 	like($tag, qr'\w+', "$desc valid tag");


-- 
Sergey Kandaurov



More information about the nginx-devel mailing list