summaryrefslogtreecommitdiff
path: root/chef/spec/unit/resource_spec.rb
blob: 698dec6fd172673b3d32a6952561bc51309207ca (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
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))

describe Chef::Resource do
  before(:each) do
    @resource = Chef::Resource.new("funk")
  end
  
  it "should create a new Chef::Resource" do
    @resource.should be_a_kind_of(Chef::Resource)
  end

  it "should have a name" do
    @resource.name.should eql("funk")
  end
  
  it "should let you set a new name" do
    @resource.name "monkey"
    @resource.name.should eql("monkey")
  end
  
  it "should not be valid without a name" do
    lambda { @resource.name false }.should raise_error(ArgumentError)
  end
  
  it "should always have a string for name" do
    lambda { @resource.name Hash.new }.should raise_error(ArgumentError)
  end
  
  it "should accept true or false for noop" do
    lambda { @resource.noop true }.should_not raise_error(ArgumentError)
    lambda { @resource.noop false }.should_not raise_error(ArgumentError)
    lambda { @resource.noop "eat it" }.should raise_error(ArgumentError)
  end
  
  it "should make notified resources appear in the actions hash" do
    @resource.collection << Chef::Resource::ZenMaster.new("coffee")
    @resource.notifies :reload, @resource.resources(:zen_master => "coffee")
    @resource.actions[:reload][:delayed][0].name.should eql("coffee")
  end
  
  it "should make notified resources be capable of acting immediately" do
    @resource.collection << Chef::Resource::ZenMaster.new("coffee")
    @resource.notifies :reload, @resource.resources(:zen_master => "coffee"), :immediate
    @resource.actions[:reload][:immediate][0].name.should eql("coffee")
  end
  
  it "should raise an exception if told to act in other than :delay or :immediate(ly)" do
    @resource.collection << Chef::Resource::ZenMaster.new("coffee")
    lambda { 
      @resource.notifies :reload, @resource.resources(:zen_master => "coffee"), :someday
    }.should raise_error(ArgumentError)
  end
  
  it "should allow multiple notified resources appear in the actions hash" do
    @resource.collection << Chef::Resource::ZenMaster.new("coffee")
    @resource.notifies :reload, @resource.resources(:zen_master => "coffee")
    @resource.actions[:reload][:delayed][0].name.should eql("coffee")
    @resource.collection << Chef::Resource::ZenMaster.new("beans")
    @resource.notifies :reload, @resource.resources(:zen_master => "beans")
    @resource.actions[:reload][:delayed][1].name.should eql("beans")
  end
  
  it "should make resources appear in the actions hash of subscribed nodes" do
    @resource.collection << Chef::Resource::ZenMaster.new("coffee")
    zr = @resource.resources(:zen_master => "coffee")
    @resource.subscribes :reload, zr
    zr.actions[:reload][:delayed][0].name.should eql("funk")
  end
  
  it "should make resources appear in the actions hash of subscribed nodes" do
    @resource.collection << Chef::Resource::ZenMaster.new("coffee")
    zr = @resource.resources(:zen_master => "coffee")
    @resource.subscribes :reload, zr
    zr.actions[:reload][:delayed][0].name.should eql("funk")
    
    @resource.collection << Chef::Resource::ZenMaster.new("bean")
    zrb = @resource.resources(:zen_master => "bean")
    zrb.subscribes :reload, zr
    zr.actions[:reload][:delayed][1].name.should eql("bean")
  end
  
  it "should make subscribed resources be capable of acting immediately" do
    @resource.collection << Chef::Resource::ZenMaster.new("coffee")
    zr = @resource.resources(:zen_master => "coffee")
    @resource.subscribes :reload, zr, :immediately
    zr.actions[:reload][:immediate][0].name.should eql("funk")
  end
  
  it "should return a value if not defined" do
    zm = Chef::Resource::ZenMaster.new("coffee")
    zm.something(true).should eql(true)
    zm.something.should eql(true)
    zm.something(false).should eql(false)
    zm.something.should eql(false)
  end
  
  it "should become a string like resource_name[name]" do
    zm = Chef::Resource::ZenMaster.new("coffee")
    zm.to_s.should eql("zen_master[coffee]")
  end
  
  it "should return the arguments passed with 'is'" do
    zm = Chef::Resource::ZenMaster.new("coffee")
    res = zm.is("one", "two", "three")
    res.should eql([ "one", "two", "three" ])
  end
  
  it "should allow arguments preceeded by is to methods" do
    @resource.noop(@resource.is(true))
    @resource.noop.should eql(true)
  end

  it "should serialize to json" do
    json = @resource.to_json
    json.should =~ /json_class/
    json.should =~ /instance_vars/
  end
  
  it "should deserialize itself from json" do
    json = @resource.to_json
    serialized_node = JSON.parse(json)
    serialized_node.should be_a_kind_of(Chef::Resource)
    serialized_node.name.should eql(@resource.name)
  end
  
  it "should allow you to set what features this resource supports" do
    support_hash = { :one => :two }
    @resource.supports(support_hash)
    @resource.supports.should eql(support_hash)
  end
  
  it "should return the current value of supports" do
    @resource.supports.should == {}
  end
  
  it "should default to throwing an error if a provider fails for a resource" do
    @resource.ignore_failure.should == false
  end
  
  it "should allow you to set whether a provider should throw exceptions with ignore_failure" do
    @resource.ignore_failure(true)
    @resource.ignore_failure.should == true
  end
  
  it "should allow you to epic_fail" do
    @resource.epic_fail(true)
    @resource.epic_fail.should == true
  end
  
end