smart_proxy_dhcp_infoblox 0.0.5 → 0.0.18

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.
@@ -0,0 +1,96 @@
1
+ require "rack/test"
2
+ require 'test_helper'
3
+ require 'json'
4
+ require 'dhcp_common/dhcp_common'
5
+ require 'dhcp/dhcp'
6
+ require 'dhcp/dependency_injection'
7
+ require 'dhcp/dhcp_api'
8
+
9
+ ENV['RACK_ENV'] = 'test'
10
+
11
+ class IntegrationTest < ::Test::Unit::TestCase
12
+ include Rack::Test::Methods
13
+
14
+ def app
15
+ app = Proxy::DhcpApi.new
16
+ app.helpers.server = @server
17
+ app
18
+ end
19
+
20
+ def setup
21
+ @free_ips = ::Proxy::DHCP::FreeIps.new
22
+ @server = ::Proxy::DHCP::Infoblox::Provider.new(nil, Object.new, Object.new, @free_ips, [],
23
+ "default")
24
+
25
+ @expected_reservation = { "name" => "testing-01", "ip" => "10.0.0.200", "mac" => "11:22:33:a9:61:09",
26
+ "subnet" => "10.0.0.0/255.255.255.0", "hostname" => "testing-01" }
27
+ end
28
+
29
+ def test_get_subnets
30
+ @server.expects(:subnets).returns([])
31
+ get "/"
32
+ assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}"
33
+ end
34
+
35
+ def test_get_network
36
+ @server.expects(:all_hosts).with("10.0.0.0").returns([@expected_reservation])
37
+ @server.expects(:all_leases).with("10.0.0.0").returns([])
38
+
39
+ get "/10.0.0.0"
40
+
41
+ assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}"
42
+ end
43
+
44
+ def test_get_unused_ip
45
+ @server.expects(:get_subnet).with("10.0.0.0").returns(::Proxy::DHCP::Subnet.new("10.0.0.0", "255.255.255.0"))
46
+ @server.expects(:all_hosts).with("10.0.0.0").returns([@expected_reservation])
47
+ @server.expects(:all_leases).with("10.0.0.0").returns([])
48
+ @free_ips.expects(:find_free_ip).with("10.0.0.100", "10.0.0.200", [@expected_reservation]).returns("10.0.0.150")
49
+
50
+ get "/10.0.0.0/unused_ip?from=10.0.0.100&to=10.0.0.200"
51
+
52
+ assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}"
53
+ parsed_response = JSON.parse(last_response.body)
54
+ assert parsed_response.key?('ip')
55
+ end
56
+
57
+ def test_get_records_by_ip
58
+ @server.expects(:find_records_by_ip).with("10.0.0.0", "10.0.0.200").returns([@expected_reservation])
59
+ get "/10.0.0.0/ip/10.0.0.200"
60
+ assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}"
61
+ parsed_response = JSON.parse(last_response.body)
62
+ assert_equal 1, parsed_response.size
63
+ assert_equal @expected_reservation, parsed_response.first
64
+ end
65
+
66
+ def test_get_record_by_mac
67
+ @server.expects(:find_record_by_mac).with("10.0.0.0", "11:22:33:a9:61:09").returns(@expected_reservation)
68
+ get "/10.0.0.0/mac/11:22:33:a9:61:09"
69
+ assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}"
70
+ assert_equal @expected_reservation, JSON.parse(last_response.body)
71
+ end
72
+
73
+ def test_create_record
74
+ record = {
75
+ "hostname" => "test-02",
76
+ "ip" => "10.0.0.250",
77
+ "mac" => "10:10:10:10:10:10",
78
+ "network" => "10.0.0.0",
79
+ }
80
+ @server.expects(:add_record).with(has_entries(record))
81
+ post "/10.0.0.0", record
82
+ assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}"
83
+ end
84
+
85
+ def test_delete_records_by_ip
86
+ @server.expects(:del_records_by_ip).with("10.0.0.0", "10.0.0.200")
87
+ delete "/10.0.0.0/ip/10.0.0.200"
88
+ assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}"
89
+ end
90
+
91
+ def test_delete_record_by_mac
92
+ @server.expects(:del_record_by_mac).with("10.0.0.0", "11:22:33:a9:61:09")
93
+ delete "/10.0.0.0/mac/11:22:33:a9:61:09"
94
+ assert last_response.ok?, "Last response was not ok: #{last_response.status} #{last_response.body}"
95
+ end
96
+ end
@@ -1,25 +1,34 @@
1
1
  require 'test_helper'
2
2
  require 'infoblox'
3
3
  require 'dhcp_common/dhcp_common'
