Move FakeServer to tests.common.v2.compute.fakes

FakeServer should not be in tests.fakes since this should
be just for generic re-usable classes.

Change-Id: I19209952de69626dfa3caadc5d1cc69b7feadeba
This commit is contained in:
lin-hua-cheng 2015-11-25 00:17:49 -08:00
parent c0676fe17f
commit b3c2668c34
3 changed files with 87 additions and 80 deletions

View File

@ -13,7 +13,9 @@
# under the License. # under the License.
# #
import copy
import mock import mock
import uuid
from openstackclient.tests import fakes from openstackclient.tests import fakes
from openstackclient.tests.identity.v2_0 import fakes as identity_fakes from openstackclient.tests.identity.v2_0 import fakes as identity_fakes
@ -139,3 +141,71 @@ class TestComputev2(utils.TestCommand):
endpoint=fakes.AUTH_URL, endpoint=fakes.AUTH_URL,
token=fakes.AUTH_TOKEN, token=fakes.AUTH_TOKEN,
) )
class FakeServer(object):
"""Fake one or more compute servers."""
@staticmethod
def create_one_server(attrs={}, methods={}):
"""Create a fake server.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, name, metadata
"""
# Set default attributes.
server_info = {
'id': 'server-id-' + uuid.uuid4().hex,
'name': 'server-name-' + uuid.uuid4().hex,
'metadata': {},
}
# Overwrite default attributes.
server_info.update(attrs)
server = fakes.FakeResource(info=copy.deepcopy(server_info),
methods=methods,
loaded=True)
return server
@staticmethod
def create_servers(attrs={}, methods={}, count=2):
"""Create multiple fake servers.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of servers to fake
:return:
A list of FakeResource objects faking the servers
"""
servers = []
for i in range(0, count):
servers.append(FakeServer.create_one_server(attrs, methods))
return servers
@staticmethod
def get_servers(servers=None, count=2):
"""Get an iterable MagicMock object with a list of faked servers.
If servers list is provided, then initialize the Mock object with the
list. Otherwise create one.
:param List servers:
A list of FakeResource objects faking servers
:param int count:
The number of servers to fake
:return:
An iterable Mock object with side_effect set to a list of faked
servers
"""
if servers is None:
servers = FakeServer.create_servers(count)
return mock.MagicMock(side_effect=servers)

View File

@ -61,7 +61,8 @@ class TestServerCreate(TestServer):
attrs = { attrs = {
'networks': {}, 'networks': {},
} }
self.new_server = fakes.FakeServer.create_one_server(attrs=attrs) self.new_server = compute_fakes.FakeServer.create_one_server(
attrs=attrs)
# This is the return value for utils.find_resource(). # This is the return value for utils.find_resource().
# This is for testing --wait option. # This is for testing --wait option.
@ -398,7 +399,7 @@ class TestServerDelete(TestServer):
def setUp(self): def setUp(self):
super(TestServerDelete, self).setUp() super(TestServerDelete, self).setUp()
self.server = fakes.FakeServer.create_one_server() self.server = compute_fakes.FakeServer.create_one_server()
# This is the return value for utils.find_resource() # This is the return value for utils.find_resource()
self.servers_mock.get.return_value = self.server self.servers_mock.get.return_value = self.server
@ -476,7 +477,7 @@ class TestServerImageCreate(TestServer):
def setUp(self): def setUp(self):
super(TestServerImageCreate, self).setUp() super(TestServerImageCreate, self).setUp()
self.server = fakes.FakeServer.create_one_server() self.server = compute_fakes.FakeServer.create_one_server()
# This is the return value for utils.find_resource() # This is the return value for utils.find_resource()
self.servers_mock.get.return_value = self.server self.servers_mock.get.return_value = self.server
@ -569,11 +570,14 @@ class TestServerPause(TestServer):
} }
def setup_servers_mock(self, count=1): def setup_servers_mock(self, count=1):
servers = fakes.FakeServer.create_servers(methods=self.methods, servers = compute_fakes.FakeServer.create_servers(
count=count) methods=self.methods,
count=count)
# This is the return value for utils.find_resource() # This is the return value for utils.find_resource()
self.servers_mock.get = fakes.FakeServer.get_servers(servers, 1) self.servers_mock.get = compute_fakes.FakeServer.get_servers(
servers,
1)
return servers return servers
@ -617,7 +621,7 @@ class TestServerResize(TestServer):
def setUp(self): def setUp(self):
super(TestServerResize, self).setUp() super(TestServerResize, self).setUp()
self.server = fakes.FakeServer.create_one_server() self.server = compute_fakes.FakeServer.create_one_server()
# This is the return value for utils.find_resource() # This is the return value for utils.find_resource()
self.servers_mock.get.return_value = self.server self.servers_mock.get.return_value = self.server
@ -806,10 +810,13 @@ class TestShelveServer(TestServer):
} }
def setup_servers_mock(self, count=1): def setup_servers_mock(self, count=1):
servers = fakes.FakeServer.create_servers(methods=self.methods, servers = compute_fakes.FakeServer.create_servers(
count=count) methods=self.methods,
count=count)
self.servers_mock.get = fakes.FakeServer.get_servers(servers, 1) self.servers_mock.get = compute_fakes.FakeServer.get_servers(
servers,
1)
return servers return servers

View File

@ -13,12 +13,10 @@
# under the License. # under the License.
# #
import copy
import json import json
import mock import mock
import six import six
import sys import sys
import uuid
from keystoneauth1 import fixture from keystoneauth1 import fixture
import requests import requests
@ -185,71 +183,3 @@ class FakeModel(dict):
return self[key] return self[key]
except KeyError: except KeyError:
raise AttributeError(key) raise AttributeError(key)
class FakeServer(object):
"""Fake one or more compute servers."""
@staticmethod
def create_one_server(attrs={}, methods={}):
"""Create a fake server.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:return:
A FakeResource object, with id, name, metadata
"""
# Set default attributes.
server_info = {
'id': 'server-id-' + uuid.uuid4().hex,
'name': 'server-name-' + uuid.uuid4().hex,
'metadata': {},
}
# Overwrite default attributes.
server_info.update(attrs)
server = FakeResource(info=copy.deepcopy(server_info),
methods=methods,
loaded=True)
return server
@staticmethod
def create_servers(attrs={}, methods={}, count=2):
"""Create multiple fake servers.
:param Dictionary attrs:
A dictionary with all attributes
:param Dictionary methods:
A dictionary with all methods
:param int count:
The number of servers to fake
:return:
A list of FakeResource objects faking the servers
"""
servers = []
for i in range(0, count):
servers.append(FakeServer.create_one_server(attrs, methods))
return servers
@staticmethod
def get_servers(servers=None, count=2):
"""Get an iterable MagicMock object with a list of faked servers.
If servers list is provided, then initialize the Mock object with the
list. Otherwise create one.
:param List servers:
A list of FakeResource objects faking servers
:param int count:
The number of servers to fake
:return:
An iterable Mock object with side_effect set to a list of faked
servers
"""
if servers is None:
servers = FakeServer.create_servers(count)
return mock.MagicMock(side_effect=servers)