@@ -230,3 +230,67 @@ def unset_keys(self, keys):
230230
231231 def get_keys (self ):
232232 return self ._keys
233+
234+
235+ class FakeFlavor (object ):
236+ """Fake one or more flavors."""
237+
238+ @staticmethod
239+ def create_one_flavor (attrs = {}):
240+ """Create a fake flavor.
241+
242+ :param Dictionary attrs:
243+ A dictionary with all attributes
244+ :return:
245+ A FakeFlavorResource object, with id, name, ram, vcpus, properties
246+ """
247+ # Set default attributes.
248+ flavor_info = {
249+ 'id' : 'flavor-id-' + uuid .uuid4 ().hex ,
250+ 'name' : 'flavor-name-' + uuid .uuid4 ().hex ,
251+ 'ram' : 8192 ,
252+ 'vcpus' : 4 ,
253+ }
254+
255+ # Overwrite default attributes.
256+ flavor_info .update (attrs )
257+
258+ flavor = FakeFlavorResource (info = copy .deepcopy (flavor_info ),
259+ loaded = True )
260+ return flavor
261+
262+ @staticmethod
263+ def create_flavors (attrs = {}, count = 2 ):
264+ """Create multiple fake flavors.
265+
266+ :param Dictionary attrs:
267+ A dictionary with all attributes
268+ :param int count:
269+ The number of flavors to fake
270+ :return:
271+ A list of FakeFlavorResource objects faking the flavors
272+ """
273+ flavors = []
274+ for i in range (0 , count ):
275+ flavors .append (FakeFlavor .create_one_flavor (attrs ))
276+
277+ return flavors
278+
279+ @staticmethod
280+ def get_flavors (flavors = None , count = 2 ):
281+ """Get an iterable MagicMock object with a list of faked flavors.
282+
283+ If flavors list is provided, then initialize the Mock object with the
284+ list. Otherwise create one.
285+
286+ :param List flavors:
287+ A list of FakeFlavorResource objects faking flavors
288+ :param int count:
289+ The number of flavors to fake
290+ :return:
291+ An iterable Mock object with side_effect set to a list of faked
292+ flavors
293+ """
294+ if flavors is None :
295+ flavors = FakeServer .create_flavors (count )
296+ return mock .MagicMock (side_effect = flavors )
0 commit comments