2015-01-23 12:29:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Rupesh Tare <rupesht@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
2015-01-24 03:05:09 +00:00
|
|
|
# Import Python libs
|
2018-01-19 14:12:59 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2015-01-24 03:05:09 +00:00
|
|
|
|
2015-01-23 12:29:22 +00:00
|
|
|
# Import Salt Testing Libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2015-01-23 12:29:22 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.influx08 as influx08
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
DB_LIST = ['A', 'B', 'C']
|
|
|
|
USER_LIST = [{'name': 'A'}, {'name': 'B'}]
|
|
|
|
|
|
|
|
|
|
|
|
class MockInfluxDBClient(object):
|
2015-02-09 18:55:54 +00:00
|
|
|
def get_list_database(self):
|
2015-01-23 12:29:22 +00:00
|
|
|
return DB_LIST
|
|
|
|
|
|
|
|
def create_database(self, name):
|
|
|
|
return name
|
|
|
|
|
|
|
|
def delete_database(self, name):
|
|
|
|
return name
|
|
|
|
|
2015-02-09 18:55:54 +00:00
|
|
|
def switch_database(self, name):
|
2015-01-23 12:29:22 +00:00
|
|
|
return name
|
|
|
|
|
2015-12-09 12:18:47 +00:00
|
|
|
def get_list_users(self):
|
2015-01-23 12:29:22 +00:00
|
|
|
return USER_LIST
|
|
|
|
|
|
|
|
def get_list_cluster_admins(self):
|
|
|
|
return USER_LIST
|
|
|
|
|
|
|
|
def update_cluster_admin_password(self, name, passwd):
|
|
|
|
return name, passwd
|
|
|
|
|
|
|
|
def update_database_user_password(self, name, passwd):
|
|
|
|
return name, passwd
|
|
|
|
|
|
|
|
def delete_cluster_admin(self, name):
|
|
|
|
return name
|
|
|
|
|
|
|
|
def delete_database_user(self, name):
|
|
|
|
return name
|
|
|
|
|
|
|
|
def query(self, query, time_precision, chunked):
|
|
|
|
return query, time_precision, chunked
|
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
class InfluxTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
TestCase for the salt.modules.at module
|
|
|
|
'''
|
|
|
|
def test_db_list(self):
|
|
|
|
"""
|
|
|
|
Test to list all InfluxDB databases
|
|
|
|
"""
|
|
|
|
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', mock_inf_db_client):
|
|
|
|
self.assertEqual(influx08.db_list(user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8086), DB_LIST)
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
def test_db_exists(self):
|
|
|
|
'''
|
|
|
|
Tests for checks if a database exists in InfluxDB
|
|
|
|
'''
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, 'db_list', side_effect=[[{'name': 'A'}],
|
2015-01-23 12:29:22 +00:00
|
|
|
None]):
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertTrue(influx08.db_exists(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
|
|
|
|
|
|
|
self.assertFalse(influx08.db_exists(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
def test_db_create(self):
|
|
|
|
'''
|
|
|
|
Test to create a database
|
|
|
|
'''
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, 'db_exists', side_effect=[True, False]):
|
|
|
|
self.assertFalse(influx08.db_create(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', mock_inf_db_client):
|
|
|
|
self.assertTrue(influx08.db_create(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
def test_db_remove(self):
|
|
|
|
'''
|
|
|
|
Test to remove a database
|
|
|
|
'''
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, 'db_exists', side_effect=[False, True]):
|
|
|
|
self.assertFalse(influx08.db_remove(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', mock_inf_db_client):
|
|
|
|
self.assertTrue(influx08.db_remove(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
def test_user_list(self):
|
|
|
|
'''
|
|
|
|
Tests for list cluster admins or database users.
|
|
|
|
'''
|
|
|
|
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', mock_inf_db_client):
|
|
|
|
self.assertListEqual(influx08.user_list(database='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8086), USER_LIST)
|
|
|
|
|
|
|
|
self.assertListEqual(influx08.user_list(user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8086), USER_LIST)
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
def test_user_exists(self):
|
|
|
|
'''
|
|
|
|
Test to checks if a cluster admin or database user exists.
|
|
|
|
'''
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, 'user_list', side_effect=[[{'name': 'A'}],
|
|
|
|
None]):
|
|
|
|
self.assertTrue(influx08.user_exists(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertFalse(influx08.user_exists(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
def test_user_chpass(self):
|
|
|
|
'''
|
|
|
|
Tests to change password for a cluster admin or a database user.
|
|
|
|
'''
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, 'user_exists', return_value=False):
|
|
|
|
self.assertFalse(influx08.user_chpass(name='A',
|
|
|
|
passwd='*',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertFalse(influx08.user_chpass(name='A',
|
|
|
|
passwd='*',
|
|
|
|
database='test',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', mock_inf_db_client):
|
|
|
|
with patch.object(influx08, 'user_exists', return_value=True):
|
|
|
|
self.assertTrue(influx08.user_chpass(name='A',
|
|
|
|
passwd='*',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
|
|
|
|
|
|
|
self.assertTrue(influx08.user_chpass(name='A',
|
|
|
|
passwd='*',
|
|
|
|
database='test',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
def test_user_remove(self):
|
|
|
|
'''
|
|
|
|
Tests to remove a cluster admin or a database user.
|
|
|
|
'''
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, 'user_exists', return_value=False):
|
|
|
|
self.assertFalse(influx08.user_remove(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertFalse(influx08.user_remove(name='A',
|
|
|
|
database='test',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', mock_inf_db_client):
|
|
|
|
with patch.object(influx08, 'user_exists', return_value=True):
|
|
|
|
self.assertTrue(influx08.user_remove(name='A',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
|
|
|
|
|
|
|
self.assertTrue(influx08.user_remove(name='A',
|
|
|
|
database='test',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
|
|
|
def test_query(self):
|
|
|
|
'''
|
|
|
|
Test for querying data
|
|
|
|
'''
|
|
|
|
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', mock_inf_db_client):
|
|
|
|
self.assertTrue(influx08.query(database='db',
|
|
|
|
query='q',
|
|
|
|
user='root',
|
|
|
|
password='root',
|
|
|
|
host='localhost',
|
|
|
|
port=8000))
|
2015-01-23 12:29:22 +00:00
|
|
|
|
2015-12-17 20:58:51 +00:00
|
|
|
def test_retention_policy_get(self):
|
|
|
|
client = MockInfluxDBClient()
|
|
|
|
policy = {'name': 'foo'}
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', MagicMock(return_value=client)):
|
2015-12-17 20:58:51 +00:00
|
|
|
client.get_list_retention_policies = MagicMock(return_value=[policy])
|
|
|
|
self.assertEqual(
|
|
|
|
policy,
|
2016-05-31 07:48:41 +00:00
|
|
|
influx08.retention_policy_get(database='db', name='foo')
|
2015-12-17 20:58:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_retention_policy_add(self):
|
|
|
|
client = MockInfluxDBClient()
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', MagicMock(return_value=client)):
|
2015-12-17 20:58:51 +00:00
|
|
|
client.create_retention_policy = MagicMock()
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertTrue(influx08.retention_policy_add(
|
2015-12-17 20:58:51 +00:00
|
|
|
database='db',
|
|
|
|
name='name',
|
|
|
|
duration='30d',
|
|
|
|
replication=1,
|
|
|
|
))
|
|
|
|
client.create_retention_policy.assert_called_once_with(
|
|
|
|
'name', '30d', 1, 'db', False)
|
|
|
|
|
|
|
|
def test_retention_policy_modify(self):
|
|
|
|
client = MockInfluxDBClient()
|
2016-05-31 07:48:41 +00:00
|
|
|
with patch.object(influx08, '_client', MagicMock(return_value=client)):
|
2015-12-17 20:58:51 +00:00
|
|
|
client.alter_retention_policy = MagicMock()
|
2016-05-31 07:48:41 +00:00
|
|
|
self.assertTrue(influx08.retention_policy_alter(
|
2015-12-17 20:58:51 +00:00
|
|
|
database='db',
|
|
|
|
name='name',
|
|
|
|
duration='30d',
|
|
|
|
replication=1,
|
|
|
|
))
|
|
|
|
client.alter_retention_policy.assert_called_once_with(
|
|
|
|
'name', 'db', '30d', 1, False)
|