diff options
| author | Tang Chen <tangchen@cn.fujitsu.com> | 2015-11-28 11:15:54 +0800 |
|---|---|---|
| committer | Tang Chen <tangchen@cn.fujitsu.com> | 2015-11-28 16:13:52 +0800 |
| commit | 0de260e8be0db504de375c5f77928931fbb0cb3b (patch) | |
| tree | fe45bcaab66926430b6f4c63dbaabb500732782b | |
| parent | 109672fecb72e5cef4bc9163d6b93fd27ad98c32 (diff) | |
| download | python-openstackclient-0de260e8be0db504de375c5f77928931fbb0cb3b.tar.gz | |
Introduce class FakeFlavor to fake one or more flavors.
Change-Id: I1b20e7d50e478ce8114ca08aa455b7acad4ea7f5
Implements: blueprint improve-flavor-unit-test
| -rw-r--r-- | openstackclient/tests/compute/v2/fakes.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py index 5513f1b1..ffa2f95e 100644 --- a/openstackclient/tests/compute/v2/fakes.py +++ b/openstackclient/tests/compute/v2/fakes.py @@ -230,3 +230,67 @@ class FakeFlavorResource(fakes.FakeResource): def get_keys(self): return self._keys + + +class FakeFlavor(object): + """Fake one or more flavors.""" + + @staticmethod + def create_one_flavor(attrs={}): + """Create a fake flavor. + + :param Dictionary attrs: + A dictionary with all attributes + :return: + A FakeFlavorResource object, with id, name, ram, vcpus, properties + """ + # Set default attributes. + flavor_info = { + 'id': 'flavor-id-' + uuid.uuid4().hex, + 'name': 'flavor-name-' + uuid.uuid4().hex, + 'ram': 8192, + 'vcpus': 4, + } + + # Overwrite default attributes. + flavor_info.update(attrs) + + flavor = FakeFlavorResource(info=copy.deepcopy(flavor_info), + loaded=True) + return flavor + + @staticmethod + def create_flavors(attrs={}, count=2): + """Create multiple fake flavors. + + :param Dictionary attrs: + A dictionary with all attributes + :param int count: + The number of flavors to fake + :return: + A list of FakeFlavorResource objects faking the flavors + """ + flavors = [] + for i in range(0, count): + flavors.append(FakeFlavor.create_one_flavor(attrs)) + + return flavors + + @staticmethod + def get_flavors(flavors=None, count=2): + """Get an iterable MagicMock object with a list of faked flavors. + + If flavors list is provided, then initialize the Mock object with the + list. Otherwise create one. + + :param List flavors: + A list of FakeFlavorResource objects faking flavors + :param int count: + The number of flavors to fake + :return: + An iterable Mock object with side_effect set to a list of faked + flavors + """ + if flavors is None: + flavors = FakeServer.create_flavors(count) + return mock.MagicMock(side_effect=flavors) |
