Exemplo n.º 1
0
class TestElasticsearchEngine(unittest.TestCase):

    def setUp(self):
        self.test_mappings = {
            'jobs': {"properties": {"job_id": {"type": "string"}}},
            'backups': {"properties": {"backup_id": {"type": "string"}}},
            'clients': {"properties": {"client_id": {"type": "string"}}},
        }

        self.mock_resp = Mock()
        self.mock_args = Mock()
        self.mock_args.test_only = False
        self.mock_args.always_yes = False
        self.mock_args.verbose = 1
        self.mock_args.select_mapping = ''
        self.mock_args.erase = False
        self.mock_args.replicas = 0
        self.es_manager = ElastichsearchEngine(es_url='http://*****:*****@patch.object(ElastichsearchEngine, 'check_index_exists')
    @patch.object(ElastichsearchEngine, 'mapping_match')
    @patch.object(ElastichsearchEngine, 'askput_mapping')
    @patch.object(ElastichsearchEngine, 'set_number_of_replicas')
    def test_put_mappings_does_nothing_when_mappings_match(self,
                                                           mock_set_number_of_replicas,
                                                           mock_askput_mapping,
                                                           mock_mapping_match,
                                                           mock_check_index_exists):
        self.es_manager.put_mappings(self.test_mappings)
        self.assertEquals(mock_askput_mapping.call_count, 0)

    @patch.object(ElastichsearchEngine, 'check_index_exists')
    @patch.object(ElastichsearchEngine, 'mapping_match')
    @patch.object(ElastichsearchEngine, 'askput_mapping')
    @patch.object(ElastichsearchEngine, 'set_number_of_replicas')
    def test_put_mappings_calls_askput_when_mappings_match_not(self,
                                                               mock_set_number_of_replicas,
                                                               mock_askput_mapping,
                                                               mock_mapping_match,
                                                               mock_check_index_exists):
        mock_mapping_match.return_value = False
        self.es_manager.put_mappings(self.test_mappings)
        self.assertEquals(mock_askput_mapping.call_count, 3)

    @patch.object(ElastichsearchEngine, 'proceed')
    @patch.object(ElastichsearchEngine, 'delete_type')
    @patch.object(ElastichsearchEngine, 'put_mapping')
    @patch.object(ElastichsearchEngine, 'set_number_of_replicas')
    def test_askput_calls_delete_and_put_mappings_when_always_yes_and_erase(self,
                                                                            mock_set_number_of_replicas,
                                                                            mock_put_mapping,
                                                                            mock_delete_type,
                                                                            mock_proceed):
        self.mock_args.yes = True
        self.mock_args.erase = True
        mock_put_mapping.side_effect = [MergeMappingException('regular test failure'), 0]
        res = self.es_manager.askput_mapping('jobs', self.test_mappings['jobs'])
        self.assertTrue(mock_put_mapping.called)
        mock_delete_type.assert_called_once_with('jobs')

    def test_askput_does_nothing_when_test_only(self):
        self.mock_args.test_only = True
        res = self.es_manager.askput_mapping('jobs', self.test_mappings['jobs'])
        self.assertEquals(None, res)


    @patch('freezer_api.cmd.db_init.requests')
    def test_mapping_match_not_found_returns_false(self, mock_requests):
        self.mock_resp.status_code = 404
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        mock_requests.get.return_value = self.mock_resp
        res = self.es_manager.mapping_match('jobs', self.test_mappings['jobs'])
        self.assertFalse(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_mapping_match_raises_Exception_on_response_not_in_200_404(self, mock_requests):
        self.mock_resp.status_code = 500
        mock_requests.get.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        self.assertRaises(Exception, self.es_manager.mapping_match,
                          'jobs', self.test_mappings['jobs'])

    @patch('freezer_api.cmd.db_init.requests')
    def test_mapping_match_return_true_when_mapping_matches(self, mock_requests):
        self.mock_resp.status_code = 200
        self.mock_resp.json.return_value = {"freezerindex": {"mappings": {"jobs": {"properties": {"job_id": {"type": "string"}}}}}}
        mock_requests.get.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        res = self.es_manager.mapping_match('jobs', self.test_mappings['jobs'])
        self.assertTrue(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_mapping_match_return_false_when_mapping_matches_not(self, mock_requests):
        self.mock_resp.status_code = 200
        self.mock_resp.text = '{"freezerindex": {"mappings": {"jobs":{"properties": {"job_id": {"type": "balloon"}}}}}}'
        mock_requests.get.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        res = self.es_manager.mapping_match('jobs', self.test_mappings['jobs'])
        self.assertFalse(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_delete_type_returns_none_on_success(self, mock_requests):
        self.mock_resp.status_code = 200
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        mock_requests.delete.return_value = self.mock_resp
        res = self.es_manager.delete_type('jobs')
        self.assertIsNone(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_delete_type_raises_Exception_on_response_code_not_200(self, mock_requests):
        self.mock_resp.status_code = requests.codes.BAD_REQUEST
        mock_requests.delete.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.BAD_REQUEST = 400
        mock_requests.codes.NOT_FOUND = 404
        self.assertRaises(Exception, self.es_manager.delete_type, 'jobs')

    @patch('freezer_api.cmd.db_init.requests')
    def test_put_mapping_returns_none_on_success(self, mock_requests):
        self.mock_resp.status_code = 200
        mock_requests.put.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        res = self.es_manager.put_mapping('jobs', self.test_mappings['jobs'])
        self.assertIsNone(res)
        url = 'http://*****:*****@patch('freezer_api.cmd.db_init.requests')
    def test_put_mapping_raises_Exception_on_response_code_not_200(self, mock_requests):
        self.mock_resp.status_code = 500
        mock_requests.put.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        self.assertRaises(Exception, self.es_manager.put_mapping, 'jobs', self.test_mappings['jobs'])

    def test_proceed_returns_true_on_user_y(self):
        with patch('six.moves.builtins.input', return_value='y') as _raw_input:
            res = self.es_manager.proceed('fancy a drink ?')
            self.assertTrue(res)
            _raw_input.assert_called_once_with('fancy a drink ?')

    def test_proceed_returns_false_on_user_n(self):
        with patch('six.moves.builtins.input', return_value='n') as _raw_input:
            res = self.es_manager.proceed('are you drunk ?')
            self.assertFalse(res)
            _raw_input.assert_called_once_with('are you drunk ?')

    def test_proceed_returns_true_when_always_yes(self):
        res = self.es_manager.proceed('ask me not', True)
        self.assertTrue(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_check_index_exists_ok_when_index_exists(self, mock_requests):
        self.mock_resp.status_code = 200
        mock_requests.post.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.BAD_REQUEST = 400
        res = self.es_manager.check_index_exists()
        self.assertEquals(res, None)

    @patch('freezer_api.cmd.db_init.requests')
    def test_check_index_exists_ok_when_index_not_exists(self, mock_requests):
        self.mock_resp.status_code = 400
        mock_requests.post.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.BAD_REQUEST = 400
        res = self.es_manager.check_index_exists()
        self.assertEquals(res, None)


    @patch('freezer_api.cmd.db_init.requests')
    def test_check_index_raises_Exception_when_return_code_not_in_OK_BADREQ(self, mock_requests):
        self.mock_resp.status_code = 500
        mock_requests.post.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.BAD_REQUEST = 400
        self.assertRaises(Exception, self.es_manager.check_index_exists)

    def test_set_number_of_replicas_returns_none_on_match(self):
        self.es_manager.number_of_replicas_match = Mock()
        self.es_manager.number_of_replicas_match.return_value = True
        res = self.es_manager.set_number_of_replicas(5)
        self.assertEquals(res, None)

    def test_set_number_of_replicas_calls_askput_when_match_not(self):
        self.es_manager.number_of_replicas_match = Mock()
        self.es_manager.askput_number_of_replicas = Mock()
        self.es_manager.number_of_replicas_match.return_value = False
        res = self.es_manager.set_number_of_replicas(5)
        self.es_manager.askput_number_of_replicas.assert_called_once_with(5)
        self.assertEquals(res, None)

    @patch('freezer_api.cmd.db_init.requests')
    def test_number_of_replicas_match_returns_true_when_match(self, mock_requests):
        self.mock_resp.status_code = 200
        self.mock_resp.json.return_value = {"freezerindex": {
            "settings": {
                "index": {
                    "creation_date": "1447167673951",
                    "number_of_replicas": "3",
                    "number_of_shards": "5",
                    "uuid": "C63kkECBS4KXNPs-KKysPQ",
                    "version": {
                        "created": "1040299"
                    }
                }
            }}}
        mock_requests.get.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        res = self.es_manager.number_of_replicas_match(3)
        self.assertTrue(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_number_of_replicas_match_returns_false_when_match_not(self, mock_requests):
        self.mock_resp.status_code = 200
        self.mock_resp.json.return_value = {"freezerindex": {
            "settings": {
                "index": {
                    "creation_date": "1447167673951",
                    "number_of_replicas": "3",
                    "number_of_shards": "5",
                    "uuid": "C63kkECBS4KXNPs-KKysPQ",
                    "version": {
                        "created": "1040299"
                    }
                }
            }}}
        mock_requests.get.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        res = self.es_manager.number_of_replicas_match(4)
        self.assertFalse(res)

    def test_askput_number_of_replicas_sets_error_when_test_only(self):
        self.mock_args.test_only = True
        res = self.es_manager.askput_number_of_replicas(4)
        self.assertIsNone(res)
        self.assertEquals(self.es_manager.exit_code, os.EX_DATAERR)

    def test_askput_number_of_replicas_returns_none_when_no_proceed(self):
        self.mock_args.test_only = False
        self.es_manager.proceed = Mock()
        self.es_manager.proceed.return_value = False
        res = self.es_manager.askput_number_of_replicas(4)
        self.assertIsNone(res)
        self.assertEquals(self.es_manager.exit_code, os.EX_OK)

    @patch('freezer_api.cmd.db_init.requests')
    def test_askput_number_of_replicas_uses_correct_number(self, mock_requests):
        self.mock_args.test_only = False
        self.es_manager.proceed = Mock()
        self.es_manager.proceed.return_value = True
        self.mock_resp.status_code = 200
        mock_requests.codes.OK = 200
        mock_requests.put.return_value = self.mock_resp
        res = self.es_manager.askput_number_of_replicas(4)
        self.assertIsNone(res)
        mock_requests.put.assert_called_once_with('http://*****:*****@patch('freezer_api.cmd.db_init.requests')
    def test_askput_number_of_replicas_raises_NumberOfReplicasException_on_request_error(self, mock_requests):
        self.mock_args.test_only = False
        self.es_manager.proceed = Mock()
        self.es_manager.proceed.return_value = True
        self.mock_resp.status_code = 500
        mock_requests.codes.OK = 200
        mock_requests.put.return_value = self.mock_resp
        self.assertRaises(NumberOfReplicasException, self.es_manager.askput_number_of_replicas, 4)
Exemplo n.º 2
0
class TestElasticsearchEngine(unittest.TestCase):

    def setUp(self):
        self.test_mappings = {
            'jobs': {"properties": {"job_id": {"type": "string"}}},
            'backups': {"properties": {"backup_id": {"type": "string"}}},
            'clients': {"properties": {"client_id": {"type": "string"}}},
        }

        self.mock_resp = Mock()
        self.es_manager = ElastichsearchEngine(es_url='http://*****:*****@patch.object(ElastichsearchEngine, 'check_index_exists')
    @patch.object(ElastichsearchEngine, 'mapping_match')
    @patch.object(ElastichsearchEngine, 'askput_mapping')
    def test_put_mappings_does_nothing_when_mappings_match(self, mock_askput_mapping, mock_mapping_match, mock_check_index_exists):
        self.es_manager.put_mappings(self.test_mappings)
        self.assertEquals(mock_askput_mapping.call_count, 0)

    @patch.object(ElastichsearchEngine, 'check_index_exists')
    @patch.object(ElastichsearchEngine, 'mapping_match')
    @patch.object(ElastichsearchEngine, 'askput_mapping')
    def test_put_mappings_calls_askput_when_mappings_match_not(self, mock_askput_mapping, mock_mapping_match, mock_check_index_exists):
        mock_mapping_match.return_value = False
        self.es_manager.put_mappings(self.test_mappings)
        self.assertEquals(mock_askput_mapping.call_count, 3)

    @patch.object(ElastichsearchEngine, 'proceed')
    @patch.object(ElastichsearchEngine, 'delete_type')
    @patch.object(ElastichsearchEngine, 'put_mapping')
    def test_askput_calls_delete_and_put_mappunts_when_always_yes(self,
                                                                  mock_put_mapping,
                                                                  mock_delete_type,
                                                                  mock_proceed):
        self.es_manager.always_yes = True
        res = self.es_manager.askput_mapping('jobs', self.test_mappings['jobs'])
        mock_delete_type.assert_called_once_with('jobs')
        mock_put_mapping.assert_called_once_with('jobs', self.test_mappings['jobs'])

    def test_askput_does_nothing_when_test_only(self):
        self.es_manager.test_only = True
        res = self.es_manager.askput_mapping('jobs', self.test_mappings['jobs'])
        self.assertEquals(None, res)


    @patch('freezer_api.cmd.db_init.requests')
    def test_mapping_match_not_found_returns_false(self, mock_requests):
        self.mock_resp.status_code = 404
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        mock_requests.get.return_value = self.mock_resp
        res = self.es_manager.mapping_match('jobs', self.test_mappings['jobs'])
        self.assertFalse(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_mapping_match_raises_Exception_on_response_not_in_200_404(self, mock_requests):
        self.mock_resp.status_code = 500
        mock_requests.get.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        self.assertRaises(Exception, self.es_manager.mapping_match,
                          'jobs', self.test_mappings['jobs'])

    @patch('freezer_api.cmd.db_init.requests')
    def test_mapping_match_return_true_when_mapping_matches(self, mock_requests):
        self.mock_resp.status_code = 200
        self.mock_resp.json.return_value = {"freezerindex": {"mappings": {"jobs": {"properties": {"job_id": {"type": "string"}}}}}}
        mock_requests.get.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        res = self.es_manager.mapping_match('jobs', self.test_mappings['jobs'])
        self.assertTrue(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_mapping_match_return_false_when_mapping_matches_not(self, mock_requests):
        self.mock_resp.status_code = 200
        self.mock_resp.text = '{"freezerindex": {"mappings": {"jobs":{"properties": {"job_id": {"type": "balloon"}}}}}}'
        mock_requests.get.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        res = self.es_manager.mapping_match('jobs', self.test_mappings['jobs'])
        self.assertFalse(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_delete_type_returns_none_on_success(self, mock_requests):
        self.mock_resp.status_code = 200
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        mock_requests.delete.return_value = self.mock_resp
        res = self.es_manager.delete_type('jobs')
        self.assertIsNone(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_delete_type_raises_Exception_on_response_code_not_200(self, mock_requests):
        self.mock_resp.status_code = requests.codes.BAD_REQUEST
        mock_requests.delete.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.BAD_REQUEST = 400
        mock_requests.codes.NOT_FOUND = 404
        self.assertRaises(Exception, self.es_manager.delete_type, 'jobs')

    @patch('freezer_api.cmd.db_init.requests')
    def test_put_mapping_returns_none_on_success(self, mock_requests):
        self.mock_resp.status_code = 200
        mock_requests.put.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        res = self.es_manager.put_mapping('jobs', self.test_mappings['jobs'])
        self.assertIsNone(res)
        url = 'http://*****:*****@patch('freezer_api.cmd.db_init.requests')
    def test_put_mapping_raises_Exception_on_response_code_not_200(self, mock_requests):
        self.mock_resp.status_code = 500
        mock_requests.put.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.NOT_FOUND = 404
        self.assertRaises(Exception, self.es_manager.put_mapping, 'jobs', self.test_mappings['jobs'])

    def test_proceed_returns_true_on_user_y(self):
        with patch('__builtin__.raw_input', return_value='y') as _raw_input:
            res = self.es_manager.proceed('fancy a drink ?')
            self.assertTrue(res)
            _raw_input.assert_called_once_with('fancy a drink ?')

    def test_proceed_returns_false_on_user_n(self):
        with patch('__builtin__.raw_input', return_value='n') as _raw_input:
            res = self.es_manager.proceed('are you drunk ?')
            self.assertFalse(res)
            _raw_input.assert_called_once_with('are you drunk ?')

    def test_proceed_returns_true_when_always_yes(self):
        self.es_manager.always_yes = True
        res = self.es_manager.proceed('ask me not')
        self.assertTrue(res)

    @patch('freezer_api.cmd.db_init.requests')
    def test_check_index_exists_ok_when_index_exists(self, mock_requests):
        self.mock_resp.status_code = 200
        mock_requests.post.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.BAD_REQUEST = 400
        res = self.es_manager.check_index_exists()
        self.assertEquals(res, None)

    @patch('freezer_api.cmd.db_init.requests')
    def test_check_index_exists_ok_when_index_not_exists(self, mock_requests):
        self.mock_resp.status_code = 400
        mock_requests.post.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.BAD_REQUEST = 400
        res = self.es_manager.check_index_exists()
        self.assertEquals(res, None)


    @patch('freezer_api.cmd.db_init.requests')
    def test_check_index_raises_Exception_when_return_code_not_in_OK_BADREQ(self, mock_requests):
        self.mock_resp.status_code = 500
        mock_requests.post.return_value = self.mock_resp
        mock_requests.codes.OK = 200
        mock_requests.codes.BAD_REQUEST = 400
        self.assertRaises(Exception, self.es_manager.check_index_exists)