salt/tests/unit/states/grafana_datasource_test.py

97 lines
3.8 KiB
Python
Raw Normal View History

2015-12-07 20:52:22 +00:00
# -*- coding: utf-8 -*-
# Import Python libs
from __future__ import absolute_import
# Import Salt Testing Libs
from salttesting import skipIf, TestCase
from salttesting.mock import (
NO_MOCK,
NO_MOCK_REASON,
Mock,
MagicMock,
patch
)
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import Salt Libs
2015-12-07 20:54:11 +00:00
from salt.states import grafana_datasource
2015-12-07 20:52:22 +00:00
2015-12-07 20:54:11 +00:00
grafana_datasource.__opts__ = {}
grafana_datasource.__salt__ = {}
2015-12-07 20:52:22 +00:00
profile = {
2015-12-08 19:24:45 +00:00
'grafana_url': 'http://grafana',
'grafana_token': 'token',
2015-12-07 20:52:22 +00:00
}
2015-12-09 16:27:12 +00:00
2015-12-07 20:52:22 +00:00
def mock_json_response(data):
2015-12-08 19:24:45 +00:00
response = MagicMock()
response.json = MagicMock(return_value=data)
return Mock(return_value=response)
2015-12-07 20:52:22 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2015-12-07 20:54:11 +00:00
class GrafanaDatasourceTestCase(TestCase):
2015-12-08 19:24:45 +00:00
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')
2015-12-07 20:52:22 +00:00
2015-12-08 19:24:45 +00:00
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)
2015-12-07 20:52:22 +00:00
2015-12-08 19:24:45 +00:00
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'}})
2015-12-07 20:52:22 +00:00
2015-12-08 19:24:45 +00:00
def test_absent(self):
with patch('requests.get', mock_json_response([])):
with patch('requests.delete') as rdelete:
ret = grafana_datasource.absent('test', profile=profile)
rdelete.assert_not_called()
self.assertTrue(ret['result'])
self.assertEqual(ret['comment'], 'Data source test already absent')
2015-12-07 20:52:22 +00:00
2015-12-08 19:24:45 +00:00
with patch('requests.get', mock_json_response([{'name': 'test', 'id': 1}])):
with patch('requests.delete') as rdelete:
ret = grafana_datasource.absent('test', profile=profile)
rdelete.assert_called_once_with(
'http://grafana/api/datasources/1',
headers={'Authorization': 'Bearer token', 'Accept': 'application/json'},
timeout=3
)
self.assertTrue(ret['result'])
self.assertEqual(ret['comment'], 'Data source test was deleted')