Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2013 NEC Corporation |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 18 | from tempest.common.utils.data_utils import rand_name |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 19 | from tempest.openstack.common import log as logging |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 20 | from tempest.scenario import manager |
| 21 | |
| 22 | |
| 23 | LOG = logging.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | class TestSnapshotPattern(manager.OfficialClientTest): |
| 27 | """ |
| 28 | This test is for snapshotting an instance and booting with it. |
| 29 | The following is the scenario outline: |
| 30 | * boot a instance and create a timestamp file in it |
| 31 | * snapshot the instance |
| 32 | * boot a second instance from the snapshot |
| 33 | * check the existence of the timestamp file in the second instance |
| 34 | |
| 35 | """ |
| 36 | |
| 37 | def _wait_for_server_status(self, server, status): |
| 38 | self.status_timeout(self.compute_client.servers, |
| 39 | server.id, |
| 40 | status) |
| 41 | |
| 42 | def _wait_for_image_status(self, image_id, status): |
| 43 | self.status_timeout(self.image_client.images, image_id, status) |
| 44 | |
| 45 | def _boot_image(self, image_id): |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 46 | create_kwargs = { |
| 47 | 'key_name': self.keypair.name |
| 48 | } |
| 49 | return self.create_server(self.compute_client, image=image_id, |
| 50 | create_kwargs=create_kwargs) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 51 | |
| 52 | def _add_keypair(self): |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 53 | self.keypair = self.create_keypair() |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 54 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 55 | def _ssh_to_server(self, server_or_ip): |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 56 | linux_client = self.get_remote_client(server_or_ip) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 57 | return linux_client.ssh_client |
| 58 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 59 | def _write_timestamp(self, server_or_ip): |
| 60 | ssh_client = self._ssh_to_server(server_or_ip) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 61 | ssh_client.exec_command('date > /tmp/timestamp; sync') |
| 62 | self.timestamp = ssh_client.exec_command('cat /tmp/timestamp') |
| 63 | |
| 64 | def _create_image(self, server): |
| 65 | snapshot_name = rand_name('scenario-snapshot-') |
| 66 | create_image_client = self.compute_client.servers.create_image |
| 67 | image_id = create_image_client(server, snapshot_name) |
| 68 | self.addCleanup(self.image_client.images.delete, image_id) |
| 69 | self._wait_for_server_status(server, 'ACTIVE') |
| 70 | self._wait_for_image_status(image_id, 'active') |
| 71 | snapshot_image = self.image_client.images.get(image_id) |
| 72 | self.assertEquals(snapshot_name, snapshot_image.name) |
| 73 | return image_id |
| 74 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 75 | def _check_timestamp(self, server_or_ip): |
| 76 | ssh_client = self._ssh_to_server(server_or_ip) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 77 | got_timestamp = ssh_client.exec_command('cat /tmp/timestamp') |
| 78 | self.assertEqual(self.timestamp, got_timestamp) |
| 79 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 80 | def _create_floating_ip(self): |
| 81 | floating_ip = self.compute_client.floating_ips.create() |
| 82 | self.addCleanup(floating_ip.delete) |
| 83 | return floating_ip |
| 84 | |
| 85 | def _set_floating_ip_to_server(self, server, floating_ip): |
| 86 | server.add_floating_ip(floating_ip) |
| 87 | |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 88 | def test_snapshot_pattern(self): |
| 89 | # prepare for booting a instance |
| 90 | self._add_keypair() |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 91 | self.create_loginable_secgroup_rule() |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 92 | |
| 93 | # boot a instance and create a timestamp file in it |
| 94 | server = self._boot_image(self.config.compute.image_ref) |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 95 | if self.config.compute.use_floatingip_for_ssh: |
| 96 | fip_for_server = self._create_floating_ip() |
| 97 | self._set_floating_ip_to_server(server, fip_for_server) |
| 98 | self._write_timestamp(fip_for_server.ip) |
| 99 | else: |
| 100 | self._write_timestamp(server) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 101 | |
| 102 | # snapshot the instance |
| 103 | snapshot_image_id = self._create_image(server) |
| 104 | |
| 105 | # boot a second instance from the snapshot |
| 106 | server_from_snapshot = self._boot_image(snapshot_image_id) |
| 107 | |
| 108 | # check the existence of the timestamp file in the second instance |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 109 | if self.config.compute.use_floatingip_for_ssh: |
| 110 | fip_for_snapshot = self._create_floating_ip() |
| 111 | self._set_floating_ip_to_server(server_from_snapshot, |
| 112 | fip_for_snapshot) |
| 113 | self._check_timestamp(fip_for_snapshot.ip) |
| 114 | else: |
| 115 | self._check_timestamp(server_from_snapshot) |