This file is indexed.

/usr/lib/python2.7/dist-packages/oslo_versionedobjects/tests/test_fixture.py is in python-oslo.versionedobjects 1.17.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#    Copyright 2015 IBM Corp.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import collections
import copy
import datetime
import hashlib

import iso8601
import mock
import six

from oslo_versionedobjects import base
from oslo_versionedobjects import exception
from oslo_versionedobjects import fields
from oslo_versionedobjects import fixture
from oslo_versionedobjects import test


class MyObject(base.VersionedObject):
    fields = {'diglett': fields.IntegerField()}

    @base.remotable
    def remotable_method(self):
        pass

    @classmethod
    @base.remotable
    def remotable_classmethod(cls):
        pass

    def non_remotable_method(self):
        pass

    @classmethod
    def non_remotable_classmethod(cls):
        pass


class MyObject2(base.VersionedObject):
    pass


class MyExtraObject(base.VersionedObject):
    pass


class TestObjectComparators(test.TestCase):
    @base.VersionedObjectRegistry.register_if(False)
    class MyComparedObject(base.VersionedObject):
        fields = {'foo': fields.IntegerField(),
                  'bar': fields.IntegerField()}

    @base.VersionedObjectRegistry.register_if(False)
    class MyComparedObjectWithTZ(base.VersionedObject):
        fields = {'tzfield': fields.DateTimeField()}

    def test_compare_obj(self):
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        my_obj = self.MyComparedObject(foo=1, bar=2)
        my_db_obj = {'foo': 1, 'bar': 2}

        fixture.compare_obj(mock_test, my_obj, my_db_obj)

        expected_calls = [(1, 1), (2, 2)]
        actual_calls = [c[0] for c in mock_test.assertEqual.call_args_list]
        for call in expected_calls:
            self.assertIn(call, actual_calls)

    def test_compare_obj_with_unset(self):
        # If the object has nothing set, and also the db object has the same
        # thing not set, it's OK.
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        my_obj = self.MyComparedObject()
        my_db_obj = {}

        fixture.compare_obj(mock_test, my_obj, my_db_obj)

        self.assertFalse(mock_test.assertEqual.called, "assertEqual should "
                         "not have been called, there is nothing to compare.")

    def test_compare_obj_with_unset_in_obj(self):
        # If the db dict has something set, but the object doesn't, that's !=
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        my_obj = self.MyComparedObject(foo=1)
        my_db_obj = {'foo': 1, 'bar': 2}

        self.assertRaises(AssertionError, fixture.compare_obj, mock_test,
                          my_obj, my_db_obj)

    def test_compare_obj_with_unset_in_db_dict(self):
        # If the object has something set, but the db dict doesn't, that's !=
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        my_obj = self.MyComparedObject(foo=1, bar=2)
        my_db_obj = {'foo': 1}

        self.assertRaises(AssertionError, fixture.compare_obj, mock_test,
                          my_obj, my_db_obj)

    def test_compare_obj_with_unset_in_obj_ignored(self):
        # If the db dict has something set, but the object doesn't, but we
        # ignore that key, we are equal
        my_obj = self.MyComparedObject(foo=1)
        my_db_obj = {'foo': 1, 'bar': 2}
        ignore = ['bar']

        fixture.compare_obj(self, my_obj, my_db_obj, allow_missing=ignore)

    def test_compare_obj_with_unset_in_db_dict_ignored(self):
        # If the object has something set, but the db dict doesn't, but we
        # ignore that key, we are equal
        my_obj = self.MyComparedObject(foo=1, bar=2)
        my_db_obj = {'foo': 1}
        ignore = ['bar']

        fixture.compare_obj(self, my_obj, my_db_obj, allow_missing=ignore)

    def test_compare_obj_with_allow_missing_unequal(self):
        # If the tested key is in allow_missing, but both the obj and db_obj
        # have the value set, we should still check it for equality
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        my_obj = self.MyComparedObject(foo=1, bar=2)
        my_db_obj = {'foo': 1, 'bar': 1}
        ignore = ['bar']

        fixture.compare_obj(mock_test, my_obj, my_db_obj,
                            allow_missing=ignore)

        expected_calls = [(1, 1), (1, 2)]
        actual_calls = [c[0] for c in mock_test.assertEqual.call_args_list]
        for call in expected_calls:
            self.assertIn(call, actual_calls)

    def test_compare_obj_with_subs(self):
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        my_obj = self.MyComparedObject(foo=1, bar=2)
        my_db_obj = {'doo': 1, 'bar': 2}
        subs = {'foo': 'doo'}

        fixture.compare_obj(mock_test, my_obj, my_db_obj, subs=subs)

        expected_calls = [(1, 1), (2, 2)]
        actual_calls = [c[0] for c in mock_test.assertEqual.call_args_list]
        for call in expected_calls:
            self.assertIn(call, actual_calls)

    def test_compare_obj_with_allow_missing(self):
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        my_obj = self.MyComparedObject(foo=1)
        my_db_obj = {'foo': 1, 'bar': 2}
        ignores = ['bar']

        fixture.compare_obj(mock_test, my_obj, my_db_obj,
                            allow_missing=ignores)

        mock_test.assertEqual.assert_called_once_with(1, 1)

    def test_compare_obj_with_comparators(self):
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        comparator = mock.Mock()
        comp_dict = {'foo': comparator}
        my_obj = self.MyComparedObject(foo=1, bar=2)
        my_db_obj = {'foo': 1, 'bar': 2}

        fixture.compare_obj(mock_test, my_obj, my_db_obj,
                            comparators=comp_dict)

        comparator.assert_called_once_with(1, 1)
        mock_test.assertEqual.assert_called_once_with(2, 2)

    def test_compare_obj_with_dt(self):
        mock_test = mock.Mock()
        mock_test.assertEqual = mock.Mock()
        dt = datetime.datetime(1955, 11, 5, tzinfo=iso8601.iso8601.Utc())
        replaced_dt = dt.replace(tzinfo=None)
        my_obj = self.MyComparedObjectWithTZ(tzfield=dt)
        my_db_obj = {'tzfield': replaced_dt}

        fixture.compare_obj(mock_test, my_obj, my_db_obj)

        mock_test.assertEqual.assert_called_once_with(replaced_dt,
                                                      replaced_dt)


