Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 171 lines (147 sloc) 5.18 kb
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
require "spec_helper"

describe Soloist::CLI do
  let(:cli) { Soloist::CLI.new }
  let(:base_path) { RSpec.configuration.tempdir }
  let(:soloistrc_path) { File.expand_path("soloistrc", 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 }
          rescue Soloist::NotFound => e
            e.message.should == "Could not find soloistrc or .soloistrc"
            raise
          end
        end.to raise_error(Soloist::NotFound)
      end
    end

    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.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.soloist_config.should_receive(:exec)
          cli.chef
        end

        it "does not run librarian" do
          Librarian::Chef::Cli.should_not_receive(:with_environment)
          cli.chef
        end
      end

      context "when the Cheffile exists" do
        let(:cli_instance) { double(:cli_instance) }

        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)
          cli.chef
        end

        context "when the user is not root" do
          context "when rvm is not present" do
            before do
              cli.soloist_config.stub(:rvm?).and_return(false)
            end
            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 rvm is present" do
            before do
              cli.soloist_config.stub(:rvm?).and_return(true)
            end
            it "creates the cache path using rvmsudo" do
              cli.soloist_config.should_receive(:exec) do |command|
                command.should =~ /rvmsudo -E/
              end
              cli.chef
            end
          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 "#run_recipe" do
    context "when the soloistrc does not exist" do
      it "raises an error" do
        expect do
          Dir.chdir(base_path) { cli.run_recipe("pineapple::wut") }
        end.to raise_error(Soloist::NotFound)
      end
    end

    context "when the soloistrc file exists" do
      before do
        File.open(soloistrc_path, "w") do |file|
          file.write(YAML.dump("recipes" => ["pineapple::wutcake"]))
        end
      end

      it "sets a recipe to run" do
        Dir.chdir(base_path) do
          cli.should_receive(:chef)
          cli.run_recipe("angst::teenage", "ennui::default")
          cli.soloist_config.royal_crown.recipes.should =~ ["angst::teenage", "ennui::default"]
        end
      end
    end
  end

  describe "#config" do
    let(:royal_crown) { Soloist::RoyalCrown.new(:node_attributes => {"a" => "b"}) }
    let(:config) { Soloist::Config.new(royal_crown) }

    before { cli.stub(:soloist_config => config) }

    it "prints the hash render of the RoyalCrown" do
      Kernel.should_receive(:ap).with({"recipes"=>[], "a" => "b"})
      cli.config
    end
  end
end
Something went wrong with that request. Please try again.