|
| 1 | +# Copyright 2020, Red Hat, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | + |
| 17 | +from oslo_utils.fixture import uuidsentinel as uuids |
| 18 | + |
| 19 | +from nova.tests.functional.api import client |
| 20 | +from nova.tests.functional import integrated_helpers |
| 21 | + |
| 22 | + |
| 23 | +class TestNonBootableImageMeta(integrated_helpers._IntegratedTestBase): |
| 24 | + """Regression test for bug 1895696 |
| 25 | +
|
| 26 | + This regression test asserts the behaviour of server creation requests when |
| 27 | + using an image with nonbootable properties either directly in the request |
| 28 | + or to create a volume that is then booted from. |
| 29 | + """ |
| 30 | + |
| 31 | + microversion = 'latest' |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + super().setUp() |
| 35 | + |
| 36 | + # Add an image to the Glance fixture with cinder_encryption_key set |
| 37 | + timestamp = datetime.datetime(2011, 1, 1, 1, 2, 3) |
| 38 | + cinder_encrypted_image = { |
| 39 | + 'id': uuids.cinder_encrypted_image_uuid, |
| 40 | + 'name': 'cinder_encryption_key_image', |
| 41 | + 'created_at': timestamp, |
| 42 | + 'updated_at': timestamp, |
| 43 | + 'deleted_at': None, |
| 44 | + 'deleted': False, |
| 45 | + 'status': 'active', |
| 46 | + 'is_public': False, |
| 47 | + 'container_format': 'ova', |
| 48 | + 'disk_format': 'vhd', |
| 49 | + 'size': '74185822', |
| 50 | + 'min_ram': 0, |
| 51 | + 'min_disk': 0, |
| 52 | + 'protected': False, |
| 53 | + 'visibility': 'public', |
| 54 | + 'tags': [], |
| 55 | + 'properties': { |
| 56 | + 'cinder_encryption_key_id': uuids.cinder_encryption_key_id, |
| 57 | + } |
| 58 | + } |
| 59 | + self.glance.create(None, cinder_encrypted_image) |
| 60 | + |
| 61 | + # Mock out nova.volume.cinder.API.{create,get} so that when n-api |
| 62 | + # requests that c-api create a volume from the above image that the |
| 63 | + # response includes cinder_encryption_key_id in the |
| 64 | + # volume_image_metadata |
| 65 | + cinder_encrypted_volume = { |
| 66 | + 'status': 'available', |
| 67 | + 'display_name': 'cinder_encrypted_volume', |
| 68 | + 'attach_status': 'detached', |
| 69 | + 'id': uuids.cinder_encrypted_volume_uuid, |
| 70 | + 'multiattach': False, |
| 71 | + 'size': 1, |
| 72 | + 'encrypted': True, |
| 73 | + 'volume_image_metadata': { |
| 74 | + 'cinder_encryption_key_id': uuids.cinder_encryption_key_id |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + def fake_cinder_create(self_api, context, size, name, description, |
| 79 | + snapshot=None, image_id=None, volume_type=None, metadata=None, |
| 80 | + availability_zone=None): |
| 81 | + if image_id == uuids.cinder_encrypted_image_uuid: |
| 82 | + return cinder_encrypted_volume |
| 83 | + self.stub_out( |
| 84 | + 'nova.volume.cinder.API.create', fake_cinder_create) |
| 85 | + |
| 86 | + def fake_cinder_get(self_api, context, volume_id, microversion=None): |
| 87 | + return cinder_encrypted_volume |
| 88 | + self.stub_out( |
| 89 | + 'nova.volume.cinder.API.get', fake_cinder_get) |
| 90 | + |
| 91 | + def test_nonbootable_metadata_image_metadata(self): |
| 92 | + """Assert behaviour when booting from an encrypted image |
| 93 | + """ |
| 94 | + server = self._build_server( |
| 95 | + name='test_nonbootable_metadata_bfv_image_metadata', |
| 96 | + image_uuid=uuids.cinder_encrypted_image_uuid, |
| 97 | + networks='none' |
| 98 | + ) |
| 99 | + # NOTE(lyarwood): This should always fail as Nova will attempt to boot |
| 100 | + # directly from this encrypted image. |
| 101 | + ex = self.assertRaises( |
| 102 | + client.OpenStackApiException, self.api.post_server, |
| 103 | + {'server': server}) |
| 104 | + self.assertEqual(400, ex.response.status_code) |
| 105 | + self.assertIn( |
| 106 | + "Direct booting of an image uploaded from an encrypted volume is " |
| 107 | + "unsupported", str(ex)) |
| 108 | + |
| 109 | + def test_nonbootable_metadata_bfv_image_metadata(self): |
| 110 | + """Assert behaviour when n-api creates volume using an encrypted image |
| 111 | + """ |
| 112 | + server = self._build_server( |
| 113 | + name='test_nonbootable_metadata_bfv_image_metadata', |
| 114 | + image_uuid='', networks='none' |
| 115 | + ) |
| 116 | + # TODO(lyarwood): Merge this into _build_server |
| 117 | + server['block_device_mapping_v2'] = [{ |
| 118 | + 'source_type': 'image', |
| 119 | + 'destination_type': 'volume', |
| 120 | + 'boot_index': 0, |
| 121 | + 'uuid': uuids.cinder_encrypted_image_uuid, |
| 122 | + 'volume_size': 1, |
| 123 | + }] |
| 124 | + |
| 125 | + # FIXME(lyarwood) n-api should ignore cinder_encryption_key_id in the |
| 126 | + # original image in this case and accept the request. |
| 127 | + ex = self.assertRaises( |
| 128 | + client.OpenStackApiException, self.api.post_server, |
| 129 | + {'server': server}) |
| 130 | + self.assertEqual(400, ex.response.status_code) |
| 131 | + self.assertIn( |
| 132 | + "Direct booting of an image uploaded from an encrypted volume is " |
| 133 | + "unsupported", str(ex)) |
| 134 | + |
| 135 | + def test_nonbootable_metadata_bfv_volume_image_metadata(self): |
| 136 | + """Assert behaviour when c-api has created volume using encrypted image |
| 137 | + """ |
| 138 | + server = self._build_server( |
| 139 | + name='test_nonbootable_metadata_bfv_volume_image_metadata', |
| 140 | + image_uuid='', networks='none' |
| 141 | + ) |
| 142 | + # TODO(lyarwood): Merge this into _build_server |
| 143 | + server['block_device_mapping_v2'] = [{ |
| 144 | + 'source_type': 'volume', |
| 145 | + 'destination_type': 'volume', |
| 146 | + 'boot_index': 0, |
| 147 | + 'uuid': uuids.cinder_encrypted_volume_uuid, |
| 148 | + }] |
| 149 | + |
| 150 | + # FIXME(lyarwood) n-api should ignore cinder_encryption_key_id in the |
| 151 | + # volume volume_image_metadata in this case and accept the request. |
| 152 | + ex = self.assertRaises( |
| 153 | + client.OpenStackApiException, self.api.post_server, |
| 154 | + {'server': server}) |
| 155 | + self.assertEqual(400, ex.response.status_code) |
| 156 | + self.assertIn( |
| 157 | + "Direct booting of an image uploaded from an encrypted volume is " |
| 158 | + "unsupported", str(ex)) |
0 commit comments