From abd154c87a75bd4100bfe72c2ed3f5d31d5cf0a2 Mon Sep 17 00:00:00 2001 From: Alexander Gubanov Date: Wed, 23 Sep 2015 23:24:06 +0300 Subject: [PATCH] Refactor tempest scenarios tests Moved methods "write_timestamp" and "get_timestamp", which are duplicated in few scenarios, to the common class. Change-Id: I527557a3cf3618ffa6589dbd1dbc92f2268ed50e --- tempest/common/utils/linux/remote_client.py | 11 ++++++++++ tempest/scenario/manager.py | 23 ++++++++++++++++++++ tempest/scenario/test_shelve_instance.py | 19 +++++----------- tempest/scenario/test_snapshot_pattern.py | 19 +++++----------- tempest/scenario/test_stamp_pattern.py | 24 +++++---------------- 5 files changed, 49 insertions(+), 47 deletions(-) diff --git a/tempest/common/utils/linux/remote_client.py b/tempest/common/utils/linux/remote_client.py index 9308390303..3bead88ff2 100644 --- a/tempest/common/utils/linux/remote_client.py +++ b/tempest/common/utils/linux/remote_client.py @@ -171,3 +171,14 @@ class RemoteClient(object): if dhcp_client == 'udhcpc' and not fixed_ip: raise ValueError("need to set 'fixed_ip' for udhcpc client") return getattr(self, '_renew_lease_' + dhcp_client)(fixed_ip=fixed_ip) + + def mount(self, dev_name, mount_path='/mnt'): + cmd_mount = 'sudo mount /dev/%s %s' % (dev_name, mount_path) + self.exec_command(cmd_mount) + + def umount(self, mount_path='/mnt'): + self.exec_command('sudo umount %s' % mount_path) + + def make_fs(self, dev_name, fs='ext4'): + cmd_mkfs = 'sudo /usr/sbin/mke2fs -t %s /dev/%s' % (fs, dev_name) + self.exec_command(cmd_mkfs) diff --git a/tempest/scenario/manager.py b/tempest/scenario/manager.py index 7ab38641db..015cd2fc40 100644 --- a/tempest/scenario/manager.py +++ b/tempest/scenario/manager.py @@ -557,6 +557,29 @@ class ScenarioTest(tempest.test.BaseTestCase): floating_ip['ip'], thing['id']) return floating_ip + def create_timestamp(self, server_or_ip, dev_name=None, mount_path='/mnt'): + ssh_client = self.get_remote_client(server_or_ip) + if dev_name is not None: + ssh_client.make_fs(dev_name) + ssh_client.mount(dev_name) + cmd_timestamp = 'sudo sh -c "date > %s/timestamp; sync"' % mount_path + ssh_client.exec_command(cmd_timestamp) + timestamp = ssh_client.exec_command('sudo cat %s/timestamp' + % mount_path) + if dev_name is not None: + ssh_client.umount(mount_path) + return timestamp + + def get_timestamp(self, server_or_ip, dev_name=None, mount_path='/mnt'): + ssh_client = self.get_remote_client(server_or_ip) + if dev_name is not None: + ssh_client.mount(dev_name) + timestamp = ssh_client.exec_command('sudo cat %s/timestamp' + % mount_path) + if dev_name is not None: + ssh_client.umount(mount_path) + return timestamp + class NetworkScenarioTest(ScenarioTest): """Base class for network scenario tests. diff --git a/tempest/scenario/test_shelve_instance.py b/tempest/scenario/test_shelve_instance.py index bf4b8e7504..dbc9bbb8b0 100644 --- a/tempest/scenario/test_shelve_instance.py +++ b/tempest/scenario/test_shelve_instance.py @@ -37,16 +37,6 @@ class TestShelveInstance(manager.ScenarioTest): """ - def _write_timestamp(self, server_or_ip): - ssh_client = self.get_remote_client(server_or_ip) - ssh_client.exec_command('date > /tmp/timestamp; sync') - self.timestamp = ssh_client.exec_command('cat /tmp/timestamp') - - def _check_timestamp(self, server_or_ip): - ssh_client = self.get_remote_client(server_or_ip) - got_timestamp = ssh_client.exec_command('cat /tmp/timestamp') - self.assertEqual(self.timestamp, got_timestamp) - def _shelve_then_unshelve_server(self, server): self.servers_client.shelve_server(server['id']) offload_time = CONF.compute.shelved_offload_time @@ -96,18 +86,19 @@ class TestShelveInstance(manager.ScenarioTest): floating_ip['id']) self.floating_ips_client.associate_floating_ip_to_server( floating_ip['ip'], server['id']) - self._write_timestamp(floating_ip['ip']) + timestamp = self.create_timestamp(floating_ip['ip']) else: - self._write_timestamp(server) + timestamp = self.create_timestamp(server) # Prevent bug #1257594 from coming back # Unshelve used to boot the instance with the original image, not # with the instance snapshot self._shelve_then_unshelve_server(server) if CONF.compute.use_floatingip_for_ssh: - self._check_timestamp(floating_ip['ip']) + timestamp2 = self.get_timestamp(floating_ip['ip']) else: - self._check_timestamp(server) + timestamp2 = self.get_timestamp(server) + self.assertEqual(timestamp, timestamp2) @test.idempotent_id('1164e700-0af0-4a4c-8792-35909a88743c') @testtools.skipUnless(CONF.compute_feature_enabled.shelve, diff --git a/tempest/scenario/test_snapshot_pattern.py b/tempest/scenario/test_snapshot_pattern.py index b5e5da805f..79b809f8e9 100644 --- a/tempest/scenario/test_snapshot_pattern.py +++ b/tempest/scenario/test_snapshot_pattern.py @@ -47,16 +47,6 @@ class TestSnapshotPattern(manager.ScenarioTest): def _add_keypair(self): self.keypair = self.create_keypair() - def _write_timestamp(self, server_or_ip): - ssh_client = self.get_remote_client(server_or_ip) - ssh_client.exec_command('date > /tmp/timestamp; sync') - self.timestamp = ssh_client.exec_command('cat /tmp/timestamp') - - def _check_timestamp(self, server_or_ip): - ssh_client = self.get_remote_client(server_or_ip) - got_timestamp = ssh_client.exec_command('cat /tmp/timestamp') - self.assertEqual(self.timestamp, got_timestamp) - @test.idempotent_id('608e604b-1d63-4a82-8e3e-91bc665c90b4') @testtools.skipUnless(CONF.compute_feature_enabled.snapshot, 'Snapshotting is not available.') @@ -70,9 +60,9 @@ class TestSnapshotPattern(manager.ScenarioTest): server = self._boot_image(CONF.compute.image_ref) if CONF.compute.use_floatingip_for_ssh: fip_for_server = self.create_floating_ip(server) - self._write_timestamp(fip_for_server['ip']) + timestamp = self.create_timestamp(fip_for_server['ip']) else: - self._write_timestamp(server) + timestamp = self.create_timestamp(server) # snapshot the instance snapshot_image = self.create_server_snapshot(server=server) @@ -83,6 +73,7 @@ class TestSnapshotPattern(manager.ScenarioTest): # check the existence of the timestamp file in the second instance if CONF.compute.use_floatingip_for_ssh: fip_for_snapshot = self.create_floating_ip(server_from_snapshot) - self._check_timestamp(fip_for_snapshot['ip']) + timestamp2 = self.get_timestamp(fip_for_snapshot['ip']) else: - self._check_timestamp(server_from_snapshot) + timestamp2 = self.get_timestamp(server_from_snapshot) + self.assertEqual(timestamp, timestamp2) diff --git a/tempest/scenario/test_stamp_pattern.py b/tempest/scenario/test_stamp_pattern.py index 91e4657c3a..b9d5fcc359 100644 --- a/tempest/scenario/test_stamp_pattern.py +++ b/tempest/scenario/test_stamp_pattern.py @@ -127,23 +127,6 @@ class TestStampPattern(manager.ScenarioTest): CONF.compute.build_interval): raise exceptions.TimeoutException - def _create_timestamp(self, server_or_ip): - ssh_client = self._ssh_to_server(server_or_ip) - ssh_client.exec_command('sudo /usr/sbin/mkfs.ext4 /dev/%s' - % CONF.compute.volume_device_name) - ssh_client.exec_command('sudo mount /dev/%s /mnt' - % CONF.compute.volume_device_name) - ssh_client.exec_command('sudo sh -c "date > /mnt/timestamp;sync"') - self.timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp') - ssh_client.exec_command('sudo umount /mnt') - - def _check_timestamp(self, server_or_ip): - ssh_client = self._ssh_to_server(server_or_ip) - ssh_client.exec_command('sudo mount /dev/%s /mnt' - % CONF.compute.volume_device_name) - got_timestamp = ssh_client.exec_command('sudo cat /mnt/timestamp') - self.assertEqual(self.timestamp, got_timestamp) - @decorators.skip_because(bug="1205344") @test.idempotent_id('10fd234a-515c-41e5-b092-8323060598c5') @testtools.skipUnless(CONF.compute_feature_enabled.snapshot, @@ -167,7 +150,8 @@ class TestStampPattern(manager.ScenarioTest): self._attach_volume(server, volume) self._wait_for_volume_available_on_the_system(ip_for_server) - self._create_timestamp(ip_for_server) + timestamp = self.create_timestamp(ip_for_server, + CONF.compute.volume_device_name) self._detach_volume(server, volume) # snapshot the volume @@ -196,4 +180,6 @@ class TestStampPattern(manager.ScenarioTest): self._wait_for_volume_available_on_the_system(ip_for_snapshot) # check the existence of the timestamp file in the volume2 - self._check_timestamp(ip_for_snapshot) + timestamp2 = self.get_timestamp(ip_for_snapshot, + CONF.compute.volume_device_name) + self.assertEqual(timestamp, timestamp2)