File: epp_face_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 (385 lines) | stat: -rw-r--r-- 15,136 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
require 'spec_helper'
require 'puppet_spec/files'

require 'puppet/face'

describe Puppet::Face[:epp, :current] do
  include PuppetSpec::Files

  let(:eppface) { Puppet::Face[:epp, :current] }

  context "validate" do
    context "from an interactive terminal" do
      before :each do
        from_an_interactive_terminal
      end

      it "validates the template referenced as an absolute file" do
        template_name = 'template1.epp'
        dir = dir_containing('templates', { template_name => "<%= |$a $b |%>" })
        template = File.join(dir, template_name)
        expect { eppface.validate(template) }.to raise_exception(Puppet::Error, /Errors while validating epp/)
      end

      it "runs error free when there are no validation errors from an absolute file" do
        template_name = 'template1.epp'
        dir = dir_containing('templates', { template_name => "just text" })
        template = File.join(dir, template_name)
        expect { eppface.validate(template) }.to_not raise_exception()
      end

      it "reports missing files" do
        expect do
          eppface.validate("missing.epp")
        end.to raise_error(Puppet::Error, /One or more file\(s\) specified did not exist.*missing\.epp/m)
      end

      context "in an environment with templates" do
        let(:dir) do
          dir_containing('environments', { 'production' => { 'modules' => {
            'm1' => { 'templates' => {
              'greetings.epp' => "<% |$subject = world| %>hello <%= $subject -%>",
              'broken.epp'    => "<% | $a $b | %> I am broken",
              'broken2.epp'   => "<% | $a $b | %> I am broken too"
            }},
            'm2' => { 'templates' => {
              'goodbye.epp'   => "<% | $subject = world |%>goodbye <%= $subject -%>",
              'broken3.epp'   => "<% | $a $b | %> I am broken too"
            }}
          }}})

        end

        around(:each) do |example|
          Puppet.settings.initialize_global_settings
          loader = Puppet::Environments::Directories.new(dir, [])
          Puppet.override(:environments => loader) do
            example.run
          end
        end

        it "parses supplied template files in different modules of a directory environment" do
          expect(eppface.validate('m1/greetings.epp')).to be_nil
          expect(eppface.validate('m2/goodbye.epp')).to be_nil
        end

        it "finds errors in supplied template file in the context of a directory environment" do
          expect { eppface.validate('m1/broken.epp') }.to raise_exception(Puppet::Error, /Errors while validating epp/)
          expect(@logs.join).to match(/Syntax error at 'b'/)
        end

        it "stops on first error by default" do
          expect { eppface.validate('m1/broken.epp', 'm1/broken2.epp') }.to raise_exception(Puppet::Error, /Errors while validating epp/)
          expect(@logs.join).to match(/Syntax error at 'b'.*broken\.epp/)
          expect(@logs.join).to_not match(/Syntax error at 'b'.*broken2\.epp/)
        end

        it "continues after error when --continue_on_error is given" do
          expect { eppface.validate('m1/broken.epp', 'm1/broken2.epp', :continue_on_error => true) }.to raise_exception(Puppet::Error, /Errors while validating epp/)
          expect(@logs.join).to match(/Syntax error at 'b'.*broken\.epp/)
          expect(@logs.join).to match(/Syntax error at 'b'.*broken2\.epp/)
        end

        it "validates all templates in the environment" do
          pending "NOT IMPLEMENTED YET"
          expect { eppface.validate(:continue_on_error => true) }.to raise_exception(Puppet::Error, /Errors while validating epp/)
          expect(@logs.join).to match(/Syntax error at 'b'.*broken\.epp/)
          expect(@logs.join).to match(/Syntax error at 'b'.*broken2\.epp/)
          expect(@logs.join).to match(/Syntax error at 'b'.*broken3\.epp/)
        end
      end
    end

    it "validates the contents of STDIN when no files given and STDIN is not a tty" do
      from_a_piped_input_of("<% | $a $oh_no | %> I am broken")
      expect { eppface.validate() }.to raise_exception(Puppet::Error, /Errors while validating epp/)
      expect(@logs.join).to match(/Syntax error at 'oh_no'/)
    end

    it "validates error free contents of STDIN when no files given and STDIN is not a tty" do
      from_a_piped_input_of("look, just text")
      expect(eppface.validate()).to be_nil
    end
  end

  context "dump" do
    it "prints the AST of a template given with the -e option" do
      expect(eppface.dump({ :e => 'hello world' })).to eq("(lambda (epp (block\n  (render-s 'hello world')\n)))\n")
    end

    it "prints the AST of a template given as an absolute file" do
      template_name = 'template1.epp'
      dir = dir_containing('templates', { template_name => "hello world" })
      template = File.join(dir, template_name)
      expect(eppface.dump(template)).to eq("(lambda (epp (block\n  (render-s 'hello world')\n)))\n")
    end

    it "adds a header between dumps by default" do
      template_name1 = 'template1.epp'
      template_name2 = 'template2.epp'
      dir = dir_containing('templates', { template_name1 => "hello world", template_name2 => "hello again"} )
      template1 = File.join(dir, template_name1)
      template2 = File.join(dir, template_name2)

      # Do not move the text block, the left margin and indentation matters
      expect(eppface.dump(template1, template2)).to eq( <<-"EOT" )
