summaryrefslogtreecommitdiff
path: root/chef/spec/unit/shef/shef_session_spec.rb
blob: 73f76321f2c98f4e790255f0b11c8478ef1fe5c3 (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
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Copyright:: Copyright (c) 2009 Daniel DeLeo
# 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 'spec_helper'
require "ostruct"


class TestableShefSession < Shef::ShefSession

  def rebuild_node
    nil
  end

  def rebuild_collection
    nil
  end

  def loading
    nil
  end

  def loading_complete
    nil
  end

end

describe Shef::ShefSession do

  it "is a singleton object" do
    Shef::ShefSession.should include(Singleton)
  end

end

describe Shef::StandAloneSession do
  before do
    @session = Shef::StandAloneSession.instance
    @node = @session.node = Chef::Node.new
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = @session.run_context = Chef::RunContext.new(@node, {}, @events)
    @recipe = @session.recipe = Chef::Recipe.new(nil, nil, @run_context)
    Shef::Extensions.extend_context_recipe(@recipe)
  end

  it "has a run_context" do
    @session.run_context.should equal(@run_context)
  end

  it "returns a collection based on it's standalone recipe file" do
    @session.resource_collection.should == @recipe.run_context.resource_collection
  end

  it "gives nil for the definitions (for now)" do
    @session.definitions.should be_nil
  end

  it "gives nil for the cookbook_loader" do
    @session.cookbook_loader.should be_nil
  end

  it "runs chef with the standalone recipe" do
    @session.stub!(:node_built?).and_return(true)
    Chef::Log.stub!(:level)
    chef_runner = mock("Chef::Runner.new", :converge => :converged)
    # pre-heat resource collection cache
    @session.resource_collection

    Chef::Runner.should_receive(:new).with(@session.recipe.run_context).and_return(chef_runner)
    @recipe.run_chef.should == :converged
  end

end

describe Shef::SoloSession do
  before do
    Chef::Config[:shef_solo] = true
    @session = Shef::SoloSession.instance
    @node = Chef::Node.new
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = @session.run_context = Chef::RunContext.new(@node, {}, @events)
    @session.node = @node
    @recipe = @session.recipe = Chef::Recipe.new(nil, nil, @run_context)
    Shef::Extensions.extend_context_recipe(@recipe)
  end

  after do
    Chef::Config[:shef_solo] = nil
  end

  it "returns a collection based on it's compilation object and the extra recipe provided by shef" do
    @session.stub!(:node_built?).and_return(true)
    kitteh = Chef::Resource::Cat.new("keyboard")
    @recipe.run_context.resource_collection << kitteh
    @session.resource_collection.should include(kitteh)
  end

  it "returns definitions from it's compilation object" do
    @session.definitions.should == @run_context.definitions
  end

  it "keeps json attribs and passes them to the node for consumption" do
    @session.node_attributes = {"besnard_lakes" => "are_the_dark_horse"}
    @session.node.besnard_lakes.should == "are_the_dark_horse"
    #pending "1) keep attribs in an ivar 2) pass them to the node 3) feed them to the node on reset"
  end

  it "generates it's resource collection from the compiled cookbooks and the ad hoc recipe" do
    @session.stub!(:node_built?).and_return(true)
    kitteh_cat = Chef::Resource::Cat.new("kitteh")
    @run_context.resource_collection << kitteh_cat
    keyboard_cat = Chef::Resource::Cat.new("keyboard_cat")
    @recipe.run_context.resource_collection << keyboard_cat
    #@session.rebuild_collection
    @session.resource_collection.should include(kitteh_cat, keyboard_cat)
  end

  it "runs chef with a resource collection from the compiled cookbooks" do
    @session.stub!(:node_built?).and_return(true)
    Chef::Log.stub!(:level)
    chef_runner = mock("Chef::Runner.new", :converge => :converged)
    Chef::Runner.should_receive(:new).with(an_instance_of(Chef::RunContext)).and_return(chef_runner)

    @recipe.run_chef.should == :converged
  end

end