smart_proxy_dhcp_infoblox 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -10
- data/config/dhcp_infoblox.yml.example +17 -0
- data/lib/smart_proxy_dhcp_infoblox.rb +9 -3
- data/lib/smart_proxy_dhcp_infoblox/common_crud.rb +75 -0
- data/lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_main.rb +39 -225
- data/lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_plugin.rb +9 -17
- data/lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_version.rb +1 -1
- data/lib/smart_proxy_dhcp_infoblox/fixed_address_crud.rb +53 -0
- data/lib/smart_proxy_dhcp_infoblox/grid_restart.rb +31 -0
- data/lib/smart_proxy_dhcp_infoblox/host_ipv4_address_crud.rb +60 -0
- data/lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb +34 -0
- data/lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb +131 -0
- data/lib/smart_proxy_dhcp_infoblox/plugin_configuration.rb +37 -0
- data/lib/smart_proxy_dhcp_infoblox/record_type_validator.rb +8 -0
- data/lib/smart_proxy_dhcp_infoblox/unused_ips.rb +47 -0
- data/test/host_and_fixedaddress_crud_test.rb +169 -0
- data/test/infoblox_provider_test.rb +61 -0
- data/test/plugin_configuration_test.rb +73 -0
- data/test/record_type_validator_test.rb +20 -0
- data/test/regex_generator_test.rb +86 -0
- data/test/test_helper.rb +0 -2
- data/test/unused_ip_test.rb +48 -0
- metadata +25 -49
- data/config/dhcp_infoblox.yml +0 -14
- data/lib/smart_proxy_dhcp_infoblox/dhcp_infoblox_dependencies.rb +0 -5
- data/test/dhcp_infoblox_record_test.rb +0 -95
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'infoblox'
|
3
|
+
require 'dhcp_common/dhcp_common'
|
4
|
+
require 'dhcp_common/record/reservation'
|
5
|
+
require 'smart_proxy_dhcp_infoblox/fixed_address_crud'
|
6
|
+
require 'smart_proxy_dhcp_infoblox/host_ipv4_address_crud'
|
7
|
+
|
8
|
+
module CommoncrudTests
|
9
|
+
def test_find_record_using_ip
|
10
|
+
@entity.expects(:find).with(@connection, 'ipv4addr' => '192.168.42.1', '_max_results' => 1).returns([@host])
|
11
|
+
assert_equal @reservation, @crud.find_record_by_ip('192.168.42.0/24', '192.168.42.1')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_find_record_using_ip_returns_nil_if_record_not_found
|
15
|
+
@entity.expects(:find).with(@connection, 'ipv4addr' => '192.168.42.1', '_max_results' => 1).returns([])
|
16
|
+
assert_nil @crud.find_record_by_ip('192.168.42.0/24', '192.168.42.1')
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_find_record_using_mac
|
20
|
+
@entity.expects(:find).with(@connection, 'mac' => '00:01:02:03:05:06', '_max_results' => 1).returns([@host])
|
21
|
+
assert_equal @reservation, @crud.find_record_by_mac('192.168.42.0/24', '00:01:02:03:05:06')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_find_record_using_mac_returns_nil_if_record_not_found
|
25
|
+
@entity.expects(:find).with(@connection, 'mac' => '00:01:02:03:05:06', '_max_results' => 1).returns([])
|
26
|
+
assert_nil @crud.find_record_by_mac('192.168.42.0/24', '00:01:02:03:05:06')
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_find_record_by_ip
|
30
|
+
@crud.expects(:find_record_by_ip).with('192.168.42.0/24', '192.168.42.1')
|
31
|
+
@crud.find_record('192.168.42.0/24', '192.168.42.1')
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_find_record_by_mac
|
35
|
+
@crud.expects(:find_record_by_mac).with('192.168.42.0/24', '00:01:02:03:05:06')
|
36
|
+
@crud.find_record('192.168.42.0/24', '00:01:02:03:05:06')
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_add_record
|
40
|
+
@crud.expects(:build_host).with(:ip => @ip, :mac => @mac, :hostname => @hostname).returns(@host)
|
41
|
+
@host.expects(:post)
|
42
|
+
|
43
|
+
@crud.add_record(:ip => @ip, :mac => @mac, :hostname => @hostname)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_add_record_with_already_existing_host
|
47
|
+
@crud.expects(:build_host).with(:ip => @ip, :mac => @mac, :hostname => @hostname).returns(@host)
|
48
|
+
@host.expects(:post).raises(Infoblox::Error.new("IB.Data.Conflict"))
|
49
|
+
@crud.expects(:find_host).with('ipv4addr' => @ip).returns(@host)
|
50
|
+
|
51
|
+
assert_raises(Proxy::DHCP::AlreadyExists) { @crud.add_record(:ip => @ip, :mac => @mac, :hostname => @hostname) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_del_record
|
55
|
+
@crud.expects(:find_host).with('ipv4addr' => @ip).returns(@host)
|
56
|
+
@host.expects(:delete)
|
57
|
+
@crud.del_record('unused', @reservation)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class HostCrudTest < Test::Unit::TestCase
|
62
|
+
include CommoncrudTests
|
63
|
+
|
64
|
+
def setup
|
65
|
+
@connection = Object.new
|
66
|
+
@crud = ::Proxy::DHCP::Infoblox::HostIpv4AddressCRUD.new(@connection)
|
67
|
+
|
68
|
+
@entity = ::Infoblox::Host
|
69
|
+
|
70
|
+
@hostname = 'test.test.com'
|
71
|
+
@mac = '00:01:02:03:05:06'
|
72
|
+
@nextserver = '192.168.42.1'
|
73
|
+
@filename = '/tftpboot.img'
|
74
|
+
@ip = '192.168.42.1'
|
75
|
+
|
76
|
+
@host = ::Infoblox::Host.new(
|
77
|
+
:name => @hostname,
|
78
|
+
:ipv4addrs => [{:ipv4addr => @ip, :mac => @mac, :nextserver => @nextserver, :use_nextserver => true,
|
79
|
+
:bootfile => @filename, :use_bootfile => true, :configure_for_dhcp => true}])
|
80
|
+
|
81
|
+
@reservation = ::Proxy::DHCP::Reservation.new(
|
82
|
+
:hostname => @hostname, :mac => @mac, :ip => @ip, :nextServer => @nextserver,
|
83
|
+
:filename => @filename, :deleteable => true,
|
84
|
+
:subnet => ::Proxy::DHCP::Subnet.new('192.168.42.0', '255.255.255.0'))
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_all_hosts
|
88
|
+
::Infoblox::Host.expects(:find).with(@connection, 'ipv4addr~' => '192\.168\.42\..+', '_max_results' => 2147483646).returns([@host])
|
89
|
+
assert_equal @reservation, @crud.all_hosts('192.168.42.0/24').first
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_build_host
|
93
|
+
built = @crud.build_host(:hostname => @hostname, :mac => @mac, :ip => @ip, :nextServer => @nextserver,
|
94
|
+
:filename => @filename, :deleteable => true)
|
95
|
+
|
96
|
+
assert_equal @host.name, built.name
|
97
|
+
assert_equal @host.ipv4addrs.size, built.ipv4addrs.size
|
98
|
+
|
99
|
+
expected_addr = @host.ipv4addrs.first
|
100
|
+
actual_addr = built.ipv4addrs.first
|
101
|
+
|
102
|
+
assert_equal expected_addr.ipv4addr, actual_addr.ipv4addr
|
103
|
+
assert_equal expected_addr.mac, actual_addr.mac
|
104
|
+
assert expected_addr.nextserver, actual_addr.nextserver
|
105
|
+
assert actual_addr.use_nextserver
|
106
|
+
assert_equal expected_addr.bootfile, actual_addr.bootfile
|
107
|
+
assert actual_addr.use_bootfile
|
108
|
+
assert actual_addr.configure_for_dhcp
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_add_record_with_collision
|
112
|
+
@crud.expects(:build_host).with(:ip => @ip, :mac => @mac, :hostname => @hostname).returns(@host)
|
113
|
+
@host.expects(:post).raises(Infoblox::Error.new("IB.Data.Conflict"))
|
114
|
+
@crud.expects(:find_host).with('ipv4addr' => @ip).returns(
|
115
|
+
::Infoblox::Host.new(:name => @hostname, :ipv4addrs => [{:ipv4addr => @ip, :mac => '11:22:33:44:55:66', :configure_for_dhcp => true}]))
|
116
|
+
|
117
|
+
assert_raises(Proxy::DHCP::Collision) { @crud.add_record(:ip => @ip, :mac => @mac, :hostname => @hostname) }
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class FixedaddressCrudTest < Test::Unit::TestCase
|
122
|
+
def setup
|
123
|
+
@connection = Object.new
|
124
|
+
@crud = ::Proxy::DHCP::Infoblox::FixedAddressCRUD.new(@connection)
|
125
|
+
|
126
|
+
@entity = ::Infoblox::Fixedaddress
|
127
|
+
|
128
|
+
@hostname = 'test.test.com'
|
129
|
+
@mac = '00:01:02:03:05:06'
|
130
|
+
@nextserver = '192.168.42.1'
|
131
|
+
@filename = '/tftpboot.img'
|
132
|
+
@ip = '192.168.42.1'
|
133
|
+
|
134
|
+
@host = ::Infoblox::Fixedaddress.new(
|
135
|
+
:name => @hostname,
|
136
|
+
:ipv4addr => @ip, :mac => @mac) # :ipv4addr => @ip, :mac => @mac) #
|
137
|
+
|
138
|
+
@reservation = ::Proxy::DHCP::Reservation.new(
|
139
|
+
:hostname => @hostname, :mac => @mac, :ip => @ip, :deleteable => true,
|
140
|
+
:subnet => ::Proxy::DHCP::Subnet.new('192.168.42.0', '255.255.255.0'))
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_all_hosts
|
144
|
+
::Infoblox::Fixedaddress.expects(:find).with(@connection, 'network' => '192.168.42.0/24', '_max_results' => 2147483646).returns([@host])
|
145
|
+
assert_equal @reservation, @crud.all_hosts('192.168.42.0/24').first
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_build_host
|
149
|
+
built = @crud.build_host(:hostname => @hostname, :mac => @mac, :ip => @ip, :nextServer => @nextserver,
|
150
|
+
:filename => @filename, :deleteable => true)
|
151
|
+
|
152
|
+
assert_equal @host.name, built.name
|
153
|
+
assert_equal @host.ipv4addr, built.ipv4addr
|
154
|
+
assert_equal @host.mac, built.mac
|
155
|
+
# assert @host.nextserver, built.nextserver
|
156
|
+
# assert built.use_nextserver
|
157
|
+
# assert_equal @host.bootfile, built.bootfile
|
158
|
+
# assert built.use_bootfile
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_add_record_with_collision
|
162
|
+
@crud.expects(:build_host).with(:ip => @ip, :mac => @mac, :hostname => @hostname).returns(@host)
|
163
|
+
@host.expects(:post).raises(Infoblox::Error.new("IB.Data.Conflict"))
|
164
|
+
@crud.expects(:find_host).with('ipv4addr' => @ip).returns(
|
165
|
+
::Infoblox::Fixedaddress.new(:name => @hostname, :ipv4addr => @ip, :mac => '11:22:33:44:55:66'))
|
166
|
+
|
167
|
+
assert_raises(Proxy::DHCP::Collision) { @crud.add_record(:ip => @ip, :mac => @mac, :hostname => @hostname) }
|
168
|
+
end
|
169
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'infoblox'
|
3
|
+
require 'dhcp_common/dhcp_common'
|
4
|
+
require 'dhcp_common/subnet'
|
5
|
+
require 'smart_proxy_dhcp_infoblox/dhcp_infoblox_main'
|
6
|
+
|
7
|
+
class InfobloxProviderTest < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
@connection = Object.new
|
10
|
+
@crud = Object.new
|
11
|
+
@restart_grid = Object.new
|
12
|
+
@unused_ips = Object.new
|
13
|
+
@managed_subnets = nil
|
14
|
+
|
15
|
+
@network = Infoblox::Network.new(:network => '192.168.42.0/24')
|
16
|
+
@subnet = ::Proxy::DHCP::Subnet.new('192.168.42.0', '255.255.255.0')
|
17
|
+
|
18
|
+
|
19
|
+
@network_2 = Infoblox::Network.new(:network => '192.168.43.0/24')
|
20
|
+
@provider = Proxy::DHCP::Infoblox::Provider.new(@connection, @crud, @restart_grid, @unused_ips, @managed_subnets)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_subnets
|
24
|
+
Infoblox::Network.expects(:all).with(@connection).returns([@network])
|
25
|
+
assert_equal [@subnet], @provider.subnets
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_subnets_returns_managed_subnets_only
|
29
|
+
provider = Proxy::DHCP::Infoblox::Provider.new(@connection, @crud, @restart_grid, @unused_ips, ['192.168.42.0/255.255.255.0'])
|
30
|
+
Infoblox::Network.expects(:all).with(@connection).returns([@network, @network_2])
|
31
|
+
assert_equal [@subnet], provider.subnets
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_find_subnet
|
35
|
+
::Infoblox::Network.expects(:find).with(@connection, 'network~' => '192.168.42.0', '_max_results' => 1).returns([@network])
|
36
|
+
assert_equal @network, @provider.find_network('192.168.42.0')
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_find_subnet_raises_exception_when_network_not_found
|
40
|
+
::Infoblox::Network.expects(:find).with(@connection, 'network~' => '192.168.42.0', '_max_results' => 1).returns([])
|
41
|
+
assert_raises(RuntimeError) { @provider.find_network('192.168.42.0') }
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_add_record_restarts_grid
|
45
|
+
@crud.stubs(:add_record)
|
46
|
+
@provider.stubs(:full_network_address)
|
47
|
+
@restart_grid.expects(:try_restart)
|
48
|
+
@provider.add_record({})
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_del_record_restarts_grid
|
52
|
+
@crud.stubs(:del_record)
|
53
|
+
@provider.stubs(:full_network_address)
|
54
|
+
@restart_grid.expects(:try_restart)
|
55
|
+
@provider.del_record(@subnet,
|
56
|
+
::Proxy::DHCP::Record.new(:name => 'test',
|
57
|
+
:ip => '192.168.42.1',
|
58
|
+
:mac => '00:01:02:03:04:05',
|
59
|
+
:subnet => ::Proxy::DHCP::Subnet.new('192.168.42.0', '255.255.255.0')))
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'infoblox'
|
3
|
+
require 'dhcp_common/dhcp_common'
|
4
|
+
require 'smart_proxy_dhcp_infoblox/plugin_configuration'
|
5
|
+
require 'smart_proxy_dhcp_infoblox/record_type_validator'
|
6
|
+
require 'smart_proxy_dhcp_infoblox/dhcp_infoblox_plugin'
|
7
|
+
require 'smart_proxy_dhcp_infoblox/host_ipv4_address_crud'
|
8
|
+
require 'smart_proxy_dhcp_infoblox/fixed_address_crud'
|
9
|
+
require 'smart_proxy_dhcp_infoblox/grid_restart'
|
10
|
+
require 'smart_proxy_dhcp_infoblox/unused_ips'
|
11
|
+
require 'smart_proxy_dhcp_infoblox/dhcp_infoblox_main'
|
12
|
+
|
13
|
+
class PluginDefaultConfigurationTest < Test::Unit::TestCase
|
14
|
+
def test_default_settings
|
15
|
+
assert_equal({:record_type => 'host', :range => false}, Proxy::DHCP::Infoblox::Plugin.default_settings)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class InfobloxDhcpProductionWiringTest < Test::Unit::TestCase
|
20
|
+
def setup
|
21
|
+
@settings = {:username => 'user', :password => 'password', :server => '127.0.0.1', :record_type => 'host',
|
22
|
+
:use_ranges => true, :subnets => ['1.1.1.0/255.255.255.0']}
|
23
|
+
@container = ::Proxy::DependencyInjection::Container.new
|
24
|
+
Proxy::DHCP::Infoblox::PluginConfiguration.new.load_dependency_injection_wirings(@container, @settings)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_connection_initialization
|
28
|
+
connection = @container.get_dependency(:connection)
|
29
|
+
assert_equal 'https://127.0.0.1', connection.host
|
30
|
+
assert_equal 'user', connection.username
|
31
|
+
assert_equal 'password', connection.password
|
32
|
+
assert_equal({:verify => false}, connection.ssl_opts)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_unused_ips_configuration
|
36
|
+
free_ips = @container.get_dependency(:unused_ips)
|
37
|
+
assert_not_nil free_ips.connection
|
38
|
+
assert free_ips.use_ranges
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_host_ipv4_crud_configuration
|
42
|
+
host = @container.get_dependency(:host_ipv4_crud)
|
43
|
+
assert_not_nil host.connection
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_fixed_address_crud_configuration
|
47
|
+
fixed_address = @container.get_dependency(:fixed_address_crud)
|
48
|
+
assert_not_nil fixed_address.connection
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_grid_restart_configuration
|
52
|
+
grid_restart = @container.get_dependency(:grid_restart)
|
53
|
+
assert_not_nil grid_restart.connection
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_provider_configuration_with_host_crud
|
57
|
+
provider = @container.get_dependency(:dhcp_provider)
|
58
|
+
assert_not_nil provider.connection
|
59
|
+
assert_not_nil provider.restart_grid
|
60
|
+
assert_not_nil provider.unused_ips
|
61
|
+
assert provider.managed_subnets.include?('1.1.1.0/255.255.255.0')
|
62
|
+
assert provider.crud.instance_of?(::Proxy::DHCP::Infoblox::HostIpv4AddressCRUD)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_provider_configuration_with_fixedaddress_crud
|
66
|
+
Proxy::DHCP::Infoblox::PluginConfiguration.new.
|
67
|
+
load_dependency_injection_wirings(@container, :username => 'user', :password => 'password',
|
68
|
+
:server => '127.0.0.1', :record_type => 'fixed_address')
|
69
|
+
|
70
|
+
provider = @container.get_dependency(:dhcp_provider)
|
71
|
+
assert provider.crud.instance_of?(::Proxy::DHCP::Infoblox::FixedAddressCRUD)
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'smart_proxy_dhcp_infoblox/record_type_validator'
|
3
|
+
|
4
|
+
class RecordTypeValidatorTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@validator = ::Proxy::DHCP::Infoblox::RecordTypeValidator.new(:dhcp_infoblox, :record_type, nil, nil)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_should_pass_when_record_type_is_host
|
10
|
+
assert @validator.validate!(:record_type => 'host')
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_pass_when_record_type_is_fixedaddress
|
14
|
+
assert @validator.validate!(:record_type => 'fixedaddress')
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_raise_exception_when_record_type_is_unrecognised
|
18
|
+
assert_raises(::Proxy::Error::ConfigurationError) { @validator.validate!(:record_type => '') }
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'smart_proxy_dhcp_infoblox/network_address_range_regex_generator'
|
3
|
+
|
4
|
+
class RangeRegExGeneratorTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@generator = ::Proxy::DHCP::Infoblox::RangeRegularExpressionGenerator.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_full_range_single_digit
|
10
|
+
assert_equal "(0?0?[0123456789])", @generator.range_regex(0, 9)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_single_digits_range
|
14
|
+
assert_equal "(0?0?[34567])", @generator.range_regex(3, 7)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_neighbour_numbers_single_digits_range
|
18
|
+
assert_equal "(0?0?[12])", @generator.range_regex(1, 2)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_full_range_double_digits
|
22
|
+
assert_equal "(0?[123456789][0123456789])", @generator.range_regex(10, 99)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_range_double_digits
|
26
|
+
assert_equal "(0?3[3456789]|0?[456][0123456789]|0?7[01234567])", @generator.range_regex(33, 77)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_neighbour_numbers_double_digits_range
|
30
|
+
assert_equal "(0?5[56])", @generator.range_regex(55, 56)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_single_and_double_digit_full_range
|
34
|
+
assert_equal "(0?0?[0123456789]|0?[123456789][0123456789])", @generator.range_regex(0, 99)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_full_range_triple_digits
|
38
|
+
assert_equal "([123456789][0123456789][0123456789])", @generator.range_regex(100, 999)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_range_triple_digits
|
42
|
+
assert_equal "(12[56789]|1[3456789][0123456789]|[23456][0123456789][0123456789]|7[012][0123456789]|73[01234])", @generator.range_regex(125, 734)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_neighbour_numbers_triple_digits_range
|
46
|
+
assert_equal "(34[56])", @generator.range_regex(345, 346)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_single_double_and_triple_digit_full_range
|
50
|
+
assert_equal "(0?0?[0123456789]|0?[123456789][0123456789]|[123456789][0123456789][0123456789])", @generator.range_regex(0, 999)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class NetworkAddressesRegularExpressionGeneratorTest < Test::Unit::TestCase
|
55
|
+
def setup
|
56
|
+
@generator = ::Proxy::DHCP::Infoblox::NetworkAddressesRegularExpressionGenerator.new
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_ranges_with_cidrs_of_multiples_of_8bit
|
60
|
+
assert_equal [[1, 1], [0, 255], [0, 255], [0, 255]], @generator.network_cidr_range_octets('1.0.0.0/8')
|
61
|
+
assert_equal [[10, 10], [10, 10], [0, 255], [0, 255]], @generator.network_cidr_range_octets('10.10.0.0/16')
|
62
|
+
assert_equal [[33, 33], [34, 34], [35, 35], [0, 255]], @generator.network_cidr_range_octets('33.34.35.0/24')
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_ranges_with_cidrs_non_multiples_of_8bits
|
66
|
+
assert_equal [[240, 255], [0, 255], [0, 255], [0, 255]], @generator.network_cidr_range_octets('240.0.0.0/4')
|
67
|
+
assert_equal [[10, 10], [128, 255], [0, 255], [0, 255]], @generator.network_cidr_range_octets('10.128.0.0/9')
|
68
|
+
assert_equal [[192, 192], [168, 168], [42, 42], [64, 127]], @generator.network_cidr_range_octets('192.168.42.64/26')
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_cidr16_range_to_regex
|
72
|
+
assert_equal '10\.10\..+\..+', @generator.range_to_regex([[10, 10], [10, 10], [0, 255], [0, 255]])
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_cidr9_range_to_regex
|
76
|
+
assert_equal '10\.(129|1[3456789][0123456789]|2[01234][0123456789]|25[01234])\..+\..+', @generator.range_to_regex([[10, 10], [128, 255], [0, 255], [0, 255]])
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_cidr26_range_to_regex
|
80
|
+
assert_equal '192\.168\.42\.(0?6[56789]|0?[789][0123456789]|1[01][0123456789]|12[0123456])', @generator.range_to_regex([[192, 192], [168, 168], [42, 42], [64, 127]])
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_generate_regex
|
84
|
+
assert_equal '192\.168\.42\.(0?6[56789]|0?7[012345678])', @generator.generate_regex('192.168.42.64/28')
|
85
|
+
end
|
86
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'infoblox'
|
3
|
+
require 'smart_proxy_dhcp_infoblox/unused_ips'
|
4
|
+
|
5
|
+
class UnusedIpTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@connection = Object.new
|
8
|
+
@unused_ips = ::Proxy::DHCP::Infoblox::UnusedIps.new(@connection, false)
|
9
|
+
@network = Infoblox::Network.new(:network => '1.1.1.0/24')
|
10
|
+
@range = Infoblox::Range.new(:start_addr => '1.1.1.0', :end_addr => '1.1.1.253')
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_excluded_ips
|
14
|
+
assert_equal ['192.168.42.254', '192.168.42.255'], @unused_ips.excluded_ips('192.168.42.0/24', '192.168.42.0', '192.168.42.253')
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_excluded_ips_is_empty_when_range_start_is_nil
|
18
|
+
assert @unused_ips.excluded_ips('192.168.42.0/24', nil, '192.168.42.253').empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_excluded_ips_is_empty_when_range_end_is_nil
|
22
|
+
assert @unused_ips.excluded_ips('192.168.42.0/24', '192.168.42.250', nil).empty?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_unused_network_ip
|
26
|
+
::Infoblox::Network.expects(:find).with(@connection, 'network~' => '1.1.1.0', '_max_results' => 1).returns([@network])
|
27
|
+
@network.expects(:next_available_ip).with(1, ['1.1.1.254', '1.1.1.255']).returns(['1.1.1.1'])
|
28
|
+
assert_equal '1.1.1.1', @unused_ips.unused_network_ip('1.1.1.0', '1.1.1.0', '1.1.1.253')
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_unused_range_ip
|
32
|
+
::Infoblox::Range.expects(:find).with(@connection, 'network~' => '1.1.1.0').returns([@range])
|
33
|
+
@range.expects(:next_available_ip).with(1).returns(['1.1.1.1'])
|
34
|
+
assert_equal '1.1.1.1', @unused_ips.unused_range_ip('1.1.1.0', '1.1.1.0', '1.1.1.253')
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_unused_ip_uses_network_api_when_use_ranges_is_false
|
38
|
+
unused_ips = ::Proxy::DHCP::Infoblox::UnusedIps.new(@connection, false)
|
39
|
+
unused_ips.expects(:unused_network_ip).with('1.1.1.0', '1.1.1.0', '1.1.1.253')
|
40
|
+
unused_ips.unused_ip('1.1.1.0', '1.1.1.0', '1.1.1.253')
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_unused_ip_uses_range_api_when_use_ranges_is_true
|
44
|
+
unused_ips = ::Proxy::DHCP::Infoblox::UnusedIps.new(@connection, true)
|
45
|
+
unused_ips.expects(:unused_range_ip).with('1.1.1.0', '1.1.1.0', '1.1.1.253')
|
46
|
+
unused_ips.unused_ip('1.1.1.0', '1.1.1.0', '1.1.1.253')
|
47
|
+
end
|
48
|
+
end
|