--- #{template1}
(lambda (epp (block
  (render-s 'hello world')
)))
--- #{template2}
(lambda (epp (block
  (render-s 'hello again')
)))
      EOT
    end

    it "dumps non validated content when given --no-validate" do
      template_name = 'template1.epp'
      dir = dir_containing('templates', { template_name => "<% 1 2 3 %>" })
      template = File.join(dir, template_name)
      expect(eppface.dump(template, :validate => false)).to eq("(lambda (epp (block\n  1\n  2\n  3\n)))\n")
    end

    it "validated content when given --validate" do
      template_name = 'template1.epp'
      dir = dir_containing('templates', { template_name => "<% 1 2 3 %>" })
      template = File.join(dir, template_name)
      expect(eppface.dump(template, :validate => true)).to eq("")
      expect(@logs.join).to match(/This Literal Integer has no effect.*\(file: .*\/template1\.epp, line: 1, column: 4\)/)
    end

    it "validated content by default" do
      template_name = 'template1.epp'
      dir = dir_containing('templates', { template_name => "<% 1 2 3 %>" })
      template = File.join(dir, template_name)
      expect(eppface.dump(template)).to eq("")
      expect(@logs.join).to match(/This Literal Integer has no effect.*\(file: .*\/template1\.epp, line: 1, column: 4\)/)
    end

    it "informs the user of files that don't exist" do
      expected_message = /One or more file\(s\) specified did not exist:\n\s*does_not_exist_here\.epp/m
      expect { eppface.dump('does_not_exist_here.epp') }.to raise_exception(Puppet::Error, expected_message)
    end

    it "dumps the AST of STDIN when no files given and STDIN is not a tty" do
      from_a_piped_input_of("hello world")
      expect(eppface.dump()).to eq("(lambda (epp (block\n  (render-s 'hello world')\n)))\n")
    end

    it "logs an error if the input cannot be parsed even if validation is off" do
      from_a_piped_input_of("<% |$a  $b| %> oh no")
      expect(eppface.dump(:validate => false)).to eq("")
      expect(@logs[0].message).to match(/Syntax error at 'b'/)
      expect(@logs[0].level).to eq(:err)
    end

    context "using 'pn' format" do
      it "prints the AST of the given expression in PN format" do
        expect(eppface.dump({ :format => 'pn', :e => 'hello world' })).to eq(
          '(lambda {:body [(epp (render-s "hello world"))]})')
      end

      it "pretty prints the AST of the given expression in PN format when --pretty is given" do
        expect(eppface.dump({ :pretty => true, :format => 'pn', :e => 'hello world' })).to eq(<<-RESULT.unindent[0..-2])
        (lambda
          {
            :body [
              (epp
                (render-s
                  "hello world"))]})
        RESULT
      end
    end

    context "using 'json' format" do
      it "prints the AST of the given expression in JSON based on the PN format" do
        expect(eppface.dump({ :format => 'json', :e => 'hello world' })).to eq(
          '{"^":["lambda",{"#":["body",[{"^":["epp",{"^":["render-s","hello world"]}]}]]}]}')
      end

      it "pretty prints the AST of the given expression in JSON based on the PN format when --pretty is given" do
        expect(eppface.dump({ :pretty => true, :format => 'json', :e => 'hello world' })).to eq(<<-RESULT.unindent[0..-2])
        {
          "^": [
            "lambda",
            {
              "#": [
                "body",
                [
                  {
                    "^": [
                      "epp",
                      {
                        "^": [
                          "render-s",
                          "hello world"
                        ]
                      }
                    ]
                  }
                ]
              ]
            }
          ]
        }
        RESULT
      end
    end
  end

  context "render" do
    it "renders input from stdin" do
      from_a_piped_input_of("hello world")
      expect(eppface.render()).to eq("hello world")
    end

    it "renders input from command line" do
      expect(eppface.render(:e => 'hello world')).to eq("hello world")
    end

    it "renders input from an absolute file" do
      template_name = 'template1.epp'
      dir = dir_containing('templates', { template_name => "absolute world" })
      template = File.join(dir, template_name)
      expect(eppface.render(template)).to eq("absolute world")
    end

    it "renders expressions" do
      expect(eppface.render(:e => '<% $x = "mr X"%>hello <%= $x %>')).to eq("hello mr X")
    end

    it "adds values given in a puppet hash given on command line with --values" do
      expect(eppface.render(:e => 'hello <%= $x %>', :values => '{x => "mr X"}')).to eq("hello mr X")
    end

    it "adds fully qualified values given in a puppet hash given on command line with --values" do
      expect(eppface.render(:e => 'hello <%= $mr::x %>', :values => '{mr::x => "mr X"}')).to eq("hello mr X")
    end

    it "adds fully qualified values with leading :: given in a puppet hash given on command line with --values" do
      expect(eppface.render(:e => 'hello <%= $::mr %>', :values => '{"::mr" => "mr X"}')).to eq("hello mr X")
    end

    it "adds values given in a puppet hash produced by a .pp file given with --values_file" do
      file_name = 'values.pp'
      dir = dir_containing('values', { file_name => '{x => "mr X"}' })
      values_file = File.join(dir, file_name)
      expect(eppface.render(:e => 'hello <%= $x %>', :values_file => values_file)).to eq("hello mr X")
    end

    it "adds values given in a yaml hash given with --values_file" do
      file_name = 'values.yaml'
      dir = dir_containing('values', { file_name => "---\n x: 'mr X'" })
      values_file = File.join(dir, file_name)
      expect(eppface.render(:e => 'hello <%= $x %>', :values_file => values_file)).to eq("hello mr X")
    end

    it "merges values from values file and command line with command line having higher precedence" do
      file_name = 'values.yaml'
      dir = dir_containing('values', { file_name => "---\n x: 'mr X'\n word: 'goodbye'" })
      values_file = File.join(dir, file_name)
      expect(eppface.render(:e => '<%= $word %> <%= $x %>',
        :values_file => values_file,
        :values => '{x => "mr Y"}')
        ).to eq("goodbye mr Y")
    end

    it "sets $facts" do
      expect(eppface.render({ :e => 'facts is hash: <%= $facts =~ Hash %>' })).to eql("facts is hash: true")
    end

    it "sets $trusted" do
      expect(eppface.render({ :e => 'trusted is hash: <%= $trusted =~ Hash %>' })).to eql("trusted is hash: true")
    end

    it 'initializes the 4x loader' do
      expect(eppface.render({ :e => <<-EPP.unindent })).to eql("\nString\n\nInteger\n\nBoolean\n")
        <% $data = [type('a',generalized), type(2,generalized), type(true,generalized)] -%>
        <% $data.each |$value| { %>
        <%= $value %>
        <% } -%>
      EPP
    end

    it "facts can be added to" do
      expect(eppface.render({
        :facts => {'the_crux' => 'biscuit'},
        :e     => '<%= $facts[the_crux] %>', 
      })).to eql("biscuit")
    end

    it "facts can be overridden" do
      expect(eppface.render({
        :facts => {'operatingsystem' => 'Merwin'},
        :e     => '<%= $facts[operatingsystem] %>', 
      })).to eql("Merwin")
    end

    context "in an environment with templates" do
      let(:dir) do
        dir_containing('environments', { 'production' => { 'modules' => {
          'm1' => { 'templates' => {
            'greetings.epp' => "<% |$subject = world| %>hello <%= $subject -%>",
            'factshash.epp' => "fact = <%= $facts[the_fact] -%>",
            'fact.epp'      => "fact = <%= $the_fact -%>",
          }},
          'm2' => { 'templates' => {
            'goodbye.epp'   => "<% | $subject = world |%>goodbye <%= $subject -%>",
          }}
        },
          'extra' => {
            'facts.yaml' => "---\n the_fact: 42"
          }
        }})

      end

      around(:each) do |example|
        Puppet.settings.initialize_global_settings
        loader = Puppet::Environments::Directories.new(dir, [])
        Puppet.override(:environments => loader) do
          example.run
        end
      end

      it "renders supplied template files in different modules of a directory environment" do
        expect(eppface.render('m1/greetings.epp')).to eq("hello world")
        expect(eppface.render('m2/goodbye.epp')).to eq("goodbye world")
      end

      it "makes facts available in $facts" do
        facts_file = File.join(dir, 'production', 'extra', 'facts.yaml')
        expect(eppface.render('m1/factshash.epp', :facts => facts_file)).to eq("fact = 42")
      end

      it "makes facts available individually" do
        facts_file = File.join(dir, 'production', 'extra', 'facts.yaml')
        expect(eppface.render('m1/fact.epp', :facts => facts_file)).to eq("fact = 42")
      end

      it "renders multiple files separated by headers by default" do
        # chomp the last newline, it is put there by heredoc
        expect(eppface.render('m1/greetings.epp', 'm2/goodbye.epp')).to eq(<<-EOT.chomp)
--- m1/greetings.epp
hello world
--- m2/goodbye.epp
goodbye world
        EOT
      end

      it "outputs multiple files verbatim when --no-headers is given" do
        expect(eppface.render('m1/greetings.epp', 'm2/goodbye.epp', :header => false)).to eq("hello worldgoodbye world")
      end
    end
  end

  def from_an_interactive_terminal
    allow(STDIN).to receive(:tty?).and_return(true)
  end

  def from_a_piped_input_of(contents)
    allow(STDIN).to receive(:tty?).and_return(false)
    allow(STDIN).to receive(:read).and_return(contents)
  end
end