Skip to content

Commit

Permalink
ncm-hostsfile: Test IPv6 and dual stack localhosts
Browse files Browse the repository at this point in the history
In these we show how dual stack entries can set takeover and overwrite the existing localhost entry or not, specifying aliases for both stacks that allow `localhost` to resolve for both protocols while still avoiding duplicate entries.
  • Loading branch information
jrha committed Jul 12, 2023
1 parent ac8cc61 commit c2b35e7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
18 changes: 18 additions & 0 deletions ncm-hostsfile/src/main/pan/components/hostsfile/config.pan
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ prefix '/software/components/${project.artifactId}';
'version' = '${no-snapshot-version}';
'active' ?= false;
'dispatch' ?= false;

# Provide variables which can be used to provide dual stack entries for localhost.
# Specifying aliases for both stacks allows localhost to resolve for both protocols
# while still avoiding duplicate entries.

final variable HOSTSFILE_LOCALHOST4 = dict(
'localhost', dict(
'ipaddr', '127.0.0.1',
'aliases', 'localhost.localdomain localhost4 localhost4.localdomain4',
),
);

final variable HOSTSFILE_LOCALHOST6 = dict(
'localhost6', dict(
'ipaddr', '::1',
'aliases', 'localhost localhost.localdomain localhost6.localdomain6',
),
);
25 changes: 23 additions & 2 deletions ncm-hostsfile/src/test/perl/configure.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use strict;
use warnings;
use Readonly;

use Test::More tests => 5;
use Test::More tests => 9;
use Test::NoWarnings;
use Test::Quattor qw(ipv4 ms_localhost);
use Test::Quattor qw(ipv4 ipv6 dual_stack_localhost ms_localhost);
use NCM::Component::hostsfile;
use Test::MockModule;

Expand Down Expand Up @@ -33,6 +33,18 @@ Readonly my $HOSTSFILE_EXPECTED_IPV4 =>
192.168.42.3 priv_3 # NCM Private Three
';

Readonly my $HOSTSFILE_EXPECTED_IPV6 =>
'# Generated by Quattor component hostsfile 2.0.0
127.0.0.1 localhost.localdomain localhost
::1 localhost6 localhost localhost.localdomain localhost6.localdomain6 # NCM
';

Readonly my $HOSTSFILE_EXPECTED_DUALSTACK_LOCALHOST =>
'# Generated by Quattor component hostsfile 2.0.0
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 # NCM
::1 localhost6 localhost localhost.localdomain localhost6.localdomain6 # NCM
';

Readonly my $HOSTSFILE_EXPECTED_MS_LOCALHOST =>
'# Generated by Quattor component hostsfile 2.0.0
127.0.0.1 localhost # NCM
Expand Down Expand Up @@ -61,6 +73,15 @@ $file_contents = '';
is($cmp->Configure($config), 1, 'Component runs correctly with profile (ipv4)');
is($file_contents, $HOSTSFILE_EXPECTED_IPV4, 'Hostsfile updated with correct contents (ipv4)');

$config = get_config_for_profile('ipv6');
$file_contents = '';
is($cmp->Configure($config), 1, 'Component runs correctly with profile (ipv6)');
is($file_contents, $HOSTSFILE_EXPECTED_IPV6, 'Hostsfile updated with correct contents (ipv6)');

$config = get_config_for_profile('dual_stack_localhost');
$file_contents = '';
is($cmp->Configure($config), 1, 'Component runs correctly with profile (dual_stack_localhost)');
is($file_contents, $HOSTSFILE_EXPECTED_DUALSTACK_LOCALHOST, 'Hostsfile updated with correct contents (dual_stack_localhost)');

$config = get_config_for_profile('ms_localhost');
$file_contents = '';
Expand Down
15 changes: 15 additions & 0 deletions ncm-hostsfile/src/test/resources/dual_stack_localhost.pan
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
object template dual_stack_localhost;

include 'components/hostsfile/config';

prefix '/software/components/hostsfile';

'active' = true;
'file' = '/tmp/hosts.local';
'takeover' = true;

# IPv4
'entries' = merge(SELF, HOSTSFILE_LOCALHOST4);

# IPv6
'entries' = merge(SELF, HOSTSFILE_LOCALHOST6);
10 changes: 10 additions & 0 deletions ncm-hostsfile/src/test/resources/ipv6.pan
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object template ipv6;

include 'components/hostsfile/config';

prefix '/software/components/hostsfile';

'active' = true;
'file' = '/tmp/hosts.local';

'entries' = merge(SELF, HOSTSFILE_LOCALHOST6);

0 comments on commit c2b35e7

Please sign in to comment.