summaryrefslogtreecommitdiff
path: root/ironic_python_agent/tests/functional/base.py
diff options
context:
space:
mode:
authorMario Villaplana <mario.villaplana@gmail.com>2015-09-15 21:28:34 +0000
committerMario Villaplana <mario.villaplana@gmail.com>2015-10-06 15:36:27 +0000
commit4a7b954d148461128f57bd148d85fa8a6b9c0dfd (patch)
tree58fe1f84fd30ef76ba002776bd14288131cecbb3 /ironic_python_agent/tests/functional/base.py
parentdcbba2b121da1525feff162aca17a8cc4adf55d6 (diff)
downloadironic-python-agent-4a7b954d148461128f57bd148d85fa8a6b9c0dfd.tar.gz
Adds more functional tests for commands
This adds additional functional tests for the commands API. It also restructures the current functional test to run in sequence as a monolithic test to preserve IPA state and test order between test runs. The time to wait for IPA to start was increased due to an intermittent race condition that occurs with the larger test suite. Partial-Bug: 1492036 Change-Id: Iff9b41fb531d34225d702b9bfd39826e3c7195ad
Diffstat (limited to 'ironic_python_agent/tests/functional/base.py')
-rw-r--r--ironic_python_agent/tests/functional/base.py47
1 files changed, 38 insertions, 9 deletions
diff --git a/ironic_python_agent/tests/functional/base.py b/ironic_python_agent/tests/functional/base.py
index d57e3d46..cdbca34a 100644
--- a/ironic_python_agent/tests/functional/base.py
+++ b/ironic_python_agent/tests/functional/base.py
@@ -24,32 +24,61 @@ from ironic_python_agent import agent
class FunctionalBase(test_base.BaseTestCase):
+
def setUp(self):
"""Start the agent and wait for it to start"""
super(FunctionalBase, self).setUp()
mpl = multiprocessing.log_to_stderr()
mpl.setLevel(logging.INFO)
- test_port = os.environ.get('TEST_PORT', '9999')
+ self.test_port = os.environ.get('TEST_PORT', '9999')
# Build a basic standalone agent using the config option defaults.
# 127.0.0.1:6835 is the fake Ironic client.
self.agent = agent.IronicPythonAgent(
'http://127.0.0.1:6835', 'localhost',
- ('0.0.0.0', int(test_port)), 3, 10, None, 300, 1, 'agent_ipmitool',
- True)
+ ('0.0.0.0', int(self.test_port)), 3, 10, None, 300, 1,
+ 'agent_ipmitool', True)
self.process = multiprocessing.Process(
target=self.agent.run)
self.process.start()
self.addCleanup(self.process.terminate)
# Wait for process to start, otherwise we have a race for tests
+ sleep_time = 0.1
tries = 0
- max_tries = os.environ.get('IPA_WAIT_TIME', '2')
- while tries < int(max_tries):
+ max_tries = int(os.environ.get('IPA_WAIT_TRIES', '100'))
+ while tries < max_tries:
try:
- return requests.get(
- 'http://localhost:%s/v1/commands' % test_port)
+ return self.request('get', 'commands')
except requests.ConnectionError:
- time.sleep(.1)
+ time.sleep(sleep_time)
tries += 1
- raise IOError('Agent did not start after %s seconds.' % max_tries)
+ raise IOError('Agent did not start after %s seconds.' % (max_tries *
+ sleep_time))
+
+ def request(self, method, path, expect_error=None, expect_json=True,
+ **kwargs):
+ """Send a request to the agent and verifies response.
+
+ :param: method type of request to send as a string
+ :param: path desired API endpoint to request, for example 'commands'
+ :param: expect_error error code to expect, if an error is expected
+ :param: expect_json whether to expect a JSON response. if True, convert
+ it to a dict before returning, otherwise return the
+ Response object
+ :param **kwargs: keyword args to pass to the request method
+ :raises: HTTPError if an error is returned that was not expected
+ :raises: AssertionError if a received HTTP status code does not match
+ expect_error
+ :returns: the response object
+ """
+ res = requests.request(method, 'http://localhost:%s/v1/%s' %
+ (self.test_port, path), **kwargs)
+ if expect_error is not None:
+ self.assertEqual(expect_error, res.status_code)
+ else:
+ res.raise_for_status()
+ if expect_json:
+ return res.json()
+ else:
+ return res