1
0

test_cassandra.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. from salt.ext import six
  16. import salt.modules.cassandra as cassandra
  17. class CassandraTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.modules.cassandra
  20. '''
  21. def setup_loader_modules(self):
  22. return {cassandra: {}}
  23. def test_compactionstats(self):
  24. '''
  25. Test for Return compactionstats info
  26. '''
  27. mock = MagicMock(return_value='A')
  28. with patch.object(cassandra, '_nodetool', mock):
  29. self.assertEqual(cassandra.compactionstats(), 'A')
  30. def test_version(self):
  31. '''
  32. Test for Return the cassandra version
  33. '''
  34. mock = MagicMock(return_value='A')
  35. with patch.object(cassandra, '_nodetool', mock):
  36. self.assertEqual(cassandra.version(), 'A')
  37. def test_netstats(self):
  38. '''
  39. Test for Return netstats info
  40. '''
  41. mock = MagicMock(return_value='A')
  42. with patch.object(cassandra, '_nodetool', mock):
  43. self.assertEqual(cassandra.netstats(), 'A')
  44. def test_tpstats(self):
  45. '''
  46. Test for Return tpstats info
  47. '''
  48. mock = MagicMock(return_value='A')
  49. with patch.object(cassandra, '_nodetool', mock):
  50. self.assertEqual(cassandra.tpstats(), 'A')
  51. def test_info(self):
  52. '''
  53. Test for Return cassandra node info
  54. '''
  55. mock = MagicMock(return_value='A')
  56. with patch.object(cassandra, '_nodetool', mock):
  57. self.assertEqual(cassandra.info(), 'A')
  58. def test_ring(self):
  59. '''
  60. Test for Return ring info
  61. '''
  62. mock = MagicMock(return_value='A')
  63. with patch.object(cassandra, '_nodetool', mock):
  64. self.assertEqual(cassandra.ring(), 'A')
  65. def test_keyspaces(self):
  66. '''
  67. Test for Return existing keyspaces
  68. '''
  69. mock_keyspaces = ['A', 'B', 'C', 'D']
  70. class MockSystemManager(object):
  71. def list_keyspaces(self):
  72. return mock_keyspaces
  73. mock_sys_mgr = MagicMock(return_value=MockSystemManager())
  74. with patch.object(cassandra, '_sys_mgr', mock_sys_mgr):
  75. self.assertEqual(cassandra.keyspaces(), mock_keyspaces)
  76. def test_column_families(self):
  77. '''
  78. Test for Return existing column families for all keyspaces
  79. '''
  80. mock_keyspaces = ['A', 'B']
  81. class MockSystemManager(object):
  82. def list_keyspaces(self):
  83. return mock_keyspaces
  84. def get_keyspace_column_families(self, keyspace):
  85. if keyspace == 'A':
  86. return {'a': 'saltines', 'b': 'biscuits'}
  87. if keyspace == 'B':
  88. return {'c': 'cheese', 'd': 'crackers'}
  89. mock_sys_mgr = MagicMock(return_value=MockSystemManager())
  90. with patch.object(cassandra, '_sys_mgr', mock_sys_mgr):
  91. self.assertEqual(cassandra.column_families('Z'),
  92. None)
  93. if six.PY3:
  94. self.assertCountEqual(cassandra.column_families('A'),
  95. ['a', 'b'])
  96. self.assertCountEqual(cassandra.column_families(),
  97. {'A': ['a', 'b'], 'B': ['c', 'd']})
  98. else:
  99. self.assertEqual(sorted(cassandra.column_families('A')),
  100. ['a', 'b'])
  101. column_families = cassandra.column_families()
  102. for key in ('A', 'B'):
  103. column_families[key] = sorted(column_families[key])
  104. self.assertEqual(column_families,
  105. {'A': ['a', 'b'], 'B': ['c', 'd']})
  106. def test_column_family_definition(self):
  107. '''
  108. Test for Return a dictionary of column family definitions for the given
  109. keyspace/column_family
  110. '''
  111. class MockSystemManager(object):
  112. def get_keyspace_column_families(self, keyspace):
  113. if keyspace == 'A':
  114. return {'a': object, 'b': object}
  115. if keyspace == 'B':
  116. raise Exception
  117. mock_sys_mgr = MagicMock(return_value=MockSystemManager())
  118. with patch.object(cassandra, '_sys_mgr', mock_sys_mgr):
  119. self.assertEqual(cassandra.column_family_definition('A', 'a'), vars(object))
  120. self.assertEqual(cassandra.column_family_definition('B', 'a'), None)