def test_check_replication_unknown_valueerror1(self, mock_urlopen): """ Force ValueError on urllib.error to test try-catch """ base_url = '.' mock_urlopen.side_effect = ValueError(Mock(return_value='')) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, 3 * [(STATUS_UNKNOWN, "Can't parse status data")])
def test_check_replication_unknown_valueerror2(self, mock_urlopen): """ Force ValueError on json to test try-catch """ base_url = 'http://localhost:6000/recon/' jdata = PropertyMock(return_value=b'X') mock_urlopen.return_value = MagicMock(read=jdata) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, 3 * [(STATUS_UNKNOWN, "Can't parse status data")])
def test_check_replication_unknown_valueerror1(self, mock_urlopen): """ Force ValueError on urllib.error to test try-catch """ base_url = '.' mock_urlopen.side_effect = ValueError(Mock(return_value='')) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, 3*[(STATUS_UNKNOWN, "Can't parse status data")])
def test_check_replication_unknown_valueerror2(self, mock_urlopen): """ Force ValueError on json to test try-catch """ base_url = 'http://localhost:6000/recon/' jdata = PropertyMock(return_value=b'X') mock_urlopen.return_value = MagicMock(read=jdata) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, 3*[(STATUS_UNKNOWN, "Can't parse status data")])
def test_check_replication_unknown_urlerror(self, mock_urlopen): """ Force urllib2.URLError to test try-catch """ base_url = 'http://localhost:6000/recon/' url = '{}replication/{}' error = 'connection refused' mock_urlopen.side_effect = urllib2.URLError(Mock(return_value=error)) result = check_replication(base_url, 60) expected_result = [(STATUS_UNKNOWN, "Can't open url: " "{}".format(url.format(base_url, name))) for name in ('account', 'object', 'container')] self.assertEqual(result, expected_result)
def test_check_replication_unknown_urlerror(self, mock_urlopen): """ Force urllib.error.URLError to test try-catch """ base_url = 'http://localhost:6000/recon/' url = '{}replication/{}' error = 'connection refused' mock_urlopen.side_effect = (urllib .error .URLError(Mock(return_value=error))) result = check_replication(base_url, 60) expected_result = [(STATUS_UNKNOWN, "Can't open url: " "{}".format(url.format(base_url, name))) for name in ('account', 'object', 'container')] self.assertEqual(result, expected_result)
def test_check_replication_ok(self, mock_timestamp): """ Replication lag and number of failures are below WARN threshold, STATUS_OK """ base_url = 'http://localhost:6000/recon/' jdata = b'{"replication_last": 1493299546.629282, ' \ b'"replication_stats": {"no_change": 0, "rsync": 0, ' \ b'"success": 0, "failure": 0, "attempted": 0, "ts_repl": 0, ' \ b'"remove": 0, "remote_merge": 0, "diff_capped": 0, ' \ b'"start": 1493299546.621624, "hashmatch": 0, "diff": 0, ' \ b'"empty": 0}, "replication_time": 0.0076580047607421875}' pmock_jdata = PropertyMock(return_value=jdata) mock_timestamp.return_value = (MagicMock(days=0, seconds=0), 0) with patch('urllib.request.urlopen') as mock_urlopen: mock_urlopen.return_value = MagicMock(read=pmock_jdata) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, [(STATUS_OK, 'OK')])
def test_check_replication_warn_failures(self, mock_timestamp): """ Replication failures over WARN threshold (below CRIT), STATUS_WARN """ base_url = 'http://localhost:6000/recon/' jdata = '{"replication_last": 1493299546.629282, ' \ '"replication_stats": {"no_change": 0, "rsync": 0, ' \ '"success": 0, "failure": 0, "attempted": 0, "ts_repl": 0, ' \ '"remove": 0, "remote_merge": 0, "diff_capped": 0, ' \ '"start": 1493299546.621624, "hashmatch": 0, "diff": 0, ' \ '"empty": 0}, "replication_time": 0.0076580047607421875}' pmock_jdata = PropertyMock(return_value=jdata) mock_timestamp.return_value = (MagicMock(seconds=0), 5) with patch('urllib2.urlopen') as mock_urlopen: mock_urlopen.return_value = MagicMock(read=pmock_jdata) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, 3 * [(STATUS_WARN, "5 replication failures")])
def test_check_replication_crit_day_plus_lag(self, mock_timestamp): """ Replication lag CRITS with day wrap, STATUS_CRIT """ base_url = 'http://localhost:6000/recon/' jdata = b'{"replication_last": 1493299546.629282, ' \ b'"replication_stats": {"no_change": 0, "rsync": 0, ' \ b'"success": 0, "failure": 0, "attempted": 0, "ts_repl": 0, ' \ b'"remove": 0, "remote_merge": 0, "diff_capped": 0, ' \ b'"start": 1493299546.621624, "hashmatch": 0, "diff": 0, ' \ b'"empty": 0}, "replication_time": 0.0076580047607421875}' pmock_jdata = PropertyMock(return_value=jdata) mock_timestamp.return_value = (MagicMock(days=2, seconds=5), 0) with patch('urllib.request.urlopen') as mock_urlopen: mock_urlopen.return_value = MagicMock(read=pmock_jdata) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, [(STATUS_CRIT, "'{}' replication lag is " "172805 seconds".format(repl)) for repl in ('account', 'object', 'container')])
def test_check_replication_crit_null_failures(self, mock_timestamp): """ Catch NULL value on failures stats, STATUS_CRIT """ base_url = 'http://localhost:6000/recon/' jdata = b'{"replication_last": 1493299546.629282, ' \ b'"replication_stats": {"no_change": 0, "rsync": 0, ' \ b'"success": 0, "failure": 0, "attempted": 0, "ts_repl": 0, ' \ b'"remove": 0, "remote_merge": 0, "diff_capped": 0, ' \ b'"start": 1493299546.621624, "hashmatch": 0, "diff": 0, ' \ b'"empty": 0}, "replication_time": 0.0076580047607421875}' pmock_jdata = PropertyMock(return_value=jdata) mock_timestamp.return_value = (MagicMock(days=0, seconds=0), -1) with patch('urllib.request.urlopen') as mock_urlopen: mock_urlopen.return_value = MagicMock(read=pmock_jdata) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual( result, 3 * [(STATUS_CRIT, "replication failures counter is NULL " "(check syslog)")])
def test_check_replication_crit_null_failures(self, mock_timestamp): """ Catch NULL value on failures stats, STATUS_CRIT """ base_url = 'http://localhost:6000/recon/' jdata = b'{"replication_last": 1493299546.629282, ' \ b'"replication_stats": {"no_change": 0, "rsync": 0, ' \ b'"success": 0, "failure": 0, "attempted": 0, "ts_repl": 0, ' \ b'"remove": 0, "remote_merge": 0, "diff_capped": 0, ' \ b'"start": 1493299546.621624, "hashmatch": 0, "diff": 0, ' \ b'"empty": 0}, "replication_time": 0.0076580047607421875}' pmock_jdata = PropertyMock(return_value=jdata) mock_timestamp.return_value = (MagicMock(days=0, seconds=0), -1) with patch('urllib.request.urlopen') as mock_urlopen: mock_urlopen.return_value = MagicMock(read=pmock_jdata) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, 3*[(STATUS_CRIT, "replication failures counter is NULL " "(check syslog)")])
def test_check_replication_crit_lag_notworking(self, mock_timestamp): """ Catch NULL replication value, STATUS_CRIT """ base_url = 'http://localhost:6000/recon/' jdata = '{"replication_last": 1493299546.629282, ' \ '"replication_stats": {"no_change": 0, "rsync": 0, ' \ '"success": 0, "failure": 0, "attempted": 0, "ts_repl": 0, ' \ '"remove": 0, "remote_merge": 0, "diff_capped": 0, ' \ '"start": 1493299546.621624, "hashmatch": 0, "diff": 0, ' \ '"empty": 0}, "replication_time": 0.0076580047607421875}' pmock_jdata = PropertyMock(return_value=jdata) mock_timestamp.return_value = (None, 0) with patch('urllib.request.urlopen') as mock_urlopen: mock_urlopen.return_value = MagicMock(read=pmock_jdata) result = check_replication(base_url, [4, 10, 4, 10]) self.assertEqual(result, [(STATUS_CRIT, "'{}' replication lag not working " "(perms issue? check syslog)".format(repl)) for repl in ('account', 'object', 'container')])