spec/lib/soloist/cli_spec.rb in soloist-1.0.0.pre vs spec/lib/soloist/cli_spec.rb in soloist-1.0.0

- old
+ new

@@ -1,15 +1,30 @@ require "spec_helper" describe Soloist::CLI do let(:cli) { Soloist::CLI.new } - let(:base_path) { Dir.mktmpdir } + let(:base_path) { RSpec.configuration.tempdir } let(:soloistrc_path) { File.expand_path("soloistrc", base_path) } - before { FileUtils.mkdir_p(base_path) } + before do + FileUtils.mkdir_p(base_path) + Soloist::Config.any_instance.stub(:exec) + end describe "#chef" do + it "receives the outside environment" do + FileUtils.touch(soloistrc_path) + Dir.chdir(base_path) do + ENV["AUTREYISM"] = "pathological-yodeling" + cli.soloist_config.should_receive(:exec) do |chef_solo| + `#{chef_solo}`.chomp.should == "pathological-yodeling" + end + cli.soloist_config.stub(:chef_solo).and_return('echo $AUTREYISM') + cli.chef + end + end + context "when the soloistrc file does not exist" do it "raises an error" do expect do begin Dir.chdir(base_path) { cli.chef } @@ -24,59 +39,88 @@ context "when the soloistrc file exists" do before do File.open(soloistrc_path, "w") do |file| file.write(YAML.dump("recipes" => ["stinky::feet"])) end + cli.soloist_config = nil + Dir.chdir(base_path) { cli.soloist_config.stub(:exec) } end it "runs the proper recipes" do - cli.stub(:exec) - Dir.chdir(base_path) { cli.chef } - cli.config.royal_crown.recipes.should =~ ["stinky::feet"] + cli.chef + cli.soloist_config.royal_crown.recipes.should =~ ["stinky::feet"] end + context "when a soloistrc_local file exists" do + let(:soloistrc_local_path) { File.expand_path("soloistrc_local", base_path) } + + before do + File.open(soloistrc_local_path, "w") do |file| + file.write(YAML.dump("recipes" => ["stinky::socks"])) + end + cli.soloist_config = nil + Dir.chdir(base_path) { cli.soloist_config.stub(:exec) } + end + + it "installs the proper recipes" do + cli.chef + cli.soloist_config.royal_crown.recipes.should =~ ["stinky::feet", "stinky::socks"] + end + end + context "when the Cheffile does not exist" do it "runs chef" do - cli.should_receive(:exec) - Dir.chdir(base_path) { cli.chef } + cli.soloist_config.should_receive(:exec) + cli.chef end it "does not run librarian" do - cli.stub(:exec) Librarian::Chef::Cli.should_not_receive(:with_environment) - Dir.chdir(base_path) { cli.chef } + cli.chef end end context "when the Cheffile exists" do let(:cli_instance) { double(:cli_instance) } - before do - FileUtils.touch(File.expand_path("Cheffile", base_path)) - cli.stub(:exec) - end + before { FileUtils.touch(File.expand_path("Cheffile", base_path)) } it "runs librarian" do Librarian::Chef::Cli.should_receive(:with_environment).and_yield Librarian::Chef::Cli.should_receive(:new).and_return(cli_instance) cli_instance.should_receive(:install) - Dir.chdir(base_path) { cli.chef } + cli.chef end - it "runs chef" do - cli.should_receive(:exec) - Dir.chdir(base_path) { cli.chef } + context "when the user is not root" do + it "creates the cache path using sudo" do + cli.soloist_config.should_receive(:exec) do |command| + command.should =~ /^sudo -E/ + end + cli.chef + end end + + context "when the user is root" do + before { Process.stub(:uid => 0) } + + it "creates the cache path" do + cli.soloist_config.should_receive(:exec) do |command| + command.should_not =~ /^sudo -E/ + end + cli.chef + end + end end end end - describe "#DO_IT_LIVE" do + describe "#run_recipe" do context "when the soloistrc does not exist" do it "raises an error" do expect do - Dir.chdir(base_path) { cli.DO_IT_LIVE("pineapple::wut") } + Dir.chdir(base_path) { cli.run_recipe("pineapple::wut") } end.to raise_error(Soloist::NotFound) end end context "when the soloistrc file exists" do @@ -87,47 +131,24 @@ end it "sets a recipe to run" do Dir.chdir(base_path) do cli.should_receive(:chef) - cli.DO_IT_LIVE("angst::teenage", "ennui::default") - cli.config.royal_crown.recipes.should =~ ["angst::teenage", "ennui::default"] + cli.run_recipe("angst::teenage", "ennui::default") + cli.soloist_config.royal_crown.recipes.should =~ ["angst::teenage", "ennui::default"] end end end end - describe "#ensure_chef_cache_path" do - context "when the cache path does not exist" do - before { File.stub(:directory? => false) } + describe "#config" do + let(:royal_crown) { Soloist::RoyalCrown.new(:node_attributes => {"a" => "b"}) } + let(:config) { Soloist::Config.new(royal_crown) } - it "creates the cache path" do - cli.should_receive(:system).with("sudo mkdir -p /var/chef/cache") - cli.ensure_chef_cache_path - end - end + before { cli.stub(:soloist_config => config) } - context "when the cache path exists" do - before { File.stub(:directory? => true) } - - it "does not create the cache path" do - cli.should_not_receive(:system) - cli.ensure_chef_cache_path - end - end - end - - describe "#chef_solo" do - before do - ENV["AUTREYISM"] = "pathological-yodeling" - FileUtils.touch(File.expand_path("soloistrc", base_path)) - end - - it "receives the outside environment" do - cli.stub(:chef_solo).and_return('echo $AUTREYISM') - cli.should_receive(:exec) do |chef_solo| - `#{chef_solo}`.chomp.should == "pathological-yodeling" - end - Dir.chdir(base_path) { cli.chef } + it "prints the hash render of the RoyalCrown" do + Kernel.should_receive(:ap).with({"recipes"=>[], "a" => "b"}) + cli.config end end end