From 5578770768ef60653ff1d7b8959bf9c713a20d04 Mon Sep 17 00:00:00 2001 From: Carlos Camacho Date: Mon, 14 May 2018 11:25:56 +0200 Subject: [PATCH] Add validation for empty file system backup on Undercloud This patch works as an addition to the exclude path when executing an Undercloud backup. In this case if we exclude all files, there is no need to execute the task to create the FS backup. Change-Id: Icd25a8d579151015c9cdf56861d2be933848a723 Closes-Bug: 1767385 --- tripleo_common/actions/undercloud.py | 18 +++++++++++------- .../tests/actions/test_undercloud.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tripleo_common/actions/undercloud.py b/tripleo_common/actions/undercloud.py index 745ceb36e..339a702bf 100644 --- a/tripleo_common/actions/undercloud.py +++ b/tripleo_common/actions/undercloud.py @@ -161,14 +161,18 @@ class CreateFileSystemBackup(base.Action): """ % (self.outfile, separated_string, self.outfile) proc_failed = False - try: - subprocess.check_call(script, shell=True) - except subprocess.CalledProcessError: - proc_failed = True - msg = 'File system backup failed' - os.remove(self.outfile) + if self.sources_path: + try: + subprocess.check_call(script, shell=True) + except subprocess.CalledProcessError: + proc_failed = True + msg = 'File system backup failed' + os.remove(self.outfile) + else: + msg = ('File system backup created succesfully at: %s' + % self.outfile) else: - msg = 'File system backup created succesfully at: ' + self.outfile + msg = 'File system backup has no files to backup' if proc_failed: # Delete failed backup here diff --git a/tripleo_common/tests/actions/test_undercloud.py b/tripleo_common/tests/actions/test_undercloud.py index 2fccb4cf1..ece06f40b 100644 --- a/tripleo_common/tests/actions/test_undercloud.py +++ b/tripleo_common/tests/actions/test_undercloud.py @@ -101,6 +101,9 @@ class CreateFileSystemBackupTest(base.TestCase): self.fsback = undercloud.CreateFileSystemBackup( '/home/stack/,/etc/hosts', '/var/tmp/undercloud-backup-ef9b_H') + self.fsemptyback = undercloud.CreateFileSystemBackup( + '', + '/var/tmp/undercloud-backup-ef9b_H') @mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client') @mock.patch('subprocess.check_call') @@ -120,6 +123,16 @@ class CreateFileSystemBackupTest(base.TestCase): '\n ') mock_check_call.assert_called_once_with(assert_string, shell=True) + @mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client') + @mock.patch('subprocess.check_call') + def test_create_empty_file_system_backup( + self, + mock_check_call, + mock_get_object_client): + self.fsemptyback.logger = mock.Mock() + self.fsemptyback.run(mock_get_object_client) + mock_check_call.assert_not_called() + class CreateBackupDirTest(base.TestCase):