def test_global_free_space_percentage(self): mounts = [ sanity_check.Mount(mount='/', device='/dev/vda1'), sanity_check.Mount(mount='/var/lib/mysql', device='/dev/vda2') ] responses = [ sanity_check.SanityCheckResult(False, "Error on mount"), sanity_check.SanityCheckResult(True, "All is OK") ] report = [] with mock.patch("sanity_check.test_free_space_on_mount", side_effect=responses) as test_patch, mock.patch( "sanity_check.get_dev_mounts", return_value=mounts) as dev_mounts_patch: report.extend(sanity_check.test_global_free_percentage(20)) dev_mounts_patch.assert_called_once_with() self.assertEqual(test_patch.mock_calls, [mock.call('/', 20), mock.call('/var/lib/mysql', 20)]) self.assertEqual(report, responses)
def test_http_checker_positive(self, url): report = sanity_check.ping_http_endpoint(url) self.assertEqual(len(report), 1) self.assertEqual(report, [ sanity_check.SanityCheckResult( True, "Attempt 1 to contact DMS succeeded.") ])
def test_http_checker_retry_and_succeed(self): with mock.patch('urllib2.urlopen') as patched_open, mock.patch( 'time.sleep') as patched_sleep: patched_open.side_effect = [ UrlopenResponse(500), UrlopenResponse(200) ] report = sanity_check.ping_http_endpoint("http://test") self.assertEqual(len(report), 2) self.assertEqual(report, [ sanity_check.SanityCheckResult( True, "Attempt 1 to contact DMS returned code 500"), sanity_check.SanityCheckResult( True, "Attempt 2 to contact DMS succeeded."), ]) self.assertEqual(patched_open.call_count, 2) self.assertEqual(patched_sleep.call_count, 1)
def test_http_checker_negative(self, url): err = "<urlopen error timed out>" with mock.patch('time.sleep'): report = sanity_check.ping_http_endpoint(url) self.assertEqual(len(report), 4) self.assertEqual(report, [ sanity_check.SanityCheckResult( True, "Attempt 1 to contact DMS failed due to:\n{}".format(err)), sanity_check.SanityCheckResult( True, "Attempt 2 to contact DMS failed due to:\n{}".format(err)), sanity_check.SanityCheckResult( True, "Attempt 3 to contact DMS failed due to:\n{}".format(err)), sanity_check.SanityCheckResult( False, "Couldn't send snitch after 3 attempts"), ])
def test_http_checker_statuscode_positive(self): with mock.patch('urllib2.urlopen') as patched_open: patched_open.return_value = UrlopenResponse(201) report = sanity_check.ping_http_endpoint('http://test') self.assertEqual(len(report), 1) self.assertEqual(report, [ sanity_check.SanityCheckResult( True, "Attempt 1 to contact DMS succeeded.") ]) patched_open.assert_called_once_with("http://test", timeout=5)
def test_http_checker_statuscode_negative(self, code): with mock.patch('urllib2.urlopen') as patched_open, mock.patch( 'time.sleep') as patched_sleep: patched_open.return_value = UrlopenResponse(code) report = sanity_check.ping_http_endpoint('http://test') self.assertEqual(len(report), 4) self.assertEqual(report, [ sanity_check.SanityCheckResult( True, "Attempt 1 to contact DMS returned code {}".format(code)), sanity_check.SanityCheckResult( True, "Attempt 2 to contact DMS returned code {}".format(code)), sanity_check.SanityCheckResult( True, "Attempt 3 to contact DMS returned code {}".format(code)), sanity_check.SanityCheckResult( False, "Couldn't send snitch after 3 attempts"), ]) patched_open.assert_called_with("http://test", timeout=5) self.assertEqual(patched_open.call_count, sanity_check.DEFAULT_HTTP_RETRIES) self.assertEqual(patched_sleep.call_count, sanity_check.DEFAULT_HTTP_RETRIES)