test_jboss7.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import salt testing libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import TestCase
  7. from tests.support.mock import MagicMock
  8. # Import salt libs
  9. from salt.utils.odict import OrderedDict
  10. import salt.modules.jboss7 as jboss7
  11. class JBoss7TestCase(TestCase, LoaderModuleMockMixin):
  12. jboss_config = {}
  13. org_run_operation = None
  14. def setup_loader_modules(self):
  15. self.org_run_operation = MagicMock()
  16. self.addCleanup(delattr, self, 'org_run_operation')
  17. return {
  18. jboss7: {
  19. '__salt__': {
  20. 'jboss7_cli.run_operation': self.org_run_operation
  21. }
  22. }
  23. }
  24. def test_create_simple_binding(self):
  25. jboss7.create_simple_binding(self.jboss_config, 'java:global/env', 'DEV')
  26. self.org_run_operation.assert_called_with(self.jboss_config, '/subsystem=naming/binding="java:global/env":add(binding-type=simple, value="DEV")')
  27. def test_create_simple_binding_with_backslash(self):
  28. jboss7.create_simple_binding(self.jboss_config, 'java:global/env', r'DEV\2')
  29. self.org_run_operation.assert_called_with(self.jboss_config, r'/subsystem=naming/binding="java:global/env":add(binding-type=simple, value="DEV\\\\2")')
  30. def test_update_binding(self):
  31. jboss7.update_simple_binding(self.jboss_config, 'java:global/env', 'INT')
  32. self.org_run_operation.assert_called_with(self.jboss_config, '/subsystem=naming/binding="java:global/env":write-attribute(name=value, value="INT")')
  33. def test_update_binding_with_backslash(self):
  34. jboss7.update_simple_binding(self.jboss_config, 'java:global/env', r'INT\2')
  35. self.org_run_operation.assert_called_with(self.jboss_config, r'/subsystem=naming/binding="java:global/env":write-attribute(name=value, value="INT\\\\2")')
  36. def test_read_binding(self):
  37. def cli_command_response(jboss_config, cli_command):
  38. if cli_command == '/subsystem=naming/binding="java:global/env":read-resource':
  39. return {'outcome': 'success',
  40. 'result': {
  41. 'binding-type': 'simple',
  42. 'value': 'DEV'
  43. }
  44. }
  45. self.org_run_operation.side_effect = cli_command_response
  46. result = jboss7.read_simple_binding(self.jboss_config, 'java:global/env')
  47. self.assertEqual(result['outcome'], 'success')
  48. self.assertEqual(result['result']['value'], 'DEV')
  49. def test_create_datasource_all_properties_included(self):
  50. def cli_command_response(jboss_config, cli_command, fail_on_error=False):
  51. if cli_command == '/subsystem=datasources/data-source="appDS":read-resource-description':
  52. return {'outcome': 'success',
  53. 'result': {
  54. 'attributes': {
  55. 'driver-name': {'type': 'STRING'},
  56. 'connection-url': {'type': 'STRING'},
  57. 'jndi-name': {'type': 'STRING'},
  58. 'user-name': {'type': 'STRING'},
  59. 'password': {'type': 'STRING'}
  60. }
  61. }
  62. }
  63. self.org_run_operation.side_effect = cli_command_response
  64. datasource_properties = OrderedDict()
  65. datasource_properties['driver-name'] = 'mysql'
  66. datasource_properties['connection-url'] = 'jdbc:mysql://localhost:3306/app'
  67. datasource_properties['jndi-name'] = 'java:jboss/datasources/appDS'
  68. datasource_properties['user-name'] = 'app'
  69. datasource_properties['password'] = 'app_password'
  70. jboss7.create_datasource(self.jboss_config, 'appDS', datasource_properties)
  71. self.org_run_operation.assert_called_with(self.jboss_config, '/subsystem=datasources/data-source="appDS":add(driver-name="mysql",connection-url="jdbc:mysql://localhost:3306/app",jndi-name="java:jboss/datasources/appDS",user-name="app",password="app_password")', fail_on_error=False)
  72. def test_create_datasource_format_boolean_value_when_string(self):
  73. def cli_command_response(jboss_config, cli_command, fail_on_error=False):
  74. if cli_command == '/subsystem=datasources/data-source="appDS":read-resource-description':
  75. return {'outcome': 'success',
  76. 'result': {
  77. 'attributes': {
  78. 'use-ccm': {'type': 'BOOLEAN'}
  79. }
  80. }
  81. }
  82. self.org_run_operation.side_effect = cli_command_response
  83. datasource_properties = OrderedDict()
  84. datasource_properties['use-ccm'] = 'true'
  85. jboss7.create_datasource(self.jboss_config, 'appDS', datasource_properties)
  86. self.org_run_operation.assert_called_with(self.jboss_config, '/subsystem=datasources/data-source="appDS":add(use-ccm=true)', fail_on_error=False)
  87. def test_create_datasource_format_boolean_value_when_boolean(self):
  88. def cli_command_response(jboss_config, cli_command, fail_on_error=False):
  89. if cli_command == '/subsystem=datasources/data-source="appDS":read-resource-description':
  90. return {'outcome': 'success',
  91. 'result': {
  92. 'attributes': {
  93. 'use-ccm': {'type': 'BOOLEAN'}
  94. }
  95. }
  96. }
  97. self.org_run_operation.side_effect = cli_command_response
  98. datasource_properties = OrderedDict()
  99. datasource_properties['use-ccm'] = True
  100. jboss7.create_datasource(self.jboss_config, 'appDS', datasource_properties)
  101. self.org_run_operation.assert_called_with(self.jboss_config, '/subsystem=datasources/data-source="appDS":add(use-ccm=true)', fail_on_error=False)
  102. def test_create_datasource_format_int_value_when_int(self):
  103. def cli_command_response(jboss_config, cli_command, fail_on_error=False):
  104. if cli_command == '/subsystem=datasources/data-source="appDS":read-resource-description':
  105. return {'outcome': 'success',
  106. 'result': {
  107. 'attributes': {
  108. 'min-pool-size': {'type': 'INT'}
  109. }
  110. }
  111. }
  112. self.org_run_operation.side_effect = cli_command_response
  113. datasource_properties = OrderedDict()
  114. datasource_properties['min-pool-size'] = 15
  115. jboss7.create_datasource(self.jboss_config, 'appDS', datasource_properties)
  116. self.org_run_operation.assert_called_with(self.jboss_config, '/subsystem=datasources/data-source="appDS":add(min-pool-size=15)', fail_on_error=False)
  117. def test_create_datasource_format_int_value_when_string(self):
  118. def cli_command_response(jboss_config, cli_command, fail_on_error=False):
  119. if cli_command == '/subsystem=datasources/data-source="appDS":read-resource-description':
  120. return {'outcome': 'success',
  121. 'result': {
  122. 'attributes': {
  123. 'min-pool-size': {'type': 'INT'}
  124. }
  125. }
  126. }
  127. self.org_run_operation.side_effect = cli_command_response
  128. datasource_properties = OrderedDict()
  129. datasource_properties['min-pool-size'] = '15'
  130. jboss7.create_datasource(self.jboss_config, 'appDS', datasource_properties)
  131. self.org_run_operation.assert_called_with(self.jboss_config, '/subsystem=datasources/data-source="appDS":add(min-pool-size=15)', fail_on_error=False)
  132. def test_read_datasource(self):
  133. def cli_command_response(jboss_config, cli_command):
  134. if cli_command == '/subsystem=datasources/data-source="appDS":read-resource':
  135. return {
  136. 'outcome': 'success',
  137. 'result': {
  138. 'driver-name': 'mysql',
  139. 'connection-url': 'jdbc:mysql://localhost:3306/app',
  140. 'jndi-name': 'java:jboss/datasources/appDS',
  141. 'user-name': 'app',
  142. 'password': 'app_password'
  143. }
  144. }
  145. self.org_run_operation.side_effect = cli_command_response
  146. ds_result = jboss7.read_datasource(self.jboss_config, 'appDS')
  147. ds_properties = ds_result['result']
  148. self.assertEqual(ds_properties['driver-name'], 'mysql')
  149. self.assertEqual(ds_properties['connection-url'], 'jdbc:mysql://localhost:3306/app')
  150. self.assertEqual(ds_properties['jndi-name'], 'java:jboss/datasources/appDS')
  151. self.assertEqual(ds_properties['user-name'], 'app')
  152. self.assertEqual(ds_properties['password'], 'app_password')
  153. def test_update_datasource(self):
  154. datasource_properties = {'driver-name': 'mysql',
  155. 'connection-url': 'jdbc:mysql://localhost:3306/app',
  156. 'jndi-name': 'java:jboss/datasources/appDS',
  157. 'user-name': 'newuser',
  158. 'password': 'app_password'}
  159. def cli_command_response(jboss_config, cli_command, fail_on_error=False):
  160. if cli_command == '/subsystem=datasources/data-source="appDS":read-resource-description':
  161. return {'outcome': 'success',
  162. 'result': {
  163. 'attributes': {
  164. 'driver-name': {'type': 'STRING'},
  165. 'connection-url': {'type': 'STRING'},
  166. 'jndi-name': {'type': 'STRING'},
  167. 'user-name': {'type': 'STRING'},
  168. 'password': {'type': 'STRING'}
  169. }
  170. }
  171. }
  172. elif cli_command == '/subsystem=datasources/data-source="appDS":read-resource':
  173. return {
  174. 'outcome': 'success',
  175. 'result': {
  176. 'driver-name': 'mysql',
  177. 'connection-url': 'jdbc:mysql://localhost:3306/app',
  178. 'jndi-name': 'java:jboss/datasources/appDS',
  179. 'user-name': 'app',
  180. 'password': 'app_password'
  181. }
  182. }
  183. elif cli_command == '/subsystem=datasources/data-source="appDS":write-attribute(name="user-name",value="newuser")':
  184. return {
  185. 'outcome': 'success',
  186. 'success': True
  187. }
  188. self.org_run_operation.side_effect = cli_command_response
  189. jboss7.update_datasource(self.jboss_config, 'appDS', datasource_properties)
  190. self.org_run_operation.assert_any_call(self.jboss_config, '/subsystem=datasources/data-source="appDS":write-attribute(name="user-name",value="newuser")', fail_on_error=False)