From 5316e4bd9073efade72eb027db4afe667da0d70c Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Tue, 13 Mar 2018 09:54:58 -0600 Subject: [PATCH] allow excluding multiple directories in rsync --- salt/modules/rsync.py | 13 ++++++++----- tests/unit/modules/test_rsync.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/salt/modules/rsync.py b/salt/modules/rsync.py index 3051437075..1e9f361ca4 100644 --- a/salt/modules/rsync.py +++ b/salt/modules/rsync.py @@ -56,7 +56,11 @@ def _check(delete, force, update, passwordfile, exclude, excludefrom, dryrun, rs if exclude: exclude = False if exclude: - options.extend(['--exclude', exclude]) + if isinstance(exclude, list): + for ex_ in exclude: + options.extend(['--exclude', ex_]) + else: + options.extend(['--exclude', exclude]) if dryrun: options.append('--dry-run') return options @@ -133,10 +137,9 @@ def rsync(src, .. code-block:: bash - salt '*' rsync.rsync {src} {dst} {delete=True} {update=True} {passwordfile=/etc/pass.crt} {exclude=xx} {rsh} - salt '*' rsync.rsync {src} {dst} {delete=True} {excludefrom=/xx.ini} {rsh} - - salt '*' rsync.rsync {src} {dst} {delete=True} {excludefrom=/xx.ini} additional_opts='["--partial", "--bwlimit=5000"]' + salt '*' rsync.rsync /path/to/src /path/to/dest delete=True update=True passwordfile=/etc/pass.crt exclude=exclude/dir + salt '*' rsync.rsync /path/to/src delete=True excludefrom=/xx.ini + salt '*' rsync.rsync /path/to/src delete=True exclude='[exclude1/dir,exclude2/dir]' additional_opts='["--partial", "--bwlimit=5000"]' ''' if not src: src = __salt__['config.option']('rsync.src') diff --git a/tests/unit/modules/test_rsync.py b/tests/unit/modules/test_rsync.py index 1f9848b623..9624769bd4 100644 --- a/tests/unit/modules/test_rsync.py +++ b/tests/unit/modules/test_rsync.py @@ -53,3 +53,33 @@ class RsyncTestCase(TestCase, LoaderModuleMockMixin): self.assertRaises(CommandExecutionError, rsync.version) self.assertEqual(rsync.version(), 'C') + + def test_rsync_excludes_list(self): + ''' + Test for rsync files from src to dst with a list of excludes + ''' + mock = { + 'config.option': MagicMock(return_value=False), + 'cmd.run_all': MagicMock() + } + with patch.dict(rsync.__salt__, mock): + rsync.rsync('src', 'dst', exclude=['test/one', 'test/two']) + mock['cmd.run_all'].assert_called_once_with( + ['rsync', '-avz', '--exclude', 'test/one', '--exclude', 'test/two', 'src', 'dst'], + python_shell=False, + ) + + def test_rsync_excludes_str(self): + ''' + Test for rsync files from src to dst with one exclude + ''' + mock = { + 'config.option': MagicMock(return_value=False), + 'cmd.run_all': MagicMock() + } + with patch.dict(rsync.__salt__, mock): + rsync.rsync('src', 'dst', exclude='test/one') + mock['cmd.run_all'].assert_called_once_with( + ['rsync', '-avz', '--exclude', 'test/one', 'src', 'dst'], + python_shell=False, + )