summaryrefslogtreecommitdiff
path: root/spec/unit/resource_collection_spec.rb
blob: d52e7e2c26ac172163b375f9e420d7cc1350db9c (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
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Christopher Walters (<cw@opscode.com>)
# Copyright:: Copyright (c) 2008, 2009 Opscode, 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::ResourceCollection do
  let(:rc) { Chef::ResourceCollection.new() }
  let(:resource) { Chef::Resource::ZenMaster.new("makoto") }

  it "should throw an error when calling a non-delegated method" do
    expect { rc.not_a_method }.to raise_error(NoMethodError)
  end

  describe "initialize" do
    it "should return a Chef::ResourceCollection" do
      expect(rc).to be_kind_of(Chef::ResourceCollection)
    end
  end

  describe "[]" do
    it "should accept Chef::Resources through [index]" do
      expect { rc[0] = resource }.not_to raise_error
      expect { rc[0] = "string" }.to raise_error(ArgumentError)
    end

    it "should allow you to fetch Chef::Resources by position" do
      rc[0] = resource
      expect(rc[0]).to eql(resource)
    end
  end

  describe "push" do
    it "should accept Chef::Resources through pushing" do
      expect { rc.push(resource) }.not_to raise_error
      expect { rc.push("string") }.to raise_error(ArgumentError)
    end
  end

  describe "<<" do
    it "should accept the << operator" do
      expect { rc << resource }.not_to raise_error
    end
  end

  describe "insert" do
    it "should accept only Chef::Resources" do
      expect { rc.insert(resource) }.not_to raise_error
      expect { rc.insert("string") }.to raise_error(ArgumentError)
    end

    it "should accept named arguments in any order" do
      rc.insert(resource, :instance_name => 'foo', :resource_type =>'bar')
      expect(rc[0]).to eq(resource)
    end

    it "should append resources to the end of the collection when not executing a run" do
      zmr = Chef::Resource::ZenMaster.new("there is no spoon")
      rc.insert(resource)
      rc.insert(zmr)
      expect(rc[0]).to eql(resource)
      expect(rc[1]).to eql(zmr)
    end

    it "should insert resources to the middle of the collection if called while executing a run" do
      resource_to_inject = Chef::Resource::ZenMaster.new("there is no spoon")
      zmr = Chef::Resource::ZenMaster.new("morpheus")
      dummy = Chef::Resource::ZenMaster.new("keanu reeves")
      rc.insert(zmr)
      rc.insert(dummy)

      rc.execute_each_resource do |resource|
        rc.insert(resource_to_inject) if resource == zmr
      end

      expect(rc[0]).to eql(zmr)
      expect(rc[1]).to eql(resource_to_inject)
      expect(rc[2]).to eql(dummy)
    end
  end

  describe "each" do
    it "should allow you to iterate over every resource in the collection" do
      load_up_resources
      results = Array.new
      expect {
        rc.each do |r|
          results << r.name
        end
      }.not_to raise_error
      results.each_index do |i|
        case i
        when 0
          expect(results[i]).to eql("dog")
        when 1
          expect(results[i]).to eql("cat")
        when 2
          expect(results[i]).to eql("monkey")
        end
      end
    end
  end

  describe "each_index" do
    it "should allow you to iterate over every resource by index" do
      load_up_resources
      results = Array.new
      expect {
        rc.each_index do |i|
          results << rc[i].name
        end
      }.not_to raise_error
      results.each_index do |i|
        case i
        when 0
          expect(results[i]).to eql("dog")
        when 1
          expect(results[i]).to eql("cat")
        when 2
          expect(results[i]).to eql("monkey")
        end
      end
    end
  end

  describe "lookup" do
    it "should allow you to find resources by name via lookup" do
      zmr = Chef::Resource::ZenMaster.new("dog")
      rc << zmr
      expect(rc.lookup(zmr.to_s)).to eql(zmr)

      zmr = Chef::Resource::ZenMaster.new("cat")
      rc[0] = zmr
      expect(rc.lookup(zmr)).to eql(zmr)

      zmr = Chef::Resource::ZenMaster.new("monkey")
      rc.push(zmr)
      expect(rc.lookup(zmr)).to eql(zmr)
    end

    it "should raise an exception if you send something strange to lookup" do
      expect { rc.lookup(:symbol) }.to raise_error(ArgumentError)
    end

    it "should raise an exception if it cannot find a resource with lookup" do
      expect { rc.lookup("zen_master[dog]") }.to raise_error(Chef::Exceptions::ResourceNotFound)
    end
  end

  describe "resources" do

    it "should find a resource by symbol and name (:zen_master => monkey)" do
      load_up_resources
      expect(rc.resources(:zen_master => "monkey").name).to eql("monkey")
    end

    it "should find a resource by symbol and array of names (:zen_master => [a,b])" do
      load_up_resources
      results = rc.resources(:zen_master => [ "monkey", "dog" ])
      expect(results.length).to eql(2)
      check_by_names(results, "monkey", "dog")
    end

    it "should find resources of multiple kinds (:zen_master => a, :file => b)" do
      load_up_resources
      results = rc.resources(:zen_master => "monkey", :file => "something")
      expect(results.length).to eql(2)
      check_by_names(results, "monkey", "something")
    end

    it "should find a resource by string zen_master[a]" do
      load_up_resources
      expect(rc.resources("zen_master[monkey]").name).to eql("monkey")
    end

    it "should find resources by strings of zen_master[a,b]" do
      load_up_resources
      results = rc.resources("zen_master[monkey,dog]")
      expect(results.length).to eql(2)
      check_by_names(results, "monkey", "dog")
    end

    it "should find resources of multiple types by strings of zen_master[a]" do
      load_up_resources
      results = rc.resources("zen_master[monkey]", "file[something]")
      expect(results.length).to eql(2)
      check_by_names(results, "monkey", "something")
    end

    it "should raise an exception if you pass a bad name to resources" do
      expect { rc.resources("michael jackson") }.to raise_error(ArgumentError)
    end

    it "should raise an exception if you pass something other than a string or hash to resource" do
      expect { rc.resources([Array.new]) }.to raise_error(ArgumentError)
    end

    it "raises an error when attempting to find a resource that does not exist" do
      expect {rc.find("script[nonesuch]")}.to raise_error(Chef::Exceptions::ResourceNotFound)
    end

  end

  describe "when validating a resource query object" do
    it "accepts a string of the form 'resource_type[resource_name]'" do
      expect(rc.validate_lookup_spec!("resource_type[resource_name]")).to be_truthy
    end

    it "accepts a single-element :resource_type => 'resource_name' Hash" do
      expect(rc.validate_lookup_spec!(:service => "apache2")).to be_truthy
    end


    it "accepts a chef resource object" do
      res = Chef::Resource.new("foo", nil)
      expect(rc.validate_lookup_spec!(res)).to be_truthy
    end

    it "rejects a malformed query string" do
      expect do
        rc.validate_lookup_spec!("resource_type[missing-end-bracket")
      end.to raise_error(Chef::Exceptions::InvalidResourceSpecification)
    end

    it "rejects an argument that is not a String, Hash, or Chef::Resource" do
      expect do
        rc.validate_lookup_spec!(Object.new)
      end.to raise_error(Chef::Exceptions::InvalidResourceSpecification)
    end

  end

  describe "to_json" do
    it "should serialize to json" do
      json = rc.to_json
      expect(json).to match(/json_class/)
      expect(json).to match(/instance_vars/)
    end

    include_examples "to_json equivalent to Chef::JSONCompat.to_json" do
      let(:jsonable) { rc }
    end
  end

  describe "self.from_json" do
    it "should not respond to this method" do
      expect(rc.respond_to?(:from_json)).to eq(false)
    end

    it "should convert from json using the CHEF::JSONCompat library" do
      rc << resource
      json = Chef::JSONCompat.to_json(rc)
      s_rc = Chef::JSONCompat.from_json(json)
      expect(s_rc).to be_a_kind_of(Chef::ResourceCollection)
      expect(s_rc[0].name).to eql(resource.name)
    end
  end

  describe "provides access to the raw resources array" do
    it "returns the resources via the all_resources method" do
      expect(rc.all_resources).to equal(rc.instance_variable_get(:@resource_list).instance_variable_get(:@resources))
    end
  end

  describe "provides access to stepable iterator" do
    it "returns the iterator object" do
      rc.instance_variable_get(:@resource_list).instance_variable_set(:@iterator, :fooboar)
      expect(rc.iterator).to eq(:fooboar)
    end
  end

  def check_by_names(results, *names)
    names.each do |res_name|
      expect(results.detect{ |res| res.name == res_name }).not_to eql(nil)
    end
  end

  def load_up_resources
    %w{dog cat monkey}.each do |n|
       rc << Chef::Resource::ZenMaster.new(n)
    end
    rc << Chef::Resource::File.new("something")
  end

end