TestXmlConfigResource.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. """
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. Ambari Agent
  17. """
  18. import os
  19. import time
  20. from unittest import TestCase
  21. from mock.mock import patch, MagicMock
  22. from resource_management.core import Environment
  23. from resource_management.core.system import System
  24. from resource_management.libraries import XmlConfig
  25. @patch.object(System, "os_family", new='redhat')
  26. class TestXmlConfigResource(TestCase):
  27. """
  28. XmlConfig="resource_management.libraries.providers.xml_config.XmlConfigProvider",
  29. Testing XmlConfig(XmlConfigProvider) with different 'resource configurations'
  30. """
  31. @patch("resource_management.core.providers.system._ensure_metadata")
  32. @patch("__builtin__.open")
  33. @patch.object(os.path, "exists")
  34. @patch.object(os.path, "isdir")
  35. @patch.object(time, "asctime")
  36. def test_action_create_empty_xml_config(self,
  37. time_asctime_mock,
  38. os_path_isdir_mock,
  39. os_path_exists_mock,
  40. open_mock,
  41. ensure_mock):
  42. """
  43. Tests if 'create' action - creates new non existent xml file and write proper data
  44. where configurations={}
  45. """
  46. os_path_isdir_mock.side_effect = [False, True]
  47. os_path_exists_mock.return_value = False
  48. time_asctime_mock.return_value = 'Wed 2014-02'
  49. result_file = MagicMock()
  50. open_mock.return_value = result_file
  51. with Environment('/') as env:
  52. XmlConfig('file.xml',
  53. conf_dir='/dir/conf',
  54. configurations={}
  55. )
  56. open_mock.assert_called_with('/dir/conf/file.xml', 'wb')
  57. result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n <configuration>\n \n </configuration>\n')
  58. @patch("resource_management.core.providers.system._ensure_metadata")
  59. @patch("__builtin__.open")
  60. @patch.object(os.path, "exists")
  61. @patch.object(os.path, "isdir")
  62. @patch.object(time, "asctime")
  63. def test_action_create_simple_xml_config(self,
  64. time_asctime_mock,
  65. os_path_isdir_mock,
  66. os_path_exists_mock,
  67. open_mock,
  68. ensure_mock):
  69. """
  70. Tests if 'create' action - creates new non existent xml file and write proper data
  71. where configurations={"Some conf":"Some value"}
  72. """
  73. os_path_isdir_mock.side_effect = [False, True]
  74. os_path_exists_mock.return_value = False
  75. time_asctime_mock.return_value = 'Wed 2014-02'
  76. result_file = MagicMock()
  77. open_mock.return_value = result_file
  78. with Environment('/') as env:
  79. XmlConfig('file.xml',
  80. conf_dir='/dir/conf',
  81. configurations={'property1': 'value1'}
  82. )
  83. open_mock.assert_called_with('/dir/conf/file.xml', 'wb')
  84. result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name>property1</name>\n <value>value1</value>\n </property>\n \n </configuration>\n')
  85. @patch("resource_management.core.providers.system._ensure_metadata")
  86. @patch("__builtin__.open")
  87. @patch.object(os.path, "exists")
  88. @patch.object(os.path, "isdir")
  89. @patch.object(time, "asctime")
  90. def test_action_create_xml_config_with_metacharacters(self,
  91. time_asctime_mock,
  92. os_path_isdir_mock,
  93. os_path_exists_mock,
  94. open_mock,
  95. ensure_mock):
  96. """
  97. Tests if 'create' action - creates new non existent xml file and write proper data
  98. where configurations={"Some conf":"Some metacharacters"}
  99. """
  100. os_path_isdir_mock.side_effect = [False, True]
  101. os_path_exists_mock.return_value = False
  102. time_asctime_mock.return_value = 'Wed 2014-02'
  103. result_file = MagicMock()
  104. open_mock.return_value = result_file
  105. with Environment('/') as env:
  106. XmlConfig('file.xml',
  107. conf_dir='/dir/conf',
  108. configurations={"": "",
  109. "prop.1": "'.'yyyy-MM-dd-HH",
  110. "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n",
  111. "prop.2": "INFO, openjpa",
  112. "prop.4": "${oozie.log.dir}/oozie.log",
  113. "prop.empty": "",
  114. },
  115. )
  116. open_mock.assert_called_with('/dir/conf/file.xml', 'wb')
  117. result_file.__enter__().write.assert_called_with(u'<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>prop.empty</name>\n <value></value>\n </property>\n \n <property>\n <name>prop.3</name>\n <value>%d{ISO8601} %5p %c{1}:%L - %m%n</value>\n </property>\n \n <property>\n <name>prop.2</name>\n <value>INFO, openjpa</value>\n </property>\n \n <property>\n <name>prop.1</name>\n <value>&#39;.&#39;yyyy-MM-dd-HH</value>\n </property>\n \n <property>\n <name>prop.4</name>\n <value>${oozie.log.dir}/oozie.log</value>\n </property>\n \n </configuration>\n')