blob: 91669d050f5042b4717abc19375b81f91b7da31f [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
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090023from tempest import test
Jay Pipes051075a2012-04-28 17:39:37 -040024
Matthew Treinish6c072292014-01-29 19:15:52 +000025CONF = config.CONF
26
Jay Pipes051075a2012-04-28 17:39:37 -040027LOG = logging.getLogger(__name__)
28
29
Ghanshyam5a305c42014-08-27 14:24:58 +090030class TestServerBasicOps(manager.ScenarioTest):
Jay Pipes051075a2012-04-28 17:39:37 -040031
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000032 """The test suite for server basic operations
Jay Pipes051075a2012-04-28 17:39:37 -040033
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000034 This smoke test case follows this basic set of operations:
Jay Pipes051075a2012-04-28 17:39:37 -040035 * Create a keypair for use in launching an instance
36 * Create a security group to control network access in instance
37 * Add simple permissive rules to the security group
38 * Launch an instance
ghanshyam416c94c2014-10-02 13:47:25 +090039 * Perform ssh to instance
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090040 * Verify metadata service
Alexander Gubanov509e2842015-06-09 15:29:51 +030041 * Verify metadata on config_drive
Jay Pipes051075a2012-04-28 17:39:37 -040042 * Terminate the instance
43 """
44
Andrea Frittolif5da28b2013-12-06 07:08:07 +000045 def setUp(self):
46 super(TestServerBasicOps, self).setUp()
Matthew Treinishf2c45012016-06-22 21:13:42 -040047 self.image_ref = CONF.compute.image_ref
48 self.flavor_ref = CONF.compute.flavor_ref
49 self.run_ssh = CONF.validation.run_validation
50 self.ssh_user = CONF.validation.image_ssh_user
Andrea Frittolif5da28b2013-12-06 07:08:07 +000051
Jordan Pittierbbb17122016-01-26 17:10:55 +010052 def verify_ssh(self, keypair):
Andrea Frittolif5da28b2013-12-06 07:08:07 +000053 if self.run_ssh:
54 # Obtain a floating IP
Alexander Gubanov2388e2a2015-11-07 11:16:28 +020055 self.fip = self.create_floating_ip(self.instance)['ip']
Andrea Frittolif5da28b2013-12-06 07:08:07 +000056 # Check ssh
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090057 self.ssh_client = self.get_remote_client(
Sean Dague20e98612016-01-06 14:33:28 -050058 ip_address=self.fip,
JordanP3fe2dc32014-11-17 13:06:01 +010059 username=self.image_utils.ssh_user(self.image_ref),
Jordan Pittierbbb17122016-01-26 17:10:55 +010060 private_key=keypair['private_key'])
Andrea Frittolif5da28b2013-12-06 07:08:07 +000061
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090062 def verify_metadata(self):
63 if self.run_ssh and CONF.compute_feature_enabled.metadata_service:
64 # Verify metadata service
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030065 md_url = 'http://169.254.169.254/latest/meta-data/public-ipv4'
66
67 def exec_cmd_and_verify_output():
68 cmd = 'curl ' + md_url
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030069 result = self.ssh_client.exec_command(cmd)
70 if result:
71 msg = ('Failed while verifying metadata on server. Result '
Alexander Gubanov2388e2a2015-11-07 11:16:28 +020072 'of command "%s" is NOT "%s".' % (cmd, self.fip))
73 self.assertEqual(self.fip, result, msg)
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030074 return 'Verification is successful!'
75
76 if not test.call_until_true(exec_cmd_and_verify_output,
77 CONF.compute.build_timeout,
78 CONF.compute.build_interval):
79 raise exceptions.TimeoutException('Timed out while waiting to '
80 'verify metadata on server. '
81 '%s is empty.' % md_url)
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090082
Alexander Gubanov509e2842015-06-09 15:29:51 +030083 def verify_metadata_on_config_drive(self):
84 if self.run_ssh and CONF.compute_feature_enabled.config_drive:
85 # Verify metadata on config_drive
86 cmd_blkid = 'blkid -t LABEL=config-2 -o device'
87 dev_name = self.ssh_client.exec_command(cmd_blkid)
88 dev_name = dev_name.rstrip()
89 self.ssh_client.exec_command('sudo mount %s /mnt' % dev_name)
90 cmd_md = 'sudo cat /mnt/openstack/latest/meta_data.json'
91 result = self.ssh_client.exec_command(cmd_md)
92 self.ssh_client.exec_command('sudo umount /mnt')
93 result = json.loads(result)
94 self.assertIn('meta', result)
95 msg = ('Failed while verifying metadata on config_drive on server.'
96 ' Result of command "%s" is NOT "%s".' % (cmd_md, self.md))
97 self.assertEqual(self.md, result['meta'], msg)
98
Chris Hoge7579c1a2015-02-26 14:12:15 -080099 @test.idempotent_id('7fff3fb3-91d8-4fd0-bd7d-0204f1f180ba')
Sean Dague3c634d12015-04-27 12:09:19 -0400100 @test.attr(type='smoke')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900101 @test.services('compute', 'network')
Tong Liu9bee3b92016-03-23 23:53:43 +0000102 def test_server_basic_ops(self):
Jordan Pittierbbb17122016-01-26 17:10:55 +0100103 keypair = self.create_keypair()
Yair Fried1fc32a12014-08-04 09:11:30 +0300104 self.security_group = self._create_security_group()
lanoux5fc14522015-09-21 08:17:35 +0000105 security_groups = [{'name': self.security_group['name']}]
106 self.md = {'meta1': 'data1', 'meta2': 'data2', 'metaN': 'dataN'}
107 self.instance = self.create_server(
108 image_id=self.image_ref,
109 flavor=self.flavor_ref,
Jordan Pittierbbb17122016-01-26 17:10:55 +0100110 key_name=keypair['name'],
lanoux5fc14522015-09-21 08:17:35 +0000111 security_groups=security_groups,
112 config_drive=CONF.compute_feature_enabled.config_drive,
113 metadata=self.md,
114 wait_until='ACTIVE')
Jordan Pittierbbb17122016-01-26 17:10:55 +0100115 self.verify_ssh(keypair)
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +0900116 self.verify_metadata()
Alexander Gubanov509e2842015-06-09 15:29:51 +0300117 self.verify_metadata_on_config_drive()
Ghanshyam5a305c42014-08-27 14:24:58 +0900118 self.servers_client.delete_server(self.instance['id'])