File: rsync_test.rb

package info (click to toggle)
vagrant 2.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,072 kB
  • sloc: ruby: 80,731; sh: 369; makefile: 9; lisp: 1
file content (97 lines) | stat: -rw-r--r-- 3,040 bytes parent folder | download | duplicates (6)
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
require_relative "../../../../base"

describe "VagrantPlugins::GuestLinux::Cap::Rsync" do
  let(:caps) do
    VagrantPlugins::GuestLinux::Plugin
      .components
      .guest_capabilities[:linux]
  end

  let(:machine) { double("machine") }
  let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
  let(:guest_directory){ "/guest/directory/path" }

  before do
    allow(machine).to receive(:communicate).and_return(comm)
  end

  after do
    comm.verify_expectations!
  end

  describe ".rsync_installed" do
    let(:cap) { caps.get(:rsync_installed) }

    it "checks if the command is installed" do
      comm.expect_command("which rsync")
      cap.rsync_installed(machine)
    end
  end

  describe ".rsync_command" do
    let(:cap) { caps.get(:rsync_command) }

    it "provides the rsync command to use" do
      expect(cap.rsync_command(machine)).to eq("sudo rsync")
    end
  end

  describe ".rsync_pre" do
    let(:cap) { caps.get(:rsync_pre) }

    it "creates target directory on guest" do
      comm.expect_command("mkdir -p #{guest_directory}")
      cap.rsync_pre(machine, :guestpath => guest_directory)
    end
  end

  describe ".rsync_post" do
    let(:cap) { caps.get(:rsync_post) }
    let(:host_directory){ '.' }
    let(:owner) { "vagrant-user" }
    let(:group) { "vagrant-group" }
    let(:excludes) { false }
    let(:options) do
      {
        hostpath: host_directory,
        guestpath: guest_directory,
        owner: owner,
        group: group,
        exclude: excludes
      }
    end

    it "chowns files within the guest directory" do
      comm.expect_command(
        "find #{guest_directory} '!' -type l -a '(' ! -user #{owner} -or " \
          "! -group #{group} ')' -exec chown #{owner}:#{group} '{}' +"
      )
      cap.rsync_post(machine, options)
    end

    context "with excludes provided" do
      let(:excludes){ ["tmp", "state/*", "path/with a/space"] }

      it "ignores files that are excluded" do
        # comm.expect_command(
        #   "find #{guest_directory} -path #{Shellwords.escape(File.join(guest_directory, excludes.first))} -prune -o " \
        #     "-path #{Shellwords.escape(File.join(guest_directory, excludes.last))} -prune -o '!' " \
        #     "-path -type l -a '(' ! -user " \
        #     "#{owner} -or ! -group #{group} ')' -exec chown #{owner}:#{group} '{}' +"
        # )
        cap.rsync_post(machine, options)
        excludes.each do |ex_path|
          expect(comm.received_commands.first).to include("-path #{Shellwords.escape(File.join(guest_directory, ex_path))} -prune")
        end
      end

      it "properly escapes excluded directories" do
        cap.rsync_post(machine, options)
        exclude_with_space = excludes.detect{|ex| ex.include?(' ')}
        escaped_exclude_with_space = Shellwords.escape(exclude_with_space)
        expect(comm.received_commands.first).not_to include(exclude_with_space)
        expect(comm.received_commands.first).to include(escaped_exclude_with_space)
      end
    end
  end
end