123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #!/usr/bin/env python
- """
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- Ambari Agent
- """
- import os
- import time
- from unittest import TestCase
- from mock.mock import patch, MagicMock
- from resource_management.core import Environment, sudo
- from resource_management.core.system import System
- from resource_management.libraries import XmlConfig
- @patch.object(System, "os_family", new='redhat')
- class TestXmlConfigResource(TestCase):
- """
- XmlConfig="resource_management.libraries.providers.xml_config.XmlConfigProvider",
- Testing XmlConfig(XmlConfigProvider) with different 'resource configurations'
- """
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- @patch.object(time, "asctime")
- def test_action_create_empty_xml_config(self,
- time_asctime_mock,
- os_path_isdir_mock,
- os_path_exists_mock,
- create_file_mock,
- ensure_mock):
- """
- Tests if 'create' action - creates new non existent xml file and write proper data
- where configurations={}
- """
- os_path_isdir_mock.side_effect = [False, True]
- os_path_exists_mock.return_value = False
- time_asctime_mock.return_value = 'Wed 2014-02'
- with Environment('/') as env:
- XmlConfig('file.xml',
- conf_dir='/dir/conf',
- configurations={},
- configuration_attributes={}
- )
- create_file_mock.assert_called_with('/dir/conf/file.xml', '<!--Wed 2014-02-->\n <configuration>\n \n </configuration>')
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- @patch.object(time, "asctime")
- def test_action_create_simple_xml_config(self,
- time_asctime_mock,
- os_path_isdir_mock,
- os_path_exists_mock,
- create_file_mock,
- ensure_mock):
- """
- Tests if 'create' action - creates new non existent xml file and write proper data
- where configurations={"Some conf":"Some value"}
- """
- os_path_isdir_mock.side_effect = [False, True]
- os_path_exists_mock.return_value = False
- time_asctime_mock.return_value = 'Wed 2014-02'
- with Environment('/') as env:
- XmlConfig('file.xml',
- conf_dir='/dir/conf',
- configurations={'property1': 'value1'},
- configuration_attributes={'attr': {'property1': 'attr_value'}}
- )
- create_file_mock.assert_called_with('/dir/conf/file.xml', '<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name>property1</name>\n <value>value1</value>\n <attr>attr_value</attr>\n </property>\n \n </configuration>')
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- @patch.object(time, "asctime")
- def test_action_create_xml_config_with_metacharacters(self,
- time_asctime_mock,
- os_path_isdir_mock,
- os_path_exists_mock,
- create_file_mock,
- ensure_mock):
- """
- Tests if 'create' action - creates new non existent xml file and write proper data
- where configurations={"Some conf":"Some metacharacters"}
- """
- os_path_isdir_mock.side_effect = [False, True]
- os_path_exists_mock.return_value = False
- time_asctime_mock.return_value = 'Wed 2014-02'
- with Environment('/') as env:
- XmlConfig('file.xml',
- conf_dir='/dir/conf',
- configurations={"": "",
- "prop.1": "'.'yyyy-MM-dd-HH",
- "prop.3": "%d{ISO8601} %5p %c{1}:%L - %m%n",
- "prop.2": "INFO, openjpa",
- "prop.4": "${oozie.log.dir}/oozie.log",
- "prop.empty": "",
- },
- configuration_attributes={
- "": {
- "prop.1": "should_not_be_printed",
- "prop.2": "should_not_be_printed",
- },
- "attr1": {
- "prop.1": "x",
- "prop.8": "not_existed",
- },
- "attr2": {
- "prop.4": "value4",
- "prop.3": "value3"
- },
- "attr_empty": {
- },
- "attr_value_empty": {
- "prop.4": "",
- "prop.empty": ""
- }
- })
- create_file_mock.assert_called_with('/dir/conf/file.xml', '<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>prop.1</name>\n <value>'.'yyyy-MM-dd-HH</value>\n <attr1>x</attr1>\n </property>\n \n <property>\n <name>prop.2</name>\n <value>INFO, openjpa</value>\n </property>\n \n <property>\n <name>prop.3</name>\n <value>%d{ISO8601} %5p %c{1}:%L - %m%n</value>\n <attr2>value3</attr2>\n </property>\n \n <property>\n <name>prop.4</name>\n <value>${oozie.log.dir}/oozie.log</value>\n <attr_value_empty></attr_value_empty>\n <attr2>value4</attr2>\n </property>\n \n <property>\n <name>prop.empty</name>\n <value></value>\n <attr_value_empty></attr_value_empty>\n </property>\n \n </configuration>')
-
- @patch("resource_management.core.providers.system._ensure_metadata")
- @patch.object(sudo, "create_file")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- @patch.object(time, "asctime")
- def test_action_create_xml_config_sorted_by_key(self,
- time_asctime_mock,
- os_path_isdir_mock,
- os_path_exists_mock,
- create_file_mock,
- ensure_mock):
- """
- Tests if 'create' action - creates new non existent xml file and writes proper data
- where configurations={"Key":"Value"} are stored in sorted by key order
- """
- os_path_isdir_mock.side_effect = [False, True]
- os_path_exists_mock.return_value = False
- time_asctime_mock.return_value = 'Wed 2014-02'
- with Environment('/') as env:
- XmlConfig('file.xml',
- conf_dir='/dir/conf',
- configurations={"": "",
- "third": "should be third",
- "first": "should be first",
- "z_last": "should be last",
- "second": "should be second",
- },
- configuration_attributes={}
- )
- create_file_mock.assert_called_with('/dir/conf/file.xml', '<!--Wed 2014-02-->\n <configuration>\n \n <property>\n <name></name>\n <value></value>\n </property>\n \n <property>\n <name>first</name>\n <value>should be first</value>\n </property>\n \n <property>\n <name>second</name>\n <value>should be second</value>\n </property>\n \n <property>\n <name>third</name>\n <value>should be third</value>\n </property>\n \n <property>\n <name>z_last</name>\n <value>should be last</value>\n </property>\n \n </configuration>')
- @patch("resource_management.libraries.providers.xml_config.File")
- @patch.object(os.path, "exists")
- @patch.object(os.path, "isdir")
- def test_action_create_arguments(self, os_path_isdir_mock ,os_path_exists_mock, file_mock):
- os_path_isdir_mock.side_effect = [False, True]
- os_path_exists_mock.return_value = False
- with Environment() as env:
- XmlConfig('xmlFile.xml',
- conf_dir='/dir/conf',
- configurations={'property1': 'value1'},
- configuration_attributes={'attr': {'property1': 'attr_value'}},
- mode = 0755,
- owner = "hdfs",
- group = "hadoop",
- encoding = "Code"
- )
- self.assertEqual(file_mock.call_args[0][0],'/dir/conf/xmlFile.xml')
- call_args = file_mock.call_args[1].copy()
- del call_args['content']
- self.assertEqual(call_args,{'owner': 'hdfs', 'group': 'hadoop', 'mode': 0755, 'encoding' : 'Code'})
|