File: master_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 (414 lines) | stat: -rw-r--r-- 14,017 bytes parent folder | download
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
require 'spec_helper'

require 'puppet/application/master'
require 'puppet/daemon'
require 'puppet/network/server'

describe Puppet::Application::Master, :unless => Puppet.features.microsoft_windows? do
  before :each do
    Puppet[:bindaddress] = '127.0.0.1'
    @master = Puppet::Application[:master]
    @daemon = double('daemon')
    allow(@daemon).to receive(:argv=)
    allow(@daemon).to receive(:server=)
    allow(@daemon).to receive(:set_signal_traps)
    allow(@daemon).to receive(:start)
    allow(Puppet::Daemon).to receive(:new).and_return(@daemon)
    allow(Puppet::Util::Log).to receive(:newdestination)

    allow(Puppet::Node.indirection).to receive(:terminus_class=)
    allow(Puppet::Node::Facts.indirection).to receive(:terminus_class=)
    allow(Puppet::Node::Facts.indirection).to receive(:cache_class=)
    allow(Puppet::Transaction::Report.indirection).to receive(:terminus_class=)
    allow(Puppet::Resource::Catalog.indirection).to receive(:terminus_class=)
    allow(Puppet::SSL::Host).to receive(:ca_location=)
  end

  it "should operate in master run_mode" do
    expect(@master.class.run_mode.name).to equal(:master)
  end

  it "should declare a main command" do
    expect(@master).to respond_to(:main)
  end

  it "should declare a compile command" do
    expect(@master).to respond_to(:compile)
  end

  it "should declare a preinit block" do
    expect(@master).to respond_to(:preinit)
  end

  describe "during preinit" do
    before :each do
      allow(@master).to receive(:trap)
    end

    it "should catch INT" do
      allow(@master).to receive(:trap) { |arg,block| arg == :INT }

      @master.preinit
    end
  end

  [:debug,:verbose].each do |option|
    it "should declare handle_#{option} method" do
      expect(@master).to respond_to("handle_#{option}".to_sym)
    end

    it "should store argument value when calling handle_#{option}" do
      expect(@master.options).to receive(:[]=).with(option, 'arg')
      @master.send("handle_#{option}".to_sym, 'arg')
    end
  end

  describe "when applying options" do
    before do
      allow(@master.command_line).to receive(:args).and_return([])
    end

    it "should set the log destination with --logdest" do
      expect(Puppet::Log).to receive(:newdestination).with("console")

      @master.handle_logdest("console")
    end

    it "should put the setdest options to true" do
      expect(@master.options).to receive(:[]=).with(:setdest,true)

      @master.handle_logdest("console")
    end

    it "should parse the log destination from ARGV" do
      allow(@master.command_line).to receive(:args).and_return(%w{--logdest /my/file})

      expect(Puppet::Util::Log).to receive(:newdestination).with("/my/file")

      @master.parse_options
    end

    it "should support dns alt names from ARGV" do
      Puppet.settings.initialize_global_settings(["--dns_alt_names", "foo,bar,baz"])

      @master.preinit
      @master.parse_options

      expect(Puppet[:dns_alt_names]).to eq("foo,bar,baz")
    end


  end

  describe "during setup" do
    before :each do
      allow(Puppet::Log).to receive(:newdestination)
      allow(Puppet).to receive(:settraps)
      allow(Puppet::SSL::CertificateAuthority).to receive(:instance)
      allow(Puppet::SSL::CertificateAuthority).to receive(:ca?)
      allow(Puppet.settings).to receive(:use)

      allow(@master.options).to receive(:[])
    end

    it "should abort stating that the master is not supported on Windows" do
      allow(Puppet.features).to receive(:microsoft_windows?).and_return(true)

      expect { @master.setup }.to raise_error(Puppet::Error, /Puppet master is not supported on Microsoft Windows/)
    end

    describe "setting up logging" do
      it "sets the log level" do
        expect(@master).to receive(:set_log_level)
        @master.setup
      end

      describe "when the log destination is not explicitly configured" do
        before do
          allow(@master.options).to receive(:[]).with(:setdest).and_return(false)
        end

        it "should log to the console when --compile is given" do
          allow(@master.options).to receive(:[]).with(:node).and_return("default")
          expect(Puppet::Util::Log).to receive(:newdestination).with(:console)
          @master.setup
        end

        it "should log to the console when the master is not daemonized or run with rack" do
          expect(Puppet::Util::Log).to receive(:newdestination).with(:console)
          Puppet[:daemonize] = false
          allow(@master.options).to receive(:[]).with(:rack).and_return(false)
          @master.setup
        end

        it "should log to syslog when the master is daemonized" do
          expect(Puppet::Util::Log).not_to receive(:newdestination).with(:console)
          expect(Puppet::Util::Log).to receive(:newdestination).with(:syslog)
          Puppet[:daemonize] = true
          allow(@master.options).to receive(:[]).with(:rack).and_return(false)
          @master.setup
        end

        it "should log to syslog when the master is run with rack" do
          expect(Puppet::Util::Log).not_to receive(:newdestination).with(:console)
          expect(Puppet::Util::Log).to receive(:newdestination).with(:syslog)
          Puppet[:daemonize] = false
          allow(@master.options).to receive(:[]).with(:rack).and_return(true)
          @master.setup
        end
      end

      it "sets the log destination using settings" do
        expect(Puppet::Util::Log).to receive(:newdestination).with("set_via_config")
        Puppet[:logdest] = "set_via_config"

        @master.setup
      end
    end

    it "should print puppet config if asked to in Puppet config" do
      allow(Puppet.settings).to receive(:print_configs?).and_return(true)
      expect(Puppet.settings).to receive(:print_configs).and_return(true)
      expect { @master.setup }.to exit_with 0
    end

    it "should exit after printing puppet config if asked to in Puppet config" do
      allow(Puppet.settings).to receive(:print_configs?).and_return(true)
      expect { @master.setup }.to exit_with 1
    end

    it "should tell Puppet.settings to use :main,:ssl,:master and :metrics category" do
      expect(Puppet.settings).to receive(:use).with(:main,:master,:ssl,:metrics)

      @master.setup
    end

    describe "with no ca" do
      it "should set the ca_location to none" do
        expect(Puppet::SSL::Host).to receive(:ca_location=).with(:none)

        @master.setup
      end

    end

    describe "with a ca configured" do
      before :each do
        allow(Puppet::SSL::CertificateAuthority).to receive(:ca?).and_return(true)
      end

      it "should set the ca_location to local" do
        expect(Puppet::SSL::Host).to receive(:ca_location=).with(:local)

        @master.setup
      end

      it "should tell Puppet.settings to use :ca category" do
        expect(Puppet.settings).to receive(:use).with(:ca)

        @master.setup
      end

      it "should instantiate the CertificateAuthority singleton" do
        expect(Puppet::SSL::CertificateAuthority).to receive(:instance)

        @master.setup
      end
    end

    it "should not set Puppet[:node_cache_terminus] by default" do
      # This is normally called early in the application lifecycle but in our
      # spec testing we don't actually do a full application initialization so
      # we call it here to validate the (possibly) overridden settings are as we
      # expect
      @master.initialize_app_defaults
      @master.setup

      expect(Puppet[:node_cache_terminus]).to be(nil)
    end

    it "should honor Puppet[:node_cache_terminus] by setting the cache_class to its value" do
      # PUP-6060 - ensure we honor this value if specified
      @master.initialize_app_defaults
      Puppet[:node_cache_terminus] = 'plain'
      @master.setup

      expect(Puppet::Node.indirection.cache_class).to eq(:plain)
    end
  end

  describe "when running" do
    before do
      @master.preinit
    end

    it "should dispatch to compile if called with --compile" do
      @master.options[:node] = "foo"
      expect(@master).to receive(:compile)
      @master.run_command
    end

    it "should dispatch to main otherwise" do
      @master.options[:node] = nil

      expect(@master).to receive(:main)
      @master.run_command
    end

    describe "the compile command" do
      before do
        Puppet[:manifest] = "site.pp"
        allow(Puppet).to receive(:err)
        allow(@master).to receive(:puts)
      end

      it "should compile a catalog for the specified node" do
        @master.options[:node] = "foo"
        expect(Puppet::Resource::Catalog.indirection).to receive(:find).with("foo").and_return(Puppet::Resource::Catalog.new)

        expect { @master.compile }.to exit_with 0
      end

      it "should convert the catalog to a pure-resource catalog and use 'JSON::pretty_generate' to pretty-print the catalog" do
        catalog = Puppet::Resource::Catalog.new
        allow(JSON).to receive(:pretty_generate)
        expect(Puppet::Resource::Catalog.indirection).to receive(:find).and_return(catalog)

        expect(catalog).to receive(:to_resource).and_return("rescat")

        @master.options[:node] = "foo"
        expect(JSON).to receive(:pretty_generate).with('rescat', :allow_nan => true, :max_nesting => false)

        expect { @master.compile }.to exit_with 0
      end

      it "should exit with error code 30 if no catalog can be found" do
        @master.options[:node] = "foo"
        expect(Puppet::Resource::Catalog.indirection).to receive(:find).and_return(nil)
        expect(Puppet).to receive(:log_exception)
        expect { @master.compile }.to exit_with 30
      end

      it "should exit with error code 30 if there's a failure" do
        @master.options[:node] = "foo"
        expect(Puppet::Resource::Catalog.indirection).to receive(:find).and_raise(ArgumentError)
        expect(Puppet).to receive(:log_exception)
        expect { @master.compile }.to exit_with 30
      end
    end

    describe "the main command" do
      before :each do
        @master.preinit
        @server = double('server')
        allow(Puppet::Network::Server).to receive(:new).and_return(@server)
        @app = double('app')
        allow(Puppet::SSL::Host).to receive(:localhost)
        allow(Puppet::SSL::CertificateAuthority).to receive(:ca?)
        allow(Process).to receive(:uid).and_return(1000)
        allow(Puppet).to receive(:service)
        Puppet[:daemonize] = false
        allow(Puppet).to receive(:notice)
        allow(Puppet).to receive(:start)
        allow(Puppet::Util).to receive(:chuser)
      end

      it "should create a Server" do
        expect(Puppet::Network::Server).to receive(:new)

        @master.main
      end

      it "should give the server to the daemon" do
        expect(@daemon).to receive(:server=).with(@server)

        @master.main
      end

      it "should generate a SSL cert for localhost" do
        expect(Puppet::SSL::Host).to receive(:localhost)

        @master.main
      end

      it "should make sure to *only* hit the CA for data" do
        allow(Puppet::SSL::CertificateAuthority).to receive(:ca?).and_return(true)

        expect(Puppet::SSL::Host).to receive(:ca_location=).with(:only)

        @master.main
      end

      def a_user_type_for(username)
        user = double('user')
        expect(Puppet::Type.type(:user)).to receive(:new).with(hash_including(name: username)).and_return(user)
        user
      end

      context "user privileges" do
        it "should drop privileges if running as root and the puppet user exists" do
          allow(Puppet.features).to receive(:root?).and_return(true)
          expect(a_user_type_for("puppet")).to receive(:exists?).and_return(true)

          expect(Puppet::Util).to receive(:chuser)

          @master.main
        end

        it "should exit and log an error if running as root and the puppet user does not exist" do
          allow(Puppet.features).to receive(:root?).and_return(true)
          expect(a_user_type_for("puppet")).to receive(:exists?).and_return(false)
          expect(Puppet).to receive(:err).with('Could not change user to puppet. User does not exist and is required to continue.')
          expect { @master.main }.to exit_with 74
        end
      end

      it "should log a deprecation notice when running a WEBrick server" do
        expect(Puppet).to receive(:deprecation_warning).with("The WEBrick Puppet master server is deprecated and will be removed in a future release. Please use Puppet Server instead. See http://links.puppet.com/deprecate-rack-webrick-servers for more information.")
        expect(Puppet).to receive(:deprecation_warning).with("Accessing 'bindaddress' as a setting is deprecated.")

        @master.main
      end

      it "should daemonize if needed" do
        Puppet[:daemonize] = true

        expect(@daemon).to receive(:daemonize)

        @master.main
      end

      it "should start the service" do
        expect(@daemon).to receive(:start)

        @master.main
      end

      describe "with --rack", :if => Puppet.features.rack? do
        before do
          require 'puppet/network/http/rack'
          allow(Puppet::Network::HTTP::Rack).to receive(:new).and_return(@app)

          allow(@master.options).to receive(:[]).with(:rack).and_return(:true)
        end

        it "it should not start a daemon" do
          expect(@daemon).not_to receive(:start)

          @master.main
        end

        it "it should return the app" do
          app = @master.main
          expect(app).to equal(@app)
        end

        it "should log a deprecation notice" do
          expect(Puppet).to receive(:deprecation_warning).with("The Rack Puppet master server is deprecated and will be removed in a future release. Please use Puppet Server instead. See http://links.puppet.com/deprecate-rack-webrick-servers for more information.")

          @master.main
        end
      end
    end
  end
end