''' Unit tests for wildfly module ''' import unittest from six import string_types from wildfly import WildflyPath, format_attr, config_query, wildfly_batch class TestWildflyPath(unittest.TestCase): ''' Tests for WildflyPath class. ''' def setUp(self): test_input = r''' /subsystem=undertow/server=default-server/host=default-host /location=\/myapp\/multimedia ''' self.instance = WildflyPath(test_input) def test_wildfly_path_str(self): self.assertEqual( str(self.instance), r'/subsystem=undertow/server=default-server/host=default-host' r'/location=\/myapp\/multimedia' ) def test_wildfly_path_len(self): self.assertEqual(len(self.instance), 8) def test_wildfly_path_lpop(self): self.instance.lpop() self.instance.lpop() self.assertEqual( str(self.instance), r'/server=default-server/host=default-host' r'/location=\/myapp\/multimedia' ) class TestFormatAttr(unittest.TestCase): ''' Tests for format_attr function. ''' def test_format_attr_return_type(self): for input_ in (None, True, 1234, 1234.5, ['123',243], 'hello', {'a': 123}): self.assertIsInstance(format_attr(input_), string_types) def test_format_attr_true(self): self.assertEqual(format_attr(True), 'True') def test_format_attr_false(self): self.assertEqual(format_attr(False), 'False') def test_format_attr_none(self): self.assertEqual(format_attr(None), 'undefined') def test_format_attr_float(self): self.assertEqual(format_attr(1234.5), '1234.500000') def test_format_attr_int(self): self.assertEqual(format_attr(1234), '1234') def test_format_attr_bigint(self): self.assertEqual(format_attr(1e10), '10000000000.000000') def test_format_attr_string(self): self.assertEqual(format_attr('hello'), '"hello"') def test_format_attr_stripquotes(self): self.assertEqual(format_attr('"hello"'), '"hello"') class TestConfigQueryFunction(unittest.TestCase): ''' Tests for config_query function. ''' def setUp(self): self.config0 = {'core-service': {'management': {'security-realm': { 'ManagementRealm': {'plug-in': None, 'server-identity': None}}}}} self.path0 = ('/core-service=management/security-realm=ManagementRealm' '/server-identity=ssl') self.config1 = {'core-service': {'management': {'security-realm': { 'ManagementRealm': {'server-identity': {'ssl': { 'alias': 'wildfly', 'key-password': 'changeit', 'keystore-password': 'changeit', 'keystore-path': 'server-admin.keystore', 'keystore-relative-to': 'jboss.server.config.dir', }}}}}}} def test_config_query_none(self): self.assertEqual(config_query(self.config0, self.path0), None) class TestBatch(unittest.TestCase): ''' Tests for wildfly_batch function. ''' def setUp(self): self.config0 = {'core-service': {'management': {'security-realm': { 'ManagementRealm': {'plug-in': None, 'server-identity': None}}}}} self.path0 = ('/core-service=management/security-realm=ManagementRealm' '/server-identity=ssl') self.attrs0 = { 'alias': 'wildfly', 'key-password': 'changeit', 'keystore-password': 'changeit', 'keystore-path': 'server-admin.keystore', 'keystore-relative-to': 'jboss.server.config.dir', } self.batch0 = '''\ /core-service=management/security-realm=ManagementRealm\ /server-identity=ssl:add(alias="wildfly",key-password="changeit",\ keystore-password="changeit",keystore-path="server-admin.keystore",\ keystore-relative-to="jboss.server.config.dir") ''' self.config1 = {'core-service': {'management': {'security-realm': { 'ManagementRealm': {'plug-in': None, 'server-identity': {'ssl': { 'alias': 'wildfly', 'key-password': 'changeit', 'keystore-password': 'changeit', 'keystore-path': 'server-admin.keystore', 'keystore-relative-to': 'jboss.server.config.dir', }}}}}}} def test_batch_add_node(self): self.assertEqual(wildfly_batch(self.config0, self.path0, self.attrs0, wrap=False), self.batch0) if __name__ == '__main__': unittest.main()