File: default_manifest_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 (215 lines) | stat: -rw-r--r-- 8,031 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require 'spec_helper'

module EnvironmentsDefaultManifestsSpec
describe "default manifests" do

  context "puppet with default_manifest settings" do
    let(:confdir) { Puppet[:confdir] }
    let(:environmentpath) { File.expand_path("envdir", confdir) }

    context "relative default" do
      let(:testingdir) { File.join(environmentpath, "testing") }

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

      it "reads manifest from ./manifest of a basic directory environment" do
        manifestsdir = File.join(testingdir, "manifests")
        FileUtils.mkdir_p(manifestsdir)

        File.open(File.join(manifestsdir, "site.pp"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("notify { 'ManifestFromRelativeDefault': }")
        end

        File.open(File.join(confdir, "puppet.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("environmentpath=#{environmentpath}")
        end

        expect(a_catalog_compiled_for_environment('testing')).to(
          include_resource('Notify[ManifestFromRelativeDefault]')
        )
      end
    end

    context "set absolute" do
      let(:testingdir) { File.join(environmentpath, "testing") }

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

      it "reads manifest from an absolute default_manifest" do
        manifestsdir = File.expand_path("manifests", confdir)
        FileUtils.mkdir_p(manifestsdir)

        File.open(File.join(confdir, "puppet.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts(<<-EOF)
  environmentpath=#{environmentpath}
  default_manifest=#{manifestsdir}
          EOF
        end

        File.open(File.join(manifestsdir, "site.pp"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("notify { 'ManifestFromAbsoluteDefaultManifest': }")
        end

        expect(a_catalog_compiled_for_environment('testing')).to(
          include_resource('Notify[ManifestFromAbsoluteDefaultManifest]')
        )
      end

      it "reads manifest from directory environment manifest when environment.conf manifest set" do
        default_manifestsdir = File.expand_path("manifests", confdir)
        File.open(File.join(confdir, "puppet.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts(<<-EOF)
  environmentpath=#{environmentpath}
  default_manifest=#{default_manifestsdir}
          EOF
        end

        manifestsdir = File.join(testingdir, "special_manifests")
        FileUtils.mkdir_p(manifestsdir)

        File.open(File.join(manifestsdir, "site.pp"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("notify { 'ManifestFromEnvironmentConfManifest': }")
        end

        File.open(File.join(testingdir, "environment.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("manifest=./special_manifests")
        end

        expect(a_catalog_compiled_for_environment('testing')).to(
          include_resource('Notify[ManifestFromEnvironmentConfManifest]')
        )
        expect(Puppet[:default_manifest]).to eq(default_manifestsdir)
      end

      it "ignores manifests in the local ./manifests if default_manifest specifies another directory" do
        default_manifestsdir = File.expand_path("manifests", confdir)
        FileUtils.mkdir_p(default_manifestsdir)

        File.open(File.join(confdir, "puppet.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts(<<-EOF)
  environmentpath=#{environmentpath}
  default_manifest=#{default_manifestsdir}
          EOF
        end

        File.open(File.join(default_manifestsdir, "site.pp"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("notify { 'ManifestFromAbsoluteDefaultManifest': }")
        end

        implicit_manifestsdir = File.join(testingdir, "manifests")
        FileUtils.mkdir_p(implicit_manifestsdir)

        File.open(File.join(implicit_manifestsdir, "site.pp"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("notify { 'ManifestFromImplicitRelativeEnvironmentManifestDirectory': }")
        end

        expect(a_catalog_compiled_for_environment('testing')).to(
          include_resource('Notify[ManifestFromAbsoluteDefaultManifest]')
        )
      end

    end

    context "with disable_per_environment_manifest true" do
      let(:manifestsdir) { File.expand_path("manifests", confdir) }
      let(:testingdir) { File.join(environmentpath, "testing") }

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

      before(:each) do
        FileUtils.mkdir_p(manifestsdir)

        File.open(File.join(confdir, "puppet.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts(<<-EOF)
  environmentpath=#{environmentpath}
  default_manifest=#{manifestsdir}
  disable_per_environment_manifest=true
          EOF
        end

        File.open(File.join(manifestsdir, "site.pp"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("notify { 'ManifestFromAbsoluteDefaultManifest': }")
        end
      end

      it "reads manifest from the default manifest setting" do
        expect(a_catalog_compiled_for_environment('testing')).to(
          include_resource('Notify[ManifestFromAbsoluteDefaultManifest]')
        )
      end

      it "refuses to compile if environment.conf specifies a different manifest" do
        File.open(File.join(testingdir, "environment.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("manifest=./special_manifests")
        end

        expect { a_catalog_compiled_for_environment('testing') }.to(
          raise_error(Puppet::Error, /disable_per_environment_manifest.*environment.conf.*manifest.*conflict/)
        )
      end

      it "reads manifest from default_manifest setting when environment.conf has manifest set if setting equals default_manifest setting" do
        File.open(File.join(testingdir, "environment.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("manifest=#{manifestsdir}")
        end

        expect(a_catalog_compiled_for_environment('testing')).to(
          include_resource('Notify[ManifestFromAbsoluteDefaultManifest]')
        )
      end

      it "logs errors if environment.conf specifies a different manifest" do
        File.open(File.join(testingdir, "environment.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts("manifest=./special_manifests")
        end

        Puppet.initialize_settings
        expect(Puppet[:environmentpath]).to eq(environmentpath)
        environment = Puppet.lookup(:environments).get('testing')
        expect(environment.manifest).to eq(manifestsdir)
        expect(@logs.first.to_s).to match(%r{disable_per_environment_manifest.*is true, but.*environment.*at #{testingdir}.*has.*environment.conf.*manifest.*#{testingdir}/special_manifests})
      end

      it "raises an error if default_manifest is not absolute" do
        File.open(File.join(confdir, "puppet.conf"), "w", :encoding => Encoding::UTF_8) do |f|
          f.puts(<<-EOF)
  environmentpath=#{environmentpath}
  default_manifest=./relative
  disable_per_environment_manifest=true
          EOF
        end

        expect { Puppet.initialize_settings }.to raise_error(Puppet::Settings::ValidationError, /default_manifest.*must be.*absolute.*when.*disable_per_environment_manifest.*true/)
      end
    end
  end

  RSpec::Matchers.define :include_resource do |expected|
    match do |actual|
      actual.resources.map(&:ref).include?(expected)
    end

    def failure_message
      "expected #{@actual.resources.map(&:ref)} to include #{expected}"
    end

    def failure_message_when_negated
      "expected #{@actual.resources.map(&:ref)} not to include #{expected}"
    end
  end

  def a_catalog_compiled_for_environment(envname)
    Puppet.initialize_settings
    expect(Puppet[:environmentpath]).to eq(environmentpath)
    node = Puppet::Node.new('testnode', :environment => 'testing')
    expect(node.environment).to eq(Puppet.lookup(:environments).get('testing'))
    Puppet::Parser::Compiler.compile(node)
  end
end
end