~ubuntu-branches/ubuntu/karmic/chef/karmic

« back to all changes in this revision

Viewing changes to chef/spec/unit/application_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Timberman
  • Date: 2009-08-12 12:18:48 UTC
  • Revision ID: james.westby@ubuntu.com-20090812121848-yl38hpd7nfsl51xz
Tags: upstream-0.7.8
ImportĀ upstreamĀ versionĀ 0.7.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
 
3
# Copyright:: Copyright (c) 2008 Opscode, Inc.
 
4
# License:: Apache License, Version 2.0
 
5
#
 
6
# Licensed under the Apache License, Version 2.0 (the "License");
 
7
# you may not use this file except in compliance with the License.
 
8
# You may obtain a copy of the License at
 
9
 
10
#     http://www.apache.org/licenses/LICENSE-2.0
 
11
 
12
# Unless required by applicable law or agreed to in writing, software
 
13
# distributed under the License is distributed on an "AS IS" BASIS,
 
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
# See the License for the specific language governing permissions and
 
16
# limitations under the License.
 
17
 
 
18
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
 
19
 
 
20
describe Chef::Application, "initialize" do
 
21
  before do
 
22
    @app = Chef::Application.new
 
23
  end
 
24
  
 
25
  it "should create an instance of Chef::Application" do
 
26
    @app.should be_kind_of(Chef::Application)
 
27
  end
 
28
end
 
29
 
 
30
  
 
31
describe Chef::Application, "reconfigure" do
 
32
  before do
 
33
    @app = Chef::Application.new
 
34
    @app.stub!(:configure_chef).and_return(true)
 
35
    @app.stub!(:configure_logging).and_return(true)
 
36
  end
 
37
  
 
38
  it "should configure chef" do
 
39
    @app.should_receive(:configure_chef).and_return(true)
 
40
    @app.reconfigure
 
41
  end
 
42
      
 
43
  it "should configure logging" do
 
44
    @app.should_receive(:configure_logging).and_return(true)
 
45
    @app.reconfigure  
 
46
  end
 
47
  
 
48
end
 
49
  
 
50
describe Chef::Application do
 
51
  before do
 
52
    @app = Chef::Application.new
 
53
  end
 
54
  
 
55
  describe "run" do
 
56
    before do
 
57
      @app.stub!(:setup_application).and_return(true)
 
58
      @app.stub!(:run_application).and_return(true)
 
59
      @app.stub!(:configure_chef).and_return(true)
 
60
      @app.stub!(:configure_logging).and_return(true)
 
61
    end
 
62
    
 
63
    it "should reconfigure the application before running" do
 
64
      @app.should_receive(:reconfigure).and_return(true)
 
65
      @app.run
 
66
    end
 
67
    
 
68
    it "should setup the application before running it" do
 
69
      @app.should_receive(:setup_application).and_return(true)
 
70
      @app.run
 
71
    end
 
72
    
 
73
    it "should run the actual application" do
 
74
      @app.should_receive(:run_application).and_return(true)
 
75
      @app.run
 
76
    end
 
77
  end
 
78
end
 
79
 
 
80
describe Chef::Application, "configure_chef" do
 
81
  before do
 
82
    @app = Chef::Application.new
 
83
    Chef::Config.stub!(:merge!).and_return(true)
 
84
    @app.stub!(:parse_options).and_return(true)
 
85
  end
 
86
  
 
87
  it "should parse the commandline options" do
 
88
    @app.should_receive(:parse_options).and_return(true)
 
89
    @app.configure_chef
 
90
  end
 
91
  
 
92
  describe "when a config_file is present" do
 
93
    before do
 
94
      @app.config[:config_file] = "/etc/chef/default.rb"
 
95
      File.stub!(:exists?).and_return(true)
 
96
      File.stub!(:readable?).and_return(true)
 
97
    end
 
98
    
 
99
    it "should configure chef::config from a file" do
 
100
      Chef::Config.should_receive(:from_file).with("/etc/chef/default.rb")
 
101
      @app.configure_chef
 
102
    end
 
103
  end
 
104
  
 
105
  describe "when there is no config_file defined" do
 
106
    before do
 
107
      @app.config[:config_file] = nil
 
108
    end
 
109
    
 
110
    it "should configure chef::config from a file" do
 
111
      Chef::Config.should_not_receive(:from_file).with("/etc/chef/default.rb")
 
112
      @app.configure_chef
 
113
    end
 
114
  end
 
115
  
 
116
  
 
117
  it "should merge the local config hash into chef::config" do
 
118
    Chef::Config.should_receive(:merge!).and_return(true)
 
119
    @app.configure_chef
 
120
  end
 
121
 
 
122
end
 
123
 
 
124
describe Chef::Application, "configure_logging" do
 
125
  before do
 
126
    @app = Chef::Application.new
 
127
    Chef::Log.stub!(:init)
 
128
    Chef::Log.stub!(:level)
 
129
  end
 
130
  
 
131
  it "should initialise the chef logger" do
 
132
    Chef::Log.should_receive(:init).with(Chef::Config[:log_location]).and_return(true)
 
133
    @app.configure_logging
 
134
  end
 
135
 
 
136
  it "should initialise the chef logger level" do
 
137
    Chef::Log.should_receive(:level).with(Chef::Config[:log_level]).and_return(true)
 
138
    @app.configure_logging
 
139
  end
 
140
 
 
141
end
 
142
 
 
143
describe Chef::Application, "class method: fatal!" do
 
144
  before do
 
145
    STDERR.stub!(:puts).with("FATAL: blah").and_return(true)
 
146
    Chef::Log.stub!(:fatal).with("blah").and_return(true)
 
147
    Process.stub!(:exit).and_return(true)
 
148
  end
 
149
  
 
150
  it "should log an error message to the logger" do
 
151
    Chef::Log.should_receive(:fatal).with("blah").and_return(true)
 
152
    Chef::Application.fatal! "blah"
 
153
  end
 
154
 
 
155
  it "should log an error message on STDERR" do
 
156
    STDERR.should_receive(:puts).with("FATAL: blah").and_return(true)
 
157
    Chef::Application.fatal! "blah"
 
158
  end
 
159
  
 
160
  describe "when an exit code is supplied" do
 
161
    it "should exit with the given exit code" do
 
162
      Process.should_receive(:exit).with(-100).and_return(true)
 
163
      Chef::Application.fatal! "blah", -100
 
164
    end
 
165
  end
 
166
  
 
167
  describe "when an exit code is not supplied" do
 
168
    it "should exit with the default exit code" do
 
169
      Process.should_receive(:exit).with(-1).and_return(true)
 
170
      Chef::Application.fatal! "blah"
 
171
    end
 
172
  end
 
173
  
 
174
end
 
175
 
 
176
describe Chef::Application, "setup_application" do
 
177
  before do
 
178
    @app = Chef::Application.new
 
179
  end
 
180
  
 
181
  it "should raise an error" do
 
182
    lambda { @app.setup_application }.should raise_error(Chef::Exceptions::Application)
 
183
  end
 
184
end
 
185
 
 
186
describe Chef::Application, "run_application" do
 
187
  before do
 
188
    @app = Chef::Application.new
 
189
  end
 
190
  
 
191
  it "should raise an error" do
 
192
    lambda { @app.run_application }.should raise_error(Chef::Exceptions::Application)
 
193
  end
 
194
end