require 'spec_helper' describe Deplomat::RemoteNode do before(:all) do @fixtures_dir = "/home/deploy/deplomat" @local_fixtures_dir = Dir.pwd + "/spec/fixtures" @node = Deplomat::RemoteNode.new(host: "deplomat-test-node", user: "deploy") @node.log_to = [] end before(:each) do @node.raise_exceptions = true end after(:all) do @node.close end after(:each) do @node.raise_exceptions = false @node.execute("rm -rf #{@fixtures_dir}/dir2/*") @node.execute("rm -rf #{@fixtures_dir}/uploaded1/*") @node.execute("rm -rf #{@fixtures_dir}/uploaded2/*") end describe "filesystem operations" do it "copies files to another folder" do @node.copy("#{@fixtures_dir}/dir1/*", "#{@fixtures_dir}/dir2/") expect(@node.execute("ls #{@fixtures_dir}/dir2/")[:out]).to have_files("file1", "file2", "subdir1") end it "moves files and dirs from one directory into another" do @node.move("#{@fixtures_dir}/dir1/*", "#{@fixtures_dir}/dir2/") expect(@node.execute("ls #{@fixtures_dir}/dir1/")[:out]).to eq("") expect(@node.execute("ls #{@fixtures_dir}/dir2/")[:out]).to have_files("file1", "file2", "subdir1") @node.move("#{@fixtures_dir}/dir2/*", "#{@fixtures_dir}/dir1/") expect(@node.execute("ls #{@fixtures_dir}/dir2/")[:out]).to eq("") expect(@node.execute("ls #{@fixtures_dir}/dir1/")[:out]).to have_files("file1", "file2", "subdir1") end it "removes files and dirs" do @node.copy("#{@fixtures_dir}/dir1/*", "#{@fixtures_dir}/dir2/") expect(@node.execute("ls #{@fixtures_dir}/dir2/")[:out]).to have_files("file1", "file2", "subdir1") @node.remove("#{@fixtures_dir}/dir2/*") expect(@node.execute("ls #{@fixtures_dir}/dir2/")[:out]).to eq("") end it "uploads files from localhost to the node" do @node.upload("#{@local_fixtures_dir}/upload1/*", "#{@fixtures_dir}/uploaded1/", except: 'except1') expect(@node.execute("ls #{@fixtures_dir}/uploaded1/")[:out]).to have_files("uploaded_file1", "uploaded_file2") expect(@node.execute("ls #{@fixtures_dir}/uploaded1/")[:out]).to not_have_files("except1") end it "changes current directory" do #@node.cd("/etc") #expect(@node.current_path).to eq("/etc/") expect(-> { @node.cd("/non-existent-path") }).to raise_exception(Deplomat::NoSuchPathError) end it "executes commands from the current directory" do @node.cd("#{@fixtures_dir}/dir1") expect(@node.execute("ls")[:out]).to have_files("file1", "file2", "subdir1") end it "creates an empty file" do @node.cd("#{@fixtures_dir}/dir2") @node.touch("myfile") expect(@node.execute("ls")[:out]).to have_files("myfile") end it "creates a directory" do @node.cd("#{@fixtures_dir}/dir2") @node.mkdir("mydir") expect(@node.execute("ls")[:out]).to have_files("mydir") end it "creates a symlink" do @node.cd("#{@fixtures_dir}/dir2") @node.create_symlink("../dir1", "./") expect(@node.execute("ls dir1")[:out]).to have_files("file1", "file2", "subdir1") end it "doesn't log ls command when checking if folder exists in `cd`" do expect(@node).to receive(:log).with("(deplomat-test-node) --> ls /home/deploy/deplomat/dir2\n", { color: "white"}).exactly(0).times @node.cd("#{@fixtures_dir}/dir2") allow(@node).to receive(:log) end end describe "handling status and errors" do it "returns the status" do @node.raise_exceptions = false expect(@node.execute("ls #{@fixtures_dir}/dir1/")[:status]).to eq(0) expect(@node.execute("ls #{@fixtures_dir}/non-existent-dir/")[:status]).to eq(2) end it "raises execution error when command fails" do @node.raise_exceptions = true expect( -> { @node.execute("ls #{@fixtures_dir}/non-existent-dir/")[:status] }).to raise_exception(Deplomat::ExecutionError) end end describe "cleaning" do before(:each) do @node.cd("#{@fixtures_dir}/cleaning") @node.execute("touch file1 file2 file3") @node.execute("mkdir current") sleep(0.01) @node.execute("mkdir dir1 dir2 dir3") end after(:each) do @node.execute("rm -rf #{@fixtures_dir}/cleaning/*") end it "cleans all the files and dirs in a given directory" do @node.clean expect(@node.execute('ls')[:out]).to be_empty end it "cleans all but last 3 entries in a given directory" do @node.clean(leave: [3, :last]) expect(@node.execute('ls')[:out]).to eq("dir1\ndir2\ndir3\n") end it "cleans all, but :except entries in a given directory" do @node.clean(except: ["current"]) expect(@node.execute('ls')[:out]).to eq("current\n") end end end