|
| 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