From 42e6f02b19f2bfb1d8de73396480a885fa7c60c1 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Tue, 6 Jun 2023 20:17:24 +0200 Subject: [PATCH] Implement rsync Introduce rsync function executing "vagrant rsync". Signed-off-by: Georg Pfuetzenreuter --- src/vagrant/__init__.py | 7 +++++++ tests/test_vagrant.py | 23 +++++++++++++++++++++++ tests/vagrantfiles/vm_Vagrantfile | 1 + 3 files changed, 31 insertions(+) diff --git a/src/vagrant/__init__.py b/src/vagrant/__init__.py index ea880a4..036c977 100644 --- a/src/vagrant/__init__.py +++ b/src/vagrant/__init__.py @@ -381,6 +381,13 @@ def provision(self, vm_name=None, provision_with=None) -> None: providers_arg = None if provision_with is None else ",".join(provision_with) self._call_vagrant_command(["provision", vm_name, prov_with_arg, providers_arg]) + def rsync(self, vm_name=None) -> None: + """ + Re-syncs data directories. + vm_name: optional VM name string. + """ + self._call_vagrant_command(["rsync", vm_name]) + def reload( self, vm_name=None, provision=None, provision_with=None, stream_output=False ) -> Optional[Iterator[str]]: diff --git a/tests/test_vagrant.py b/tests/test_vagrant.py index 546c727..e979ad6 100644 --- a/tests/test_vagrant.py +++ b/tests/test_vagrant.py @@ -604,6 +604,29 @@ def test_provisioning(vm_dir): assert test_file_contents == "foo", "The test file should contain 'foo'" +@pytest.mark.parametrize("vm_dir", (VM_VAGRANTFILE,), indirect=True) +def test_rsync(vm_dir, test_dir): + """ + Test whether rsync updates a synchronized file from containing + 'I like hats' to 'I like turtles'. + """ + testfile = "python_vagrant_rsync_test_file" + testfile_path = "{}/{}".format(test_dir, testfile) + ilike1 = "hats" + ilike2 = "turtles" + with open(testfile_path, "w", encoding="UTF-8") as fh: + fh.write("I like {}".format(ilike1)) + v = vagrant.Vagrant(vm_dir, quiet_stdout=False, quiet_stderr=False) + v.up() + output = v.ssh(command="cat /vagrant/{}".format(testfile)) + assert output.strip() == "I like {}".format(ilike1) + with open(testfile_path, "w", encoding="UTF-8") as fh: + fh.write("I like {}".format(ilike2)) + v.rsync() + output = v.ssh(command="cat /vagrant/{}".format(testfile)) + assert output.strip() == "I like {}".format(ilike2) + + @pytest.mark.parametrize("vm_dir", (MULTIVM_VAGRANTFILE,), indirect=True) def test_multivm_lifecycle(vm_dir): v = vagrant.Vagrant(vm_dir) diff --git a/tests/vagrantfiles/vm_Vagrantfile b/tests/vagrantfiles/vm_Vagrantfile index 3e128dc..23f4066 100644 --- a/tests/vagrantfiles/vm_Vagrantfile +++ b/tests/vagrantfiles/vm_Vagrantfile @@ -1,4 +1,5 @@ Vagrant.configure("2") do |config| config.vbguest.auto_update = false if Vagrant.has_plugin?("vagrant-vbguest") config.vm.box = "generic/alpine315" + config.vm.synced_folder ".", "/vagrant", type: "rsync" end