@@ -145,6 +145,83 @@ def get_networks(networks=None, count=2):
145145 return mock .MagicMock (side_effect = networks )
146146
147147
148+ class FakePort (object ):
149+ """Fake one or more ports."""
150+
151+ @staticmethod
152+ def create_one_port (attrs = {}, methods = {}):
153+ """Create a fake port.
154+
155+ :param Dictionary attrs:
156+ A dictionary with all attributes
157+ :param Dictionary methods:
158+ A dictionary with all methods
159+ :return:
160+ A FakeResource object, with id, name, admin_state_up,
161+ status, tenant_id
162+ """
163+ # Set default attributes.
164+ port_attrs = {
165+ 'id' : 'port-id-' + uuid .uuid4 ().hex ,
166+ 'name' : 'port-name-' + uuid .uuid4 ().hex ,
167+ 'status' : 'ACTIVE' ,
168+ 'admin_state_up' : True ,
169+ 'tenant_id' : 'project-id-' + uuid .uuid4 ().hex ,
170+ }
171+
172+ # Overwrite default attributes.
173+ port_attrs .update (attrs )
174+
175+ # Set default methods.
176+ port_methods = {}
177+
178+ # Overwrite default methods.
179+ port_methods .update (methods )
180+
181+ port = fakes .FakeResource (info = copy .deepcopy (port_attrs ),
182+ methods = copy .deepcopy (port_methods ),
183+ loaded = True )
184+ return port
185+
186+ @staticmethod
187+ def create_ports (attrs = {}, methods = {}, count = 2 ):
188+ """Create multiple fake ports.
189+
190+ :param Dictionary attrs:
191+ A dictionary with all attributes
192+ :param Dictionary methods:
193+ A dictionary with all methods
194+ :param int count:
195+ The number of ports to fake
196+ :return:
197+ A list of FakeResource objects faking the ports
198+ """
199+ ports = []
200+ for i in range (0 , count ):
201+ ports .append (FakePort .create_one_port (attrs , methods ))
202+
203+ return ports
204+
205+ @staticmethod
206+ def get_ports (ports = None , count = 2 ):
207+ """Get an iterable MagicMock object with a list of faked ports.
208+
209+ If ports list is provided, then initialize the Mock object with the
210+ list. Otherwise create one.
211+
212+ :param List ports:
213+ A list of FakeResource objects faking ports
214+ :param int count:
215+ The number of ports to fake
216+ :return:
217+ An iterable Mock object with side_effect set to a list of faked
218+ ports
219+ """
220+ if ports is None :
221+ ports = FakePort .create_ports (count )
222+ return mock .MagicMock (side_effect = ports )
223+
224+
148225class FakeRouter (object ):
149226 """Fake one or more routers."""
150227
0 commit comments