Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 1 | # Copyright 2013 NEC Corporation |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Adam Gandelman | fbc95ac | 2014-06-19 17:33:43 -0700 | [diff] [blame] | 16 | import testtools |
| 17 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 18 | from tempest import config |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 19 | from tempest.openstack.common import log |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 20 | from tempest.scenario import manager |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 21 | from tempest import test |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 22 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 24 | |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 25 | LOG = log.getLogger(__name__) |
| 26 | |
| 27 | |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 28 | class TestSnapshotPattern(manager.ScenarioTest): |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 29 | """ |
| 30 | This test is for snapshotting an instance and booting with it. |
| 31 | The following is the scenario outline: |
| 32 | * boot a instance and create a timestamp file in it |
| 33 | * snapshot the instance |
| 34 | * boot a second instance from the snapshot |
| 35 | * check the existence of the timestamp file in the second instance |
| 36 | |
| 37 | """ |
| 38 | |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 39 | def _boot_image(self, image_id): |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 40 | security_groups = [self.security_group] |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 41 | create_kwargs = { |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 42 | 'key_name': self.keypair['name'], |
Grishkin | 0f1e11c | 2014-05-04 20:44:52 +0400 | [diff] [blame] | 43 | 'security_groups': security_groups |
Ken'ichi Ohmichi | 61f272b | 2013-08-15 15:58:53 +0900 | [diff] [blame] | 44 | } |
Giulio Fidente | 61cadca | 2013-09-24 18:33:37 +0200 | [diff] [blame] | 45 | return self.create_server(image=image_id, create_kwargs=create_kwargs) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 46 | |
| 47 | def _add_keypair(self): |
Ken'ichi Ohmichi | 599d1b8 | 2013-08-19 18:48:37 +0900 | [diff] [blame] | 48 | self.keypair = self.create_keypair() |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 49 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 50 | def _ssh_to_server(self, server_or_ip): |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 51 | try: |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 52 | return self.get_remote_client(server_or_ip) |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 53 | except Exception: |
Matthew Treinish | 7adb695 | 2014-06-16 10:05:57 -0400 | [diff] [blame] | 54 | LOG.exception('Initializing SSH connection failed') |
Nachi Ueno | 95b4128 | 2014-01-15 06:54:21 -0800 | [diff] [blame] | 55 | self._log_console_output() |
Matthew Treinish | 7adb695 | 2014-06-16 10:05:57 -0400 | [diff] [blame] | 56 | raise |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 57 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 58 | def _write_timestamp(self, server_or_ip): |
| 59 | ssh_client = self._ssh_to_server(server_or_ip) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 60 | ssh_client.exec_command('date > /tmp/timestamp; sync') |
| 61 | self.timestamp = ssh_client.exec_command('cat /tmp/timestamp') |
| 62 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 63 | def _check_timestamp(self, server_or_ip): |
| 64 | ssh_client = self._ssh_to_server(server_or_ip) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 65 | got_timestamp = ssh_client.exec_command('cat /tmp/timestamp') |
| 66 | self.assertEqual(self.timestamp, got_timestamp) |
| 67 | |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 68 | def _create_floating_ip(self): |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 69 | _, floating_ip = self.floating_ips_client.create_floating_ip() |
| 70 | self.addCleanup(self.delete_wrapper, |
| 71 | self.floating_ips_client.delete_floating_ip, |
| 72 | floating_ip['id']) |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 73 | return floating_ip |
| 74 | |
| 75 | def _set_floating_ip_to_server(self, server, floating_ip): |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 76 | self.floating_ips_client.associate_floating_ip_to_server( |
| 77 | floating_ip['ip'], server['id']) |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 78 | |
Adam Gandelman | fbc95ac | 2014-06-19 17:33:43 -0700 | [diff] [blame] | 79 | @testtools.skipUnless(CONF.compute_feature_enabled.snapshot, |
| 80 | 'Snapshotting is not available.') |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 81 | @test.services('compute', 'network', 'image') |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 82 | def test_snapshot_pattern(self): |
| 83 | # prepare for booting a instance |
| 84 | self._add_keypair() |
Yair Fried | 1fc32a1 | 2014-08-04 09:11:30 +0300 | [diff] [blame] | 85 | self.security_group = self._create_security_group() |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 86 | |
| 87 | # boot a instance and create a timestamp file in it |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 88 | server = self._boot_image(CONF.compute.image_ref) |
| 89 | if CONF.compute.use_floatingip_for_ssh: |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 90 | fip_for_server = self._create_floating_ip() |
| 91 | self._set_floating_ip_to_server(server, fip_for_server) |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 92 | self._write_timestamp(fip_for_server['ip']) |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 93 | else: |
| 94 | self._write_timestamp(server) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 95 | |
| 96 | # snapshot the instance |
Ken'ichi Ohmichi | a491223 | 2013-08-26 14:03:25 +0900 | [diff] [blame] | 97 | snapshot_image = self.create_server_snapshot(server=server) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 98 | |
| 99 | # boot a second instance from the snapshot |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 100 | server_from_snapshot = self._boot_image(snapshot_image['id']) |
Masayuki Igawa | a6de155 | 2013-06-18 17:08:24 +0900 | [diff] [blame] | 101 | |
| 102 | # check the existence of the timestamp file in the second instance |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 103 | if CONF.compute.use_floatingip_for_ssh: |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 104 | fip_for_snapshot = self._create_floating_ip() |
| 105 | self._set_floating_ip_to_server(server_from_snapshot, |
| 106 | fip_for_snapshot) |
nithya-ganesan | 882595e | 2014-07-29 18:51:07 +0000 | [diff] [blame] | 107 | self._check_timestamp(fip_for_snapshot['ip']) |
fujioka yuuichi | a11994e | 2013-07-09 11:19:51 +0900 | [diff] [blame] | 108 | else: |
| 109 | self._check_timestamp(server_from_snapshot) |