Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 1 | |
| 2 | import json |
| 3 | import time |
| 4 | |
| 5 | from kong import exceptions |
| 6 | from kong import openstack |
Soren Hansen | 6adacc8 | 2011-09-09 13:34:35 +0200 | [diff] [blame] | 7 | from kong import tests |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 8 | from kong.common import ssh |
| 9 | |
| 10 | import unittest2 as unittest |
| 11 | |
| 12 | |
Soren Hansen | 6adacc8 | 2011-09-09 13:34:35 +0200 | [diff] [blame] | 13 | class ServerActionsTest(tests.FunctionalTest): |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 14 | |
| 15 | multi_node = openstack.Manager().config.env.multi_node |
| 16 | |
| 17 | def setUp(self): |
Soren Hansen | 6adacc8 | 2011-09-09 13:34:35 +0200 | [diff] [blame] | 18 | super(ServerActionsTest, self).setUp() |
Soren Hansen | d6b047a | 2011-09-09 13:39:32 +0200 | [diff] [blame] | 19 | self.os = openstack.Manager(self.nova) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 20 | |
Soren Hansen | a86180a | 2011-09-09 16:22:26 +0200 | [diff] [blame] | 21 | self.image_ref = self.glance['image_id'] |
| 22 | # self.image_ref_alt = self.os.config.env.image_ref_alt |
| 23 | self.flavor_ref = self.nova['flavor_ref'] |
| 24 | self.flavor_ref_alt = self.nova['flavor_ref_alt'] |
| 25 | self.ssh_timeout = self.nova['ssh_timeout'] |
| 26 | self.build_timeout = self.nova['build_timeout'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 27 | |
| 28 | self.server_password = 'testpwd' |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 29 | self.server_name = 'stacktester1' |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 30 | |
| 31 | expected_server = { |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 32 | 'name': self.server_name, |
| 33 | 'imageRef': self.image_ref, |
| 34 | 'flavorRef': self.flavor_ref, |
| 35 | 'adminPass': self.server_password, |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | created_server = self.os.nova.create_server(expected_server) |
| 39 | |
| 40 | self.server_id = created_server['id'] |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 41 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 42 | |
| 43 | server = self.os.nova.get_server(self.server_id) |
| 44 | |
| 45 | # KNOWN-ISSUE lp? |
| 46 | #self.access_ip = server['accessIPv4'] |
| 47 | self.access_ip = server['addresses']['public'][0]['addr'] |
| 48 | |
| 49 | # Ensure server came up |
| 50 | self._assert_ssh_password() |
| 51 | |
| 52 | def tearDown(self): |
| 53 | self.os.nova.delete_server(self.server_id) |
| 54 | |
| 55 | def _get_ssh_client(self, password): |
| 56 | return ssh.Client(self.access_ip, 'root', password, self.ssh_timeout) |
| 57 | |
| 58 | def _assert_ssh_password(self, password=None): |
| 59 | _password = password or self.server_password |
| 60 | client = self._get_ssh_client(_password) |
| 61 | self.assertTrue(client.test_connection_auth()) |
| 62 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 63 | def _wait_for_server_status(self, server_id, status): |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 64 | try: |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 65 | self.os.nova.wait_for_server_status(server_id, status, |
| 66 | timeout=self.build_timeout) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 67 | except exceptions.TimeoutException: |
| 68 | self.fail("Server failed to change status to %s" % status) |
| 69 | |
| 70 | def _get_boot_time(self): |
| 71 | """Return the time the server was started""" |
| 72 | output = self._read_file("/proc/uptime") |
| 73 | uptime = float(output.split().pop(0)) |
| 74 | return time.time() - uptime |
| 75 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 76 | def _write_file(self, filename, contents, password=None): |
| 77 | command = "echo -n %s > %s" % (contents, filename) |
| 78 | return self._exec_command(command, password) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 79 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 80 | def _read_file(self, filename, password=None): |
| 81 | command = "cat %s" % filename |
| 82 | return self._exec_command(command, password) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 83 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 84 | def _exec_command(self, command, password=None): |
| 85 | if password is None: |
| 86 | password = self.server_password |
| 87 | client = self._get_ssh_client(password) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 88 | return client.exec_command(command) |
| 89 | |
| 90 | def test_reboot_server_soft(self): |
| 91 | """Reboot a server (SOFT)""" |
| 92 | |
| 93 | # SSH and get the uptime |
| 94 | initial_time_started = self._get_boot_time() |
| 95 | |
| 96 | # Make reboot request |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 97 | post_body = json.dumps({'reboot': {'type': 'SOFT'}}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 98 | url = "/servers/%s/action" % self.server_id |
| 99 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 100 | self.assertEqual(response['status'], '202') |
| 101 | |
| 102 | # Assert status transition |
| 103 | # KNOWN-ISSUE |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 104 | #self._wait_for_server_status(self.server_id, 'REBOOT') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 105 | ssh_client = self._get_ssh_client(self.server_password) |
| 106 | ssh_client.connect_until_closed() |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 107 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 108 | |
| 109 | # SSH and verify uptime is less than before |
| 110 | post_reboot_time_started = self._get_boot_time() |
| 111 | self.assertTrue(initial_time_started < post_reboot_time_started) |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 112 | test_reboot_server_soft.tags = ['nova'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 113 | |
| 114 | def test_reboot_server_hard(self): |
| 115 | """Reboot a server (HARD)""" |
| 116 | |
| 117 | # SSH and get the uptime |
| 118 | initial_time_started = self._get_boot_time() |
| 119 | |
| 120 | # Make reboot request |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 121 | post_body = json.dumps({'reboot': {'type': 'HARD'}}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 122 | url = "/servers/%s/action" % self.server_id |
| 123 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 124 | self.assertEqual(response['status'], '202') |
| 125 | |
| 126 | # Assert status transition |
| 127 | # KNOWN-ISSUE |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 128 | #self._wait_for_server_status(self.server_id, 'HARD_REBOOT') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 129 | ssh_client = self._get_ssh_client(self.server_password) |
| 130 | ssh_client.connect_until_closed() |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 131 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 132 | |
| 133 | # SSH and verify uptime is less than before |
| 134 | post_reboot_time_started = self._get_boot_time() |
| 135 | self.assertTrue(initial_time_started < post_reboot_time_started) |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 136 | test_reboot_server_hard.tags = ['nova'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 137 | |
| 138 | def test_change_server_password(self): |
| 139 | """Change root password of a server""" |
| 140 | |
| 141 | # SSH into server using original password |
| 142 | self._assert_ssh_password() |
| 143 | |
| 144 | # Change server password |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 145 | post_body = json.dumps({'changePassword': {'adminPass': 'test123'}}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 146 | url = '/servers/%s/action' % self.server_id |
| 147 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 148 | |
| 149 | # Assert status transition |
| 150 | self.assertEqual('202', response['status']) |
| 151 | # KNOWN-ISSUE |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 152 | self._wait_for_server_status(self.server_id, 'PASSWORD') |
| 153 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 154 | |
| 155 | # SSH into server using new password |
| 156 | self._assert_ssh_password('test123') |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 157 | test_change_server_password.tags = ['nova'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 158 | |
Soren Hansen | a86180a | 2011-09-09 16:22:26 +0200 | [diff] [blame] | 159 | @tests.skip("rebuild test needs alternate image, but we only have one") |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 160 | def test_rebuild(self): |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 161 | """Rebuild a server""" |
| 162 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 163 | FILENAME = '/tmp/testfile' |
| 164 | CONTENTS = 'WORDS' |
| 165 | |
| 166 | # write file to server |
| 167 | self._write_file(FILENAME, CONTENTS) |
| 168 | self.assertEqual(self._read_file(FILENAME), CONTENTS) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 169 | |
| 170 | # Make rebuild request |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 171 | post_body = json.dumps({'rebuild': {'imageRef': self.image_ref_alt}}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 172 | url = '/servers/%s/action' % self.server_id |
| 173 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 174 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 175 | # check output |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 176 | self.assertEqual('202', response['status']) |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 177 | rebuilt_server = json.loads(body)['server'] |
| 178 | generated_password = rebuilt_server['adminPass'] |
| 179 | |
| 180 | # Ensure correct status transition |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 181 | # KNOWN-ISSUE |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 182 | #self._wait_for_server_status(self.server_id, 'REBUILD') |
| 183 | self._wait_for_server_status(self.server_id, 'BUILD') |
| 184 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 185 | |
| 186 | # Treats an issue where we ssh'd in too soon after rebuild |
| 187 | time.sleep(30) |
| 188 | |
| 189 | # Check that the instance's imageRef matches the new imageRef |
| 190 | server = self.os.nova.get_server(self.server_id) |
| 191 | ref_match = self.image_ref_alt == server['image']['links'][0]['href'] |
| 192 | id_match = self.image_ref_alt == server['image']['id'] |
| 193 | self.assertTrue(ref_match or id_match) |
| 194 | |
| 195 | # SSH into the server to ensure it came back up |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 196 | self._assert_ssh_password(generated_password) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 197 | |
| 198 | # make sure file is gone |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 199 | self.assertEqual(self._read_file(FILENAME, generated_password), '') |
| 200 | |
| 201 | # test again with a specified password |
| 202 | self._write_file(FILENAME, CONTENTS, generated_password) |
| 203 | _contents = self._read_file(FILENAME, generated_password) |
| 204 | self.assertEqual(_contents, CONTENTS) |
| 205 | |
| 206 | specified_password = 'some_password' |
| 207 | |
| 208 | # Make rebuild request |
| 209 | post_body = json.dumps({ |
| 210 | 'rebuild': { |
| 211 | 'imageRef': self.image_ref, |
| 212 | 'adminPass': specified_password, |
| 213 | } |
| 214 | }) |
| 215 | url = '/servers/%s/action' % self.server_id |
| 216 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 217 | |
| 218 | # check output |
| 219 | self.assertEqual('202', response['status']) |
| 220 | rebuilt_server = json.loads(body)['server'] |
| 221 | self.assertEqual(rebuilt_server['adminPass'], specified_password) |
| 222 | |
| 223 | # Ensure correct status transition |
| 224 | # KNOWN-ISSUE |
| 225 | #self._wait_for_server_status(self.server_id, 'REBUILD') |
| 226 | self._wait_for_server_status(self.server_id, 'BUILD') |
| 227 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
| 228 | |
| 229 | # Treats an issue where we ssh'd in too soon after rebuild |
| 230 | time.sleep(30) |
| 231 | |
| 232 | # Check that the instance's imageRef matches the new imageRef |
| 233 | server = self.os.nova.get_server(self.server_id) |
| 234 | ref_match = self.image_ref == server['image']['links'][0]['href'] |
| 235 | id_match = self.image_ref == server['image']['id'] |
| 236 | self.assertTrue(ref_match or id_match) |
| 237 | |
| 238 | # SSH into the server to ensure it came back up |
| 239 | self._assert_ssh_password(specified_password) |
| 240 | |
| 241 | # make sure file is gone |
| 242 | self.assertEqual(self._read_file(FILENAME, specified_password), '') |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 243 | test_rebuild.tags = ['nova'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 244 | |
| 245 | @unittest.skipIf(not multi_node, 'Multiple compute nodes required') |
| 246 | def test_resize_server_confirm(self): |
| 247 | """Resize a server""" |
| 248 | # Make resize request |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 249 | post_body = json.dumps({'resize': {'flavorRef': self.flavor_ref_alt}}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 250 | url = '/servers/%s/action' % self.server_id |
| 251 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 252 | |
| 253 | # Wait for status transition |
| 254 | self.assertEqual('202', response['status']) |
| 255 | # KNOWN-ISSUE |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 256 | #self._wait_for_server_status(self.server_id, 'VERIFY_RESIZE') |
| 257 | self._wait_for_server_status(self.server_id, 'RESIZE-CONFIRM') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 258 | |
| 259 | # Ensure API reports new flavor |
| 260 | server = self.os.nova.get_server(self.server_id) |
| 261 | self.assertEqual(self.flavor_ref_alt, server['flavor']['id']) |
| 262 | |
| 263 | #SSH into the server to ensure it came back up |
| 264 | self._assert_ssh_password() |
| 265 | |
| 266 | # Make confirmResize request |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 267 | post_body = json.dumps({'confirmResize': 'null'}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 268 | url = '/servers/%s/action' % self.server_id |
| 269 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 270 | |
| 271 | # Wait for status transition |
| 272 | self.assertEqual('204', response['status']) |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 273 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 274 | |
| 275 | # Ensure API still reports new flavor |
| 276 | server = self.os.nova.get_server(self.server_id) |
| 277 | self.assertEqual(self.flavor_ref_alt, server['flavor']['id']) |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 278 | test_resize_server_confirm.tags = ['nova'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 279 | |
| 280 | @unittest.skipIf(not multi_node, 'Multiple compute nodes required') |
| 281 | def test_resize_server_revert(self): |
| 282 | """Resize a server, then revert""" |
| 283 | |
| 284 | # Make resize request |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 285 | post_body = json.dumps({'resize': {'flavorRef': self.flavor_ref_alt}}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 286 | url = '/servers/%s/action' % self.server_id |
| 287 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 288 | |
| 289 | # Wait for status transition |
| 290 | self.assertEqual('202', response['status']) |
| 291 | # KNOWN-ISSUE |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 292 | #self._wait_for_server_status(self.server_id, 'VERIFY_RESIZE') |
| 293 | self._wait_for_server_status(self.server_id, 'RESIZE-CONFIRM') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 294 | |
| 295 | # SSH into the server to ensure it came back up |
| 296 | self._assert_ssh_password() |
| 297 | |
| 298 | # Ensure API reports new flavor |
| 299 | server = self.os.nova.get_server(self.server_id) |
| 300 | self.assertEqual(self.flavor_ref_alt, server['flavor']['id']) |
| 301 | |
| 302 | # Make revertResize request |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 303 | post_body = json.dumps({'revertResize': 'null'}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 304 | url = '/servers/%s/action' % self.server_id |
| 305 | response, body = self.os.nova.request('POST', url, body=post_body) |
| 306 | |
| 307 | # Assert status transition |
| 308 | self.assertEqual('202', response['status']) |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 309 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 310 | |
| 311 | # Ensure flavor ref was reverted to original |
| 312 | server = self.os.nova.get_server(self.server_id) |
| 313 | self.assertEqual(self.flavor_ref, server['flavor']['id']) |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 314 | test_resize_server_revert.tags = ['nova'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 315 | |
| 316 | |
| 317 | class SnapshotTests(unittest.TestCase): |
| 318 | |
| 319 | def setUp(self): |
| 320 | self.os = openstack.Manager() |
| 321 | |
| 322 | self.image_ref = self.os.config.env.image_ref |
| 323 | self.flavor_ref = self.os.config.env.flavor_ref |
| 324 | self.ssh_timeout = self.os.config.nova.ssh_timeout |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 325 | self.build_timeout = self.os.config.nova.build_timeout |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 326 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 327 | self.server_name = 'stacktester1' |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 328 | |
| 329 | expected_server = { |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 330 | 'name': self.server_name, |
| 331 | 'imageRef': self.image_ref, |
| 332 | 'flavorRef': self.flavor_ref, |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | created_server = self.os.nova.create_server(expected_server) |
| 336 | self.server_id = created_server['id'] |
| 337 | |
| 338 | def tearDown(self): |
| 339 | self.os.nova.delete_server(self.server_id) |
| 340 | |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 341 | def _wait_for_server_status(self, server_id, status): |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 342 | try: |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 343 | self.os.nova.wait_for_server_status(server_id, status, |
| 344 | timeout=self.build_timeout) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 345 | except exceptions.TimeoutException: |
| 346 | self.fail("Server failed to change status to %s" % status) |
| 347 | |
| 348 | def test_snapshot_server_active(self): |
| 349 | """Create image from an existing server""" |
| 350 | |
| 351 | # Wait for server to come up before running this test |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 352 | self._wait_for_server_status(self.server_id, 'ACTIVE') |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 353 | |
| 354 | # Create snapshot |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 355 | image_data = {'name': 'backup'} |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 356 | req_body = json.dumps({'createImage': image_data}) |
| 357 | url = '/servers/%s/action' % self.server_id |
| 358 | response, body = self.os.nova.request('POST', url, body=req_body) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 359 | |
| 360 | self.assertEqual(response['status'], '202') |
| 361 | image_ref = response['location'] |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 362 | snapshot_id = image_ref.rsplit('/', 1)[1] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 363 | |
| 364 | # Get snapshot and check its attributes |
| 365 | resp, body = self.os.nova.request('GET', '/images/%s' % snapshot_id) |
| 366 | snapshot = json.loads(body)['image'] |
| 367 | self.assertEqual(snapshot['name'], image_data['name']) |
| 368 | server_ref = snapshot['server']['links'][0]['href'] |
| 369 | self.assertTrue(server_ref.endswith('/%s' % self.server_id)) |
| 370 | |
| 371 | # Ensure image is actually created |
| 372 | self.os.nova.wait_for_image_status(snapshot['id'], 'ACTIVE') |
| 373 | |
| 374 | # Cleaning up |
| 375 | self.os.nova.request('DELETE', '/images/%s' % snapshot_id) |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 376 | test_snapshot_server_active.tags = ['nova', 'glance'] |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 377 | |
| 378 | def test_snapshot_server_inactive(self): |
| 379 | """Ensure inability to snapshot server in BUILD state""" |
| 380 | |
| 381 | # Create snapshot |
Soren Hansen | 5f4ad83 | 2011-09-09 14:08:19 +0200 | [diff] [blame] | 382 | req_body = json.dumps({'createImage': {'name': 'backup'}}) |
Soren Hansen | bc1d3a0 | 2011-09-08 13:33:17 +0200 | [diff] [blame] | 383 | url = '/servers/%s/action' % self.server_id |
| 384 | response, body = self.os.nova.request('POST', url, body=req_body) |
| 385 | |
| 386 | # KNOWN-ISSUE - we shouldn't be able to snapshot a building server |
| 387 | #self.assertEqual(response['status'], '400') # what status code? |
| 388 | self.assertEqual(response['status'], '202') |
| 389 | snapshot_id = response['location'].rsplit('/', 1)[1] |
| 390 | # Delete image for now, won't need this once correct status code is in |
| 391 | self.os.nova.request('DELETE', '/images/%s' % snapshot_id) |
Soren Hansen | fce58c5 | 2011-09-09 16:07:13 +0200 | [diff] [blame] | 392 | test_snapshot_server_inactive.tags = ['nova', 'glance'] |