summaryrefslogtreecommitdiff
path: root/spec/unit/audit/control_group_data_spec.rb
blob: 2e21a13b175ab41f7ad34a4764808c2aa9c749d7 (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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#
# Author:: Tyler Ball (<tball@chef.io>)
# Author:: Claire McQuin (<claire@chef.io>)
#
# Copyright:: Copyright 2014-2016, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"
require "securerandom"

describe Chef::Audit::AuditData do

  let(:node_name) { "noodles" }
  let(:run_id) { SecureRandom.uuid }
  let(:audit_data) { described_class.new(node_name, run_id) }

  let(:control_group_1) { double("control group 1") }
  let(:control_group_2) { double("control group 2") }

  describe "#add_control_group" do
    context "when no control groups have been added" do
      it "stores the control group" do
        audit_data.add_control_group(control_group_1)
        expect(audit_data.control_groups).to include(control_group_1)
      end

    end

    context "when adding additional control groups" do

      before do
        audit_data.add_control_group(control_group_1)
      end

      it "stores the control group" do
        audit_data.add_control_group(control_group_2)
        expect(audit_data.control_groups).to include(control_group_2)
      end

      it "stores all control groups" do
        audit_data.add_control_group(control_group_2)
        expect(audit_data.control_groups).to include(control_group_1)
      end
    end
  end

  describe "#to_hash" do

    let(:audit_data_hash) { audit_data.to_hash }

    it "returns a hash" do
      expect(audit_data_hash).to be_a(Hash)
    end

    it "describes a Chef::Audit::AuditData object" do
      keys = [:node_name, :run_id, :start_time, :end_time, :control_groups]
      expect(audit_data_hash.keys).to match_array(keys)
    end

    describe ":control_groups" do

      let(:control_hash_1) { { :name => "control group 1" } }
      let(:control_hash_2) { { :name => "control group 2" } }

      let(:control_groups) { audit_data_hash[:control_groups] }

      context "with no control groups added" do
        it "is an empty list" do
          expect(control_groups).to eq []
        end
      end

      context "with one control group added" do

        before do
          allow(audit_data).to receive(:control_groups).and_return([control_group_1])
        end

        it "is a one-element list containing the control group hash" do
          expect(control_group_1).to receive(:to_hash).once.and_return(control_hash_1)
          expect(control_groups.size).to eq 1
          expect(control_groups).to include(control_hash_1)
        end
      end

      context "with multiple control groups added" do

        before do
          allow(audit_data).to receive(:control_groups).and_return([control_group_1, control_group_2])
        end

        it "is a list of control group hashes" do
          expect(control_group_1).to receive(:to_hash).and_return(control_hash_1)
          expect(control_group_2).to receive(:to_hash).and_return(control_hash_2)
          expect(control_groups.size).to eq 2
          expect(control_groups).to include(control_hash_1)
          expect(control_groups).to include(control_hash_2)
        end
      end
    end
  end
end

describe Chef::Audit::ControlData do

  let(:name) { "ramen" }
  let(:resource_type) { double("Service") }
  let(:resource_name) { "mysql" }
  let(:context) { nil }
  let(:line_number) { 27 }

  let(:control_data) { described_class.new(name: name,
                                           resource_type: resource_type, resource_name: resource_name,
                                           context: context, line_number: line_number) }

  describe "#to_hash" do

    let(:control_data_hash) { control_data.to_hash }

    it "returns a hash" do
      expect(control_data_hash).to be_a(Hash)
    end

    it "describes a Chef::Audit::ControlData object" do
      keys = [:name, :resource_type, :resource_name, :context, :status, :details]
      expect(control_data_hash.keys).to match_array(keys)
    end

    context "when context is nil" do

      it "sets :context to an empty array" do
        expect(control_data_hash[:context]).to eq []
      end

    end

    context "when context is non-nil" do

      let(:context) { ["outer"] }

      it "sets :context to its value" do
        expect(control_data_hash[:context]).to eq context
      end
    end
  end
end

describe Chef::Audit::ControlGroupData do

  let(:name) { "balloon" }
  let(:control_group_data) { described_class.new(name) }

  shared_context "control data" do

    let(:name) { "" }
    let(:resource_type) { nil }
    let(:resource_name) { nil }
    let(:context) { nil }
    let(:line_number) { 0 }

    let(:control_data) {
      {
        :name => name,
        :resource_type => resource_type,
        :resource_name => resource_name,
        :context => context,
        :line_number => line_number,
      }
    }

  end

  shared_context "control" do
    include_context "control data"

    let(:control) { Chef::Audit::ControlData.new(name: name,
                                                 resource_type: resource_type, resource_name: resource_name,
                                                 context: context, line_number: line_number) }

    before do
      allow(Chef::Audit::ControlData).to receive(:new).
        with(name: name, resource_type: resource_type,
             resource_name: resource_name, context: context,
             line_number: line_number).
        and_return(control)
    end
  end

  describe "#new" do
    it "has status \"success\"" do
      expect(control_group_data.status).to eq "success"
    end
  end

  describe "#example_success" do
    include_context "control"

    def notify_success
      control_group_data.example_success(control_data)
    end

    it "increments the number of successful audits" do
      num_success = control_group_data.number_succeeded
      notify_success
      expect(control_group_data.number_succeeded).to eq (num_success + 1)
    end

    it "does not increment the number of failed audits" do
      num_failed = control_group_data.number_failed
      notify_success
      expect(control_group_data.number_failed).to eq (num_failed)
    end

    it "marks the audit's status as success" do
      notify_success
      expect(control.status).to eq "success"
    end

    it "does not modify its own status" do
      expect(control_group_data).to_not receive(:status=)
      status = control_group_data.status
      notify_success
      expect(control_group_data.status).to eq status
    end

    it "saves the control" do
      controls = control_group_data.controls
      expect(controls).to_not include(control)
      notify_success
      expect(controls).to include(control)
    end
  end

  describe "#example_failure" do
    include_context "control"

    let(:details) { "poop" }

    def notify_failure
      control_group_data.example_failure(control_data, details)
    end

    it "does not increment the number of successful audits" do
      num_success = control_group_data.number_succeeded
      notify_failure
      expect(control_group_data.number_succeeded).to eq num_success
    end

    it "increments the number of failed audits" do
      num_failed = control_group_data.number_failed
      notify_failure
      expect(control_group_data.number_failed).to eq (num_failed + 1)
    end

    it "marks the audit's status as failure" do
      notify_failure
      expect(control.status).to eq "failure"
    end

    it "marks its own status as failure" do
      notify_failure
      expect(control_group_data.status).to eq "failure"
    end

    it "saves the control" do
      controls = control_group_data.controls
      expect(controls).to_not include(control)
      notify_failure
      expect(controls).to include(control)
    end

    context "when details are not provided" do

      let(:details) { nil }

      it "does not save details to the control" do
        default_details = control.details
        expect(control).to_not receive(:details=)
        notify_failure
        expect(control.details).to eq default_details
      end
    end

    context "when details are provided" do

      let(:details) { "yep that didn't work" }

      it "saves details to the control" do
        notify_failure
        expect(control.details).to eq details
      end
    end
  end

  shared_examples "multiple audits" do |success_or_failure|
    include_context "control"

    let(:num_success) { 0 }
    let(:num_failure) { 0 }

    before do
      if num_failure == 0
        num_success.times { control_group_data.example_success(control_data) }
      elsif num_success == 0
        num_failure.times { control_group_data.example_failure(control_data, nil) }
      end
    end

    it "counts the number of successful audits" do
      expect(control_group_data.number_succeeded).to eq num_success
    end

    it "counts the number of failed audits" do
      expect(control_group_data.number_failed).to eq num_failure
    end

    it "marks its status as \"#{success_or_failure}\"" do
      expect(control_group_data.status).to eq success_or_failure
    end
  end

  context "when all audits pass" do
    include_examples "multiple audits", "success" do
      let(:num_success) { 3 }
    end
  end

  context "when one audit fails" do
    shared_examples "mixed audit results" do
      include_examples "multiple audits", "failure" do

        let(:audit_results) { [] }
        let(:num_success) { audit_results.count("success") }
        let(:num_failure) { 1 }

        before do
          audit_results.each do |result|
            if result == "success"
              control_group_data.example_success(control_data)
            else
              control_group_data.example_failure(control_data, nil)
            end
          end
        end
      end
    end

    context "and it's the first audit" do
      include_examples "mixed audit results" do
        let(:audit_results) { %w(failure success success) }
      end
    end

    context "and it's an audit in the middle" do
      include_examples "mixed audit results" do
        let(:audit_results) { %w(success failure success) }
      end
    end

    context "and it's the last audit" do
      include_examples "mixed audit results" do
        let(:audit_results) { %w(success success failure) }
      end
    end
  end

  context "when all audits fail" do
    include_examples "multiple audits", "failure" do
      let(:num_failure) { 3 }
    end
  end

  describe "#to_hash" do

    let(:control_group_data_hash) { control_group_data.to_hash }

    it "returns a hash" do
      expect(control_group_data_hash).to be_a(Hash)
    end

    it "describes a Chef::Audit::ControlGroupData object" do
      keys = [:name, :status, :number_succeeded, :number_failed,
        :controls, :id]
      expect(control_group_data_hash.keys).to match_array(keys)
    end

    describe ":controls" do

      let(:control_group_controls) { control_group_data_hash[:controls] }

      context "with no controls added" do
        it "is an empty list" do
          expect(control_group_controls).to eq []
        end
      end

      context "with one control added" do
        include_context "control"

        let(:control_list) { [control_data] }
        let(:control_hash) { control.to_hash }

        before do
          expect(control_group_data).to receive(:controls).twice.and_return(control_list)
          expect(control_data).to receive(:to_hash).and_return(control_hash)
        end

        it "is a one-element list containing the control hash" do
          expect(control_group_controls.size).to eq 1
          expect(control_group_controls).to include(control_hash)
        end

        it "adds a sequence number to the control" do
          control_group_data.to_hash
          expect(control_hash).to have_key(:sequence_number)
        end

      end

      context "with multiple controls added" do

        let(:control_hash_1) { { :line_number => 27 } }
        let(:control_hash_2) { { :line_number => 13 } }
        let(:control_hash_3) { { :line_number => 35 } }

        let(:control_1) { double("control 1",
          :line_number => control_hash_1[:line_number],
          :to_hash => control_hash_1) }
        let(:control_2) { double("control 2",
          :line_number => control_hash_2[:line_number],
          :to_hash => control_hash_2) }
        let(:control_3) { double("control 3",
          :line_number => control_hash_3[:line_number],
          :to_hash => control_hash_3) }

        let(:control_list) { [control_1, control_2, control_3] }
        let(:ordered_control_hashes) { [control_hash_2, control_hash_1, control_hash_3] }

        before do
          # Another way to do this would be to call #example_success
          # or #example_failure per control hash, but we'd have to
          # then stub #create_control and it's a lot of extra stubbing work.
          # We can't stub the controls reader to return a list of
          # controls because of the call to sort! and the following
          # reading of controls.
          control_group_data.instance_variable_set(:@controls, control_list)
        end

        it "is a list of control group hashes ordered by line number" do
          expect(control_group_controls.size).to eq 3
          expect(control_group_controls).to eq ordered_control_hashes
        end

        it "assigns sequence numbers in order" do
          control_group_data.to_hash
          ordered_control_hashes.each_with_index do |control_hash, idx|
            expect(control_hash[:sequence_number]).to eq idx + 1
          end
        end
      end
    end
  end

end