summaryrefslogtreecommitdiff
path: root/spec
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
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')
-rw-r--r--spec/unit/config_spec.rb11
-rw-r--r--spec/unit/environment_spec.rb79
-rw-r--r--spec/unit/exceptions_spec.rb5
3 files changed, 94 insertions, 1 deletions
diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb
index f35b47d50a..a8677a9a06 100644
--- a/spec/unit/config_spec.rb
+++ b/spec/unit/config_spec.rb
@@ -1,5 +1,6 @@
#
# Author:: Adam Jacob (<adam@opscode.com>)
+# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
@@ -208,6 +209,16 @@ describe Chef::Config do
Chef::Config.platform_specific_path("/var/chef/data_bags")
Chef::Config[:data_bag_path].should == data_bag_path
end
+
+ it "Chef::Config[:environment_path] defaults to /var/chef/environments" do
+ environment_path = if windows?
+ "C:\\chef\\environments"
+ else
+ "/var/chef/environments"
+ end
+
+ Chef::Config[:environment_path].should == environment_path
+ end
end
describe "Chef::Config[:user_home]" do
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
diff --git a/spec/unit/exceptions_spec.rb b/spec/unit/exceptions_spec.rb
index a979d2f6b9..fdf515de54 100644
--- a/spec/unit/exceptions_spec.rb
+++ b/spec/unit/exceptions_spec.rb
@@ -1,6 +1,7 @@
#
# Author:: Thomas Bishop (<bishop.thomas@gmail.com>)
# Author:: Christopher Walters (<cw@opscode.com>)
+# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
# Copyright:: Copyright (c) 2010 Thomas Bishop
# Copyright:: Copyright (c) 2010 Opscode, Inc.
# License:: Apache License, Version 2.0
@@ -62,7 +63,9 @@ describe Chef::Exceptions do
Chef::Exceptions::ResourceNotFound => RuntimeError,
Chef::Exceptions::InvalidResourceSpecification => ArgumentError,
Chef::Exceptions::SolrConnectionError => RuntimeError,
- Chef::Exceptions::InvalidDataBagPath => ArgumentError
+ Chef::Exceptions::InvalidDataBagPath => ArgumentError,
+ Chef::Exceptions::InvalidEnvironmentPath => ArgumentError,
+ Chef::Exceptions::EnvironmentNotFound => RuntimeError
}
exception_to_super_class.each do |exception, expected_super_class|