blob: e2f8adb8cbb768a0ff2ef0ab746ca027f2dc8b58 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipes051075a2012-04-28 17:39:37 -04002# 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
Alexander Gubanov509e2842015-06-09 15:29:51 +030016import json
17
Doug Hellmann583ce2c2015-03-11 14:55:46 +000018from oslo_log import log as logging
19
Matthew Treinish6c072292014-01-29 19:15:52 +000020from tempest import config
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030021from tempest import exceptions
Sean Dague6dbc6da2013-05-08 17:49:46 -040022from tempest.scenario import manager
Matthew Treinishd75edef2014-04-11 15:57:16 -040023from tempest.scenario import utils as test_utils
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090024from tempest import test
Jay Pipes051075a2012-04-28 17:39:37 -040025
Matthew Treinish6c072292014-01-29 19:15:52 +000026CONF = config.CONF
27
Jay Pipes051075a2012-04-28 17:39:37 -040028LOG = logging.getLogger(__name__)
29
Matthew Treinisha0048cb2014-04-08 17:44:42 -040030load_tests = test_utils.load_tests_input_scenario_utils
Andrea Frittolif5da28b2013-12-06 07:08:07 +000031
Jay Pipes051075a2012-04-28 17:39:37 -040032
Ghanshyam5a305c42014-08-27 14:24:58 +090033class TestServerBasicOps(manager.ScenarioTest):
Jay Pipes051075a2012-04-28 17:39:37 -040034
35 """
36 This smoke test case follows this basic set of operations:
37
38 * Create a keypair for use in launching an instance
39 * Create a security group to control network access in instance
40 * Add simple permissive rules to the security group
41 * Launch an instance
ghanshyam416c94c2014-10-02 13:47:25 +090042 * Perform ssh to instance
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090043 * Verify metadata service
Alexander Gubanov509e2842015-06-09 15:29:51 +030044 * Verify metadata on config_drive
Jay Pipes051075a2012-04-28 17:39:37 -040045 * Terminate the instance
46 """
47
Andrea Frittolif5da28b2013-12-06 07:08:07 +000048 def setUp(self):
49 super(TestServerBasicOps, self).setUp()
50 # Setup image and flavor the test instance
51 # Support both configured and injected values
52 if not hasattr(self, 'image_ref'):
Matthew Treinish6c072292014-01-29 19:15:52 +000053 self.image_ref = CONF.compute.image_ref
Andrea Frittolif5da28b2013-12-06 07:08:07 +000054 if not hasattr(self, 'flavor_ref'):
Matthew Treinish6c072292014-01-29 19:15:52 +000055 self.flavor_ref = CONF.compute.flavor_ref
Matthew Treinish96cadf42015-05-14 19:45:59 -040056 self.image_utils = test_utils.ImageUtils(self.manager)
Andrea Frittolif5da28b2013-12-06 07:08:07 +000057 if not self.image_utils.is_flavor_enough(self.flavor_ref,
58 self.image_ref):
59 raise self.skipException(
60 '{image} does not fit in {flavor}'.format(
61 image=self.image_ref, flavor=self.flavor_ref
62 )
63 )
Matthew Treinishe5cca002015-05-11 15:36:50 -040064 self.run_ssh = CONF.validation.run_validation and \
Andrea Frittolif5da28b2013-12-06 07:08:07 +000065 self.image_utils.is_sshable_image(self.image_ref)
66 self.ssh_user = self.image_utils.ssh_user(self.image_ref)
67 LOG.debug('Starting test for i:{image}, f:{flavor}. '
68 'Run ssh: {ssh}, user: {ssh_user}'.format(
69 image=self.image_ref, flavor=self.flavor_ref,
70 ssh=self.run_ssh, ssh_user=self.ssh_user))
71
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +090072 def add_keypair(self):
73 self.keypair = self.create_keypair()
Jay Pipes051075a2012-04-28 17:39:37 -040074
ivan-zhu19977392013-01-12 21:57:55 +080075 def boot_instance(self):
Andrea Frittolif5da28b2013-12-06 07:08:07 +000076 # Create server with image and flavor from input scenario
Ken'ichi Ohmichi1b3461e2014-12-02 03:41:07 +000077 security_groups = [{'name': self.security_group['name']}]
Alexander Gubanov509e2842015-06-09 15:29:51 +030078 self.md = {'meta1': 'data1', 'meta2': 'data2', 'metaN': 'dataN'}
Jay Pipes051075a2012-04-28 17:39:37 -040079 create_kwargs = {
Ghanshyam5a305c42014-08-27 14:24:58 +090080 'key_name': self.keypair['name'],
Alexander Gubanov509e2842015-06-09 15:29:51 +030081 'security_groups': security_groups,
82 'config_drive': CONF.compute_feature_enabled.config_drive,
83 'metadata': self.md
Jay Pipes051075a2012-04-28 17:39:37 -040084 }
Matthew Treinishb7144eb2013-12-13 22:57:35 +000085 self.instance = self.create_server(image=self.image_ref,
86 flavor=self.flavor_ref,
87 create_kwargs=create_kwargs)
ivan-zhu19977392013-01-12 21:57:55 +080088
Andrea Frittolif5da28b2013-12-06 07:08:07 +000089 def verify_ssh(self):
90 if self.run_ssh:
91 # Obtain a floating IP
ghanshyam9a3a9a22015-08-18 17:03:55 +090092 self.floating_ip = (self.floating_ips_client.create_floating_ip()
93 ['floating_ip'])
Ghanshyam5a305c42014-08-27 14:24:58 +090094 self.addCleanup(self.delete_wrapper,
95 self.floating_ips_client.delete_floating_ip,
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090096 self.floating_ip['id'])
Andrea Frittolif5da28b2013-12-06 07:08:07 +000097 # Attach a floating IP
Ghanshyam5a305c42014-08-27 14:24:58 +090098 self.floating_ips_client.associate_floating_ip_to_server(
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090099 self.floating_ip['ip'], self.instance['id'])
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000100 # Check ssh
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +0900101 self.ssh_client = self.get_remote_client(
102 server_or_ip=self.floating_ip['ip'],
JordanP3fe2dc32014-11-17 13:06:01 +0100103 username=self.image_utils.ssh_user(self.image_ref),
104 private_key=self.keypair['private_key'])
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000105
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +0900106 def verify_metadata(self):
107 if self.run_ssh and CONF.compute_feature_enabled.metadata_service:
108 # Verify metadata service
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +0300109 md_url = 'http://169.254.169.254/latest/meta-data/public-ipv4'
110
111 def exec_cmd_and_verify_output():
112 cmd = 'curl ' + md_url
113 floating_ip = self.floating_ip['ip']
114 result = self.ssh_client.exec_command(cmd)
115 if result:
116 msg = ('Failed while verifying metadata on server. Result '
117 'of command "%s" is NOT "%s".' % (cmd, floating_ip))
118 self.assertEqual(floating_ip, result, msg)
119 return 'Verification is successful!'
120
121 if not test.call_until_true(exec_cmd_and_verify_output,
122 CONF.compute.build_timeout,
123 CONF.compute.build_interval):
124 raise exceptions.TimeoutException('Timed out while waiting to '
125 'verify metadata on server. '
126 '%s is empty.' % md_url)
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +0900127
Alexander Gubanov509e2842015-06-09 15:29:51 +0300128 def verify_metadata_on_config_drive(self):
129 if self.run_ssh and CONF.compute_feature_enabled.config_drive:
130 # Verify metadata on config_drive
131 cmd_blkid = 'blkid -t LABEL=config-2 -o device'
132 dev_name = self.ssh_client.exec_command(cmd_blkid)
133 dev_name = dev_name.rstrip()
134 self.ssh_client.exec_command('sudo mount %s /mnt' % dev_name)
135 cmd_md = 'sudo cat /mnt/openstack/latest/meta_data.json'
136 result = self.ssh_client.exec_command(cmd_md)
137 self.ssh_client.exec_command('sudo umount /mnt')
138 result = json.loads(result)
139 self.assertIn('meta', result)
140 msg = ('Failed while verifying metadata on config_drive on server.'
141 ' Result of command "%s" is NOT "%s".' % (cmd_md, self.md))
142 self.assertEqual(self.md, result['meta'], msg)
143
Chris Hoge7579c1a2015-02-26 14:12:15 -0800144 @test.idempotent_id('7fff3fb3-91d8-4fd0-bd7d-0204f1f180ba')
Sean Dague3c634d12015-04-27 12:09:19 -0400145 @test.attr(type='smoke')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900146 @test.services('compute', 'network')
ivan-zhu19977392013-01-12 21:57:55 +0800147 def test_server_basicops(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +0900148 self.add_keypair()
Yair Fried1fc32a12014-08-04 09:11:30 +0300149 self.security_group = self._create_security_group()
ivan-zhu19977392013-01-12 21:57:55 +0800150 self.boot_instance()
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000151 self.verify_ssh()
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +0900152 self.verify_metadata()
Alexander Gubanov509e2842015-06-09 15:29:51 +0300153 self.verify_metadata_on_config_drive()
Ghanshyam5a305c42014-08-27 14:24:58 +0900154 self.servers_client.delete_server(self.instance['id'])