summaryrefslogtreecommitdiff
path: root/spec/unit/resource_collection_spec.rb
blob: 10f7d43cdef1e2f21b4b25cb22537533c8b5f74d (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
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
# 
# This program and entire repository is free software; you can
# redistribute it and/or modify it under the terms of the GNU 
# General Public License as published by the Free Software 
# Foundation; either version 2 of the License, or any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))

describe Chef::ResourceCollection do
  
  before(:each) do
    @rc = Chef::ResourceCollection.new()
    @resource = Chef::Resource::ZenMaster.new("makoto")
  end
  
  it "should return a Chef::ResourceCollection" do
    @rc.should be_kind_of(Chef::ResourceCollection)
  end
  
  it "should accept Chef::Resources" do
    lambda { @rc[0] = @resource }.should_not raise_error
    lambda { @rc[0] = "string" }.should raise_error
  end
  
  it "should accept Chef::Resources through pushing" do
    lambda { @rc.push(@resource) }.should_not raise_error
    lambda { @rc.push("string") }.should raise_error
  end
  
  it "should allow you to fetch Chef::Resources by position" do
    @rc[0] = @resource
    @rc[0].should eql(@resource)
  end
  
  it "should accept the << operator" do
    lambda { @rc << @resource }.should_not raise_error
  end
  
  it "should allow you to iterate over every resource in the collection" do
    load_up_resources
    results = Array.new
    lambda { 
      @rc.each do |r|
        results << r.name
      end
    }.should_not raise_error
    results.each_index do |i|
      case i
      when 0
        results[i].should eql("dog")
      when 1
        results[i].should eql("cat")
      when 2
        results[i].should eql("monkey")
      end
    end
  end
  
  it "should allow you to iterate over every resource by index" do
    load_up_resources
    results = Array.new
    lambda { 
      @rc.each_index do |i|
        results << @rc[i].name
      end 
    }.should_not raise_error()
    results.each_index do |i|
      case i
      when 0
        results[i].should eql("dog")
      when 1
        results[i].should eql("cat")
      when 2
        results[i].should eql("monkey")
      end
    end
  end
  
  it "should allow you to find resources by name via lookup" do
    zmr = Chef::Resource::ZenMaster.new("dog")
    @rc << zmr
    @rc.lookup(zmr.to_s).should eql(zmr)

    zmr = Chef::Resource::ZenMaster.new("cat")
    @rc[0] = zmr
    @rc.lookup(zmr).should eql(zmr)
    
    zmr = Chef::Resource::ZenMaster.new("monkey")
    @rc.push(zmr)
    @rc.lookup(zmr).should eql(zmr)
  end
  
  it "should raise an exception if you send something strange to lookup" do
    lambda { @rc.lookup(:symbol) }.should raise_error(ArgumentError)
  end
  
  it "should raise an exception if it cannot find a resource with lookup" do
    lambda { @rc.lookup("zen_master[dog]") }.should raise_error(ArgumentError)
  end

  it "should find a resource by symbol and name (:zen_master => monkey)" do
    load_up_resources
    @rc.resources(:zen_master => "monkey").name.should 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" ])
    results.length.should 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")
    results.length.should eql(2)
    check_by_names(results, "monkey", "something")
  end

  it "should find a resource by string zen_master[a]" do
    load_up_resources
    @rc.resources("zen_master[monkey]").name.should 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]")
    results.length.should 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]")
    results.length.should eql(2)
    check_by_names(results, "monkey", "something")
  end
  
  it "should raise an exception if you pass a bad name to resources" do
    lambda { @rc.resources("michael jackson") }.should raise_error(ArgumentError)    
  end
  
  it "should raise an exception if you pass something other than a string or hash to resource" do
    lambda { @rc.resources([Array.new]) }.should raise_error(ArgumentError)
  end

  def check_by_names(results, *names)
    names.each do |res_name|
      results.detect{ |res| res.name == res_name }.should_not 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