File: settings_interpolation_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (110 lines) | stat: -rw-r--r-- 3,661 bytes parent folder | download | duplicates (4)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'pp'
require 'spec_helper'
require 'puppet_spec/settings'

module SettingsInterpolationSpec
describe "interpolating $environment" do
  include PuppetSpec::Settings

  let(:confdir) { Puppet[:confdir] }
  let(:cmdline_args) { ['--confdir', confdir, '--vardir', Puppet[:vardir], '--hiera_config', Puppet[:hiera_config]] }

  before(:each) do
    FileUtils.mkdir_p(confdir)
  end

  shared_examples_for "a setting that does not interpolate $environment" do

    before(:each) do
      set_puppet_conf(confdir, <<-EOF)
        environmentpath=$confdir/environments
        #{setting}=#{value}
      EOF
    end

    it "does not interpolate $environment" do
      Puppet.initialize_settings(cmdline_args)
      expect(Puppet[:environmentpath]).to eq("#{confdir}/environments")
      expect(Puppet[setting.intern]).to eq(expected)
    end

    it "displays the interpolated value in the warning" do
      Puppet.initialize_settings(cmdline_args)
      Puppet[setting.intern]
      expect(@logs).to have_matching_log(/cannot interpolate \$environment within '#{setting}'.*Its value will remain #{Regexp.escape(expected)}/)
    end
  end

  describe "config_version" do
    it "interpolates $environment" do
      envname = 'testing'
      setting = 'config_version'
      value = '/some/script $environment'
      expected = "#{File.expand_path('/some/script')} testing"

      set_puppet_conf(confdir, <<-EOF)
        environmentpath=$confdir/environments
        environment=#{envname}
      EOF

      set_environment_conf("#{confdir}/environments", envname, <<-EOF)
        #{setting}=#{value}
      EOF

      Puppet.initialize_settings(cmdline_args)
      expect(Puppet[:environmentpath]).to eq("#{confdir}/environments")
      environment = Puppet.lookup(:environments).get(envname)
      expect(environment.config_version).to eq(expected)
      expect(@logs).to be_empty
    end
  end

  describe "basemodulepath" do
    let(:setting) { "basemodulepath" }
    let(:value) { "$confdir/environments/$environment/modules:$confdir/environments/$environment/other_modules" }
    let(:expected) { "#{confdir}/environments/$environment/modules:#{confdir}/environments/$environment/other_modules" }

    it_behaves_like "a setting that does not interpolate $environment"

    it "logs a single warning for multiple instaces of $environment in the setting" do
      set_puppet_conf(confdir, <<-EOF)
        environmentpath=$confdir/environments
        #{setting}=#{value}
      EOF

      Puppet.initialize_settings(cmdline_args)
      expect(@logs.map(&:to_s).grep(/cannot interpolate \$environment within '#{setting}'/).count).to eq(1)
    end
  end

  describe "environment" do
    let(:setting) { "environment" }
    let(:value) { "whatareyouthinking$environment" }
    let(:expected) { value }

    it_behaves_like "a setting that does not interpolate $environment"
  end

  describe "the default_manifest" do
    let(:setting) { "default_manifest" }
    let(:value) { "$confdir/manifests/$environment" }
    let(:expected) { "#{confdir}/manifests/$environment" }

    it_behaves_like "a setting that does not interpolate $environment"
  end

  it "does not interpolate $environment and logs a warning when interpolating environmentpath" do
    setting = 'environmentpath'
    value = "$confdir/environments/$environment"
    expected = "#{confdir}/environments/$environment"

    set_puppet_conf(confdir, <<-EOF)
      #{setting}=#{value}
    EOF

    Puppet.initialize_settings(cmdline_args)
    expect(Puppet[setting.intern]).to eq(expected)
    expect(@logs).to have_matching_log(/cannot interpolate \$environment within '#{setting}'/)
  end
end
end