126 lines
4.6 KiB
Python
Raw Normal View History

2014-01-07 17:55:02 -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.
"""
import httmock
import json
import mock
import time
import unittest
2014-01-07 18:11:57 -08:00
from teeth_agent import errors
2014-01-28 11:25:36 -08:00
from teeth_agent import hardware
2014-01-07 17:55:02 -08:00
from teeth_agent import overlord_agent_api
API_URL = 'http://agent-api.overlord.example.org/'
class TestBaseTeethAgent(unittest.TestCase):
def setUp(self):
self.api_client = overlord_agent_api.APIClient(API_URL)
2014-01-28 11:25:36 -08:00
self.hardware_info = [
hardware.HardwareInfo(hardware.HardwareType.MAC_ADDRESS,
'a:b:c:d'),
hardware.HardwareInfo(hardware.HardwareType.MAC_ADDRESS,
'0:1:2:3'),
]
2014-01-07 17:55:02 -08:00
def test_successful_heartbeat(self):
expected_heartbeat_before = time.time() + 120
response = httmock.response(status_code=204, headers={
'Heartbeat-Before': expected_heartbeat_before,
})
self.api_client.session.request = mock.Mock()
self.api_client.session.request.return_value = response
2014-01-07 17:55:02 -08:00
heartbeat_before = self.api_client.heartbeat(
2014-01-28 11:25:36 -08:00
hardware_info=self.hardware_info,
2014-01-07 17:55:02 -08:00
version='15',
mode='STANDBY',
uuid='fake-uuid')
2014-01-07 17:55:02 -08:00
self.assertEqual(heartbeat_before, expected_heartbeat_before)
request_args = self.api_client.session.request.call_args[0]
self.assertEqual(request_args[0], 'PUT')
self.assertEqual(request_args[1], API_URL + 'v1/nodes/fake-uuid/vendor'
'_passthru/heartbeat')
2014-01-07 17:55:02 -08:00
data = self.api_client.session.request.call_args[1]['data']
content = json.loads(data)
2014-01-07 17:55:02 -08:00
self.assertEqual(content['mode'], 'STANDBY')
self.assertEqual(content['version'], '15')
2014-01-28 11:25:36 -08:00
self.assertEqual(content['hardware'], [
{
'type': 'mac_address',
'id': 'a:b:c:d',
},
{
'type': 'mac_address',
'id': '0:1:2:3',
},
])
self.assertEqual(content['agent_url'], API_URL.rstrip('/'))
2014-01-07 18:11:57 -08:00
def test_heartbeat_requests_exception(self):
self.api_client.session.request = mock.Mock()
self.api_client.session.request.side_effect = Exception('api is down!')
2014-01-07 18:11:57 -08:00
self.assertRaises(errors.HeartbeatError,
self.api_client.heartbeat,
2014-01-28 11:25:36 -08:00
hardware_info=self.hardware_info,
2014-01-07 18:11:57 -08:00
version='15',
mode='STANDBY',
uuid='fake-uuid')
2014-01-07 18:11:57 -08:00
def test_heartbeat_invalid_status_code(self):
response = httmock.response(status_code=404)
self.api_client.session.request = mock.Mock()
self.api_client.session.request.return_value = response
2014-01-07 18:11:57 -08:00
self.assertRaises(errors.HeartbeatError,
self.api_client.heartbeat,
2014-01-28 11:25:36 -08:00
hardware_info=self.hardware_info,
2014-01-07 18:11:57 -08:00
version='15',
mode='STANDBY',
uuid='fake-uuid')
2014-01-07 18:11:57 -08:00
def test_heartbeat_missing_heartbeat_before_header(self):
response = httmock.response(status_code=204)
self.api_client.session.request = mock.Mock()
self.api_client.session.request.return_value = response
2014-01-07 18:11:57 -08:00
self.assertRaises(errors.HeartbeatError,
self.api_client.heartbeat,
2014-01-28 11:25:36 -08:00
hardware_info=self.hardware_info,
2014-01-07 18:11:57 -08:00
version='15',
mode='STANDBY',
uuid='fake-uuid')
2014-01-07 18:11:57 -08:00
def test_heartbeat_invalid_heartbeat_before_header(self):
response = httmock.response(status_code=204, headers={
'Heartbeat-Before': 'tomorrow',
})
self.api_client.session.request = mock.Mock()
self.api_client.session.request.return_value = response
2014-01-07 18:11:57 -08:00
self.assertRaises(errors.HeartbeatError,
self.api_client.heartbeat,
2014-01-28 11:25:36 -08:00
hardware_info=self.hardware_info,
2014-01-07 18:11:57 -08:00
version='15',
mode='STANDBY',
uuid='fake-uuid')