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

This commit is contained in:
Jenkins 2015-11-26 04:03:00 +00:00 committed by Gerrit Code Review
commit 487fff8cdb
3 changed files with 87 additions and 80 deletions

View File

@ -13,7 +13,9 @@
# under the License.
#
import copy
import mock
import uuid
from openstackclient.tests import fakes
from openstackclient.tests.identity.v2_0 import fakes as identity_fakes
@ -139,3 +141,71 @@ class TestComputev2(utils.TestCommand):
endpoint=fakes.AUTH_URL,
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 = {
'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 for testing --wait option.
@ -398,7 +399,7 @@ class TestServerDelete(TestServer):
def setUp(self):
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()
self.servers_mock.get.return_value = self.server
@ -476,7 +477,7 @@ class TestServerImageCreate(TestServer):
def setUp(self):
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()
self.servers_mock.get.return_value = self.server
@ -569,11 +570,14 @@ class TestServerPause(TestServer):
}
def setup_servers_mock(self, count=1):
servers = fakes.FakeServer.create_servers(methods=self.methods,
count=count)
servers = compute_fakes.FakeServer.create_servers(
methods=self.methods,
count=count)
# 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
@ -617,7 +621,7 @@ class TestServerResize(TestServer):
def setUp(self):
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()
self.servers_mock.get.return_value = self.server
@ -806,10 +810,13 @@ class TestShelveServer(TestServer):
}
def setup_servers_mock(self, count=1):
servers = fakes.FakeServer.create_servers(methods=self.methods,
count=count)
servers = compute_fakes.FakeServer.create_servers(
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

View File

@ -13,12 +13,10 @@
# under the License.
#
import copy
import json
import mock
import six
import sys
import uuid
from keystoneauth1 import fixture
import requests
@ -185,71 +183,3 @@ class FakeModel(dict):
return self[key]
except KeyError:
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)