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.scenario import manager |
| 19 | |
| 20 | |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 21 | class TestSnapshotPattern(manager.OfficialClientTest): |
| 22 | """ |
| 23 | This test is for snapshotting an instance and booting with it. |
| 24 | The following is the scenario outline: |
| 25 | * boot a instance and create a timestamp file in it |
| 26 | * snapshot the instance |
| 27 | * boot a second instance from the snapshot |
| 28 | * check the existence of the timestamp file in the second instance |
| 29 | |
| 30 | """ |
| 31 | |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 32 | def _boot_image(self, image_id): |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 33 | create_kwargs = { |
| 34 | 'key_name': self.keypair.name |
| 35 | } |
| 36 | return self.create_server(self.compute_client, image=image_id, |
| 37 | create_kwargs=create_kwargs) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 38 | |
| 39 | def _add_keypair(self): |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 40 | self.keypair = self.create_keypair() |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 41 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 42 | def _ssh_to_server(self, server_or_ip): |
Ken'ichi Ohmichi | b3aa912 | 2013-08-22 23:27:25 +0900 | [diff] [blame] | 43 | linux_client = self.get_remote_client(server_or_ip) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 44 | return linux_client.ssh_client |
| 45 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 46 | def _write_timestamp(self, server_or_ip): |
| 47 | ssh_client = self._ssh_to_server(server_or_ip) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 48 | ssh_client.exec_command('date > /tmp/timestamp; sync') |
| 49 | self.timestamp = ssh_client.exec_command('cat /tmp/timestamp') |
| 50 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 51 | def _check_timestamp(self, server_or_ip): |
| 52 | ssh_client = self._ssh_to_server(server_or_ip) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 53 | got_timestamp = ssh_client.exec_command('cat /tmp/timestamp') |
| 54 | self.assertEqual(self.timestamp, got_timestamp) |
| 55 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 56 | def _create_floating_ip(self): |
| 57 | floating_ip = self.compute_client.floating_ips.create() |
| 58 | self.addCleanup(floating_ip.delete) |
| 59 | return floating_ip |
| 60 | |
| 61 | def _set_floating_ip_to_server(self, server, floating_ip): |
| 62 | server.add_floating_ip(floating_ip) |
| 63 | |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 64 | def test_snapshot_pattern(self): |
| 65 | # prepare for booting a instance |
| 66 | self._add_keypair() |
Ken'ichi Ohmichi | 3c1f519 | 2013-08-19 19:02:15 +0900 | [diff] [blame] | 67 | self.create_loginable_secgroup_rule() |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 68 | |
| 69 | # boot a instance and create a timestamp file in it |
| 70 | server = self._boot_image(self.config.compute.image_ref) |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 71 | if self.config.compute.use_floatingip_for_ssh: |
| 72 | fip_for_server = self._create_floating_ip() |
| 73 | self._set_floating_ip_to_server(server, fip_for_server) |
| 74 | self._write_timestamp(fip_for_server.ip) |
| 75 | else: |
| 76 | self._write_timestamp(server) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 77 | |
| 78 | # snapshot the instance |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 79 | snapshot_image = self.create_server_snapshot(server=server) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 80 | |
| 81 | # boot a second instance from the snapshot |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 82 | server_from_snapshot = self._boot_image(snapshot_image.id) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 83 | |
| 84 | # check the existence of the timestamp file in the second instance |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 85 | if self.config.compute.use_floatingip_for_ssh: |
| 86 | fip_for_snapshot = self._create_floating_ip() |
| 87 | self._set_floating_ip_to_server(server_from_snapshot, |
| 88 | fip_for_snapshot) |
| 89 | self._check_timestamp(fip_for_snapshot.ip) |
| 90 | else: |
| 91 | self._check_timestamp(server_from_snapshot) |