class FakeResource(base.VersionedObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    fields = {
        'identifier': fields.Field(fields.Integer(), default=123)
    }


class TestObjectVersionChecker(test.TestCase):
    def setUp(self):
        super(TestObjectVersionChecker, self).setUp()
        objects = [MyObject, MyObject2, ]
        self.obj_classes = {obj.__name__: [obj] for obj in objects}
        self.ovc = fixture.ObjectVersionChecker(obj_classes=self.obj_classes)

    def test_get_hashes(self):
        # Make sure get_hashes retrieves the fingerprint of all objects
        fp = 'ashketchum'
        with mock.patch.object(self.ovc, '_get_fingerprint') as mock_gf:
            mock_gf.return_value = fp
            actual = self.ovc.get_hashes()

        expected = self._generate_hashes(self.obj_classes, fp)
        self.assertEqual(expected, actual, "ObjectVersionChecker is not "
                         "getting the fingerprints of all registered "
                         "objects.")

    def test_get_hashes_with_extra_data(self):
        # Make sure get_hashes uses the extra_data_func
        fp = 'garyoak'
        mock_func = mock.MagicMock()
        with mock.patch.object(self.ovc, '_get_fingerprint') as mock_gf:
            mock_gf.return_value = fp
            actual = self.ovc.get_hashes(extra_data_func=mock_func)

        expected = self._generate_hashes(self.obj_classes, fp)
        expected_calls = [((name,), {'extra_data_func': mock_func})
                          for name in self.obj_classes.keys()]

        self.assertEqual(expected, actual, "ObjectVersionChecker is not "
                         "getting the fingerprints of all registered "
                         "objects.")

        self.assertEqual(len(expected_calls), len(mock_gf.call_args_list),
                         "get_hashes() did not call get the fingerprints of "
                         "all objects in the registry.")
        for call in expected_calls:
            self.assertIn(call, mock_gf.call_args_list,
                          "get_hashes() did not call _get_fingerprint()"
                          "correctly.")

    def test_test_hashes_none_changed(self):
        # Make sure test_hashes() generates an empty dictionary when
        # there are no objects that have changed
        fp = 'pikachu'
        hashes = self._generate_hashes(self.obj_classes, fp)

        with mock.patch.object(self.ovc, 'get_hashes') as mock_gh:
            mock_gh.return_value = hashes
            # I'm so sorry, but they have to be named this way
            actual_expected, actual_actual = self.ovc.test_hashes(hashes)

        expected_expected = expected_actual = {}

        self.assertEqual(expected_expected, actual_expected, "There are no "
                         "objects changed, so the 'expected' return value "
                         "should contain no objects.")
        self.assertEqual(expected_actual, actual_actual, "There are no "
                         "objects changed, so the 'actual' return value "
                         "should contain no objects.")

    def test_test_hashes_class_not_added(self):
        # Make sure the expected and actual values differ when a class
        # was added to the registry, but not the static dictionary
        fp = 'gyrados'
        new_classes = copy.copy(self.obj_classes)
        self._add_class(new_classes, MyExtraObject)

        expected_hashes = self._generate_hashes(self.obj_classes, fp)
        actual_hashes = self._generate_hashes(new_classes, fp)

        with mock.patch.object(self.ovc, 'get_hashes') as mock_gh:
            mock_gh.return_value = actual_hashes
            actual_exp, actual_act = self.ovc.test_hashes(expected_hashes)

        expected_expected = {MyExtraObject.__name__: None}
        expected_actual = {MyExtraObject.__name__: fp}

        self.assertEqual(expected_expected, actual_exp, "Expected hashes "
                         "should not contain the fingerprint of the class "
                         "that has not been added to the expected hash "
                         "dictionary.")
        self.assertEqual(expected_actual, actual_act, "The actual hash "
                         "should contain the class that was added to the "
                         "registry.")

    def test_test_hashes_new_fp_incorrect(self):
        # Make sure the expected and actual values differ when a fingerprint
        # was changed, but the static dictionary was not updated
        fp1 = 'beedrill'
        fp2 = 'snorlax'
        expected_hashes = self._generate_hashes(self.obj_classes, fp1)
        actual_hashes = copy.copy(expected_hashes)

        actual_hashes[MyObject.__name__] = fp2

        with mock.patch.object(self.ovc, 'get_hashes') as mock_gh:
            mock_gh.return_value = actual_hashes
            actual_exp, actual_act = self.ovc.test_hashes(expected_hashes)

        expected_expected = {MyObject.__name__: fp1}
        expected_actual = {MyObject.__name__: fp2}

        self.assertEqual(expected_expected, actual_exp, "Expected hashes "
                         "should contain the updated object with the old "
                         "hash.")
        self.assertEqual(expected_actual, actual_act, "Actual hashes "
                         "should contain the updated object with the new "
                         "hash.")

    def test_test_hashes_passes_extra_func(self):
        # Make sure that test_hashes passes the extra_func to get_hashes
        mock_extra_func = mock.Mock()

        with mock.patch.object(self.ovc, 'get_hashes') as mock_get_hashes:
            self.ovc.test_hashes({}, extra_data_func=mock_extra_func)

            mock_get_hashes.assert_called_once_with(
                extra_data_func=mock_extra_func)

    def test_get_dependency_tree(self):
        # Make sure get_dependency_tree() gets the dependencies of all
        # objects in the registry
        with mock.patch.object(self.ovc, '_get_dependencies') as mock_gd:
            self.ovc.get_dependency_tree()

        expected_calls = [(({}, MyObject),), (({}, MyObject2),)]

        self.assertEqual(2, len(mock_gd.call_args_list),
                         "get_dependency_tree() tried to get the dependencies"
                         " too many times.")

        for call in expected_calls:
            self.assertIn(call, mock_gd.call_args_list,
                          "get_dependency_tree() did not get the dependencies "
                          "of the objects correctly.")

    def test_test_relationships_none_changed(self):
        # Make sure test_relationships() generates an empty dictionary when
        # no relationships have been changed
        dep_tree = {}
        # tree will be {'MyObject': {'MyObject2': '1.0'}}
        self._add_dependency(MyObject, MyObject2, dep_tree)

        with mock.patch.object(self.ovc, 'get_dependency_tree') as mock_gdt:
            mock_gdt.return_value = dep_tree
            actual_exp, actual_act = self.ovc.test_relationships(dep_tree)

        expected_expected = expected_actual = {}

        self.assertEqual(expected_expected, actual_exp, "There are no "
                         "objects changed, so the 'expected' return value "
                         "should contain no objects.")
        self.assertEqual(expected_actual, actual_act, "There are no "
                         "objects changed, so the 'actual' return value "
                         "should contain no objects.")

    def test_test_relationships_rel_added(self):
        # Make sure expected and actual relationships differ if a
        # relationship is added to a class
        exp_tree = {}
        actual_tree = {}
        self._add_dependency(MyObject, MyObject2, exp_tree)
        self._add_dependency(MyObject, MyObject2, actual_tree)
        self._add_dependency(MyObject, MyExtraObject, actual_tree)

        with mock.patch.object(self.ovc, 'get_dependency_tree') as mock_gdt:
            mock_gdt.return_value = actual_tree
            actual_exp, actual_act = self.ovc.test_relationships(exp_tree)

        expected_expected = {'MyObject': {'MyObject2': '1.0'}}
        expected_actual = {'MyObject': {'MyObject2': '1.0',
                                        'MyExtraObject': '1.0'}}

        self.assertEqual(expected_expected, actual_exp, "The expected "
                         "relationship tree is not being built from changes "
                         "correctly.")
        self.assertEqual(expected_actual, actual_act, "The actual "
                         "relationship tree is not being built from changes "
                         "correctly.")

    def test_test_relationships_class_added(self):
        # Make sure expected and actual relationships differ if a new
        # class is added to the relationship tree
        exp_tree = {}
        actual_tree = {}
        self._add_dependency(MyObject, MyObject2, exp_tree)
        self._add_dependency(MyObject, MyObject2, actual_tree)
        self._add_dependency(MyObject2, MyExtraObject, actual_tree)

        with mock.patch.object(self.ovc, 'get_dependency_tree') as mock_gdt:
            mock_gdt.return_value = actual_tree
            actual_exp, actual_act = self.ovc.test_relationships(exp_tree)

        expected_expected = {'MyObject2': None}
        expected_actual = {'MyObject2': {'MyExtraObject': '1.0'}}

        self.assertEqual(expected_expected, actual_exp, "The expected "
                         "relationship tree is not being built from changes "
                         "correctly.")
        self.assertEqual(expected_actual, actual_act, "The actual "
                         "relationship tree is not being built from changes "
                         "correctly.")

    def test_test_compatibility_routines(self):
        # Make sure test_compatibility_routines() checks the object
        # compatibility of all objects in the registry
        del self.ovc.obj_classes[MyObject2.__name__]

        with mock.patch.object(self.ovc, '_test_object_compatibility') as toc:
            self.ovc.test_compatibility_routines()

        toc.assert_called_once_with(MyObject, manifest=None, init_args=[],
                                    init_kwargs={})

    def test_test_compatibility_routines_with_manifest(self):
        # Make sure test_compatibility_routines() uses the version manifest
        del self.ovc.obj_classes[MyObject2.__name__]
        man = {'who': 'cares'}

        with mock.patch.object(self.ovc, '_test_object_compatibility') as toc:
            with mock.patch('oslo_versionedobjects.base'
                            '.obj_tree_get_versions') as otgv:
                otgv.return_value = man
                self.ovc.test_compatibility_routines(use_manifest=True)

        otgv.assert_called_once_with(MyObject.__name__)
        toc.assert_called_once_with(MyObject, manifest=man, init_args=[],
                                    init_kwargs={})

    def test_test_compatibility_routines_with_args_kwargs(self):
        # Make sure test_compatibility_routines() uses init args/kwargs
        del self.ovc.obj_classes[MyObject2.__name__]
        init_args = {MyObject: [1]}
        init_kwargs = {MyObject: {'foo': 'bar'}}

        with mock.patch.object(self.ovc, '_test_object_compatibility') as toc:
            self.ovc.test_compatibility_routines(init_args=init_args,
                                                 init_kwargs=init_kwargs)

        toc.assert_called_once_with(MyObject, manifest=None, init_args=[1],
                                    init_kwargs={'foo': 'bar'})

    def test_test_relationships_in_order(self):
        # Make sure test_relationships_in_order() tests the relationships
        # of all objects in the registry
        with mock.patch.object(self.ovc,
                               '_test_relationships_in_order') as mock_tr:
            self.ovc.test_relationships_in_order()

        expected_calls = [((MyObject,),), ((MyObject2,),)]

        self.assertEqual(2, len(mock_tr.call_args_list),
                         "test_relationships_in_order() tested too many "
                         "relationships.")
        for call in expected_calls:
            self.assertIn(call, mock_tr.call_args_list,
                          "test_relationships_in_order() did not test the "
                          "relationships of the individual objects "
                          "correctly.")

    def test_test_relationships_in_order_positive(self):
        # Make sure a correct relationship ordering doesn't blow up
        rels = {'bellsprout': [('1.0', '1.0'), ('1.1', '1.2'),
                               ('1.3', '1.3')]}
        MyObject.obj_relationships = rels

        self.ovc._test_relationships_in_order(MyObject)

    def test_test_relationships_in_order_negative(self):
        # Make sure an out-of-order relationship does blow up
        rels = {'rattata': [('1.0', '1.0'), ('1.1', '1.2'),
                            ('1.3', '1.1')]}
        MyObject.obj_relationships = rels

        self.assertRaises(AssertionError,
                          self.ovc._test_relationships_in_order, MyObject)

    def test_find_remotable_method(self):
        # Make sure we can find a remotable method on an object
        method = self.ovc._find_remotable_method(MyObject,
                                                 MyObject.remotable_method)

        self.assertEqual(MyObject.remotable_method.original_fn,
                         method,
                         "_find_remotable_method() did not find the remotable"
                         " method of MyObject.")

    def test_find_remotable_method_classmethod(self):
        # Make sure we can find a remotable classmethod on an object
        rcm = MyObject.remotable_classmethod
        method = self.ovc._find_remotable_method(MyObject, rcm)

        expected = rcm.__get__(None, MyObject).original_fn
        self.assertEqual(expected, method, "_find_remotable_method() did not "
                         "find the remotable classmethod.")

    def test_find_remotable_method_non_remotable_method(self):
        # Make sure nothing is found when we have only a non-remotable method
        nrm = MyObject.non_remotable_method
        method = self.ovc._find_remotable_method(MyObject, nrm)

        self.assertIsNone(method, "_find_remotable_method() found a method "
                          "that isn't remotable.")

    def test_find_remotable_method_non_remotable_classmethod(self):
        # Make sure we don't find a non-remotable classmethod
        nrcm = MyObject.non_remotable_classmethod
        method = self.ovc._find_remotable_method(MyObject, nrcm)

        self.assertIsNone(method, "_find_remotable_method() found a method "
                          "that isn't remotable.")

    def test_get_fingerprint(self):
        # Make sure _get_fingerprint() generates a consistent fingerprint
        MyObject.VERSION = '1.1'
        argspec = 'vulpix'

        with mock.patch('inspect.getargspec') as mock_gas:
            mock_gas.return_value = argspec
            fp = self.ovc._get_fingerprint(MyObject.__name__)

        exp_fields = sorted(list(MyObject.fields.items()))
        exp_methods = sorted([('remotable_method', argspec),
                              ('remotable_classmethod', argspec)])
        expected_relevant_data = (exp_fields, exp_methods)
        expected_hash = hashlib.md5(six.binary_type(repr(
            expected_relevant_data).encode())).hexdigest()
        expected_fp = '%s-%s' % (MyObject.VERSION, expected_hash)

        self.assertEqual(expected_fp, fp, "_get_fingerprint() did not "
                                          "generate a correct fingerprint.")

    def test_get_fingerprint_with_child_versions(self):
        # Make sure _get_fingerprint() generates a consistent fingerprint
        # when child_versions are present
        child_versions = {'1.0': '1.0', '1.1': '1.1'}
        MyObject.VERSION = '1.1'
        MyObject.child_versions = child_versions
        argspec = 'onix'

        with mock.patch('inspect.getargspec') as mock_gas:
            mock_gas.return_value = argspec
            fp = self.ovc._get_fingerprint(MyObject.__name__)

        exp_fields = sorted(list(MyObject.fields.items()))
        exp_methods = sorted([('remotable_method', argspec),
                              ('remotable_classmethod', argspec)])
        exp_child_versions = collections.OrderedDict(sorted(
            child_versions.items()))
        exp_relevant_data = (exp_fields, exp_methods, exp_child_versions)

        expected_hash = hashlib.md5(six.binary_type(repr(
            exp_relevant_data).encode())).hexdigest()
        expected_fp = '%s-%s' % (MyObject.VERSION, expected_hash)

        self.assertEqual(expected_fp, fp, "_get_fingerprint() did not "
                                          "generate a correct fingerprint.")

    def test_get_fingerprint_with_extra_data(self):
        # Make sure _get_fingerprint() uses extra_data_func when it is
        # supplied
        class ExtraDataObj(base.VersionedObject):
            pass

        def get_data(obj_class):
            return (obj_class,)

        ExtraDataObj.VERSION = '1.1'
        argspec = 'cubone'
        self._add_class(self.obj_classes, ExtraDataObj)

        with mock.patch('inspect.getargspec') as mock_gas:
            mock_gas.return_value = argspec
            fp = self.ovc._get_fingerprint(ExtraDataObj.__name__,
                                           extra_data_func=get_data)

        exp_fields = []
        exp_methods = []
        exp_extra_data = ExtraDataObj
        exp_relevant_data = (exp_fields, exp_methods, exp_extra_data)

        expected_hash = hashlib.md5(six.binary_type(repr(
            exp_relevant_data).encode())).hexdigest()
        expected_fp = '%s-%s' % (ExtraDataObj.VERSION, expected_hash)

        self.assertEqual(expected_fp, fp, "_get_fingerprint() did not "
                                          "generate a correct fingerprint.")

    def test_get_dependencies(self):
        # Make sure _get_dependencies() generates a correct tree when parsing
        # an object
        self._add_class(self.obj_classes, MyExtraObject)
        MyObject.fields['subob'] = fields.ObjectField('MyExtraObject')
        MyExtraObject.VERSION = '1.0'
        tree = {}

        self.ovc._get_dependencies(tree, MyObject)

        expected_tree = {'MyObject': {'MyExtraObject': '1.0'}}

        self.assertEqual(expected_tree, tree, "_get_dependencies() did "
                         "not generate a correct dependency tree.")

    def test_test_object_compatibility(self):
        # Make sure _test_object_compatibility() tests obj_to_primitive()
        # on each prior version to the current version
        to_prim = mock.MagicMock(spec=callable)
        MyObject.VERSION = '1.1'
        MyObject.obj_to_primitive = to_prim

        self.ovc._test_object_compatibility(MyObject)

        expected_calls = [((), {'target_version': '1.0'}),
                          ((), {'target_version': '1.1'})]

        self.assertEqual(expected_calls, to_prim.call_args_list,
                         "_test_object_compatibility() did not test "
                         "obj_to_primitive() on the correct target versions")

    def test_test_object_compatibility_args_kwargs(self):
        # Make sure _test_object_compatibility() tests obj_to_primitive()
        # with the correct args and kwargs to init
        to_prim = mock.MagicMock(spec=callable)
        MyObject.obj_to_primitive = to_prim
        MyObject.VERSION = '1.1'
        args = [1]
        kwargs = {'foo': 'bar'}

        with mock.patch.object(MyObject, '__init__',
                               return_value=None) as mock_init:
            self.ovc._test_object_compatibility(MyObject, init_args=args,
                                                init_kwargs=kwargs)

        expected_init = ((1,), {'foo': 'bar'})
        expected_init_calls = [expected_init, expected_init]
        self.assertEqual(expected_init_calls, mock_init.call_args_list,
                         "_test_object_compatibility() did not call "
                         "__init__() properly on the object")

        expected_to_prim = [((), {'target_version': '1.0'}),
                            ((), {'target_version': '1.1'})]
        self.assertEqual(expected_to_prim, to_prim.call_args_list,
                         "_test_object_compatibility() did not test "
                         "obj_to_primitive() on the correct target versions")

    def _add_class(self, obj_classes, cls):
        obj_classes[cls.__name__] = [cls]

    def _generate_hashes(self, classes, fp):
        # Generate hashes for classes, giving fp as the fingerprint
        # for all classes
        return {cls: fp for cls in classes.keys()}

    def _add_dependency(self, parent_cls, child_cls, tree):
        # Add a dependency to the tree with the parent class holding
        # version 1.0 of the given child class
        deps = tree.get(parent_cls.__name__, {})
        deps[child_cls.__name__] = '1.0'
        tree[parent_cls.__name__] = deps


class TestVersionedObjectRegistryFixture(test.TestCase):

    primitive = {'versioned_object.name': 'FakeResource',
                 'versioned_object.namespace': 'versionedobjects',
                 'versioned_object.version': '1.0',
                 'versioned_object.data': {'identifier': 123}}

    def test_object_registered_temporarily(self):
        # Test object that has not been registered
        self.assertRaises(
            exception.UnsupportedObjectError,
            FakeResource.obj_from_primitive,
            self.primitive)

        with fixture.VersionedObjectRegistryFixture() as obj_registry:
            # Register object locally
            obj_registry.setUp()
            obj_registry.register(FakeResource)

            # Test object has now been registered
            obj = FakeResource.obj_from_primitive(
                self.primitive)
            self.assertEqual(obj.identifier, 123)
            self.assertEqual('1.0', obj.VERSION)

        # Test object that is no longer registered
        self.assertRaises(
            exception.UnsupportedObjectError,
            FakeResource.obj_from_primitive,
            self.primitive)


class TestStableObjectJsonFixture(test.TestCase):
    def test_changes_sort(self):
        @base.VersionedObjectRegistry.register_if(False)
        class TestObject(base.VersionedObject):
            fields = {'z': fields.StringField(),
                      'a': fields.StringField()}

            def obj_what_changed(self):
                return ['z', 'a']

        obj = TestObject(a='foo', z='bar')
        self.assertEqual(['z', 'a'],
                         obj.obj_to_primitive()['versioned_object.changes'])
        with fixture.StableObjectJsonFixture():
            self.assertEqual(
                ['a', 'z'],
                obj.obj_to_primitive()['versioned_object.changes'])