summaryrefslogtreecommitdiff
path: root/spec/unit/environment_spec.rb
diff options
context:
space:
mode:
authorNick Stielau <nick.stielau@gmail.com>2013-01-08 08:11:41 -0800
committerBryan McLellan <btm@opscode.com>2013-05-30 09:41:13 -0700
commit9ca147c954ff5e93c2c5d80263486bbfbbbc4666 (patch)
treefc4b577de65490ca4056b5ec9eb6fea79381d3f8 /spec/unit/environment_spec.rb
parent8813848a337e8d0bb93aca57edb022f3c2d1df88 (diff)
downloadchef-9ca147c954ff5e93c2c5d80263486bbfbbbc4666.tar.gz
Adding support for environments when running under chef-solo.
This commit refactors and rebases Kyle Goodwin's original work for CHEF-3356, as per Bryan McLellan's 12/17 comments.
Diffstat (limited to 'spec/unit/environment_spec.rb')
-rw-r--r--spec/unit/environment_spec.rb79
1 files changed, 79 insertions, 0 deletions
diff --git a/spec/unit/environment_spec.rb b/spec/unit/environment_spec.rb
index 99232dff08..8bbf003e6a 100644
--- a/spec/unit/environment_spec.rb
+++ b/spec/unit/environment_spec.rb
@@ -2,6 +2,7 @@
# Author:: Stephen Delano (<stephen@ospcode.com>)
# Author:: Seth Falcon (<seth@ospcode.com>)
# Author:: John Keiser (<jkeiser@ospcode.com>)
+# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
# Copyright:: Copyright 2010-2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
@@ -360,4 +361,82 @@ describe Chef::Environment do
end
end
+ describe "when loading" do
+ describe "in solo mode" do
+ before do
+ Chef::Config[:solo] = true
+ Chef::Config[:environment_path] = '/var/chef/environments'
+ end
+
+ after do
+ Chef::Config[:solo] = false
+ end
+
+ it "should get the environment from the environment_path" do
+ File.should_receive(:directory?).with(Chef::Config[:environment_path]).and_return(true)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:environment_path], 'foo.json')).and_return(false)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:environment_path], 'foo.rb')).exactly(2).times.and_return(true)
+ File.should_receive(:readable?).with(File.join(Chef::Config[:environment_path], 'foo.rb')).and_return(true)
+ role_dsl="name \"foo\"\ndescription \"desc\"\n"
+ IO.should_receive(:read).with(File.join(Chef::Config[:environment_path], 'foo.rb')).and_return(role_dsl)
+ Chef::Environment.load('foo')
+ end
+
+ it "should return a Chef::Environment object from JSON" do
+ File.should_receive(:directory?).with(Chef::Config[:environment_path]).and_return(true)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:environment_path], 'foo.json')).and_return(true)
+ environment_hash = {
+ "name" => "foo",
+ "default_attributes" => {
+ "foo" => {
+ "bar" => 1
+ }
+ },
+ "json_class" => "Chef::Environment",
+ "description" => "desc",
+ "chef_type" => "environment"
+ }
+ IO.should_receive(:read).with(File.join(Chef::Config[:environment_path], 'foo.json')).and_return(JSON.dump(environment_hash))
+ environment = Chef::Environment.load('foo')
+
+ environment.should be_a_kind_of(Chef::Environment)
+ environment.name.should == environment_hash['name']
+ environment.description.should == environment_hash['description']
+ environment.default_attributes.should == environment_hash['default_attributes']
+ end
+
+ it "should return a Chef::Environment object from Ruby DSL" do
+ File.should_receive(:directory?).with(Chef::Config[:environment_path]).and_return(true)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:environment_path], 'foo.json')).and_return(false)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:environment_path], 'foo.rb')).exactly(2).times.and_return(true)
+ File.should_receive(:readable?).with(File.join(Chef::Config[:environment_path], 'foo.rb')).and_return(true)
+ role_dsl="name \"foo\"\ndescription \"desc\"\n"
+ IO.should_receive(:read).with(File.join(Chef::Config[:environment_path], 'foo.rb')).and_return(role_dsl)
+ environment = Chef::Environment.load('foo')
+
+ environment.should be_a_kind_of(Chef::Environment)
+ environment.name.should == 'foo'
+ environment.description.should == 'desc'
+ end
+
+ it 'should raise an error if the configured environment_path is invalid' do
+ File.should_receive(:directory?).with(Chef::Config[:environment_path]).and_return(false)
+
+ lambda {
+ Chef::Environment.load('foo')
+ }.should raise_error Chef::Exceptions::InvalidEnvironmentPath, "Environment path '/var/chef/environments' is invalid"
+ end
+
+ it 'should raise an error if the file does not exist' do
+ File.should_receive(:directory?).with(Chef::Config[:environment_path]).and_return(true)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:environment_path], 'foo.json')).and_return(false)
+ File.should_receive(:exists?).with(File.join(Chef::Config[:environment_path], 'foo.rb')).and_return(false)
+
+ lambda {
+ Chef::Environment.load('foo')
+ }.should raise_error Chef::Exceptions::EnvironmentNotFound, "Environment 'foo' could not be loaded from disk"
+ end
+ end
+ end
+
end