-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
INFRA-507 Added ip_range_size() and associated unit tests
- Loading branch information
1 parent
56070a9
commit 9f17f06
Showing
4 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# ex: syntax=ruby ts=2 sw=2 si et | ||
# frozen_string_literal: true | ||
|
||
begin | ||
require 'puppetx/ip' | ||
rescue LoadError | ||
# work around for puppet bug SERVER-973 | ||
Puppet.info('Puppet did not autoload from the lib directory... falling back to relative path load.') | ||
require File.join(File.expand_path(File.join(__FILE__, '../../../..')), 'puppetx/ip') | ||
end | ||
|
||
module Puppet::Parser::Functions | ||
newfunction(:ip_range_size, type: :rvalue) do |args| | ||
PuppetX::Ip.new(args[0]).range_size(args[1]).to_i | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'ip_range_size' do | ||
# please note that these tests are examples only | ||
# you will need to replace the params and return value | ||
# with your expectations | ||
it 'given adjacent IPv4 CIDR addresses, returns range size of 2' do | ||
is_expected.to run.with_params('192.168.1.1/32', '192.168.1.2/32').and_return(2) | ||
end | ||
it 'given adjacent IPv6 CIDR addresses, returns range size of 2' do | ||
is_expected.to run.with_params('2001:db8::8c28:c929:72db:49fd/64', '2001:db8::8c28:c929:72db:49fe/64').and_return(2) | ||
end | ||
it 'given adjacent IPv4 plain addresses, returns range size of 2' do | ||
is_expected.to run.with_params('192.168.1.1', '192.168.1.2').and_return(2) | ||
end | ||
it 'given adjacent IPv6 plain addresses, returns range size of 2' do | ||
is_expected.to run.with_params('2001:db8::8c28:c929:72db:49fd', '2001:db8::8c28:c929:72db:49fe').and_return(2) | ||
end | ||
it 'given adjacent IPv4 plain addresses in reversed order, returns range size of 2' do | ||
is_expected.to run.with_params('192.168.1.2', '192.168.1.1').and_return(2) | ||
end | ||
it 'given adjacent IPv6 plain addresses in reversed order, returns range size of 2' do | ||
is_expected.to run.with_params('2001:db8::8c28:c929:72db:49fe', '2001:db8::8c28:c929:72db:49fd').and_return(2) | ||
end | ||
it 'given IPv4 CIDR addresses with same offset in different /24 networks, returns range size of 257' do | ||
is_expected.to run.with_params('192.168.1.1/24', '192.168.2.1/24').and_return(257) | ||
end | ||
it 'given IPv6 CIDR addresses with same offset in different /56 networks, returns range size of 257' do | ||
is_expected.to run.with_params('2001:db8::8c28:c929:72db:49fe/56', '2001:db8::8c28:c929:72db:4afe/56').and_return(257) | ||
end | ||
it 'given IPv4 plain addresses in wildly different networks, returns correct range size' do | ||
is_expected.to run.with_params('10.0.57.1', '192.168.2.1').and_return(3_064_449_281) | ||
end | ||
it 'given IPv6 plain addresses in wildly different networks, returns correct range size' do | ||
is_expected.to run | ||
.with_params('2001:db8::8c28:c929:72db:49fe/64', 'fe80::9656:d028:8652:66b6/64') | ||
.and_return(295_747_758_515_978_496_797_848_443_371_614_837_945) | ||
end | ||
it 'given addresses in different families, raises an exception' do | ||
is_expected.to run | ||
.with_params('10.0.57.1', 'fe80::9656:d028:8652:66b6') | ||
.and_raise_error(ArgumentError, %r{addresses must be in same family}) | ||
is_expected.to run | ||
.with_params('fe80::9656:d028:8652:66b6', '10.0.57.1') | ||
.and_raise_error(ArgumentError, %r{addresses must be in same family}) | ||
end | ||
end |