def test_present():
    with patch("requests.get", mock_json_response([])):
        with patch("requests.post") as rpost:
            ret = grafana_datasource.present("test", "type", "url", profile=profile)
            rpost.assert_called_once_with(
                "http://grafana/api/datasources",
                grafana_datasource._get_json_data("test", "type", "url"),
                headers={
                    "Authorization": "Bearer token",
                    "Accept": "application/json",
                },
                timeout=3,
            )
            assert ret["result"]
            assert ret["comment"] == "New data source test added"

    data = grafana_datasource._get_json_data("test", "type", "url")
    data.update({"id": 1, "orgId": 1})
    with patch("requests.get", mock_json_response([data])):
        with patch("requests.put") as rput:
            ret = grafana_datasource.present("test", "type", "url", profile=profile)
            rput.assert_called_once_with(
                "http://grafana/api/datasources/1",
                grafana_datasource._get_json_data("test", "type", "url"),
                headers={
                    "Authorization": "Bearer token",
                    "Accept": "application/json",
                },
                timeout=3,
            )
            assert ret["result"]
            assert ret["comment"] == "Data source test already up-to-date"
            assert ret["changes"] == {}

        with patch("requests.put") as rput:
            ret = grafana_datasource.present("test", "type", "newurl", profile=profile)
            rput.assert_called_once_with(
                "http://grafana/api/datasources/1",
                grafana_datasource._get_json_data("test", "type", "newurl"),
                headers={
                    "Authorization": "Bearer token",
                    "Accept": "application/json",
                },
                timeout=3,
            )
            assert ret["result"]
            assert ret["comment"] == "Data source test updated"
            assert ret["changes"] == {"old": {"url": "url"}, "new": {"url": "newurl"}}
Example #2
0
    def test_present(self):
        with patch('requests.get', mock_json_response([])):
            with patch('requests.post') as rpost:
                ret = grafana_datasource.present('test', 'type', 'url', profile=profile)
                rpost.assert_called_once_with(
                    'http://grafana/api/datasources',
                    grafana_datasource._get_json_data('test', 'type', 'url'),
                    headers={'Authorization': 'Bearer token', 'Accept': 'application/json'},
                    timeout=3
                )
                self.assertTrue(ret['result'])
                self.assertEqual(ret['comment'], 'New data source test added')

        data = grafana_datasource._get_json_data('test', 'type', 'url')
        data.update({'id': 1, 'orgId': 1})
        with patch('requests.get', mock_json_response([data])):
            with patch('requests.put') as rput:
                ret = grafana_datasource.present('test', 'type', 'url', profile=profile)
                rput.assert_called_once_with(
                    'http://grafana/api/datasources/1',
                    grafana_datasource._get_json_data('test', 'type', 'url'),
                    headers={'Authorization': 'Bearer token', 'Accept': 'application/json'},
                    timeout=3
                )
                self.assertTrue(ret['result'])
                self.assertEqual(ret['comment'], 'Data source test already up-to-date')
                self.assertEqual(ret['changes'], None)

            with patch('requests.put') as rput:
                ret = grafana_datasource.present('test', 'type', 'newurl', profile=profile)
                rput.assert_called_once_with(
                    'http://grafana/api/datasources/1',
                    grafana_datasource._get_json_data('test', 'type', 'newurl'),
                    headers={'Authorization': 'Bearer token', 'Accept': 'application/json'},
                    timeout=3
                )
                self.assertTrue(ret['result'])
                self.assertEqual(ret['comment'], 'Data source test updated')
                self.assertEqual(ret['changes'], {'old': {'url': 'url'}, 'new': {'url': 'newurl'}})
    def test_present(self):
        with patch('requests.get', mock_json_response([])):
            with patch('requests.post') as rpost:
                ret = grafana_datasource.present('test', 'type', 'url', profile=profile)
                rpost.assert_called_once_with(
                    'http://grafana/api/datasources',
                    grafana_datasource._get_json_data('test', 'type', 'url'),
                    headers={'Authorization': 'Bearer token', 'Accept': 'application/json'},
                    timeout=3
                )
                self.assertTrue(ret['result'])
                self.assertEqual(ret['comment'], 'New data source test added')

        data = grafana_datasource._get_json_data('test', 'type', 'url')
        data.update({'id': 1, 'orgId': 1})
        with patch('requests.get', mock_json_response([data])):
            with patch('requests.put') as rput:
                ret = grafana_datasource.present('test', 'type', 'url', profile=profile)
                rput.assert_called_once_with(
                    'http://grafana/api/datasources/1',
                    grafana_datasource._get_json_data('test', 'type', 'url'),
                    headers={'Authorization': 'Bearer token', 'Accept': 'application/json'},
                    timeout=3
                )
                self.assertTrue(ret['result'])
                self.assertEqual(ret['comment'], 'Data source test already up-to-date')
                self.assertEqual(ret['changes'], None)

            with patch('requests.put') as rput:
                ret = grafana_datasource.present('test', 'type', 'newurl', profile=profile)
                rput.assert_called_once_with(
                    'http://grafana/api/datasources/1',
                    grafana_datasource._get_json_data('test', 'type', 'newurl'),
                    headers={'Authorization': 'Bearer token', 'Accept': 'application/json'},
                    timeout=3
                )
                self.assertTrue(ret['result'])
                self.assertEqual(ret['comment'], 'Data source test updated')
                self.assertEqual(ret['changes'], {'old': {'url': 'url'}, 'new': {'url': 'newurl'}})