Add backup restore for glance images

* copy image
* delete from fs before restore
* restore image
* download restored image over glance

Change-Id: Ibe5759419fe307e3613a89ed7b0da73742e0621c
diff --git a/tcp_tests/managers/backup_restore_manager.py b/tcp_tests/managers/backup_restore_manager.py
index 365ff6f..4665eb8 100644
--- a/tcp_tests/managers/backup_restore_manager.py
+++ b/tcp_tests/managers/backup_restore_manager.py
@@ -153,31 +153,37 @@
     # #################Backup_Restore_Glance###################
 
     def copy_glance_images_to_backup(self, path_to_backup,
-                                     tgt="I@glance:server and *01*"):
+                                     tgt="ctl03"):
         cmd = 'cp -a /var/lib/glance/images/. {}'.format(path_to_backup)
-        return self.salt_api.enforce_state(
-            tgt, 'cmd.run', cmd)
+        step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)}
+        return self.execute_command(step, 'Copy glance images to backup')
+
+    def get_image_uud(self, tgt='ctl03'):
+        cmd = (". /root/keystonercv3; "
+               "openstack image list -c ID|  awk 'NR==4'| cut -d '|' -f 2")
+        step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)}
+        res = self.execute_command(step, 'Get uuid of image on fs',
+                                   return_res=True)
+        return res
+
+    def delete_image_from_fs(self, uuid, tgt="ctl03"):
+        cmd = ('cd /var/lib/glance/images/; rm {}'.format(uuid))
+        step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)}
+        self.execute_command(step, 'Delete image before restore')
 
     def copy_glance_images_from_backup(self, path_to_backup,
-                                       tgt="I@glance:server and *01*"):
+                                       tgt="ctl03"):
         cmd = 'cp -a {}/. /var/lib/glance/images/'.format(path_to_backup)
-        return self.salt_api.enforce_state(
-            tgt, 'cmd.run', cmd)
+        step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)}
+        return self.execute_command(step, 'Copy to glance')
 
-    def check_images_after_backup(self, tgt="I@keystone:client"):
-        # TODO If the context of the Glance
-        # images files is lost, run the following commands:
-        # salt -C 'I@glance:server' cmd.run
-        # "chown glance:glance <IMAGE_FILE_NAME>"
-        # salt -C 'I@glance:server' cmd.run "chmod 640 <IMAGE_FILE_NAME>"
-        cmd = '. /root/keystonercv3; openstack image list'
-        return self.salt_api.enforce_state(tgt, 'cmd.run', cmd)
+    def check_image_on_fs(self, uuid, tgt="ctl03"):
+        cmd = ('ls /var/lib/glance/images/ | grep {}'.format(uuid))
+        step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)}
+        self.execute_command(step, 'Check image exists after restore')
 
-    # #################Backup_Restore_cinder_volumes_and_snapshots###
-    # TODO Verify that a complete backup was created on
-    # the MySQL Galera Database Master node
-    # ls /var/backups/mysql/xtrabackup/full
-    # TODO(tleontovich): add method to check needed configs
-    # TODO (tleontovich): check pillars
-    # TODO (tleontovich): check  backup is created, and
-    # restore restores
+    def check_image_after_backup(self, uuid, tgt="ctl03"):
+        cmd = ('. /root/keystonercv3; '
+               'openstack image save {} --file test'.format(uuid))
+        step = {'cmd': cmd, 'node_name': self.get_node_name(tgt)}
+        self.execute_command(step, 'Save image after backup')
diff --git a/tcp_tests/managers/execute_commands.py b/tcp_tests/managers/execute_commands.py
index ba12678..adb76dc 100644
--- a/tcp_tests/managers/execute_commands.py
+++ b/tcp_tests/managers/execute_commands.py
@@ -77,7 +77,7 @@
                 LOG.info(log_msg)
                 self.action_download(step)
 
-    def execute_command(self, step, msg):
+    def execute_command(self, step, msg, return_res=None):
         # Required fields
         cmd = step.get('cmd')
         node_name = step.get('node_name')
@@ -102,6 +102,8 @@
                     msg + retry_msg, '=' * len(msg + retry_msg)))
 
                 result = remote.execute(cmd, verbose=True)
+                if return_res:
+                    return result
 
                 # Workaround of exit code 0 from salt in case of failures
                 failed = 0
diff --git a/tcp_tests/tests/system/test_backup_restore.py b/tcp_tests/tests/system/test_backup_restore.py
index d63433b..d32f71c 100644
--- a/tcp_tests/tests/system/test_backup_restore.py
+++ b/tcp_tests/tests/system/test_backup_restore.py
@@ -24,7 +24,7 @@
     def test_backup_cfg_backupninja_rsync(
             self, underlay, config, openstack_deployed,
             salt_actions, show_step):
-        """Test add policy for Nova service
+        """Test backup restore master node
 
         Scenario:
             1. Prepare salt on hosts
@@ -70,3 +70,57 @@
         backup.ping_minions('cfg01')
 
         LOG.info("*************** DONE **************")
+
+
+class TestBackupVCP(object):
+    """Test class for testing backup restore of VCP nodes"""
+
+    def test_backup_restore_glance_images(
+            self, underlay, config, openstack_deployed,
+            salt_actions, show_step):
+        """Test backup restore glance images
+
+        Scenario:
+            1. Prepare salt on hosts
+            2. Setup controller nodes
+            3. Setup compute nodes
+            4. Copy the images to the backup destination
+            5. Get image file uuid on fs
+            6. Delete image on fs
+            7. Copy the images from the backup directory to the Glance folder
+            8. Verify if the restored Glance images are available
+            9. Download image from glance
+        """
+        backup = backup_restore_manager.BackupRestoreManager(
+                config=config, underlay=underlay, salt_api=salt_actions)
+        # STEP #1,2,3
+        show_step(1)
+        show_step(2)
+        show_step(3)
+
+        # STEP #4
+        show_step(4)
+        backup.copy_glance_images_to_backup(
+            path_to_backup='/srv/volumes/backup/')
+
+        # STEP #5
+        show_step(5)
+        uuid = backup.get_image_uud()['stdout'][0].rstrip()
+
+        # STEP #6
+        show_step(6)
+        backup.delete_image_from_fs(uuid)
+
+        # STEP #7
+        show_step(7)
+        backup.copy_glance_images_from_backup(
+            path_to_backup='/srv/volumes/backup/')
+
+        # STEP #8
+        show_step(8)
+        backup.check_image_on_fs(uuid)
+
+        # STEP #9
+        show_step(9)
+        backup.check_image_after_backup(uuid)
+        LOG.info("*************** DONE **************")