summaryrefslogtreecommitdiff
path: root/spec/unit/data_collector/resource_report_spec.rb
blob: 278e94b53b6559e51d630da651f7602c42955646 (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
#
# Author:: Salim Afiune (<afiune@chef.io)
#
# Copyright:: Copyright 2012-2018, 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"

describe Chef::DataCollector::ResourceReport do
  let(:cookbook_repo_path) { File.join(CHEF_SPEC_DATA, "cookbooks") }
  let(:cookbook_collection) { Chef::CookbookCollection.new(Chef::CookbookLoader.new(cookbook_repo_path)) }
  let(:node) { Chef::Node.new }
  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:run_context) { Chef::RunContext.new(node, cookbook_collection, events) }
  let(:resource) { Chef::Resource.new("zelda", run_context) }
  let(:report) { described_class.new(resource, :create) }

  describe "#skipped" do
    let(:conditional) { double("Chef::Resource::Conditional") }

    it "should set status and conditional" do
      report.skipped(conditional)
      expect(report.conditional).to eq conditional
      expect(report.status).to eq "skipped"
    end
  end

  describe "#up_to_date" do
    it "should set status" do
      report.up_to_date
      expect(report.status).to eq "up-to-date"
    end
  end

  describe "#updated" do
    it "should set status" do
      report.updated
      expect(report.status).to eq "updated"
    end
  end

  describe "#elapsed_time_in_milliseconds" do

    context "when elapsed_time is not set" do
      it "should return nil" do
        allow(report).to receive(:elapsed_time).and_return(nil)
        expect(report.elapsed_time_in_milliseconds).to eq nil
      end
    end

    context "when elapsed_time is set" do
      it "should return it in milliseconds" do
        allow(report).to receive(:elapsed_time).and_return(1)
        expect(report.elapsed_time_in_milliseconds).to eq 1000
      end
    end
  end

  describe "#failed" do
    let(:exception) { double("Chef::Exception::Test") }

    it "should set exception and status" do
      report.failed(exception)
      expect(report.exception).to eq exception
      expect(report.status).to eq "failed"
    end
  end

  describe "#to_hash" do
    context "for a simple_resource" do
      let(:resource) do
        klass = Class.new(Chef::Resource) do
          resource_name "zelda"
        end
        klass.new("hyrule", run_context)
      end
      let(:hash) do
        {
          "after" => {},
          "before" => {},
          "delta" => "",
          "duration" => "",
          "id" => "hyrule",
          "ignore_failure" => false,
          "name" => "hyrule",
          "result" => "create",
          "status" => "unprocessed",
          "type" => :zelda,
        }
      end

      it "returns a hash containing the expected values" do
        expect(report.to_hash).to eq hash
      end
    end

    context "for a lazy_resource that got skipped" do
      let(:resource) do
        klass = Class.new(Chef::Resource) do
          resource_name "butters"
          property :sword, String, name_property: true, identity: true
        end
        resource = klass.new("hyrule")
        resource.sword = Chef::DelayedEvaluator.new { nil }
        resource
      end
      let(:hash) do
        {
          "after" => {},
          "before" => {},
          "delta" => "",
          "duration" => "",
          "conditional" => "because",
          "id" => "unknown identity (due to Chef::Exceptions::ValidationFailed)",
          "ignore_failure" => false,
          "name" => "hyrule",
          "result" => "create",
          "status" => "skipped",
          "type" => :butters,
        }
      end
      let(:conditional) do
        double("Chef::Resource::Conditional", :to_text => "because")
      end

      it "should handle any Exception and throw a helpful message by mocking the identity" do
        report.skipped(conditional)
        expect(report.to_hash).to eq hash
      end
    end
  end
end