summaryrefslogtreecommitdiff
path: root/packages/chef-server/spec/controllers/nodes_spec.rb
blob: a86fa4138cc155f0a47bb28c9652af04bfdca5ff (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
require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')

describe Nodes, "index action" do  
  it "should get a list of all the nodes" do
    Chef::Node.should_receive(:list).and_return(["one"])
    dispatch_to(Nodes, :index) do |c|
      c.stub!(:display)
    end
  end
  
  it "should send a list of nodes to display" do
    Chef::Node.stub!(:list).and_return(["one"])
    dispatch_to(Nodes, :index) do |c|
      c.should_receive(:display).with(["one"])
    end
  end
end

describe Nodes, "show action" do  
  it "should load a node from the filestore based on the id" do
    node = stub("Node", :null_object => true)
    Chef::Node.should_receive(:load).with("bond").once.and_return(node)
    dispatch_to(Nodes, :show, { :id => "bond" }) do |c|
      c.should_receive(:display).with(node).once.and_return(true)
    end
  end
  
  it "should return 200 on a well formed request" do
    node = stub("Node", :null_object => true)
     Chef::Node.should_receive(:load).with("bond").once.and_return(node)
     controller = dispatch_to(Nodes, :show, { :id => "bond" }) do |c|
       c.stub!(:display)
     end
     controller.status.should eql(200)
  end
  
  it "should raise a BadRequest if the id is not found" do
    Chef::Node.should_receive(:load).with("bond").once.and_raise(RuntimeError)
    lambda { 
      dispatch_to(Nodes, :show, { :id => "bond" }) 
    }.should raise_error(Merb::ControllerExceptions::BadRequest)
  end
end

describe Nodes, "create action" do
  it "should create a node from an inflated object" do
    mnode = mock("Node", :null_object => true)
    mnode.stub!(:name).and_return("bond")
    mnode.should_receive(:save).once.and_return(true)
    controller = dispatch_to(Nodes, :create) do |c|
      c.stub!(:params).and_return({ "inflated_object" => mnode })
      c.stub!(:session).and_return({
        :openid => 'http://localhost/openid/server/node/bond',
        :level => :node,
        :node_name => "bond",
      })
      c.stub!(:display)
    end
    controller.status.should eql(202)
  end
  
  it "should raise an exception if it cannot inflate an object" do
    lambda { 
      dispatch_to(Nodes, :create) do |c|
        c.stub!(:params).and_return({ })
      end
    }.should raise_error(Merb::Controller::BadRequest)
  end
end

describe Nodes, "update action" do
  it "should update a node from an inflated object" do
    mnode = mock("Node", :null_object => true)
    mnode.stub!(:name).and_return("one")
    Chef::FileStore.should_receive(:store).with("node", "one", mnode).once.and_return(true)
    controller = dispatch_to(Nodes, :update, { :id => "one" }) do |c|
      c.stub!(:session).and_return({
        :openid => 'http://localhost/openid/server/node/one',
        :level => :node,
        :node_name => "one",
      })
      c.stub!(:params).and_return({ "inflated_object" => mnode })
      c.stub!(:display)
    end
    controller.status.should eql(202)
  end
  
  it "should raise an exception if it cannot inflate an object" do
    lambda { dispatch_to(Nodes, :update) }.should raise_error(Merb::Controller::BadRequest)
  end
end

describe Nodes, "destroy action" do
  def do_destroy
    dispatch_to(Nodes, :destroy, { :id => "one" }) do |c|
      c.stub!(:display)
    end
  end
  
  it "should load the node it's about to destroy from the filestore" do
    mnode = stub("Node", :null_object => true)
    Chef::FileStore.should_receive(:load).with("node", "one").once.and_return(mnode)
    Chef::FileStore.stub!(:delete)
    do_destroy
  end
  
  it "should raise an exception if it cannot find the node to destroy" do
    Chef::FileStore.should_receive(:load).with("node", "one").once.and_raise(RuntimeError)
    lambda { do_destroy }.should raise_error(Merb::Controller::BadRequest)
  end
  
  it "should remove the node from the filestore" do
    mnode = stub("Node", :null_object => true)
    Chef::FileStore.stub!(:load).with("node", "one").and_return(mnode)
    Chef::FileStore.should_receive(:delete).with("node", "one")
    do_destroy
  end
  
  it "should remove the node from the search index" do
    mnode = stub("Node", :null_object => true)
    Chef::FileStore.stub!(:load).with("node", "one").and_return(mnode)
    Chef::FileStore.stub!(:delete)
    do_destroy
  end
  
  it "should return the node it just deleted" do
    mnode = stub("Node", :null_object => true)
    Chef::FileStore.stub!(:load).with("node", "one").and_return(mnode)
    Chef::FileStore.stub!(:delete)
    dispatch_to(Nodes, :destroy, { :id => "one" }) do |c|
       c.should_receive(:display).once.with(mnode)
    end
  end
  
  it "should return a status of 202" do
    mnode = stub("Node", :null_object => true)
    Chef::FileStore.stub!(:load).with("node", "one").and_return(mnode)
    Chef::FileStore.stub!(:delete)
    controller = do_destroy
    controller.status.should eql(202)
  end
end

describe Nodes, "compile action" do
  before(:each) do
    @compile = stub("Compile", :null_object => true)
    @node = stub("Node", :null_object => true)
    @node.stub!(:[]).and_return(true)
    @node.stub!(:[]=).and_return(true)
    @node.stub!(:recipes).and_return([])
    @compile.stub!(:load_definitions).and_return(true)
    @compile.stub!(:load_recipes).and_return(true)
    @compile.stub!(:collection).and_return([])
    @compile.stub!(:node, @node)
    @compile.stub!(:load_node).and_return(true)
    @stored_node = stub("Stored Node", :null_object => true)
  end
  
  def do_compile
    Chef::FileStore.stub!(:store).and_return(true)
    Chef::FileStore.stub!(:load).and_return(@stored_node)
    Chef::Compile.stub!(:new).and_return(@compile)
    dispatch_to(Nodes, :compile, { :id => "one" }) do |c|
      c.stub!(:display)
    end
  end
  
  it "should load the node from the node resource" do
    @compile.should_receive(:load_node).with("one").and_return(true)
    do_compile
  end
  
  it "should merge the data with the currently stored node" do
    node1 = Chef::Node.new
    node1.name "adam"
    node1.music "crowe"
    node1.recipes << "monkey"
    @compile.stub!(:node).and_return(node1)
    @stored_node = Chef::Node.new
    @stored_node.name "adam"
    @stored_node.music "crown"
    @stored_node.woot "woot"
    @stored_node.recipes << "monkeysoup"
    do_compile
    node1.name.should eql("adam")
    node1.music.should eql("crown")
    node1.woot.should eql("woot")
    node1.recipes.should eql([ "monkey", "monkeysoup" ])
  end
  
  it "should load definitions" do
    @compile.should_receive(:load_definitions)
    do_compile
  end
  
  it "should load recipes" do
    @compile.should_receive(:load_recipes)
    do_compile
  end
  
  it "should display the collection and node object" do
    Chef::FileStore.stub!(:load).and_return(@stored_node)
    Chef::Compile.stub!(:new).and_return(@compile)
    dispatch_to(Nodes, :compile, { :id => "one" }) do |c|
      c.should_receive(:display).with({ :collection => [], :node => nil })
    end
  end
  
  it "should return 200" do
    controller = do_compile
    controller.status.should eql(200)
  end
  
end