138 lines
4.5 KiB
Python
Raw Normal View History

2013-12-20 16:14:20 -08:00
"""
Copyright 2013 Rackspace, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
2014-01-05 21:46:49 -08:00
import json
2013-12-20 16:14:20 -08:00
import time
import unittest
import mock
import pkg_resources
2014-01-05 21:46:49 -08:00
from teeth_rest import encoding
2013-12-20 16:14:20 -08:00
from teeth_agent import base
2013-12-26 17:46:50 -08:00
from teeth_agent import errors
2013-12-20 16:14:20 -08:00
2014-01-05 21:46:49 -08:00
EXPECTED_ERROR = RuntimeError('command execution failed')
class FooTeethAgentCommandResult(base.AsyncCommandResult):
def execute(self):
if self.command_params['fail']:
raise EXPECTED_ERROR
else:
return 'command execution succeeded'
2013-12-20 16:14:20 -08:00
class TestBaseTeethAgent(unittest.TestCase):
def setUp(self):
2014-01-05 21:46:49 -08:00
self.encoder = encoding.RESTJSONEncoder(
encoding.SerializationViews.PUBLIC,
indent=4)
2014-01-06 12:02:31 -08:00
self.agent = base.BaseTeethAgent('fake_host',
'fake_port',
'fake_api',
'TEST_MODE')
2013-12-20 16:14:20 -08:00
2014-01-05 21:46:49 -08:00
def assertEqualEncoded(self, a, b):
# Evidently JSONEncoder.default() can't handle None (??) so we have to
# use encode() to generate JSON, then json.loads() to get back a python
# object.
a_encoded = self.encoder.encode(a)
b_encoded = self.encoder.encode(b)
self.assertEqual(json.loads(a_encoded), json.loads(b_encoded))
2013-12-20 16:14:20 -08:00
def test_get_status(self):
started_at = time.time()
self.agent.started_at = started_at
status = self.agent.get_status()
self.assertIsInstance(status, base.TeethAgentStatus)
self.assertEqual(status.mode, 'TEST_MODE')
self.assertEqual(status.started_at, started_at)
self.assertEqual(status.version,
pkg_resources.get_distribution('teeth-agent').version)
def test_execute_command(self):
do_something_impl = mock.Mock()
self.agent.command_map = {
'do_something': do_something_impl,
}
2013-12-20 16:14:20 -08:00
self.agent.execute_command('do_something', foo='bar')
do_something_impl.assert_called_once_with('do_something', foo='bar')
2013-12-20 16:14:20 -08:00
2013-12-26 17:46:50 -08:00
def test_execute_invalid_command(self):
self.assertRaises(errors.InvalidCommandError,
self.agent.execute_command,
'do_something',
foo='bar')
2013-12-20 16:14:20 -08:00
@mock.patch('werkzeug.serving.run_simple')
def test_run(self, mocked_run_simple):
self.agent.run()
mocked_run_simple.assert_called_once_with('fake_host',
'fake_port',
self.agent.api)
self.assertRaises(RuntimeError, self.agent.run)
2014-01-05 21:46:49 -08:00
def test_async_command_success(self):
result = FooTeethAgentCommandResult('foo_command', {'fail': False})
expected_result = {
'id': result.id,
'command_name': 'foo_command',
'command_params': {
'fail': False,
},
'command_status': 'RUNNING',
'command_result': None,
'command_error': None,
}
self.assertEqualEncoded(result, expected_result)
result.start()
result.join()
expected_result['command_status'] = 'SUCCEEDED'
expected_result['command_result'] = 'command execution succeeded'
self.assertEqualEncoded(result, expected_result)
def test_async_command_failure(self):
result = FooTeethAgentCommandResult('foo_command', {'fail': True})
expected_result = {
'id': result.id,
'command_name': 'foo_command',
'command_params': {
'fail': True,
},
'command_status': 'RUNNING',
'command_result': None,
'command_error': None,
}
self.assertEqualEncoded(result, expected_result)
result.start()
result.join()
expected_result['command_status'] = 'FAILED'
expected_result['command_error'] = errors.CommandExecutionError(
str(EXPECTED_ERROR))
self.assertEqualEncoded(result, expected_result)