Changeset View
Changeset View
Standalone View
Standalone View
swh/icinga_plugins/tests/test_vault.py
Show First 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | def test_vault_immediate_success(requests_mock, mocker): | ||||
scenario.install_mock(requests_mock) | scenario.install_mock(requests_mock) | ||||
get_storage_mock = mocker.patch('swh.icinga_plugins.vault.get_storage') | get_storage_mock = mocker.patch('swh.icinga_plugins.vault.get_storage') | ||||
get_storage_mock.side_effect = FakeStorage | get_storage_mock.side_effect = FakeStorage | ||||
sleep_mock = mocker.patch('time.sleep') | sleep_mock = mocker.patch('time.sleep') | ||||
result = invoke([ | result = invoke([ | ||||
'check-vault', | |||||
'--swh-web-url', 'mock://swh-web.example.org', | '--swh-web-url', 'mock://swh-web.example.org', | ||||
'--swh-storage-url', 'foo://example.org', | '--swh-storage-url', 'foo://example.org', | ||||
'check-vault', 'directory', | 'directory', | ||||
]) | ]) | ||||
assert re.match( | assert re.match( | ||||
rf'^VAULT OK - cooking directory {dir_id} took ' | rf'^VAULT OK - cooking directory {dir_id} took ' | ||||
r'[0-9]\.[0-9]{2}s and succeeded.\n' | r'[0-9]\.[0-9]{2}s and succeeded.\n' | ||||
r"\| 'total_time' = [0-9]\.[0-9]{2}s$", | r"\| 'total_time' = [0-9]\.[0-9]{2}s$", | ||||
result.output) | result.output) | ||||
assert result.exit_code == 0, result.output | assert result.exit_code == 0, result.output | ||||
Show All 14 Lines | def test_vault_delayed_success(requests_mock, mocker): | ||||
scenario.install_mock(requests_mock) | scenario.install_mock(requests_mock) | ||||
get_storage_mock = mocker.patch('swh.icinga_plugins.vault.get_storage') | get_storage_mock = mocker.patch('swh.icinga_plugins.vault.get_storage') | ||||
get_storage_mock.side_effect = FakeStorage | get_storage_mock.side_effect = FakeStorage | ||||
sleep_mock = mocker.patch('time.sleep') | sleep_mock = mocker.patch('time.sleep') | ||||
result = invoke([ | result = invoke([ | ||||
'check-vault', | |||||
'--swh-web-url', 'mock://swh-web.example.org', | '--swh-web-url', 'mock://swh-web.example.org', | ||||
'--swh-storage-url', 'foo://example.org', | '--swh-storage-url', 'foo://example.org', | ||||
'check-vault', 'directory', | 'directory', | ||||
]) | ]) | ||||
assert re.match( | assert re.match( | ||||
rf'^VAULT OK - cooking directory {dir_id} took ' | rf'^VAULT OK - cooking directory {dir_id} took ' | ||||
r'[0-9]\.[0-9]{2}s and succeeded.\n' | r'[0-9]\.[0-9]{2}s and succeeded.\n' | ||||
r"\| 'total_time' = [0-9]\.[0-9]{2}s$", | r"\| 'total_time' = [0-9]\.[0-9]{2}s$", | ||||
result.output) | result.output) | ||||
assert result.exit_code == 0, result.output | assert result.exit_code == 0, result.output | ||||
Show All 13 Lines | def test_vault_failure(requests_mock, mocker): | ||||
scenario.install_mock(requests_mock) | scenario.install_mock(requests_mock) | ||||
get_storage_mock = mocker.patch('swh.icinga_plugins.vault.get_storage') | get_storage_mock = mocker.patch('swh.icinga_plugins.vault.get_storage') | ||||
get_storage_mock.side_effect = FakeStorage | get_storage_mock.side_effect = FakeStorage | ||||
sleep_mock = mocker.patch('time.sleep') | sleep_mock = mocker.patch('time.sleep') | ||||
result = invoke([ | result = invoke([ | ||||
'check-vault', | |||||
'--swh-web-url', 'mock://swh-web.example.org', | '--swh-web-url', 'mock://swh-web.example.org', | ||||
'--swh-storage-url', 'foo://example.org', | '--swh-storage-url', 'foo://example.org', | ||||
'check-vault', 'directory', | 'directory', | ||||
], catch_exceptions=True) | ], catch_exceptions=True) | ||||
assert re.match( | assert re.match( | ||||
rf'^VAULT CRITICAL - cooking directory {dir_id} took ' | rf'^VAULT CRITICAL - cooking directory {dir_id} took ' | ||||
r'[0-9]\.[0-9]{2}s and failed with: foobar\n' | r'[0-9]\.[0-9]{2}s and failed with: foobar\n' | ||||
r"\| 'total_time' = [0-9]\.[0-9]{2}s\n$", | r"\| 'total_time' = [0-9]\.[0-9]{2}s\n$", | ||||
result.output) | result.output) | ||||
assert result.exit_code == 2, result.output | assert result.exit_code == 2, result.output | ||||
Show All 25 Lines | def test_vault_timeout(requests_mock, mocker): | ||||
sleep_mock = mocker.patch('time.sleep') | sleep_mock = mocker.patch('time.sleep') | ||||
real_time = time.time | real_time = time.time | ||||
mocker.patch( | mocker.patch( | ||||
'time.time', side_effect=lambda: real_time() + time_offset) | 'time.time', side_effect=lambda: real_time() + time_offset) | ||||
result = invoke([ | result = invoke([ | ||||
'check-vault', | |||||
'--swh-web-url', 'mock://swh-web.example.org', | '--swh-web-url', 'mock://swh-web.example.org', | ||||
'--swh-storage-url', 'foo://example.org', | '--swh-storage-url', 'foo://example.org', | ||||
'check-vault', 'directory', | 'directory', | ||||
], catch_exceptions=True) | ], catch_exceptions=True) | ||||
assert re.match( | assert re.match( | ||||
rf'^VAULT CRITICAL - cooking directory {dir_id} took more than ' | rf'^VAULT CRITICAL - cooking directory {dir_id} took more than ' | ||||
r'[0-9]+\.[0-9]{2}s and has status: foo\n' | r'[0-9]+\.[0-9]{2}s and has status: foo\n' | ||||
r"\| 'total_time' = [0-9]{4}\.[0-9]{2}s\n$", | r"\| 'total_time' = [0-9]{4}\.[0-9]{2}s\n$", | ||||
result.output) | result.output) | ||||
assert result.exit_code == 2, result.output | assert result.exit_code == 2, result.output | ||||
assert sleep_mock.call_count == 2 | assert sleep_mock.call_count == 2 |