4
+ require 'dhcp_common/free_ips'
4
5
  require 'smart_proxy_dhcp_infoblox/plugin_configuration'
5
- require 'smart_proxy_dhcp_infoblox/record_type_validator'
6
6
  require 'smart_proxy_dhcp_infoblox/dhcp_infoblox_plugin'
7
7
  require 'smart_proxy_dhcp_infoblox/host_ipv4_address_crud'
8
8
  require 'smart_proxy_dhcp_infoblox/fixed_address_crud'
9
9
  require 'smart_proxy_dhcp_infoblox/grid_restart'
10
- require 'smart_proxy_dhcp_infoblox/unused_ips'
11
10
  require 'smart_proxy_dhcp_infoblox/dhcp_infoblox_main'
12
11
 
13
12
  class PluginDefaultConfigurationTest < Test::Unit::TestCase
14
13
  def test_default_settings
15
- assert_equal({:record_type => 'host', :range => false}, Proxy::DHCP::Infoblox::Plugin.default_settings)
14
+ assert_equal({ :record_type => 'fixedaddress',
15
+ :used_ips_search_type => "record_type",
16
+ :blacklist_duration_minutes => 30 * 60,
17
+ :wait_after_restart => 10,
18
+ :dns_view => "default",
19
+ :options => [],
20
+ :network_view => "default" },
21
+ Proxy::DHCP::Infoblox::Plugin.default_settings)
16
22
  end
17
23
  end
18
24
 
19
25
  class InfobloxDhcpProductionWiringTest < Test::Unit::TestCase
20
26
  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']}
27
+ @network_view = "network_view"
28
+ @dns_view = "dns_view"
29
+ @settings = { :username => 'user', :password => 'password', :server => '127.0.0.1', :record_type => 'fixedaddress',
30
+ :used_ips_search_type => 'record_type', :subnets => ['1.1.1.0/255.255.255.0'], :blacklist_duration_minutes => 300,
31
+ :wait_after_restart => 1,:dns_view => @dns_view, :network_view => @network_view }
23
32
  @container = ::Proxy::DependencyInjection::Container.new
24
33
  Proxy::DHCP::Infoblox::PluginConfiguration.new.load_dependency_injection_wirings(@container, @settings)
25
34
  end
@@ -29,23 +38,25 @@ class InfobloxDhcpProductionWiringTest < Test::Unit::TestCase
29
38
  assert_equal 'https://127.0.0.1', connection.host
30
39
  assert_equal 'user', connection.username
31
40
  assert_equal 'password', connection.password
32
- assert_equal({:verify => false}, connection.ssl_opts)
41
+ assert_equal({ :verify => true }, connection.ssl_opts)
33
42
  end
34
43
 
35
44
  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
45
+ unused_ips = @container.get_dependency(:unused_ips)
46
+ assert unused_ips
47
+ assert_equal 300, unused_ips.blacklist_interval
39
48
  end
40
49
 
41
50
  def test_host_ipv4_crud_configuration
42
51
  host = @container.get_dependency(:host_ipv4_crud)
43
52
  assert_not_nil host.connection
53
+ assert_equal @dns_view, host.dns_view
44
54
  end
45
55
 
46
56
  def test_fixed_address_crud_configuration
47
57
  fixed_address = @container.get_dependency(:fixed_address_crud)
48
58
  assert_not_nil fixed_address.connection
59
+ assert_equal @network_view, fixed_address.network_view
49
60
  end
50
61
 
51
62
  def test_grid_restart_configuration
@@ -57,15 +68,16 @@ class InfobloxDhcpProductionWiringTest < Test::Unit::TestCase
57
68
  provider = @container.get_dependency(:dhcp_provider)
58
69
  assert_not_nil provider.connection
59
70
  assert_not_nil provider.restart_grid
60
- assert_not_nil provider.unused_ips
71
+ assert_not_nil provider.free_ips
72
+ assert_equal @network_view, provider.network_view
61
73
  assert provider.managed_subnets.include?('1.1.1.0/255.255.255.0')
62
- assert provider.crud.instance_of?(::Proxy::DHCP::Infoblox::HostIpv4AddressCRUD)
74
+ assert provider.crud.instance_of?(::Proxy::DHCP::Infoblox::FixedAddressCRUD)
63
75
  end
64
76
 
65
77
  def test_provider_configuration_with_fixedaddress_crud
66
78
  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')
79
+ load_dependency_injection_wirings(@container, :username => 'user', :password => 'password', :server => '127.0.0.1',
80
+ :record_type => 'fixed_address', :used_ips_search_type => 'record_type')
69
81
 
70
82
  provider = @container.get_dependency(:dhcp_provider)
