blob: 3aab2b88f946503384a4882d0fad5b7f1f2c1f1e [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
Alexander Gubanovdac47382016-06-24 16:49:36 +030017import re
Alexander Gubanov509e2842015-06-09 15:29:51 +030018
zhufl44fce682016-11-23 15:37:49 +080019from tempest.common import waiters
Matthew Treinish6c072292014-01-29 19:15:52 +000020from tempest import config
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030021from tempest import exceptions
Jordan Pittier35a63752016-08-30 13:09:12 +020022from tempest.lib.common.utils import test_utils
Sean Dague6dbc6da2013-05-08 17:49:46 -040023from tempest.scenario import manager
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 -040028
Ghanshyam5a305c42014-08-27 14:24:58 +090029class TestServerBasicOps(manager.ScenarioTest):
Jay Pipes051075a2012-04-28 17:39:37 -040030
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000031 """The test suite for server basic operations
Jay Pipes051075a2012-04-28 17:39:37 -040032
Ken'ichi Ohmichic4e4f1c2015-11-17 08:16:12 +000033 This smoke test case follows this basic set of operations:
Jay Pipes051075a2012-04-28 17:39:37 -040034 * Create a keypair for use in launching an instance
35 * Create a security group to control network access in instance
36 * Add simple permissive rules to the security group
37 * Launch an instance
ghanshyam416c94c2014-10-02 13:47:25 +090038 * Perform ssh to instance
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090039 * Verify metadata service
Alexander Gubanov509e2842015-06-09 15:29:51 +030040 * Verify metadata on config_drive
Jay Pipes051075a2012-04-28 17:39:37 -040041 * Terminate the instance
42 """
43
Andrea Frittolif5da28b2013-12-06 07:08:07 +000044 def setUp(self):
45 super(TestServerBasicOps, self).setUp()
Matthew Treinishf2c45012016-06-22 21:13:42 -040046 self.image_ref = CONF.compute.image_ref
47 self.flavor_ref = CONF.compute.flavor_ref
48 self.run_ssh = CONF.validation.run_validation
49 self.ssh_user = CONF.validation.image_ssh_user
Andrea Frittolif5da28b2013-12-06 07:08:07 +000050
Jordan Pittierbbb17122016-01-26 17:10:55 +010051 def verify_ssh(self, keypair):
Andrea Frittolif5da28b2013-12-06 07:08:07 +000052 if self.run_ssh:
53 # Obtain a floating IP
Alexander Gubanov2388e2a2015-11-07 11:16:28 +020054 self.fip = self.create_floating_ip(self.instance)['ip']
Andrea Frittolif5da28b2013-12-06 07:08:07 +000055 # Check ssh
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090056 self.ssh_client = self.get_remote_client(
Sean Dague20e98612016-01-06 14:33:28 -050057 ip_address=self.fip,
YAMAMOTO Takashi42189bf2016-06-24 16:02:53 +090058 username=self.ssh_user,
Jordan Pittierbbb17122016-01-26 17:10:55 +010059 private_key=keypair['private_key'])
Andrea Frittolif5da28b2013-12-06 07:08:07 +000060
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090061 def verify_metadata(self):
62 if self.run_ssh and CONF.compute_feature_enabled.metadata_service:
63 # Verify metadata service
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030064 md_url = 'http://169.254.169.254/latest/meta-data/public-ipv4'
65
66 def exec_cmd_and_verify_output():
67 cmd = 'curl ' + md_url
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030068 result = self.ssh_client.exec_command(cmd)
69 if result:
70 msg = ('Failed while verifying metadata on server. Result '
Alexander Gubanov2388e2a2015-11-07 11:16:28 +020071 'of command "%s" is NOT "%s".' % (cmd, self.fip))
72 self.assertEqual(self.fip, result, msg)
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030073 return 'Verification is successful!'
74
Jordan Pittier35a63752016-08-30 13:09:12 +020075 if not test_utils.call_until_true(exec_cmd_and_verify_output,
76 CONF.compute.build_timeout,
77 CONF.compute.build_interval):
Yaroslav Lobankov117a48f2015-08-11 11:40:44 +030078 raise exceptions.TimeoutException('Timed out while waiting to '
79 'verify metadata on server. '
80 '%s is empty.' % md_url)
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +090081
Clark Boylandf73bdf2016-07-06 09:55:28 -070082 def _mount_config_drive(self):
83 cmd_blkid = 'blkid | grep -i config-2'
84 result = self.ssh_client.exec_command(cmd_blkid)
85 dev_name = re.match('([^:]+)', result).group()
86 self.ssh_client.exec_command('sudo mount %s /mnt' % dev_name)
87
88 def _unmount_config_drive(self):
89 self.ssh_client.exec_command('sudo umount /mnt')
90
Alexander Gubanov509e2842015-06-09 15:29:51 +030091 def verify_metadata_on_config_drive(self):
92 if self.run_ssh and CONF.compute_feature_enabled.config_drive:
93 # Verify metadata on config_drive
Clark Boylandf73bdf2016-07-06 09:55:28 -070094 self._mount_config_drive()
Alexander Gubanov509e2842015-06-09 15:29:51 +030095 cmd_md = 'sudo cat /mnt/openstack/latest/meta_data.json'
96 result = self.ssh_client.exec_command(cmd_md)
Clark Boylandf73bdf2016-07-06 09:55:28 -070097 self._unmount_config_drive()
Alexander Gubanov509e2842015-06-09 15:29:51 +030098 result = json.loads(result)
99 self.assertIn('meta', result)
100 msg = ('Failed while verifying metadata on config_drive on server.'
101 ' Result of command "%s" is NOT "%s".' % (cmd_md, self.md))
102 self.assertEqual(self.md, result['meta'], msg)
103
Clark Boylandf73bdf2016-07-06 09:55:28 -0700104 def verify_networkdata_on_config_drive(self):
105 if self.run_ssh and CONF.compute_feature_enabled.config_drive:
106 # Verify network data on config_drive
107 self._mount_config_drive()
108 cmd_md = 'sudo cat /mnt/openstack/latest/network_data.json'
109 result = self.ssh_client.exec_command(cmd_md)
110 self._unmount_config_drive()
111 result = json.loads(result)
112 self.assertIn('services', result)
113 self.assertIn('links', result)
114 self.assertIn('networks', result)
115 # TODO(clarkb) construct network_data from known network
116 # instance info and do direct comparison.
117
Chris Hoge7579c1a2015-02-26 14:12:15 -0800118 @test.idempotent_id('7fff3fb3-91d8-4fd0-bd7d-0204f1f180ba')
Sean Dague3c634d12015-04-27 12:09:19 -0400119 @test.attr(type='smoke')
Masayuki Igawa4ded9f02014-02-17 15:05:59 +0900120 @test.services('compute', 'network')
Tong Liu9bee3b92016-03-23 23:53:43 +0000121 def test_server_basic_ops(self):
Jordan Pittierbbb17122016-01-26 17:10:55 +0100122 keypair = self.create_keypair()
Jordan Pittier56c125e2016-09-16 16:20:22 +0200123 security_group = self._create_security_group()
lanoux5fc14522015-09-21 08:17:35 +0000124 self.md = {'meta1': 'data1', 'meta2': 'data2', 'metaN': 'dataN'}
125 self.instance = self.create_server(
126 image_id=self.image_ref,
127 flavor=self.flavor_ref,
Jordan Pittierbbb17122016-01-26 17:10:55 +0100128 key_name=keypair['name'],
Jordan Pittier56c125e2016-09-16 16:20:22 +0200129 security_groups=[{'name': security_group['name']}],
lanoux5fc14522015-09-21 08:17:35 +0000130 config_drive=CONF.compute_feature_enabled.config_drive,
131 metadata=self.md,
132 wait_until='ACTIVE')
Jordan Pittierbbb17122016-01-26 17:10:55 +0100133 self.verify_ssh(keypair)
YAMAMOTO Takashi1f62af22015-06-16 03:29:50 +0900134 self.verify_metadata()
Alexander Gubanov509e2842015-06-09 15:29:51 +0300135 self.verify_metadata_on_config_drive()
Clark Boylandf73bdf2016-07-06 09:55:28 -0700136 self.verify_networkdata_on_config_drive()
Ghanshyam5a305c42014-08-27 14:24:58 +0900137 self.servers_client.delete_server(self.instance['id'])
zhufl44fce682016-11-23 15:37:49 +0800138 waiters.wait_for_server_termination(
139 self.servers_client, self.instance['id'], ignore_error=False)