File: fqdn_rand_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (87 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'spec_helper'
require 'puppet_spec/scope'

describe "the fqdn_rand function" do
  include PuppetSpec::Scope

  it "returns an integer" do
    expect(fqdn_rand(3)).to be_an(Integer)
  end

  it "provides a random number strictly less than the given max" do
    expect(fqdn_rand(3)).to satisfy {|n| n < 3 }
  end

  it "provides the same 'random' value on subsequent calls for the same host" do
    expect(fqdn_rand(3)).to eql(fqdn_rand(3))
  end

  it "considers the same host and same extra arguments to have the same random sequence" do
    first_random = fqdn_rand(3, :extra_identifier => [1, "same", "host"])
    second_random = fqdn_rand(3, :extra_identifier => [1, "same", "host"])

    expect(first_random).to eql(second_random)
  end

  it "allows extra arguments to control the random value on a single host" do
    first_random = fqdn_rand(10000, :extra_identifier => [1, "different", "host"])
    second_different_random = fqdn_rand(10000, :extra_identifier => [2, "different", "host"])

    expect(first_random).not_to eql(second_different_random)
  end

  it "should return different sequences of value for different hosts" do
    val1 = fqdn_rand(1000000000, :host => "first.host.com")
    val2 = fqdn_rand(1000000000, :host => "second.host.com")

    expect(val1).not_to eql(val2)
  end

  it "should return a specific value with given set of inputs on non-fips enabled host" do
    allow(Puppet::Util::Platform).to receive(:fips_enabled?).and_return(false)

    expect(fqdn_rand(3000, :host => 'dummy.fqdn.net')).to eql(338)
  end

  it "should return a specific value with given set of inputs on fips enabled host" do
    allow(Puppet::Util::Platform).to receive(:fips_enabled?).and_return(true)

    expect(fqdn_rand(3000, :host => 'dummy.fqdn.net')).to eql(278)
  end

  it "should return a specific value with given seed on a non-fips enabled host" do
    allow(Puppet::Util::Platform).to receive(:fips_enabled?).and_return(false)

    expect(fqdn_rand(5000, :extra_identifier => ['expensive job 33'])).to eql(3374)
  end

  it "should return a specific value with given seed on a fips enabled host" do
    allow(Puppet::Util::Platform).to receive(:fips_enabled?).and_return(true)

    expect(fqdn_rand(5000, :extra_identifier => ['expensive job 33'])).to eql(2389)
  end

  it "returns the same value if only host differs by case" do
    val1 = fqdn_rand(1000000000, :host => "host.example.com", :extra_identifier => [nil, true])
    val2 = fqdn_rand(1000000000, :host => "HOST.example.com", :extra_identifier => [nil, true])

    expect(val1).to eql(val2)
  end

  it "returns the same value if only host differs by case and an initial seed is given" do
    val1 = fqdn_rand(1000000000, :host => "host.example.com", :extra_identifier => ['a seed', true])
    val2 = fqdn_rand(1000000000, :host => "HOST.example.com", :extra_identifier => ['a seed', true])

    expect(val1).to eql(val2)
  end

  def fqdn_rand(max, args = {})
    host = args[:host] || '127.0.0.1'
    extra = args[:extra_identifier] || []

    scope = create_test_scope_for_node('localhost')
    scope.set_facts({ 'networking' => { 'fqdn' => host }})

    scope.function_fqdn_rand([max] + extra)
  end
end