summaryrefslogtreecommitdiff
path: root/spec/integration/environments/settings_interpolation_spec.rb
blob: bcdc136c73fdb8563d14752e0b9472cc0f2ce2d5 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'pp'
require 'spec_helper'

module SettingsInterpolationSpec
describe "interpolating $environment" do
  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

  context "when environmentpath is set" do

    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

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

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

  context "when environmentpath is not set" do
    it "does interpolate $environment in config_version" do
      value = "/some/script $environment"
      expect = "/some/script production"
      assert_does_interpolate_environment("config_version", value, expect)
    end

    it "does interpolate $environment in basemodulepath" do
      value = "$confdir/environments/$environment/modules:$confdir/environments/$environment/other_modules"
      expected = "#{confdir}/environments/production/modules:#{confdir}/environments/production/other_modules"
      assert_does_interpolate_environment("basemodulepath", value, expected)
    end

    it "does interpolate $environment in default_manifest, which is fine, because this setting isn't used" do
      value = "$confdir/manifests/$environment"
      expected = "#{confdir}/manifests/production"

      assert_does_interpolate_environment("default_manifest", value, expected)
    end

    it "raises something" do
      value = expected = "whatareyouthinking$environment"
      expect {
        assert_does_interpolate_environment("environment", value, expected)
      }.to raise_error(SystemStackError, /stack level too deep/)
    end
  end

  def set_puppet_conf(confdir, settings)
    write_file(File.join(confdir, "puppet.conf"), settings)
  end

  def set_environment_conf(environmentpath, environment, settings)
    envdir = File.join(environmentpath, environment)
    FileUtils.mkdir_p(envdir)
    write_file(File.join(envdir, 'environment.conf'), settings)
  end

  def write_file(file, contents)
    File.open(file, "w") do |f|
      f.puts(contents)
    end
  end
end
end