@@ -356,6 +356,84 @@ def get_routers(routers=None, count=2):
356356 return mock .MagicMock (side_effect = routers )
357357
358358
359+ class FakeSecurityGroup (object ):
360+ """Fake one or more security groups."""
361+
362+ @staticmethod
363+ def create_one_security_group (attrs = {}, methods = {}):
364+ """Create a fake security group.
365+
366+ :param Dictionary attrs:
367+ A dictionary with all attributes
368+ :param Dictionary methods:
369+ A dictionary with all methods
370+ :return:
371+ A FakeResource object, with id, name, etc.
372+ """
373+ # Set default attributes.
374+ security_group_attrs = {
375+ 'id' : 'security-group-id-' + uuid .uuid4 ().hex ,
376+ 'name' : 'security-group-name-' + uuid .uuid4 ().hex ,
377+ 'description' : 'security-group-description-' + uuid .uuid4 ().hex ,
378+ 'tenant_id' : 'project-id-' + uuid .uuid4 ().hex ,
379+ 'security_group_rules' : [],
380+ }
381+
382+ # Overwrite default attributes.
383+ security_group_attrs .update (attrs )
384+
385+ # Set default methods.
386+ security_group_methods = {}
387+
388+ # Overwrite default methods.
389+ security_group_methods .update (methods )
390+
391+ security_group = fakes .FakeResource (
392+ info = copy .deepcopy (security_group_attrs ),
393+ methods = copy .deepcopy (security_group_methods ),
394+ loaded = True )
395+ return security_group
396+
397+ @staticmethod
398+ def create_security_groups (attrs = {}, methods = {}, count = 2 ):
399+ """Create multiple fake security groups.
400+
401+ :param Dictionary attrs:
402+ A dictionary with all attributes
403+ :param Dictionary methods:
404+ A dictionary with all methods
405+ :param int count:
406+ The number of security groups to fake
407+ :return:
408+ A list of FakeResource objects faking the security groups
409+ """
410+ security_groups = []
411+ for i in range (0 , count ):
412+ security_groups .append (
413+ FakeRouter .create_one_security_group (attrs , methods ))
414+
415+ return security_groups
416+
417+ @staticmethod
418+ def get_security_groups (security_groups = None , count = 2 ):
419+ """Get an iterable MagicMock object with a list of faked security groups.
420+
421+ If security group list is provided, then initialize the Mock object
422+ with the list. Otherwise create one.
423+
424+ :param List security groups:
425+ A list of FakeResource objects faking security groups
426+ :param int count:
427+ The number of security groups to fake
428+ :return:
429+ An iterable Mock object with side_effect set to a list of faked
430+ security groups
431+ """
432+ if security_groups is None :
433+ security_groups = FakeRouter .create_security_groups (count )
434+ return mock .MagicMock (side_effect = security_groups )
435+
436+
359437class FakeSubnet (object ):
360438 """Fake one or more subnets."""
361439
0 commit comments