Skip to content

Commit b0cd985

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "image_meta: Provide image_ref as the id when fetching from instance"
2 parents 696fbab + 9d037f7 commit b0cd985

File tree

12 files changed

+149
-105
lines changed

12 files changed

+149
-105
lines changed

nova/objects/image_meta.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ def from_instance(cls, instance):
124124
"""
125125
sysmeta = utils.instance_sys_meta(instance)
126126
image_meta = utils.get_image_from_system_metadata(sysmeta)
127+
128+
# NOTE(lyarwood): Provide the id of the image in image_meta if it
129+
# wasn't persisted in the system_metadata of the instance previously.
130+
# This is only provided to allow users of image_meta to avoid the need
131+
# to pass around references to instance.image_ref alongside image_meta.
132+
if image_meta.get('id') is None and instance.image_ref:
133+
image_meta['id'] = instance.image_ref
134+
127135
return cls.from_dict(image_meta)
128136

129137
@classmethod

nova/tests/unit/api/openstack/compute/test_server_actions.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def test_reboot_soft_with_hard_in_progress_raises_conflict(self):
267267
def _test_rebuild_preserve_ephemeral(self, value=None):
268268
return_server = fakes.fake_compute_get(
269269
project_id=fakes.FAKE_PROJECT_ID,
270-
image_ref='2',
270+
image_ref=uuids.image_ref,
271271
vm_state=vm_states.ACTIVE,
272272
host='fake_host')
273273
self.stub_out('nova.compute.api.API.get', return_server)
@@ -302,7 +302,7 @@ def test_rebuild_preserve_ephemeral_default(self):
302302
def test_rebuild_accepted_minimum(self):
303303
return_server = fakes.fake_compute_get(
304304
project_id=fakes.FAKE_PROJECT_ID,
305-
image_ref='2',
305+
image_ref=uuids.image_ref,
306306
vm_state=vm_states.ACTIVE, host='fake_host')
307307
self.stub_out('nova.compute.api.API.get', return_server)
308308
self_href = 'http://localhost/v2/servers/%s' % FAKE_UUID
@@ -316,7 +316,7 @@ def test_rebuild_accepted_minimum(self):
316316
robj = self.controller._action_rebuild(self.req, FAKE_UUID, body=body)
317317
body = robj.obj
318318

319-
self.assertEqual(body['server']['image']['id'], '2')
319+
self.assertEqual(body['server']['image']['id'], uuids.image_ref)
320320
self.assertEqual(len(body['server']['adminPass']),
321321
CONF.password_length)
322322

@@ -361,7 +361,7 @@ def test_rebuild_accepted_minimum_pass_disabled(self):
361361

362362
return_server = fakes.fake_compute_get(
363363
project_id=fakes.FAKE_PROJECT_ID,
364-
image_ref='2',
364+
image_ref=uuids.image_ref,
365365
vm_state=vm_states.ACTIVE, host='fake_host')
366366
self.stub_out('nova.compute.api.API.get', return_server)
367367
self_href = 'http://localhost/v2/servers/%s' % FAKE_UUID
@@ -375,7 +375,7 @@ def test_rebuild_accepted_minimum_pass_disabled(self):
375375
robj = self.controller._action_rebuild(self.req, FAKE_UUID, body=body)
376376
body = robj.obj
377377

378-
self.assertEqual(body['server']['image']['id'], '2')
378+
self.assertEqual(body['server']['image']['id'], uuids.image_ref)
379379
self.assertNotIn("adminPass", body['server'])
380380

381381
self.assertEqual(robj['location'], self_href)
@@ -473,7 +473,7 @@ def test_rebuild_bad_entity(self):
473473
def test_rebuild_admin_pass(self):
474474
return_server = fakes.fake_compute_get(
475475
project_id=fakes.FAKE_PROJECT_ID,
476-
image_ref='2',
476+
image_ref=uuids.image_ref,
477477
vm_state=vm_states.ACTIVE, host='fake_host')
478478
self.stub_out('nova.compute.api.API.get', return_server)
479479

@@ -487,7 +487,7 @@ def test_rebuild_admin_pass(self):
487487
body = self.controller._action_rebuild(self.req, FAKE_UUID,
488488
body=body).obj
489489

490-
self.assertEqual(body['server']['image']['id'], '2')
490+
self.assertEqual(body['server']['image']['id'], uuids.image_ref)
491491
self.assertEqual(body['server']['adminPass'], 'asdf')
492492

493493
def test_rebuild_admin_pass_pass_disabled(self):
@@ -497,7 +497,7 @@ def test_rebuild_admin_pass_pass_disabled(self):
497497

498498
return_server = fakes.fake_compute_get(
499499
project_id=fakes.FAKE_PROJECT_ID,
500-
image_ref='2',
500+
image_ref=FAKE_UUID,
501501
vm_state=vm_states.ACTIVE, host='fake_host')
502502
self.stub_out('nova.compute.api.API.get', return_server)
503503

@@ -511,7 +511,7 @@ def test_rebuild_admin_pass_pass_disabled(self):
511511
body = self.controller._action_rebuild(self.req, FAKE_UUID,
512512
body=body).obj
513513

514-
self.assertEqual(body['server']['image']['id'], '2')
514+
self.assertEqual(body['server']['image']['id'], FAKE_UUID)
515515
self.assertNotIn('adminPass', body['server'])
516516

517517
def test_rebuild_server_not_found(self):
@@ -578,12 +578,20 @@ def test_rebuild_when_kernel_not_exists(self):
578578

579579
def return_image_meta(*args, **kwargs):
580580
image_meta_table = {
581-
'2': {'id': uuids.image_id, 'status': 'active',
582-
'container_format': 'ari'},
583-
'155d900f-4e14-4e4c-a73d-069cbf4541e6':
584-
{'id': uuids.image_id, 'status': 'active',
585-
'container_format': 'raw',
586-
'properties': {'kernel_id': 1, 'ramdisk_id': 2}},
581+
uuids.image_1_id: {
582+
'id': uuids.image_1_id,
583+
'status': 'active',
584+
'container_format': 'ari'
585+
},
586+
uuids.image_2_id: {
587+
'id': uuids.image_2_id,
588+
'status': 'active',
589+
'container_format': 'raw',
590+
'properties': {
591+
'kernel_id': uuids.kernel_id,
592+
'ramdisk_id': uuids.ramdisk_id
593+
}
594+
},
587595
}
588596
image_id = args[2]
589597
try:
@@ -597,7 +605,7 @@ def return_image_meta(*args, **kwargs):
597605
return_image_meta)
598606
body = {
599607
"rebuild": {
600-
"imageRef": "155d900f-4e14-4e4c-a73d-069cbf4541e6",
608+
"imageRef": uuids.image_2_id,
601609
},
602610
}
603611
self.assertRaises(webob.exc.HTTPBadRequest,

nova/tests/unit/api/openstack/compute/test_servers.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def _get_server_data_dict(self, uuid, image_bookmark, flavor_bookmark,
410410
"status": status,
411411
"hostId": '',
412412
"image": {
413-
"id": "10",
413+
"id": FAKE_UUID,
414414
"links": [
415415
{
416416
"rel": "bookmark",
@@ -474,7 +474,8 @@ def _get_server_data_dict(self, uuid, image_bookmark, flavor_bookmark,
474474
}
475475

476476
def test_get_server_by_id(self):
477-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
477+
image_bookmark = "http://localhost/%s/images/%s" % (
478+
self.project_id, FAKE_UUID)
478479
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
479480

480481
uuid = FAKE_UUID
@@ -499,7 +500,8 @@ def test_get_server_empty_az(self):
499500
self.assertEqual(res_dict['server']['OS-EXT-AZ:availability_zone'], '')
500501

501502
def test_get_server_with_active_status_by_id(self):
502-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
503+
image_bookmark = "http://localhost/%s/images/%s" % (
504+
self.project_id, FAKE_UUID)
503505
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
504506

505507
res_dict = self.controller.show(self.request, FAKE_UUID)
@@ -517,7 +519,8 @@ def test_get_server_with_active_status_by_id(self):
517519
'numa_topology'], cell_down_support=False)
518520

519521
def test_get_server_with_id_image_ref_by_id(self):
520-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
522+
image_bookmark = "http://localhost/%s/images/%s" % (
523+
self.project_id, FAKE_UUID)
521524
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
522525

523526
res_dict = self.controller.show(self.request, FAKE_UUID)
@@ -1710,12 +1713,12 @@ def test_get_all_server_details(self):
17101713
],
17111714
}
17121715
expected_image = {
1713-
"id": "10",
1716+
"id": FAKE_UUID,
17141717
"links": [
17151718
{
17161719
"rel": "bookmark",
1717-
"href": ('http://localhost/%s/images/10' %
1718-
self.project_id),
1720+
"href": ('http://localhost/%s/images/%s' % (
1721+
self.project_id, FAKE_UUID)),
17191722
},
17201723
],
17211724
}
@@ -1838,7 +1841,8 @@ def _get_server_data_dict(self, uuid, image_bookmark, flavor_bookmark,
18381841
return server_dict
18391842

18401843
def test_show(self):
1841-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
1844+
image_bookmark = "http://localhost/%s/images/%s" % (
1845+
self.project_id, FAKE_UUID)
18421846
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
18431847

18441848
res_dict = self.controller.show(self.request, FAKE_UUID)
@@ -1880,7 +1884,8 @@ def fake_get_all(context, search_opts=None,
18801884
req.environ['nova.context'])
18811885

18821886
servers_list = self.controller.detail(req)
1883-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
1887+
image_bookmark = "http://localhost/%s/images/%s" % (
1888+
self.project_id, FAKE_UUID)
18841889
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
18851890
expected_server = self._get_server_data_dict(FAKE_UUID,
18861891
image_bookmark,
@@ -1941,7 +1946,8 @@ def _get_server_data_dict(self, uuid, image_bookmark, flavor_bookmark,
19411946
return server_dict
19421947

19431948
def _test_get_server_with_lock(self, locked_by):
1944-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
1949+
image_bookmark = "http://localhost/%s/images/%s" % (
1950+
self.project_id, FAKE_UUID)
19451951
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
19461952
req = self.req(self.path_with_id % FAKE_UUID)
19471953
project_id = req.environ['nova.context'].project_id
@@ -2121,7 +2127,8 @@ def _verify_host_status_policy_behavior(self, func, mock_get_host_status):
21212127
policy.set_rules(orig_rules)
21222128

21232129
def test_show(self):
2124-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
2130+
image_bookmark = "http://localhost/%s/images/%s" % (
2131+
self.project_id, FAKE_UUID)
21252132
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
21262133
res_dict = self.controller.show(self.request, FAKE_UUID)
21272134
expected_server = self._get_server_data_dict(FAKE_UUID,
@@ -2166,7 +2173,8 @@ def fake_get_all(context, search_opts=None,
21662173

21672174
servers_list = self.controller.detail(req)
21682175
self.assertEqual(2, len(servers_list['servers']))
2169-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
2176+
image_bookmark = "http://localhost/%s/images/%s" % (
2177+
self.project_id, FAKE_UUID)
21702178
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
21712179
expected_server = self._get_server_data_dict(FAKE_UUID,
21722180
image_bookmark,
@@ -2237,7 +2245,8 @@ def _get_server_data_dict(self, uuid, image_bookmark, flavor_bookmark,
22372245
return server_dict
22382246

22392247
def _test_get_server_with_description(self, description):
2240-
image_bookmark = "http://localhost/%s/images/10" % self.project_id
2248+
image_bookmark = "http://localhost/%s/images/%s" % (
2249+
self.project_id, FAKE_UUID)
22412250
flavor_bookmark = "http://localhost/%s/flavors/2" % self.project_id
22422251
req = self.req(self.path_with_id % FAKE_UUID)
22432252
project_id = req.environ['nova.context'].project_id

nova/tests/unit/api/openstack/fakes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def _return_servers_objs(context, search_opts=None, limit=None,
428428

429429
def stub_instance(id=1, user_id=None, project_id=None, host=None,
430430
node=None, vm_state=None, task_state=None,
431-
reservation_id="", uuid=FAKE_UUID, image_ref="10",
431+
reservation_id="", uuid=FAKE_UUID, image_ref=FAKE_UUID,
432432
flavor_id="1", name=None, key_name='',
433433
access_ipv4=None, access_ipv6=None, progress=0,
434434
auto_disk_config=False, display_name=None,

0 commit comments

Comments
 (0)