71
83
  assert provider.crud.instance_of?(::Proxy::DHCP::Infoblox::FixedAddressCRUD)
data/test/test_helper.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'test/unit'
2
- require 'mocha/setup'
2
+ require 'mocha/test_unit'
3
3
 
4
4
  require 'smart_proxy_for_testing'
5
5
 
@@ -1,48 +1,58 @@
1
1
  require 'test_helper'
2
2
  require 'infoblox'
3
- require 'smart_proxy_dhcp_infoblox/unused_ips'
3
+ require 'dhcp_common/free_ips'
4
+ require 'smart_proxy_dhcp_infoblox/dhcp_infoblox_main'
5
+ require 'ostruct'
4
6
 
5
7
  class UnusedIpTest < Test::Unit::TestCase
6
8
  def setup
7
9
  @connection = Object.new
8
- @unused_ips = ::Proxy::DHCP::Infoblox::UnusedIps.new(@connection, false)
10
+ @crud = Object.new
11
+ @restart_grid = Object.new
12
+ @managed_subnets = nil
13
+ @network_view = "another"
14
+ @unused_ips = ::Proxy::DHCP::FreeIps.new
9
15
  @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')
16
+ @provider = Proxy::DHCP::Infoblox::Provider.new(@connection, @crud, @restart_grid,
17
+ @unused_ips, @managed_subnets, @network_view)
11
18
  end
12
19
 
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
20
+ def test_unused_network_ip
21
+ expected_ip = '1.1.1.1'
22
+ @provider.expects(:get_subnet).with('1.1.0.0').returns(::Proxy::DHCP::Subnet.new('1.1.0.0', '255.255.0.0'))
23
+ @provider.expects(:all_hosts).with('1.1.0.0').returns([host = Object.new])
24
+ @provider.expects(:all_leases).with('1.1.0.0').returns([lease = Object.new])
25
+ @unused_ips.expects(:find_free_ip).with('1.1.0.1', '1.1.255.254', [host, lease]).returns(expected_ip)
16
26
 
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
27
+ ip = @provider.unused_ip('1.1.0.0', nil, nil, nil)
20
28
 
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?
29
+ assert_equal expected_ip, ip
23
30
  end
24
31
 
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
32
+ def test_unused_network_ip_with_mac_address_present
33
+ expected_ip = '1.1.1.1'
34
+ mac_address = '00:01:02:03:04:05'
35
+ @provider.expects(:get_subnet).with('1.1.0.0').returns(subnet = ::Proxy::DHCP::Subnet.new('1.1.0.0', '255.255.0.0'))
36
+ @provider.expects(:find_ip_by_mac_address_and_range).with(subnet, mac_address, '1.1.0.1', '1.1.255.254').returns(expected_ip)
30
37
 
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
38
+ ip = @provider.unused_ip('1.1.0.0', mac_address, nil, nil)
36
39
 
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')
40
+ assert_equal expected_ip, ip
41
41
  end
42
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')
43
+ def test_unused_network_ip_with_mac_address_present_with_address_outside_range
44
+ expected_ip = '1.1.1.1'
45
+ start_range = '1.1.10.1'
46
+ end_range = '1.1.100.1'
47
+ mac_address = '00:01:02:03:04:05'
48
+ @provider.expects(:get_subnet).with('1.1.0.0').returns(subnet = ::Proxy::DHCP::Subnet.new('1.1.0.0', '255.255.0.0'))
49
+ @crud.expects(:find_record_by_mac).with('1.1.0.0/16', mac_address).returns(OpenStruct.new(:ip => '10.0.0.1'))
50
+ @provider.expects(:all_hosts).with('1.1.0.0').returns([host = Object.new])
51
+ @provider.expects(:all_leases).with('1.1.0.0').returns([lease = Object.new])
52
+ @unused_ips.expects(:find_free_ip).with(start_range, end_range, [host, lease]).returns(expected_ip)
53
+
54
+ ip = @provider.unused_ip('1.1.0.0', mac_address, start_range, end_range)
55
+
56
+ assert_equal expected_ip, ip
47
57
  end
48
58
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_proxy_dhcp_infoblox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Klaas Demter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-12 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: infoblox
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
13
27
  description: Infoblox DHCP provider plugin for Foreman's smart proxy
14
28
  email:
15
29
  - demter@atix.de
@@ -32,18 +46,16 @@ files:
32
46
  - lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb
33
47
  - lib/smart_proxy_dhcp_infoblox/network_address_range_regex_generator.rb
34
48
  - lib/smart_proxy_dhcp_infoblox/plugin_configuration.rb
35
- - lib/smart_proxy_dhcp_infoblox/record_type_validator.rb
36
- - lib/smart_proxy_dhcp_infoblox/unused_ips.rb
37
49
  - test/host_and_fixedaddress_crud_test.rb
