require 'spec_helper' describe Deplomat::Node do before(:each) do @fixtures_dir = Dir.pwd + "/spec/fixtures" @node = Deplomat::Node.new(logfile: "#{@fixtures_dir}/deplomat.log", log_to: [:file, :stdout]) end it "checks if file exists" do expect(@node.file_exists?("#{@fixtures_dir}/existing_file.txt")).to be_falsey expect(@node.file_exists?("/non-existent-path")).to be_falsey FileUtils.touch("#{@fixtures_dir}/existing_file.txt") expect(@node.file_exists?("#{@fixtures_dir}/existing_file.txt")).to be_truthy FileUtils.rm("#{@fixtures_dir}/existing_file.txt") end describe "logging" do after(:each) do @node.log_to = [] @node.remove("#{@fixtures_dir}/deplomat.log") end it "logs to the stdout" do expect($stdout).to receive(:print).exactly(4).times @node.execute("ls #{@fixtures_dir}/dir1") end it "logs to a log file" do @node.log_to = [:file] @node.execute("ls #{@fixtures_dir}/dir1") log = File.readlines("#{@fixtures_dir}/deplomat.log") expect(log.size).to eq(4) end it "puts additional messages into the terminal when required" do expect($stdout).to receive(:print).once.with("hello\n".white) expect($stdout).to receive(:print).once.with("--> ls #{@fixtures_dir}/dir1\n".white) expect($stdout).to receive(:print).once.with(" file1\n".light_black) expect($stdout).to receive(:print).once.with(" file2\n".light_black) expect($stdout).to receive(:print).once.with(" subdir1\n".light_black) expect($stdout).to receive(:print).once.with("bye\n".white) @node.execute("ls #{@fixtures_dir}/dir1", message: ["hello", "bye"]) end it "updates requisite counter" do requisites_counter_fn = "#{Dir.pwd}/spec/.deployment_requisites_counter" @node.cd Dir.pwd + "/spec" File.open(requisites_counter_fn, "w") { |f| f.puts 1 } @node.update_requisite_number!(2) expect(File.readlines(requisites_counter_fn).first.to_i).to eq(2) expect(-> { @node.update_requisite_number!(1) }).to raise_exception(Deplomat::RequisitesNumberError) File.unlink(requisites_counter_fn) expect(-> { @node.update_requisite_number!(3) }).to raise_exception(Deplomat::RequisitesNumberError) end end end