Skip to content

Commit 7b4d900

Browse files
committed
Test fqdn_rand_base64() and fqdn_rand_string()
1 parent f86d897 commit 7b4d900

4 files changed

Lines changed: 250 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper_acceptance'
3+
4+
describe 'fqdn_rand_base64 function', :unless => unsupported_platforms.include?(fact('operatingsystem')) do
5+
describe 'success' do
6+
let(:facts_d) do
7+
if fact('is_pe', '--puppet') == "true"
8+
if fact('osfamily') =~ /windows/i
9+
if fact('kernelmajversion').to_f < 6.0
10+
'c:/documents and settings/all users/application data/puppetlabs/facter/facts.d'
11+
else
12+
'c:/programdata/puppetlabs/facter/facts.d'
13+
end
14+
else
15+
'/etc/puppetlabs/facter/facts.d'
16+
end
17+
else
18+
'/etc/facter/facts.d'
19+
end
20+
end
21+
after :each do
22+
shell("if [ -f '#{facts_d}/fqdn.txt' ] ; then rm '#{facts_d}/fqdn.txt' ; fi")
23+
end
24+
before :each do
25+
#no need to create on windows, pe creates by default
26+
if fact('osfamily') !~ /windows/i
27+
shell("mkdir -p '#{facts_d}'")
28+
end
29+
end
30+
it 'generates random base64 strings' do
31+
shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
32+
pp = <<-eos
33+
$l = 10
34+
$o = fqdn_rand_base64($l)
35+
notice(inline_template('fqdn_rand_base64 is <%= @o.inspect %>'))
36+
eos
37+
38+
apply_manifest(pp, :catch_failures => true) do |r|
39+
expect(r.stdout).to match(/fqdn_rand_base64 is "8ySYp0dq0B"/)
40+
end
41+
end
42+
it 'generates random base64 strings with custom seeds' do
43+
shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
44+
pp = <<-eos
45+
$l = 10
46+
$s = 'seed'
47+
$o = fqdn_rand_base64($l, $s)
48+
notice(inline_template('fqdn_rand_base64 is <%= @o.inspect %>'))
49+
eos
50+
51+
apply_manifest(pp, :catch_failures => true) do |r|
52+
expect(r.stdout).to match(/fqdn_rand_base64 is "6J2c4oMRUJ"/)
53+
end
54+
end
55+
end
56+
describe 'failure' do
57+
it 'handles improper argument counts'
58+
it 'handles non-numbers for length argument'
59+
end
60+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper_acceptance'
3+
4+
describe 'fqdn_rand_string function', :unless => unsupported_platforms.include?(fact('operatingsystem')) do
5+
describe 'success' do
6+
let(:facts_d) do
7+
if fact('is_pe', '--puppet') == "true"
8+
if fact('osfamily') =~ /windows/i
9+
if fact('kernelmajversion').to_f < 6.0
10+
'c:/documents and settings/all users/application data/puppetlabs/facter/facts.d'
11+
else
12+
'c:/programdata/puppetlabs/facter/facts.d'
13+
end
14+
else
15+
'/etc/puppetlabs/facter/facts.d'
16+
end
17+
else
18+
'/etc/facter/facts.d'
19+
end
20+
end
21+
after :each do
22+
shell("if [ -f '#{facts_d}/fqdn.txt' ] ; then rm '#{facts_d}/fqdn.txt' ; fi")
23+
end
24+
before :each do
25+
#no need to create on windows, pe creates by default
26+
if fact('osfamily') !~ /windows/i
27+
shell("mkdir -p '#{facts_d}'")
28+
end
29+
end
30+
it 'generates random alphanumeric strings' do
31+
shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
32+
pp = <<-eos
33+
$l = 10
34+
$o = fqdn_rand_string($l)
35+
notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
36+
eos
37+
38+
apply_manifest(pp, :catch_failures => true) do |r|
39+
expect(r.stdout).to match(/fqdn_rand_string is "7oDp0KOr1b"/)
40+
end
41+
end
42+
it 'generates random alphanumeric strings with custom seeds' do
43+
shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
44+
pp = <<-eos
45+
$l = 10
46+
$s = 'seed'
47+
$o = fqdn_rand_string($l, $s)
48+
notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
49+
eos
50+
51+
apply_manifest(pp, :catch_failures => true) do |r|
52+
expect(r.stdout).to match(/fqdn_rand_string is "3HS4mbuI3E"/)
53+
end
54+
end
55+
end
56+
describe 'failure' do
57+
it 'handles improper argument counts'
58+
it 'handles non-numbers for length argument'
59+
end
60+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the fqdn_rand_base64 function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
expect(Puppet::Parser::Functions.function("fqdn_rand_base64")).to eq("function_fqdn_rand_base64")
9+
end
10+
11+
it "should raise an ArgumentError if there is less than 1 argument" do
12+
expect { fqdn_rand_base64() }.to( raise_error(ArgumentError, /wrong number of arguments/))
13+
end
14+
15+
it "should raise an ArgumentError if argument 1 isn't a positive integer" do
16+
expect { fqdn_rand_base64(0) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
17+
expect { fqdn_rand_base64(-1) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
18+
expect { fqdn_rand_base64(0.5) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
19+
end
20+
21+
it "provides a random base64 string" do
22+
base64 = %r{\A[a-zA-Z0-9+/]*\z}
23+
expect(fqdn_rand_base64(100).match(base64)).not_to eq(nil)
24+
end
25+
26+
it "provides a random string exactly as long as the given length" do
27+
expect(fqdn_rand_base64(10).size).to eql(10)
28+
end
29+
30+
it "provides the same 'random' value on subsequent calls for the same host" do
31+
expect(fqdn_rand_base64(10)).to eql(fqdn_rand_base64(10))
32+
end
33+
34+
it "considers the same host and same extra arguments to have the same random sequence" do
35+
first_random = fqdn_rand_base64(10, :extra_identifier => [1, "same", "host"])
36+
second_random = fqdn_rand_base64(10, :extra_identifier => [1, "same", "host"])
37+
38+
expect(first_random).to eql(second_random)
39+
end
40+
41+
it "allows extra arguments to control the random value on a single host" do
42+
first_random = fqdn_rand_base64(10, :extra_identifier => [1, "different", "host"])
43+
second_different_random = fqdn_rand_base64(10, :extra_identifier => [2, "different", "host"])
44+
45+
expect(first_random).not_to eql(second_different_random)
46+
end
47+
48+
it "should return different strings for different hosts" do
49+
val1 = fqdn_rand_base64(10, :host => "first.host.com")
50+
val2 = fqdn_rand_base64(10, :host => "second.host.com")
51+
52+
expect(val1).not_to eql(val2)
53+
end
54+
55+
def fqdn_rand_base64(max, args = {})
56+
host = args[:host] || '127.0.0.1'
57+
extra = args[:extra_identifier] || []
58+
59+
scope = PuppetlabsSpec::PuppetInternals.scope
60+
scope.stubs(:[]).with("::fqdn").returns(host)
61+
scope.stubs(:lookupvar).with("::fqdn").returns(host)
62+
63+
scope.function_fqdn_rand_base64([max] + extra)
64+
end
65+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the fqdn_rand_string function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
expect(Puppet::Parser::Functions.function("fqdn_rand_string")).to eq("function_fqdn_rand_string")
9+
end
10+
11+
it "should raise an ArgumentError if there is less than 1 argument" do
12+
expect { fqdn_rand_string() }.to( raise_error(ArgumentError, /wrong number of arguments/))
13+
end
14+
15+
it "should raise an ArgumentError if argument 1 isn't a positive integer" do
16+
expect { fqdn_rand_string(0) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
17+
expect { fqdn_rand_string(-1) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
18+
expect { fqdn_rand_string(0.5) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
19+
end
20+
21+
it "provides a random alphanumeric string" do
22+
string = %r{\A[a-zA-Z0-9]*\z}
23+
expect(fqdn_rand_string(100).match(string)).not_to eq(nil)
24+
end
25+
26+
it "provides a random string exactly as long as the given length" do
27+
expect(fqdn_rand_string(10).size).to eql(10)
28+
end
29+
30+
it "provides the same 'random' value on subsequent calls for the same host" do
31+
expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10))
32+
end
33+
34+
it "considers the same host and same extra arguments to have the same random sequence" do
35+
first_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
36+
second_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
37+
38+
expect(first_random).to eql(second_random)
39+
end
40+
41+
it "allows extra arguments to control the random value on a single host" do
42+
first_random = fqdn_rand_string(10, :extra_identifier => [1, "different", "host"])
43+
second_different_random = fqdn_rand_string(10, :extra_identifier => [2, "different", "host"])
44+
45+
expect(first_random).not_to eql(second_different_random)
46+
end
47+
48+
it "should return different strings for different hosts" do
49+
val1 = fqdn_rand_string(10, :host => "first.host.com")
50+
val2 = fqdn_rand_string(10, :host => "second.host.com")
51+
52+
expect(val1).not_to eql(val2)
53+
end
54+
55+
def fqdn_rand_string(max, args = {})
56+
host = args[:host] || '127.0.0.1'
57+
extra = args[:extra_identifier] || []
58+
59+
scope = PuppetlabsSpec::PuppetInternals.scope
60+
scope.stubs(:[]).with("::fqdn").returns(host)
61+
scope.stubs(:lookupvar).with("::fqdn").returns(host)
62+
63+
scope.function_fqdn_rand_string([max] + extra)
64+
end
65+
end

0 commit comments

Comments
 (0)