38
50
  - test/infoblox_provider_test.rb
51
+ - test/integration_test.rb
39
52
  - test/plugin_configuration_test.rb
40
- - test/record_type_validator_test.rb
41
53
  - test/regex_generator_test.rb
42
54
  - test/test_helper.rb
43
55
  - test/unused_ip_test.rb
44
56
  homepage: https://github.com/theforeman/smart_proxy_dhcp_infoblox
45
57
  licenses:
46
- - GPLv3
58
+ - GPL-3.0
47
59
  metadata: {}
48
60
  post_install_message:
49
61
  rdoc_options: []
@@ -53,23 +65,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
65
  requirements:
54
66
  - - ">="
55
67
  - !ruby/object:Gem::Version
56
- version: '0'
68
+ version: '2.5'
57
69
  required_rubygems_version: !ruby/object:Gem::Requirement
58
70
  requirements:
59
71
  - - ">="
60
72
  - !ruby/object:Gem::Version
61
73
  version: '0'
62
74
  requirements: []
63
- rubyforge_project:
64
- rubygems_version: 2.2.2
75
+ rubygems_version: 3.3.26
65
76
  signing_key:
66
77
  specification_version: 4
67
78
  summary: Infoblox DHCP provider plugin for Foreman's smart proxy
68
79
  test_files:
69
80
  - test/host_and_fixedaddress_crud_test.rb
70
81
  - test/infoblox_provider_test.rb
82
+ - test/integration_test.rb
71
83
  - test/plugin_configuration_test.rb
72
- - test/record_type_validator_test.rb
73
84
  - test/regex_generator_test.rb
74
85
  - test/test_helper.rb
75
86
  - test/unused_ip_test.rb
@@ -1,8 +0,0 @@
1
- module ::Proxy::DHCP::Infoblox
2
- class RecordTypeValidator < ::Proxy::PluginValidators::Base
3
- def validate!(settings)
4
- return true if ['host', 'fixedaddress'].include?(settings[:record_type])
5
- raise ::Proxy::Error::ConfigurationError, "Setting 'record_type' can be set to either 'host' or 'fixedaddress'"
6
- end
7
- end
8
- end
@@ -1,47 +0,0 @@
1
- require 'ipaddr'
2
- require 'smart_proxy_dhcp_infoblox/ip_address_arithmetic'
3
-
4
- module Proxy::DHCP::Infoblox
5
- class UnusedIps
6
- include IpAddressArithmetic
7
- attr_reader :connection, :use_ranges
8
-
9
- def initialize(connection, use_ranges)
10
- @connection = connection
11
- @use_ranges = use_ranges
12
- end
13
-
14
- def unused_ip(network_address, from_ip_address, to_ip_address)
15
- @use_ranges ? unused_range_ip(network_address, from_ip_address, to_ip_address) : unused_network_ip(network_address, from_ip_address, to_ip_address)
16
- end
17
-
18
- def unused_network_ip(network_address, from_ip_address, to_ip_address)
19
- find_network(network_address).next_available_ip(1, excluded_ips(find_network(network_address).network, from_ip_address, to_ip_address)).first
20
- end
21
-
22
- def unused_range_ip(network_address, from_ip_address, to_ip_address)
23
- find_range(network_address, from_ip_address, to_ip_address).next_available_ip(1).first
24
- end
25
-
26
- def excluded_ips(subnet_address, from, to)
27
- return [] if from.nil? || to.nil?
28
- (IPAddr.new(network_cidr_to_range(subnet_address).first)..IPAddr.new(network_cidr_to_range(subnet_address).last)).to_a.map(&:to_s) -
29
- (IPAddr.new(from)..IPAddr.new(to)).to_a.map(&:to_s)
30
- end
31
-
32
- def find_range(network_address, from, to)
33
- ranges = ::Infoblox::Range.find(@connection, 'network~' => network_address)
34
- range = (from.nil? || to.nil?) ? ranges.first : ranges.find {|r| r.start_addr == from && r.end_addr == to}
35
- raise "No Ranges found for #{network_address} network" if range.nil?
36
- range
37
- end
38
-
39
- def find_network(network_address)
40
- return @memoized_network if !@memoized_network.nil? && @memoized_address == network_address
41
- @memoized_address = network_address
42
- @memoized_network = ::Infoblox::Network.find(@connection, 'network~' => network_address, '_max_results' => 1).first
43
- raise "Subnet #{network_address} not found" if @memoized_network.nil?
44
- @memoized_network
45
- end
46
- end
47
- end
@@ -1,20 +0,0 @@
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