salt/tests/unit/modules/test_influx08.py
rallytime 3273bbdab7
Merge branch '2017.7' into '2018.3'
Conflicts:
  - doc/ref/configuration/master.rst
  - doc/ref/modules/all/index.rst
  - doc/topics/grains/index.rst
  - doc/topics/releases/2016.3.4.rst
  - doc/topics/spm/spm_formula.rst
  - doc/topics/tutorials/cron.rst
  - doc/topics/tutorials/index.rst
  - doc/topics/tutorials/stormpath.rst
  - salt/engines/slack.py
  - salt/log/handlers/fluent_mod.py
  - salt/modules/cyg.py
  - salt/modules/junos.py
  - salt/modules/namecheap_dns.py
  - salt/modules/namecheap_domains.py
  - salt/modules/namecheap_ns.py
  - salt/modules/namecheap_ssl.py
  - salt/modules/namecheap_users.py
  - salt/modules/reg.py
  - salt/modules/tomcat.py
  - salt/modules/vault.py
  - salt/modules/win_file.py
  - salt/modules/zpool.py
  - salt/output/highstate.py
  - salt/renderers/pass.py
  - salt/runners/cache.py
  - salt/states/boto_apigateway.py
  - salt/states/boto_iam.py
  - salt/states/boto_route53.py
  - salt/states/msteams.py
  - salt/states/reg.py
  - salt/states/win_iis.py
  - tests/integration/modules/test_cmdmod.py
  - tests/integration/states/test_user.py
  - tests/support/helpers.py
  - tests/unit/cloud/clouds/test_openstack.py
  - tests/unit/fileserver/test_gitfs.py
  - tests/unit/modules/test_junos.py
  - tests/unit/pillar/test_git.py
  - tests/unit/states/test_win_path.py
  - tests/unit/test_pillar.py
  - tests/unit/utils/test_format_call.py
  - tests/unit/utils/test_utils.py
  - tests/unit/utils/test_warnings.py
2018-06-01 14:54:12 -04:00

287 lines
12 KiB
Python

# -*- coding: utf-8 -*-
'''
:codeauthor: Rupesh Tare <rupesht@saltstack.com>
'''
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
# Import Salt Testing Libs
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
# Import Salt Libs
import salt.modules.influx08 as influx08
DB_LIST = ['A', 'B', 'C']
USER_LIST = [{'name': 'A'}, {'name': 'B'}]
class MockInfluxDBClient(object):
def get_list_database(self):
return DB_LIST
def create_database(self, name):
return name
def delete_database(self, name):
return name
def switch_database(self, name):
return name
def get_list_users(self):
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())
with patch.object(influx08, '_client', mock_inf_db_client):
self.assertEqual(influx08.db_list(user='root',
password='root',
host='localhost',
port=8086), DB_LIST)
def test_db_exists(self):
'''
Tests for checks if a database exists in InfluxDB
'''
with patch.object(influx08, 'db_list', side_effect=[[{'name': 'A'}],
None]):
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))
def test_db_create(self):
'''
Test to create a database
'''
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))
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
with patch.object(influx08, '_client', mock_inf_db_client):
self.assertTrue(influx08.db_create(name='A',
user='root',
password='root',
host='localhost',
port=8000))
def test_db_remove(self):
'''
Test to remove a database
'''
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))
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
with patch.object(influx08, '_client', mock_inf_db_client):
self.assertTrue(influx08.db_remove(name='A',
user='root',
password='root',
host='localhost',
port=8000))
def test_user_list(self):
'''
Tests for list cluster admins or database users.
'''
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
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)
def test_user_exists(self):
'''
Test to checks if a cluster admin or database user exists.
'''
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))
self.assertFalse(influx08.user_exists(name='A',
user='root',
password='root',
host='localhost',
port=8000))
def test_user_chpass(self):
'''
Tests to change password for a cluster admin or a database user.
'''
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))
self.assertFalse(influx08.user_chpass(name='A',
passwd='*',
database='test',
user='root',
password='root',
host='localhost',
port=8000))
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
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))
def test_user_remove(self):
'''
Tests to remove a cluster admin or a database user.
'''
with patch.object(influx08, 'user_exists', return_value=False):
self.assertFalse(influx08.user_remove(name='A',
user='root',
password='root',
host='localhost',
port=8000))
self.assertFalse(influx08.user_remove(name='A',
database='test',
user='root',
password='root',
host='localhost',
port=8000))
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
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))
def test_query(self):
'''
Test for querying data
'''
mock_inf_db_client = MagicMock(return_value=MockInfluxDBClient())
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))
def test_retention_policy_get(self):
client = MockInfluxDBClient()
policy = {'name': 'foo'}
with patch.object(influx08, '_client', MagicMock(return_value=client)):
client.get_list_retention_policies = MagicMock(return_value=[policy])
self.assertEqual(
policy,
influx08.retention_policy_get(database='db', name='foo')
)
def test_retention_policy_add(self):
client = MockInfluxDBClient()
with patch.object(influx08, '_client', MagicMock(return_value=client)):
client.create_retention_policy = MagicMock()
self.assertTrue(influx08.retention_policy_add(
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()
with patch.object(influx08, '_client', MagicMock(return_value=client)):
client.alter_retention_policy = MagicMock()
self.assertTrue(influx08.retention_policy_alter(
database='db',
name='name',
duration='30d',
replication=1,
))
client.alter_retention_policy.assert_called_once_with(
'name', 'db', '30d', 1, False)