-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- test the upload part (mocking requests.post ) - test the existence of the endpoint (marked as failing for now) - clean configcheck
- Loading branch information
Showing
3 changed files
with
122 additions
and
16 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import unittest | ||
import os.path | ||
import mock | ||
from utils.flare import Flare | ||
|
||
def get_mocked_config(): | ||
return { | ||
'api_key': 'APIKEY', | ||
'dd_url': 'https://app.datadoghq.com', | ||
} | ||
|
||
def get_mocked_version(): | ||
return '6.6.6' | ||
|
||
def get_mocked_temp(): | ||
return os.path.join( | ||
os.path.dirname(os.path.realpath(__file__)), | ||
'flare' | ||
) | ||
|
||
def mocked_strftime(t): | ||
return '1' | ||
|
||
class FakeResponse(object): | ||
def __init__(self, status_code=200): | ||
self.status_code = status_code | ||
self.text = '{"case_id":1337}' | ||
|
||
class FlareTest(unittest.TestCase): | ||
|
||
@mock.patch('utils.flare.strftime', side_effect=mocked_strftime) | ||
@mock.patch('tempfile.gettempdir', side_effect=get_mocked_temp) | ||
@mock.patch('config.get_version', side_effect=get_mocked_version) | ||
@mock.patch('utils.flare.get_config', side_effect=get_mocked_config) | ||
def test_init(self, mock_config, mock_version, mock_tempdir, mock_strftime): | ||
f = Flare(case_id=1337) | ||
conf = mock_config() | ||
self.assertEqual(f._case_id, 1337) | ||
self.assertEqual(f._api_key, conf['api_key']) | ||
self.assertEqual(f._url, 'https://6-6-6-flare.agent.datadoghq.com/support/flare') | ||
self.assertEqual(f._tar_path, os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2")) | ||
|
||
@mock.patch('utils.flare.requests.post', return_value=FakeResponse()) | ||
@mock.patch('config.get_version', side_effect=get_mocked_version) | ||
@mock.patch('utils.flare.strftime', side_effect=mocked_strftime) | ||
@mock.patch('tempfile.gettempdir', side_effect=get_mocked_temp) | ||
@mock.patch('utils.flare.get_config', side_effect=get_mocked_config) | ||
def test_upload_with_case(self, mock_config, mock_tempdir, mock_stfrtime, mock_version, mock_requests): | ||
f = Flare(case_id=1337) | ||
|
||
assert not mock_requests.called | ||
f.upload(confirmation=False) | ||
assert mock_requests.called | ||
args, kwargs = mock_requests.call_args_list[0] | ||
self.assertEqual( | ||
args, | ||
('https://6-6-6-flare.agent.datadoghq.com/support/flare/1337?api_key=APIKEY',) | ||
) | ||
self.assertEqual( | ||
kwargs['files']['flare_file'].name, | ||
os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2") | ||
) | ||
self.assertEqual(kwargs['data']['case_id'], 1337) | ||
self.assertEqual(kwargs['data']['email'], '') | ||
assert kwargs['data']['hostname'] | ||
|
||
@mock.patch('utils.flare.requests.post', return_value=FakeResponse()) | ||
@mock.patch('config.get_version', side_effect=get_mocked_version) | ||
@mock.patch('utils.flare.strftime', side_effect=mocked_strftime) | ||
@mock.patch('tempfile.gettempdir', side_effect=get_mocked_temp) | ||
@mock.patch('utils.flare.get_config', side_effect=get_mocked_config) | ||
def test_upload_no_case(self, mock_config, mock_tempdir, mock_stfrtime, mock_version, mock_requests): | ||
f = Flare() | ||
f._ask_for_email = lambda: 'test@example.com' | ||
|
||
assert not mock_requests.called | ||
f.upload(confirmation=False) | ||
assert mock_requests.called | ||
args, kwargs = mock_requests.call_args_list[0] | ||
self.assertEqual( | ||
args, | ||
('https://6-6-6-flare.agent.datadoghq.com/support/flare?api_key=APIKEY',) | ||
) | ||
self.assertEqual( | ||
kwargs['files']['flare_file'].name, | ||
os.path.join(get_mocked_temp(), "datadog-agent-1.tar.bz2") | ||
) | ||
self.assertEqual(kwargs['data']['case_id'], None) | ||
self.assertEqual(kwargs['data']['email'], 'test@example.com') | ||
assert kwargs['data']['hostname'] | ||
|
||
@unittest.expectedFailure | ||
@mock.patch('utils.flare.strftime', side_effect=mocked_strftime) | ||
@mock.patch('tempfile.gettempdir', side_effect=get_mocked_temp) | ||
@mock.patch('utils.flare.get_config', side_effect=get_mocked_config) | ||
def test_endpoint(self, mock_config, mock_temp, mock_stfrtime): | ||
f = Flare() | ||
f._ask_for_email = lambda: None | ||
try: | ||
f.upload(confirmation=False) | ||
raise Exception('Should fail before') | ||
except Exception, e: | ||
self.assertEqual(str(e), "Invalid inputs: {'email': None}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters