summaryrefslogtreecommitdiff
path: root/chef/spec/unit/node_spec.rb
blob: 605bb8b628241e72f5ecd93b3af462a83d40fe02 (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
#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# 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::Node, "new method" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
  end
  
  it "should create a new Chef::Node" do
     @node.should be_a_kind_of(Chef::Node)
  end
end

describe Chef::Node, "name" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
  end
  
  it "should allow you to set a name with name(something)" do
    lambda { @node.name("latte") }.should_not raise_error
  end
  
  it "should return the name with name()" do
    @node.name("latte")
    @node.name.should eql("latte")
  end
  
  it "should always have a string for name" do
    lambda { @node.name(Hash.new) }.should raise_error(ArgumentError)
  end
  
end

describe Chef::Node, "attributes" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
  end
  
  it "should have attributes" do
    @node.attribute.should be_a_kind_of(Hash)
  end
  
  it "should allow attributes to be accessed by name or symbol directly on node[]" do
    @node.attribute["locust"] = "something"
    @node[:locust].should eql("something")
    @node["locust"].should eql("something")
  end
  
  it "should return nil if it cannot find an attribute with node[]" do
    @node["secret"].should eql(nil)
  end
  
  it "should allow you to set an attribute via node[]=" do
    @node["secret"] = "shush"
    @node["secret"].should eql("shush")
  end
  
  it "should allow you to query whether an attribute exists with attribute?" do
    @node.attribute["locust"] = "something"
    @node.attribute?("locust").should eql(true)
    @node.attribute?("no dice").should eql(false)
  end

  it "should allow you to set an attribute via method_missing" do
    @node.sunshine "is bright"
    @node.attribute[:sunshine].should eql("is bright")
  end
  
  it "should allow you get get an attribute via method_missing" do
    @node.sunshine "is bright"
    @node.sunshine.should eql("is bright")
  end
  
  it "should raise an ArgumentError if you ask for an attribute that doesn't exist via method_missing" do
    lambda { @node.sunshine }.should raise_error(ArgumentError)
  end

  it "should allow you to iterate over attributes with each_attribute" do
    @node.sunshine "is bright"
    @node.canada "is a nice place"
    seen_attributes = Hash.new
    @node.each_attribute do |a,v|
      seen_attributes[a] = v
    end
    seen_attributes.should have_key(:sunshine)
    seen_attributes.should have_key(:canada)
    seen_attributes[:sunshine].should == "is bright"
    seen_attributes[:canada].should == "is a nice place"
  end

end

describe Chef::Node, "recipes" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
  end
  
  it "should have an array of recipes that should be applied" do
    @node.recipes.should be_a_kind_of(Array)
  end
  
  it "should allow you to query whether or not it has a recipe applied with recipe?" do
    @node.recipes << "sunrise"
    @node.recipe?("sunrise").should eql(true)
    @node.recipe?("not at home").should eql(false)
  end
  
  it "should allow you to set recipes with arguments" do
    @node.recipes "one", "two"
    @node.recipe?("one").should eql(true)
    @node.recipe?("two").should eql(true)
  end
  
end

describe Chef::Node, "from file" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
  end
  
  it "should load a node from a ruby file" do
    @node.from_file(File.join(File.dirname(__FILE__), "..", "data", "nodes", "test.rb"))
    @node.name.should eql("test.example.com short")
    @node.sunshine.should eql("in")
    @node.something.should eql("else")
    @node.recipes.should eql(["operations-master", "operations-monitoring"])
  end
  
  it "should raise an exception if the file cannot be found or read" do
    lambda { @node.from_file("/tmp/monkeydiving") }.should raise_error(IOError)
  end
end

describe Chef::Node, "find_file" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
  end
    
  it "should load a node from a file by fqdn" do
    @node.find_file("test.example.com")
    @node.name.should == "test.example.com"
  end
  
  it "should load a node from a file by hostname" do
    File.stub!(:exists?).and_return(true)
    File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.example.com.rb")).and_return(false)
    @node.find_file("test.example.com")
    @node.name.should == "test.example.com short"
  end
  
  it "should load a node from the default file" do
    File.stub!(:exists?).and_return(true)
    File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.example.com.rb")).and_return(false)
    File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.rb")).and_return(false)
    @node.find_file("test.example.com")
    @node.name.should == "test.example.com default"
  end
  
  it "should raise an ArgumentError if it cannot find any node file at all" do
    File.stub!(:exists?).and_return(true)
    File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.example.com.rb")).and_return(false)
    File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "test.rb")).and_return(false)
    File.should_receive(:exists?).with(File.join(Chef::Config[:node_path], "default.rb")).and_return(false)
    lambda { @node.find_file("test.example.com") }.should raise_error(ArgumentError)
  end
