1 #! /usr/bin/env ruby -S rspec
4 describe "the fqdn_rand_string function" do
5 let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
8 expect(Puppet::Parser::Functions.function("fqdn_rand_string")).to eq("function_fqdn_rand_string")
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/))
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/))
21 it "provides a valid alphanumeric string when no character set is provided" do
23 string = %r{\A[a-zA-Z0-9]{#{length}}\z}
24 expect(fqdn_rand_string(length).match(string)).not_to eq(nil)
27 it "provides a valid alphanumeric string when an undef character set is provided" do
29 string = %r{\A[a-zA-Z0-9]{#{length}}\z}
30 expect(fqdn_rand_string(length, :charset => nil).match(string)).not_to eq(nil)
33 it "provides a valid alphanumeric string when an empty character set is provided" do
35 string = %r{\A[a-zA-Z0-9]{#{length}}\z}
36 expect(fqdn_rand_string(length, :charset => '').match(string)).not_to eq(nil)
39 it "uses a provided character set" do
41 charset = '!@#$%^&*()-_=+'
42 string = %r{\A[#{charset}]{#{length}}\z}
43 expect(fqdn_rand_string(length, :charset => charset).match(string)).not_to eq(nil)
46 it "provides a random string exactly as long as the given length" do
47 expect(fqdn_rand_string(10).size).to eql(10)
50 it "provides the same 'random' value on subsequent calls for the same host" do
51 expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10))
54 it "considers the same host and same extra arguments to have the same random sequence" do
55 first_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
56 second_random = fqdn_rand_string(10, :extra_identifier => [1, "same", "host"])
58 expect(first_random).to eql(second_random)
61 it "allows extra arguments to control the random value on a single host" do
62 first_random = fqdn_rand_string(10, :extra_identifier => [1, "different", "host"])
63 second_different_random = fqdn_rand_string(10, :extra_identifier => [2, "different", "host"])
65 expect(first_random).not_to eql(second_different_random)
68 it "should return different strings for different hosts" do
69 val1 = fqdn_rand_string(10, :host => "first.host.com")
70 val2 = fqdn_rand_string(10, :host => "second.host.com")
72 expect(val1).not_to eql(val2)
75 def fqdn_rand_string(max, args = {})
76 host = args[:host] || '127.0.0.1'
77 charset = args[:charset]
78 extra = args[:extra_identifier] || []
80 scope = PuppetlabsSpec::PuppetInternals.scope
81 scope.stubs(:[]).with("::fqdn").returns(host)
82 scope.stubs(:lookupvar).with("::fqdn").returns(host)
85 if args.has_key?(:charset) or !extra.empty?
86 function_args << charset
88 function_args += extra
89 scope.function_fqdn_rand_string(function_args)