blob: 239e1209f06e8d8157b04dfe17b9e3bae5d275fb [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
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000035 """The test suite for server basic operations
Jay Pipes051075a2012-04-28 17:39:37 -040036
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000037 This smoke test case follows this basic set of operations:
Jay Pipes051075a2012-04-28 17:39:37 -040038 * 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
Andrea Frittolif5da28b2013-12-06 07:08:07 +000075 def verify_ssh(self):
76 if self.run_ssh:
77 # Obtain a floating IP
Alexander Gubanov2388e2a2015-11-07 11:16:28 +020078 self.fip = self.create_floating_ip(self.instance)['ip']
Andrea Frittolif5da28b2013-12-06 07:08:07 +000079 # Check ssh
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090080 self.ssh_client = self.get_remote_client(
Alexander Gubanov2388e2a2015-11-07 11:16:28 +020081 server_or_ip=self.fip,
JordanP3fe2dc32014-11-17 13:06:01 +010082 username=self.image_utils.ssh_user(self.image_ref),
83 private_key=self.keypair['private_key'])
Andrea Frittolif5da28b2013-12-06 07:08:07 +000084
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090085 def verify_metadata(self):
86 if self.run_ssh and CONF.compute_feature_enabled.metadata_service:
87 # Verify metadata service
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030088 md_url = 'http://169.254.169.254/latest/meta-data/public-ipv4'
89
90 def exec_cmd_and_verify_output():
91 cmd = 'curl ' + md_url
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030092 result = self.ssh_client.exec_command(cmd)
93 if result:
94 msg = ('Failed while verifying metadata on server. Result '
Alexander Gubanov2388e2a2015-11-07 11:16:28 +020095 'of command "%s" is NOT "%s".' % (cmd, self.fip))
96 self.assertEqual(self.fip, result, msg)
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030097 return 'Verification is successful!'
98
99 if not test.call_until_true(exec_cmd_and_verify_output,
100 CONF.compute.build_timeout,
101 CONF.compute.build_interval):
102 raise exceptions.TimeoutException('Timed out while waiting to '
103 'verify metadata on server. '
104 '%s is empty.' % md_url)
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +0900105
Alexander Gubanov509e2842015-06-09 15:29:51 +0300106 def verify_metadata_on_config_drive(self):
107 if self.run_ssh and CONF.compute_feature_enabled.config_drive:
108 # Verify metadata on config_drive
109 cmd_blkid = 'blkid -t LABEL=config-2 -o device'
110 dev_name = self.ssh_client.exec_command(cmd_blkid)
111 dev_name = dev_name.rstrip()
112 self.ssh_client.exec_command('sudo mount %s /mnt' % dev_name)
113 cmd_md = 'sudo cat /mnt/openstack/latest/meta_data.json'
114 result = self.ssh_client.exec_command(cmd_md)
115 self.ssh_client.exec_command('sudo umount /mnt')
116 result = json.loads(result)
117 self.assertIn('meta', result)
118 msg = ('Failed while verifying metadata on config_drive on server.'
119 ' Result of command "%s" is NOT "%s".' % (cmd_md, self.md))
120 self.assertEqual(self.md, result['meta'], msg)
121
Chris Hoge7579c1a2015-02-26 14:12:15 -0800122 @test.idempotent_id('7fff3fb3-91d8-4fd0-bd7d-0204f1f180ba')
Sean Dague3c634d12015-04-27 12:09:19 -0400123 @test.attr(type='smoke')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900124 @test.services('compute', 'network')
ivan-zhu19977392013-01-12 21:57:55 +0800125 def test_server_basicops(self):
Ken'ichi Ohmichi599d1b82013-08-19 18:48:37 +0900126 self.add_keypair()
Yair Fried1fc32a12014-08-04 09:11:30 +0300127 self.security_group = self._create_security_group()
lanoux5fc14522015-09-21 08:17:35 +0000128 security_groups = [{'name': self.security_group['name']}]
129 self.md = {'meta1': 'data1', 'meta2': 'data2', 'metaN': 'dataN'}
130 self.instance = self.create_server(
131 image_id=self.image_ref,
132 flavor=self.flavor_ref,
133 key_name=self.keypair['name'],
134 security_groups=security_groups,
135 config_drive=CONF.compute_feature_enabled.config_drive,
136 metadata=self.md,
137 wait_until='ACTIVE')
Andrea Frittolif5da28b2013-12-06 07:08:07 +0000138 self.verify_ssh()
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +0900139 self.verify_metadata()
Alexander Gubanov509e2842015-06-09 15:29:51 +0300140 self.verify_metadata_on_config_drive()
Ghanshyam5a305c42014-08-27 14:24:58 +0900141 self.servers_client.delete_server(self.instance['id'])