update tests
This commit is contained in:
@@ -24,6 +24,7 @@ from werkzeug import wrappers
|
|||||||
|
|
||||||
from teeth_rest import encoding
|
from teeth_rest import encoding
|
||||||
|
|
||||||
|
from teeth_agent import agent
|
||||||
from teeth_agent import api
|
from teeth_agent import api
|
||||||
from teeth_agent import base
|
from teeth_agent import base
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ class TestTeethAPI(unittest.TestCase):
|
|||||||
return client.open(self._get_env_builder(method, path, data, query))
|
return client.open(self._get_env_builder(method, path, data, query))
|
||||||
|
|
||||||
def test_get_agent_status(self):
|
def test_get_agent_status(self):
|
||||||
status = base.TeethAgentStatus('TEST_MODE', time.time(), 'v72ac9')
|
status = agent.TeethAgentStatus('TEST_MODE', time.time(), 'v72ac9')
|
||||||
mock_agent = mock.MagicMock()
|
mock_agent = mock.MagicMock()
|
||||||
mock_agent.get_status.return_value = status
|
mock_agent.get_status.return_value = status
|
||||||
api_server = api.TeethAgentAPIServer(mock_agent)
|
api_server = api.TeethAgentAPIServer(mock_agent)
|
||||||
@@ -120,7 +121,7 @@ class TestTeethAPI(unittest.TestCase):
|
|||||||
True,
|
True,
|
||||||
{'test': 'result'})
|
{'test': 'result'})
|
||||||
|
|
||||||
mock_agent = mock.create_autospec(base.BaseTeethAgent)
|
mock_agent = mock.create_autospec(agent.TeethAgent)
|
||||||
mock_agent.list_command_results.return_value = [
|
mock_agent.list_command_results.return_value = [
|
||||||
cmd_result,
|
cmd_result,
|
||||||
]
|
]
|
||||||
@@ -144,7 +145,7 @@ class TestTeethAPI(unittest.TestCase):
|
|||||||
serialized_cmd_result = cmd_result.serialize(
|
serialized_cmd_result = cmd_result.serialize(
|
||||||
encoding.SerializationViews.PUBLIC)
|
encoding.SerializationViews.PUBLIC)
|
||||||
|
|
||||||
mock_agent = mock.create_autospec(base.BaseTeethAgent)
|
mock_agent = mock.create_autospec(agent.TeethAgent)
|
||||||
mock_agent.get_command_result.return_value = cmd_result
|
mock_agent.get_command_result.return_value = cmd_result
|
||||||
|
|
||||||
api_server = api.TeethAgentAPIServer(mock_agent)
|
api_server = api.TeethAgentAPIServer(mock_agent)
|
||||||
|
@@ -23,6 +23,7 @@ import pkg_resources
|
|||||||
|
|
||||||
from teeth_rest import encoding
|
from teeth_rest import encoding
|
||||||
|
|
||||||
|
from teeth_agent import agent
|
||||||
from teeth_agent import base
|
from teeth_agent import base
|
||||||
from teeth_agent import errors
|
from teeth_agent import errors
|
||||||
|
|
||||||
@@ -38,10 +39,15 @@ class FooTeethAgentCommandResult(base.AsyncCommandResult):
|
|||||||
return 'command execution succeeded'
|
return 'command execution succeeded'
|
||||||
|
|
||||||
|
|
||||||
|
class FakeMode(base.BaseAgentMode):
|
||||||
|
def __init__(self):
|
||||||
|
super(FakeMode, self).__init__('FAKE')
|
||||||
|
|
||||||
|
|
||||||
class TestHeartbeater(unittest.TestCase):
|
class TestHeartbeater(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.mock_agent = mock.Mock()
|
self.mock_agent = mock.Mock()
|
||||||
self.heartbeater = base.TeethAgentHeartbeater(self.mock_agent)
|
self.heartbeater = agent.TeethAgentHeartbeater(self.mock_agent)
|
||||||
self.heartbeater.api = mock.Mock()
|
self.heartbeater.api = mock.Mock()
|
||||||
self.heartbeater.stop_event = mock.Mock()
|
self.heartbeater.stop_event = mock.Mock()
|
||||||
|
|
||||||
@@ -108,17 +114,15 @@ class TestHeartbeater(unittest.TestCase):
|
|||||||
self.assertEqual(self.heartbeater.error_delay, 2.7)
|
self.assertEqual(self.heartbeater.error_delay, 2.7)
|
||||||
|
|
||||||
|
|
||||||
class TestBaseTeethAgent(unittest.TestCase):
|
class TestBaseAgent(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.encoder = encoding.RESTJSONEncoder(
|
self.encoder = encoding.RESTJSONEncoder(
|
||||||
encoding.SerializationViews.PUBLIC,
|
encoding.SerializationViews.PUBLIC,
|
||||||
indent=4)
|
indent=4)
|
||||||
self.agent = base.BaseTeethAgent(None,
|
self.agent = agent.TeethAgent('https://fake_api.example.org:8081/',
|
||||||
9999,
|
('localhost', 9999),
|
||||||
None,
|
('localhost', 9999),
|
||||||
9999,
|
FakeMode())
|
||||||
'https://fake_api.example.org:8081/',
|
|
||||||
'TEST_MODE')
|
|
||||||
|
|
||||||
def assertEqualEncoded(self, a, b):
|
def assertEqualEncoded(self, a, b):
|
||||||
# Evidently JSONEncoder.default() can't handle None (??) so we have to
|
# Evidently JSONEncoder.default() can't handle None (??) so we have to
|
||||||
@@ -133,17 +137,14 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
self.agent.started_at = started_at
|
self.agent.started_at = started_at
|
||||||
|
|
||||||
status = self.agent.get_status()
|
status = self.agent.get_status()
|
||||||
self.assertIsInstance(status, base.TeethAgentStatus)
|
self.assertIsInstance(status, agent.TeethAgentStatus)
|
||||||
self.assertEqual(status.mode, 'TEST_MODE')
|
|
||||||
self.assertEqual(status.started_at, started_at)
|
self.assertEqual(status.started_at, started_at)
|
||||||
self.assertEqual(status.version,
|
self.assertEqual(status.version,
|
||||||
pkg_resources.get_distribution('teeth-agent').version)
|
pkg_resources.get_distribution('teeth-agent').version)
|
||||||
|
|
||||||
def test_execute_command(self):
|
def test_execute_command(self):
|
||||||
do_something_impl = mock.Mock()
|
do_something_impl = mock.Mock()
|
||||||
self.agent.command_map = {
|
self.agent.mode_implementation['do_something'] = do_something_impl
|
||||||
'do_something': do_something_impl,
|
|
||||||
}
|
|
||||||
|
|
||||||
self.agent.execute_command('do_something', foo='bar')
|
self.agent.execute_command('do_something', foo='bar')
|
||||||
do_something_impl.assert_called_once_with('do_something', foo='bar')
|
do_something_impl.assert_called_once_with('do_something', foo='bar')
|
||||||
@@ -159,12 +160,10 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
wsgi_server = wsgi_server_cls.return_value
|
wsgi_server = wsgi_server_cls.return_value
|
||||||
wsgi_server.start.side_effect = KeyboardInterrupt()
|
wsgi_server.start.side_effect = KeyboardInterrupt()
|
||||||
|
|
||||||
self.agent.get_api_facing_ip_address = mock.Mock()
|
|
||||||
self.agent.get_api_facing_ip_address.return_value = '1.2.3.4'
|
|
||||||
self.agent.heartbeater = mock.Mock()
|
self.agent.heartbeater = mock.Mock()
|
||||||
self.agent.run()
|
self.agent.run()
|
||||||
|
|
||||||
listen_addr = ('1.2.3.4', 9999)
|
listen_addr = ('localhost', 9999)
|
||||||
wsgi_server_cls.assert_called_once_with(listen_addr, self.agent.api)
|
wsgi_server_cls.assert_called_once_with(listen_addr, self.agent.api)
|
||||||
wsgi_server.start.assert_called_once_with()
|
wsgi_server.start.assert_called_once_with()
|
||||||
wsgi_server.stop.assert_called_once_with()
|
wsgi_server.stop.assert_called_once_with()
|
||||||
|
@@ -19,9 +19,9 @@ import unittest
|
|||||||
from teeth_agent import decom
|
from teeth_agent import decom
|
||||||
|
|
||||||
|
|
||||||
class TestBaseTeethAgent(unittest.TestCase):
|
class TestDecomMode(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.agent = decom.DecomAgent(None, 9999, None, 9999, 'fake_api')
|
self.agent_mode = decom.DecomMode()
|
||||||
|
|
||||||
def test_decom_mode(self):
|
def test_decom_mode(self):
|
||||||
self.assertEqual(self.agent.mode, 'DECOM')
|
self.assertEqual(self.agent_mode.name, 'DECOM')
|
||||||
|
@@ -22,12 +22,12 @@ from teeth_agent import errors
|
|||||||
from teeth_agent import standby
|
from teeth_agent import standby
|
||||||
|
|
||||||
|
|
||||||
class TestBaseTeethAgent(unittest.TestCase):
|
class TestStandbyMode(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.agent = standby.StandbyAgent(None, 9999, None, 9999, 'fake_api')
|
self.agent_mode = standby.StandbyMode()
|
||||||
|
|
||||||
def test_standby_mode(self):
|
def test_standby_mode(self):
|
||||||
self.assertEqual(self.agent.mode, 'STANDBY')
|
self.assertEqual(self.agent_mode.name, 'STANDBY')
|
||||||
|
|
||||||
def _build_fake_image_info(self):
|
def _build_fake_image_info(self):
|
||||||
return {
|
return {
|
||||||
@@ -41,7 +41,7 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def test_validate_image_info_success(self):
|
def test_validate_image_info_success(self):
|
||||||
self.agent._validate_image_info(self._build_fake_image_info())
|
self.agent_mode._validate_image_info(self._build_fake_image_info())
|
||||||
|
|
||||||
def test_validate_image_info_missing_field(self):
|
def test_validate_image_info_missing_field(self):
|
||||||
for field in ['id', 'urls', 'hashes']:
|
for field in ['id', 'urls', 'hashes']:
|
||||||
@@ -49,7 +49,7 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
del invalid_info[field]
|
del invalid_info[field]
|
||||||
|
|
||||||
self.assertRaises(errors.InvalidCommandParamsError,
|
self.assertRaises(errors.InvalidCommandParamsError,
|
||||||
self.agent._validate_image_info,
|
self.agent_mode._validate_image_info,
|
||||||
invalid_info)
|
invalid_info)
|
||||||
|
|
||||||
def test_validate_image_info_invalid_urls(self):
|
def test_validate_image_info_invalid_urls(self):
|
||||||
@@ -57,7 +57,7 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
invalid_info['urls'] = 'this_is_not_a_list'
|
invalid_info['urls'] = 'this_is_not_a_list'
|
||||||
|
|
||||||
self.assertRaises(errors.InvalidCommandParamsError,
|
self.assertRaises(errors.InvalidCommandParamsError,
|
||||||
self.agent._validate_image_info,
|
self.agent_mode._validate_image_info,
|
||||||
invalid_info)
|
invalid_info)
|
||||||
|
|
||||||
def test_validate_image_info_empty_urls(self):
|
def test_validate_image_info_empty_urls(self):
|
||||||
@@ -73,7 +73,7 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
invalid_info['hashes'] = 'this_is_not_a_dict'
|
invalid_info['hashes'] = 'this_is_not_a_dict'
|
||||||
|
|
||||||
self.assertRaises(errors.InvalidCommandParamsError,
|
self.assertRaises(errors.InvalidCommandParamsError,
|
||||||
self.agent._validate_image_info,
|
self.agent_mode._validate_image_info,
|
||||||
invalid_info)
|
invalid_info)
|
||||||
|
|
||||||
def test_validate_image_info_empty_hashes(self):
|
def test_validate_image_info_empty_hashes(self):
|
||||||
@@ -85,13 +85,13 @@ class TestBaseTeethAgent(unittest.TestCase):
|
|||||||
invalid_info)
|
invalid_info)
|
||||||
|
|
||||||
def test_cache_images_success(self):
|
def test_cache_images_success(self):
|
||||||
result = self.agent.cache_images('cache_images',
|
result = self.agent_mode.cache_images('cache_images',
|
||||||
[self._build_fake_image_info()])
|
[self._build_fake_image_info()])
|
||||||
result.join()
|
result.join()
|
||||||
|
|
||||||
def test_cache_images_invalid_image_list(self):
|
def test_cache_images_invalid_image_list(self):
|
||||||
self.assertRaises(errors.InvalidCommandParamsError,
|
self.assertRaises(errors.InvalidCommandParamsError,
|
||||||
self.agent.cache_images,
|
self.agent_mode.cache_images,
|
||||||
'cache_images',
|
'cache_images',
|
||||||
{'foo': 'bar'})
|
{'foo': 'bar'})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user