summaryrefslogtreecommitdiff
path: root/spec/unit/node_spec.rb
blob: 753d056404a1a3d0d478f2a5763b8fff8653ec70 (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
#
# 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::Node 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

  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
  
  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 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
  
  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 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
  
  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
  
  it "should load a node from a file by fqdn" do
    node = Chef::Node.find("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 = Chef::Node.find("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 = Chef::Node.find("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 { Chef::Node.find("test.example.com") }.should raise_error(ArgumentError)
  end

  it "should serialize itself as json" do
    node = Chef::Node.find("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
    original_node = Chef::Node.find("test.example.com")
    json = original_node.to_json
    serialized_node = JSON.parse(json)
    serialized_node.should be_a_kind_of(Chef::Node)
    serialized_node.name.should eql(original_node.name)
    original_node.each_attribute do |k,v|
      serialized_node[k].should eql(v)
    end
    serialized_node.recipes.should eql(original_node.recipes)
  end
  
  it "should return a list of node names based on which files are in the node_path" do
    list = Chef::Node.list
    list.should be_a_kind_of(Array)
    list[0].should == "default"
    list[1].should == "test.example.com"
    list[2].should == "test"
  end

  it "should turn into a string like node[name]" do
    @node.name("airplane")
    @node.to_s.should eql("node[airplane]")
  end

end