]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/functions/fqdn_rand_string_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / fqdn_rand_string_spec.rb
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 valid alphanumeric string when no character set is provided" do
22     length = 100
23     string = %r{\A[a-zA-Z0-9]{#{length}}\z}
24     expect(fqdn_rand_string(length).match(string)).not_to eq(nil)
25   end
26
27   it "provides a valid alphanumeric string when an undef character set is provided" do
28     length = 100
29     string = %r{\A[a-zA-Z0-9]{#{length}}\z}
30     expect(fqdn_rand_string(length, :charset => nil).match(string)).not_to eq(nil)
31   end
32
33   it "provides a valid alphanumeric string when an empty character set is provided" do
34     length = 100
35     string = %r{\A[a-zA-Z0-9]{#{length}}\z}
36     expect(fqdn_rand_string(length, :charset => '').match(string)).not_to eq(nil)
37   end
38
39   it "uses a provided character set" do
40     length = 100
41     charset = '!@#$%^&*()-_=+'
42     string = %r{\A[#{charset}]{#{length}}\z}
43     expect(fqdn_rand_string(length, :charset => charset).match(string)).not_to eq(nil)
44   end
45
46   it "provides a random string exactly as long as the given length" do
47     expect(fqdn_rand_string(10).size).to eql(10)
48   end
49
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))
52   end
53
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"])
57
58     expect(first_random).to eql(second_random)
59   end
60
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"])
64
65     expect(first_random).not_to eql(second_different_random)
66   end
67
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")
71
72     expect(val1).not_to eql(val2)
73   end
74
75   def fqdn_rand_string(max, args = {})
76     host = args[:host] || '127.0.0.1'
77     charset = args[:charset]
78     extra = args[:extra_identifier] || []
79
80     scope = PuppetlabsSpec::PuppetInternals.scope
81     scope.stubs(:[]).with("::fqdn").returns(host)
82     scope.stubs(:lookupvar).with("::fqdn").returns(host)
83
84     function_args = [max]
85     if args.has_key?(:charset) or !extra.empty?
86       function_args << charset
87     end
88     function_args += extra
89     scope.function_fqdn_rand_string(function_args)
90   end
91 end