@@ -474,3 +474,77 @@ def setUp(self):
474474 endpoint = fakes .AUTH_URL ,
475475 token = fakes .AUTH_TOKEN ,
476476 )
477+
478+
479+ class FakeType (object ):
480+ """Fake one or more type."""
481+
482+ @staticmethod
483+ def create_one_type (attrs = None , methods = None ):
484+ """Create a fake type.
485+
486+ :param Dictionary attrs:
487+ A dictionary with all attributes
488+ :param Dictionary methods:
489+ A dictionary with all methods
490+ :return:
491+ A FakeResource object with id, name, description, etc.
492+ """
493+ attrs = attrs or {}
494+ methods = methods or {}
495+
496+ # Set default attributes.
497+ type_info = {
498+ "id" : 'type-id-' + uuid .uuid4 ().hex ,
499+ "name" : 'type-name-' + uuid .uuid4 ().hex ,
500+ "description" : 'type-description-' + uuid .uuid4 ().hex ,
501+ "extra_specs" : {"foo" : "bar" },
502+ "is_public" : True ,
503+ }
504+
505+ # Overwrite default attributes.
506+ type_info .update (attrs )
507+
508+ volume_type = fakes .FakeResource (
509+ info = copy .deepcopy (type_info ),
510+ methods = methods ,
511+ loaded = True )
512+ return volume_type
513+
514+ @staticmethod
515+ def create_types (attrs = None , count = 2 ):
516+ """Create multiple fake types.
517+
518+ :param Dictionary attrs:
519+ A dictionary with all attributes
520+ :param int count:
521+ The number of types to fake
522+ :return:
523+ A list of FakeResource objects faking the types
524+ """
525+ volume_types = []
526+ for i in range (0 , count ):
527+ volume_type = FakeType .create_one_type (attrs )
528+ volume_types .append (volume_type )
529+
530+ return volume_types
531+
532+ @staticmethod
533+ def get_types (types = None , count = 2 ):
534+ """Get an iterable MagicMock object with a list of faked types.
535+
536+ If types list is provided, then initialize the Mock object with the
537+ list. Otherwise create one.
538+
539+ :param List types:
540+ A list of FakeResource objects faking types
541+ :param Integer count:
542+ The number of types to be faked
543+ :return
544+ An iterable Mock object with side_effect set to a list of faked
545+ types
546+ """
547+ if types is None :
548+ types = FakeType .create_types (count )
549+
550+ return mock .MagicMock (side_effect = types )
0 commit comments