summaryrefslogtreecommitdiffstats
path: root/spec/unit/application/inspect_spec.rb
blob: f1518299aa0dbb977051a849797784af3c0cebc6 (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
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
#!/usr/bin/env ruby

require 'spec_helper'

require 'puppet/application/inspect'
require 'puppet/resource/catalog'
require 'puppet/indirector/catalog/yaml'
require 'puppet/indirector/report/rest'
require 'puppet/indirector/file_bucket_file/rest'

describe Puppet::Application::Inspect do
  include PuppetSpec::Files

  before :each do
    @inspect = Puppet::Application[:inspect]
  end

  describe "during setup" do
    it "should print its configuration if asked" do
      Puppet[:configprint] = "all"

      Puppet.settings.expects(:print_configs).returns(true)
      lambda { @inspect.setup }.should raise_error(SystemExit)
    end

    it "should fail if reporting is turned off" do
      Puppet[:report] = false
      lambda { @inspect.setup }.should raise_error(/report=true/)
    end
  end

  describe "when executing" do
    before :each do
      Puppet[:report] = true
      @inspect.options[:logset] = true
      Puppet::Transaction::Report::Rest.any_instance.stubs(:save)
      @inspect.setup
    end

    it "should retrieve the local catalog" do
      Puppet::Resource::Catalog::Yaml.any_instance.expects(:find).with {|request| request.key == Puppet[:certname] }.returns(Puppet::Resource::Catalog.new)

      @inspect.run_command
    end

    it "should save the report to REST" do
      Puppet::Resource::Catalog::Yaml.any_instance.stubs(:find).returns(Puppet::Resource::Catalog.new)
      Puppet::Transaction::Report::Rest.any_instance.expects(:save).with {|request| request.instance.host == Puppet[:certname] }

      @inspect.run_command
    end

    it "should audit the specified properties" do
      catalog = Puppet::Resource::Catalog.new
      file = Tempfile.new("foo")
      file.puts("file contents")
      file.close
      resource = Puppet::Resource.new(:file, file.path, :parameters => {:audit => "all"})
      catalog.add_resource(resource)
      Puppet::Resource::Catalog::Yaml.any_instance.stubs(:find).returns(catalog)

      events = nil

      Puppet::Transaction::Report::Rest.any_instance.expects(:save).with do |request|
        events = request.instance.resource_statuses.values.first.events
      end

      @inspect.run_command

      properties = events.inject({}) do |property_values, event|
        property_values.merge(event.property => event.previous_value)
      end
      properties["ensure"].should == :file
      properties["content"].should == "{md5}#{Digest::MD5.hexdigest("file contents\n")}"
      properties.has_key?("target").should == false
    end

    it "should set audited to true for all events" do
      catalog = Puppet::Resource::Catalog.new
      file = Tempfile.new("foo")
      resource = Puppet::Resource.new(:file, file.path, :parameters => {:audit => "all"})
      catalog.add_resource(resource)
      Puppet::Resource::Catalog::Yaml.any_instance.stubs(:find).returns(catalog)

      events = nil

      Puppet::Transaction::Report::Rest.any_instance.expects(:save).with do |request|
        events = request.instance.resource_statuses.values.first.events
      end

      @inspect.run_command

      events.each do |event|
        event.audited.should == true
      end
    end

    it "should not report irrelevent attributes if the resource is absent" do
      catalog = Puppet::Resource::Catalog.new
      file = Tempfile.new("foo")
      resource = Puppet::Resource.new(:file, file.path, :parameters => {:audit => "all"})
      file.delete
      catalog.add_resource(resource)
      Puppet::Resource::Catalog::Yaml.any_instance.stubs(:find).returns(catalog)

      events = nil

      Puppet::Transaction::Report::Rest.any_instance.expects(:save).with do |request|
        events = request.instance.resource_statuses.values.first.events
      end

      @inspect.run_command

      properties = events.inject({}) do |property_values, event|
        property_values.merge(event.property => event.previous_value)
      end
      properties.should == {"ensure" => :absent}
    end

    describe "when archiving to a bucket" do
      before :each do
        Puppet[:archive_files] = true
        Puppet[:archive_file_server] = "filebucketserver"
        @catalog = Puppet::Resource::Catalog.new
        Puppet::Resource::Catalog::Yaml.any_instance.stubs(:find).returns(@catalog)
      end

      describe "when auditing files" do
        before :each do
          @file = tmpfile("foo")
          @resource = Puppet::Resource.new(:file, @file, :parameters => {:audit => "content"})
          @catalog.add_resource(@resource)
        end

        it "should send an existing file to the file bucket" do
          File.open(@file, 'w') { |f| f.write('stuff') }
          Puppet::FileBucketFile::Rest.any_instance.expects(:head).with do |request|
            request.server == Puppet[:archive_file_server]
          end.returns(false)
          Puppet::FileBucketFile::Rest.any_instance.expects(:save).with do |request|
            request.server == Puppet[:archive_file_server] and request.instance.contents == 'stuff'
          end
          @inspect.run_command
        end

        it "should not send unreadable files" do
          File.open(@file, 'w') { |f| f.write('stuff') }
          File.chmod(0, @file)
          Puppet::FileBucketFile::Rest.any_instance.expects(:head).never
          Puppet::FileBucketFile::Rest.any_instance.expects(:save).never
          @inspect.run_command
        end

        it "should not try to send non-existent files" do
          Puppet::FileBucketFile::Rest.any_instance.expects(:head).never
          Puppet::FileBucketFile::Rest.any_instance.expects(:save).never
          @inspect.run_command
        end

        it "should not try to send files whose content we are not auditing" do
          @resource[:audit] = "group"
          Puppet::FileBucketFile::Rest.any_instance.expects(:head).never
          Puppet::FileBucketFile::Rest.any_instance.expects(:save).never
          @inspect.run_command
        end

        it "should continue if bucketing a file fails" do
          File.open(@file, 'w') { |f| f.write('stuff') }
          Puppet::FileBucketFile::Rest.any_instance.stubs(:head).returns false
          Puppet::FileBucketFile::Rest.any_instance.stubs(:save).raises "failure"
          Puppet::Transaction::Report::Rest.any_instance.expects(:save).with do |request|
            @report = request.instance
          end

          @inspect.run_command

          @report.logs.first.should_not == nil
          @report.logs.first.message.should =~ /Could not back up/
        end
      end

      describe "when auditing non-files" do
        before :each do
          Puppet::Type.newtype(:stub_type) do
            newparam(:name) do
              desc "The name var"
              isnamevar
            end

            newproperty(:content) do
              desc "content"
              def retrieve
                :whatever
              end
            end
          end

          @resource = Puppet::Resource.new(:stub_type, 'foo', :parameters => {:audit => "all"})
          @catalog.add_resource(@resource)
        end

        after :each do
          Puppet::Type.rmtype(:stub_type)
        end

        it "should not try to send non-files" do
          Puppet::FileBucketFile::Rest.any_instance.expects(:head).never
          Puppet::FileBucketFile::Rest.any_instance.expects(:save).never
          @inspect.run_command
        end
      end
    end

    describe "when there are failures" do
      before :each do
        Puppet::Type.newtype(:stub_type) do
          newparam(:name) do
            desc "The name var"
            isnamevar
          end

          newproperty(:content) do
            desc "content"
            def retrieve
              raise "failed"
            end
          end
        end

        @catalog = Puppet::Resource::Catalog.new
        Puppet::Resource::Catalog::Yaml.any_instance.stubs(:find).returns(@catalog)

        Puppet::Transaction::Report::Rest.any_instance.expects(:save).with do |request|
          @report = request.instance
        end
      end

      after :each do
        Puppet::Type.rmtype(:stub_type)
      end

      it "should mark the report failed and create failed events for each property" do
        @resource = Puppet::Resource.new(:stub_type, 'foo', :parameters => {:audit => "all"})
        @catalog.add_resource(@resource)

        @inspect.run_command

        @report.status.should == "failed"
        @report.logs.select{|log| log.message =~ /Could not inspect/}.size.should == 1
        @report.resource_statuses.size.should == 1
        @report.resource_statuses['Stub_type[foo]'].events.size.should == 1

        event = @report.resource_statuses['Stub_type[foo]'].events.first
        event.property.should == "content"
        event.status.should == "failure"
        event.audited.should == true
        event.instance_variables.should_not include("@previous_value")
      end

      it "should continue to the next resource" do
        @resource = Puppet::Resource.new(:stub_type, 'foo', :parameters => {:audit => "all"})
        @other_resource = Puppet::Resource.new(:stub_type, 'bar', :parameters => {:audit => "all"})
        @catalog.add_resource(@resource)
        @catalog.add_resource(@other_resource)

        @inspect.run_command

        @report.resource_statuses.size.should == 2
        @report.resource_statuses.keys.should =~ ['Stub_type[foo]', 'Stub_type[bar]']
      end
    end
  end

  after :all do
    Puppet::Resource::Catalog.indirection.reset_terminus_class
    Puppet::Transaction::Report.indirection.terminus_class = :processor
  end
end