summaryrefslogtreecommitdiff
path: root/spec/unit/runner_spec.rb
blob: f73d5efb2271b28c828fc40703e150756b1c85a8 (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
#
# 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::Runner do
  before(:each) do
    @mock_node = mock("Node", :null_object => true)
    @mock_collection = mock("Resource Collection", :null_object => true)
    @mock_provider = mock("Provider", :null_object => true)
    @mock_resource = mock("Resource", :null_object => true)
    new_runner
  end
  
  it "should require a Node and a ResourceCollection" do
    @mock_node.should_receive(:kind_of?).once.and_return(true)
    @mock_collection.should_receive(:kind_of?).once.and_return(true)
    runner = Chef::Runner.new(@mock_node, @mock_collection)
    runner.should be_a_kind_of(Chef::Runner)
  end
  
  it "should raise an exception if you pass the wrong kind of object to new" do
    @mock_node.stub!(:kind_of?).and_return(false)
    @mock_collecton.stub!(:kind_of?).and_return(false)
    lambda { Chef::Runner.new(@mock_node, @mock_collection) }.should raise_error(ArgumentError)    
  end
  
  it "should pass each resource in the collection to a provider" do
    @collection.should_receive(:each).once
    @runner.converge
  end
  
  it "should use the provider specified by the resource (if it has one)" do
    provider = Chef::Provider::Easy.new(@node, @collection[0])
    @collection[0].should_receive(:provider).once.and_return(Chef::Provider::Easy)
    Chef::Provider::Easy.should_receive(:new).once.and_return(provider)
    @runner.converge
  end
  
  it "should use the platform provider if it has one" do
    Chef::Platform.should_receive(:find_provider_for_node).once.and_return(Chef::Provider::SnakeOil)
    @runner.converge
  end
  
  it "should run the action for each resource" do
    Chef::Platform.should_receive(:find_provider_for_node).once.and_return(Chef::Provider::SnakeOil)
    provider = Chef::Provider::SnakeOil.new(@node, @collection[0])
    provider.should_receive(:action_sell).once.and_return(true)
    Chef::Provider::SnakeOil.should_receive(:new).once.and_return(provider)
    @runner.converge
  end
  
  it "should execute immediate actions on changed resources" do
    Chef::Platform.should_receive(:find_provider_for_node).exactly(3).times.and_return(Chef::Provider::SnakeOil)
    provider = Chef::Provider::SnakeOil.new(@node, @collection[0])
    Chef::Provider::SnakeOil.should_receive(:new).exactly(3).times.and_return(provider)   
    @collection << Chef::Resource::Cat.new("peanut", @collection)
    @collection[1].notifies :buy, @collection[0], :immediately
    @collection[1].updated = true
    provider.should_receive(:action_buy).once.and_return(true)
    @runner.converge
  end
  
  it "should execute delayed actions on changed resources" do
    Chef::Platform.should_receive(:find_provider_for_node).exactly(3).times.and_return(Chef::Provider::SnakeOil)
    provider = Chef::Provider::SnakeOil.new(@node, @collection[0])
    Chef::Provider::SnakeOil.should_receive(:new).exactly(3).times.and_return(provider)   
    @collection << Chef::Resource::Cat.new("peanut", @collection)
    @collection[1].notifies :buy, @collection[0], :delayed
    @collection[1].updated = true
    provider.should_receive(:action_buy).once.and_return(true)
    @runner.converge
  end
  
  def new_runner
    @node = Chef::Node.new
    @node.name "latte"
    @node.operatingsystem "mac_os_x"
    @node.operatingsystemversion "10.5.1"
    @collection = Chef::ResourceCollection.new()
    @collection << Chef::Resource::Cat.new("loulou", @collection)
    Chef::Platform.set(
      :resource => :cat,
      :provider => Chef::Provider::SnakeOil
    )
    @runner = Chef::Runner.new(@node, @collection)
  end
end