File: munin_node_configure_hostenumeration.t

package info (click to toggle)
munin 2.0.76-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,068 kB
  • sloc: perl: 11,684; java: 1,924; sh: 1,632; makefile: 636; javascript: 365; python: 267
file content (96 lines) | stat: -rw-r--r-- 2,654 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use warnings;
use strict;

use Test::More tests => 15;

use Socket;

use_ok('Munin::Node::Configure::HostEnumeration');

# use the 192.0.2.0/24 block
my @slash_24 = map { "192.168.2.$_" } 0 .. 255;

### resolve
{
	my @tests = (
		[ 'localhost',            'Resolve IPv4 hostname' ],
		[ '127.0.0.1',            'Resolve IPv4 numeric address' ],
#		[ 'ip6-localhost',        'Resolve IPv6 hostname' ],
#		[ '::1',                  'Resolve IPv6 numeric address' ],
	);

	while (my $test = shift @tests) {
		my ($from, $msg) = @$test;

		my $ip = eval {	Munin::Node::Configure::HostEnumeration::_resolve($from) };
		ok(!$@, "$msg - no exception");
		ok($ip, "$msg - got a value");
	}
}


### hosts_in_net
{
	my $hosts_in_net = \&Munin::Node::Configure::HostEnumeration::_hosts_in_net;
	my (@ips, @expected);

	@ips = $hosts_in_net->('192.168.2.123', 24);
	is_deeply(\@ips, \@slash_24, 'Class C is expanded correctly');

	@ips = $hosts_in_net->('192.168.2.123', 32);
	@expected = ('192.168.2.123');
	is_deeply(\@ips, \@expected, 'Single IP');

	@ips = $hosts_in_net->('192.168.2.123');
	@expected = ('192.168.2.123');
	is_deeply(\@ips, \@expected, 'No netmask');
}


# emulate its behaviour for selected, known-good values
sub _resolve
{
	my $name = shift;

	my @valid = (
		[ '192.168.2.123', 'test'    ],
		[ '192.168.2.1',   'gateway' ],
	);

	foreach my $host (@valid) {
		my ($ip, $hostname, $resolved) = @$host;

		return inet_aton($ip) if $name eq $ip
		                      or $name eq $hostname
		                      or $name eq "$hostname.example.com";
	}

	die "Unable to resolve $name: invalid test value!";
}


no warnings;
*Munin::Node::Configure::HostEnumeration::_resolve = \&_resolve;
use warnings;

### expand_hosts
{
	my @tests = (
		[ [ '192.168.2.123'                  ], [ '192.168.2.123'      ], 'Single IP',                 ],
		[ [ '192.168.2.123/24'               ], [ @slash_24,           ], 'IP-based CIDR range',       ],
		[ [ 'test'                           ], [ 'test'               ], 'Single hostname',           ],
		[ [ 'test/24'                        ], [ @slash_24,           ], 'Hostname-based CIDR range', ],
		[ [ 'test.example.com'               ], [ 'test.example.com'   ], 'Single FQDN',               ],
		[ [ 'test.example.com/24'            ], [ @slash_24,           ], 'FQDN-based CIDR range',     ],
		[ [ 'test.example.com/24', 'gateway' ], [ @slash_24, 'gateway' ], 'Multiple specifications',   ],
	);

	while (my $test = shift @tests) {
		my ($hosts, $expected, $msg) = @$test;

		@$hosts = Munin::Node::Configure::HostEnumeration::expand_hosts(@$hosts);
		is_deeply($hosts, $expected, $msg);
	}
}