summaryrefslogtreecommitdiffstats
path: root/spec/unit/application/queue_spec.rb
blob: 15e3927a11fcf22d784e7e5382ad489b995b598f (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
166
167
168
169
170
171
172
173
#!/usr/bin/env rspec
require 'spec_helper'

require 'puppet/application/queue'
require 'puppet/indirector/catalog/queue'

describe Puppet::Application::Queue do
  before :each do
    @queue = Puppet::Application[:queue]
    @queue.stubs(:puts)
    @daemon = stub_everything 'daemon', :daemonize => nil
    Puppet::Util::Log.stubs(:newdestination)

    Puppet::Resource::Catalog.indirection.stubs(:terminus_class=)
  end

  it "should ask Puppet::Application to parse Puppet configuration file" do
    @queue.should_parse_config?.should be_true
  end

  it "should declare a main command" do
    @queue.should respond_to(:main)
  end

  it "should declare a preinit block" do
    @queue.should respond_to(:preinit)
  end

  describe "in preinit" do
    it "should catch INT" do
      Signal.expects(:trap).with { |arg,block| arg == :INT }

      @queue.preinit
    end

    it "should init :verbose to false" do
      @queue.preinit

      @queue.options[:verbose].should be_false
    end

    it "should init :debug to false" do
      @queue.preinit

      @queue.options[:debug].should be_false
    end

    it "should create a Daemon instance and copy ARGV to it" do
      ARGV.expects(:dup).returns "eh"
      daemon = mock("daemon")
      daemon.expects(:argv=).with("eh")
      Puppet::Daemon.expects(:new).returns daemon
      @queue.preinit
    end
  end

  describe "when handling options" do

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

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

  describe "during setup" do
    before :each do
      @queue.options.stubs(:[])
      @queue.daemon.stubs(:daemonize)
      Puppet.stubs(:info)
      Puppet.features.stubs(:stomp?).returns true
      Puppet::Resource::Catalog.indirection.stubs(:terminus_class=)
      Puppet.stubs(:settraps)
      Puppet.settings.stubs(:print_config?)
      Puppet.settings.stubs(:print_config)
    end

    it "should fail if the stomp feature is missing" do
      Puppet.features.expects(:stomp?).returns false
      lambda { @queue.setup }.should raise_error(ArgumentError)
    end

    it "should print puppet config if asked to in Puppet config" do
      Puppet.settings.stubs(:print_configs?).returns(true)
      Puppet.settings.expects(:print_configs).returns(true)
      expect { @queue.setup }.to exit_with 0
    end

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

    it "should call setup_logs" do
      @queue.expects(:setup_logs)
      @queue.setup
    end

    describe "when setting up logs" do
      before :each do
        Puppet::Util::Log.stubs(:newdestination)
      end

      it "should set log level to debug if --debug was passed" do
        @queue.options.stubs(:[]).with(:debug).returns(true)
        @queue.setup_logs
        Puppet::Util::Log.level.should == :debug
      end

      it "should set log level to info if --verbose was passed" do
        @queue.options.stubs(:[]).with(:verbose).returns(true)
        @queue.setup_logs
        Puppet::Util::Log.level.should == :info
      end

      [:verbose, :debug].each do |level|
        it "should set console as the log destination with level #{level}" do
          @queue.options.stubs(:[]).with(level).returns(true)

          Puppet::Util::Log.expects(:newdestination).with(:console)

          @queue.setup_logs
        end
      end
    end

    it "should configure the Catalog class to use ActiveRecord" do
      Puppet::Resource::Catalog.indirection.expects(:terminus_class=).with(:active_record)

      @queue.setup
    end

    it "should daemonize if needed" do
      Puppet.expects(:[]).with(:daemonize).returns(true)

      @queue.daemon.expects(:daemonize)

      @queue.setup
    end
  end

  describe "when running" do
    before :each do
      @queue.stubs(:sleep_forever)
      Puppet::Resource::Catalog::Queue.stubs(:subscribe)
      Thread.list.each { |t| t.stubs(:join) }
    end

    it "should subscribe to the queue" do
      Puppet::Resource::Catalog::Queue.expects(:subscribe)
      @queue.main
    end

    it "should log and save each catalog passed by the queue" do
      catalog = Puppet::Resource::Catalog.new('eh')
      Puppet::Resource::Catalog.indirection.expects(:save).with(catalog)

      Puppet::Resource::Catalog::Queue.expects(:subscribe).yields(catalog)
      Puppet.expects(:notice).times(2)
      @queue.main
    end

    it "should join all of the running threads" do
      Thread.list.each { |t| t.expects(:join) }
      @queue.main
    end
  end
end