13
13
import mock
14
14
from oslo_utils import uuidutils
15
15
16
+ from nova import exception
16
17
from nova import objects
17
18
from nova .objects import instance_mapping
18
19
from nova .tests .unit .objects import test_cell_mapping
@@ -25,6 +26,7 @@ def get_db_mapping(**updates):
25
26
'instance_uuid' : uuidutils .generate_uuid (),
26
27
'cell_id' : None ,
27
28
'project_id' : 'fake-project' ,
29
+ 'user_id' : 'fake-user' ,
28
30
'created_at' : None ,
29
31
'updated_at' : None ,
30
32
'queued_for_delete' : False ,
@@ -77,13 +79,15 @@ def test_create(self, create_in_db):
77
79
mapping_obj .cell_mapping = objects .CellMapping (self .context ,
78
80
id = db_mapping ['cell_mapping' ]['id' ])
79
81
mapping_obj .project_id = db_mapping ['project_id' ]
82
+ mapping_obj .user_id = db_mapping ['user_id' ]
80
83
81
84
mapping_obj .create ()
82
85
create_in_db .assert_called_once_with (self .context ,
83
86
{'instance_uuid' : uuid ,
84
87
'queued_for_delete' : False ,
85
88
'cell_id' : db_mapping ['cell_mapping' ]['id' ],
86
- 'project_id' : db_mapping ['project_id' ]})
89
+ 'project_id' : db_mapping ['project_id' ],
90
+ 'user_id' : db_mapping ['user_id' ]})
87
91
self .compare_obj (mapping_obj , db_mapping ,
88
92
subs = {'cell_mapping' : 'cell_id' },
89
93
comparators = {
@@ -98,12 +102,14 @@ def test_create_cell_mapping_none(self, create_in_db):
98
102
mapping_obj .instance_uuid = uuid
99
103
mapping_obj .cell_mapping = None
100
104
mapping_obj .project_id = db_mapping ['project_id' ]
105
+ mapping_obj .user_id = db_mapping ['user_id' ]
101
106
102
107
mapping_obj .create ()
103
108
create_in_db .assert_called_once_with (self .context ,
104
109
{'instance_uuid' : uuid ,
105
110
'queued_for_delete' : False ,
106
- 'project_id' : db_mapping ['project_id' ]})
111
+ 'project_id' : db_mapping ['project_id' ],
112
+ 'user_id' : db_mapping ['user_id' ]})
107
113
self .compare_obj (mapping_obj , db_mapping ,
108
114
subs = {'cell_mapping' : 'cell_id' })
109
115
self .assertIsNone (mapping_obj .cell_mapping )
@@ -116,13 +122,15 @@ def test_create_cell_mapping_with_qfd_true(self, create_in_db):
116
122
mapping_obj .instance_uuid = db_mapping ['instance_uuid' ]
117
123
mapping_obj .cell_mapping = None
118
124
mapping_obj .project_id = db_mapping ['project_id' ]
125
+ mapping_obj .user_id = db_mapping ['user_id' ]
119
126
mapping_obj .queued_for_delete = True
120
127
121
128
mapping_obj .create ()
122
129
create_in_db .assert_called_once_with (self .context ,
123
130
{'instance_uuid' : db_mapping ['instance_uuid' ],
124
131
'queued_for_delete' : True ,
125
- 'project_id' : db_mapping ['project_id' ]})
132
+ 'project_id' : db_mapping ['project_id' ],
133
+ 'user_id' : db_mapping ['user_id' ]})
126
134
127
135
@mock .patch .object (instance_mapping .InstanceMapping , '_save_in_db' )
128
136
def test_save (self , save_in_db ):
@@ -162,13 +170,32 @@ def test_obj_make_compatible(self):
162
170
im_obj = instance_mapping .InstanceMapping (context = self .context )
163
171
fake_im_obj = instance_mapping .InstanceMapping (context = self .context ,
164
172
instance_uuid = uuid ,
165
- queued_for_delete = False )
173
+ queued_for_delete = False ,
174
+ user_id = 'fake-user' )
175
+ obj_primitive = fake_im_obj .obj_to_primitive ('1.1' )
176
+ obj = im_obj .obj_from_primitive (obj_primitive )
177
+ self .assertIn ('queued_for_delete' , obj )
178
+ self .assertNotIn ('user_id' , obj )
179
+
166
180
obj_primitive = fake_im_obj .obj_to_primitive ('1.0' )
167
181
obj = im_obj .obj_from_primitive (obj_primitive )
168
182
self .assertIn ('instance_uuid' , obj )
169
183
self .assertEqual (uuid , obj .instance_uuid )
170
184
self .assertNotIn ('queued_for_delete' , obj )
171
185
186
+ @mock .patch ('nova.objects.instance_mapping.LOG.error' )
187
+ def test_obj_load_attr (self , mock_log ):
188
+ im_obj = instance_mapping .InstanceMapping ()
189
+ # Access of unset user_id should have special handling
190
+ self .assertRaises (exception .ObjectActionError , im_obj .obj_load_attr ,
191
+ 'user_id' )
192
+ msg = ('The unset user_id attribute of an unmigrated instance mapping '
193
+ 'should not be accessed.' )
194
+ mock_log .assert_called_once_with (msg )
195
+ # Access of any other unset attribute should fall back to base class
196
+ self .assertRaises (NotImplementedError , im_obj .obj_load_attr ,
197
+ 'project_id' )
198
+
172
199
173
200
class TestInstanceMappingObject (test_objects ._LocalTest ,
174
201
_TestInstanceMappingObject ):
0 commit comments