end

describe Chef::Node, "json" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
  end
  
  it "should serialize itself as json" do
    @node.find_file("test.example.com")
    json = @node.to_json()
    json.should =~ /json_class/
    json.should =~ /name/
    json.should =~ /attributes/
    json.should =~ /recipes/
  end
  
  it "should deserialize itself from json" do
    @node.find_file("test.example.com")
    json = @node.to_json
    serialized_node = JSON.parse(json)
    serialized_node.should be_a_kind_of(Chef::Node)
    serialized_node.name.should eql(@node.name)
    @node.each_attribute do |k,v|
      serialized_node[k].should eql(v)
    end
    serialized_node.recipes.should eql(@node.recipes)
  end
end

describe Chef::Node, "to_index" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
    @node.foo("bar")
  end
  
  it "should return a hash with :index attributes" do
    @node.name("airplane")
    @node.to_index.should == { :foo => "bar", :index_name => "node", :id => "node_airplane", :name => "airplane" }
  end
end


describe Chef::Node, "to_s" do
  before(:each) do
    Chef::Config.node_path(File.join(File.dirname(__FILE__), "..", "data", "nodes"))
    @node = Chef::Node.new()
  end
  
  it "should turn into a string like node[name]" do
    @node.name("airplane")
    @node.to_s.should eql("node[airplane]")
  end
end

describe Chef::Node, "list" do  
  before(:each) do
    mock_couch = mock("Chef::CouchDB")
    mock_couch.stub!(:list).and_return(
    {
      "rows" => [
        {
          "value" => "a",
          "key"   => "avenue"
        }
      ]
    }
    )
    Chef::CouchDB.stub!(:new).and_return(mock_couch)    
  end
  
  it "should retrieve a list of nodes from CouchDB" do
    Chef::Node.list.should eql(["avenue"])
  end
  
  it "should return just the ids if inflate is false" do
    Chef::Node.list(false).should eql(["avenue"])
  end
  
  it "should return the full objects if inflate is true" do
    Chef::Node.list(true).should eql(["a"])
  end
end

describe Chef::Node, "load" do
  it "should load a node from couchdb by name" do
    mock_couch = mock("Chef::CouchDB")
    mock_couch.should_receive(:load).with("node", "coffee").and_return(true)
    Chef::CouchDB.stub!(:new).and_return(mock_couch)
    Chef::Node.load("coffee")
  end
end

describe Chef::Node, "destroy" do
  it "should delete this node from couchdb" do
    mock_couch = mock("Chef::CouchDB")
    mock_couch.should_receive(:delete).with("node", "bob", 1).and_return(true)
    Chef::CouchDB.stub!(:new).and_return(mock_couch)
    node = Chef::Node.new
    node.name "bob"
    node.couchdb_rev = 1
    Chef::Queue.should_receive(:send_msg).with(:queue, :remove, node)
    node.destroy
  end
end

describe Chef::Node, "save" do
  before(:each) do
    @mock_couch = mock("Chef::CouchDB")
    @mock_couch.stub!(:store).and_return({ "rev" => 33 })
    Chef::CouchDB.stub!(:new).and_return(@mock_couch)
    Chef::Queue.stub!(:send_msg).and_return(true)
    @node = Chef::Node.new
    @node.name "bob"
    @node.couchdb_rev = 1
  end
  
  it "should save the node to couchdb" do
    Chef::Queue.should_receive(:send_msg).with(:queue, :index, @node)
    @mock_couch.should_receive(:store).with("node", "bob", @node).and_return({ "rev" => 33 })
    @node.save
  end
  
  it "should store the new couchdb_rev" do
    @node.save
    @node.couchdb_rev.should eql(33)
  end
end

describe Chef::Node, "create_design_document" do
  it "should create our design document" do
    mock_couch = mock("Chef::CouchDB")
    mock_couch.should_receive(:create_design_document).with("nodes", Chef::Node::DESIGN_DOCUMENT)
    Chef::CouchDB.stub!(:new).and_return(mock_couch)
    Chef::Node.create_design_document
  end
end