You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ao...@apache.org on 2014/08/01 16:01:47 UTC
git commit: AMBARI-6674. potential language/encoding problem with
mysql (aonishuk)
Repository: ambari
Updated Branches:
refs/heads/trunk d728f13bb -> 99e42776e
AMBARI-6674. potential language/encoding problem with mysql (aonishuk)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/99e42776
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/99e42776
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/99e42776
Branch: refs/heads/trunk
Commit: 99e42776ebd7bdb5ab97e450b7683626e5a22028
Parents: d728f13
Author: Andrew Onishuk <ao...@hortonworks.com>
Authored: Fri Aug 1 17:01:41 2014 +0300
Committer: Andrew Onishuk <ao...@hortonworks.com>
Committed: Fri Aug 1 17:01:41 2014 +0300
----------------------------------------------------------------------
.../resource_management/TestFileResource.py | 31 ++++++++++++++++++++
.../TestXmlConfigResource.py | 26 +++++++++++++++-
.../python/resource_management/core/logger.py | 2 +-
.../core/providers/system.py | 2 ++
.../core/resources/system.py | 1 +
.../libraries/functions/format.py | 2 +-
.../libraries/providers/xml_config.py | 3 +-
.../libraries/resources/xml_config.py | 2 ++
8 files changed, 65 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/99e42776/ambari-agent/src/test/python/resource_management/TestFileResource.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/resource_management/TestFileResource.py b/ambari-agent/src/test/python/resource_management/TestFileResource.py
index 7da0dbd..be6c6e7 100644
--- a/ambari-agent/src/test/python/resource_management/TestFileResource.py
+++ b/ambari-agent/src/test/python/resource_management/TestFileResource.py
@@ -330,3 +330,34 @@ class TestFileResource(TestCase):
self.assertEqual(chmod_mock.call_count, 1)
self.assertEqual(chown_mock.call_count, 0)
+
+ @patch("resource_management.core.providers.system._ensure_metadata")
+ @patch("resource_management.core.providers.system.FileProvider._get_content")
+ @patch("__builtin__.open")
+ @patch.object(os.path, "exists")
+ @patch.object(os.path, "isdir")
+ def test_action_create_encoding(self, isdir_mock, exists_mock, open_mock, get_content_mock ,ensure_mock):
+
+ isdir_mock.side_effect = [False, True]
+ exists_mock.return_value = True
+ content_mock = MagicMock()
+ old_content_mock = MagicMock()
+ get_content_mock.return_value = content_mock
+ new_file = MagicMock()
+ open_mock.return_value = new_file
+ enter_file_mock = MagicMock()
+ enter_file_mock.read = MagicMock(return_value=old_content_mock)
+ new_file.__enter__ = MagicMock(return_value=enter_file_mock)
+ with Environment('/') as env:
+ File('/directory/file',
+ action='create',
+ mode=0777,
+ content='file-content',
+ encoding = "UTF-8"
+ )
+
+
+ open_mock.assert_called_with('/directory/file', 'wb')
+ content_mock.encode.assert_called_with('UTF-8')
+ old_content_mock.decode.assert_called_with('UTF-8')
+
http://git-wip-us.apache.org/repos/asf/ambari/blob/99e42776/ambari-agent/src/test/python/resource_management/TestXmlConfigResource.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/resource_management/TestXmlConfigResource.py b/ambari-agent/src/test/python/resource_management/TestXmlConfigResource.py
index 2ecfc4d..e7a61e4 100644
--- a/ambari-agent/src/test/python/resource_management/TestXmlConfigResource.py
+++ b/ambari-agent/src/test/python/resource_management/TestXmlConfigResource.py
@@ -193,4 +193,28 @@ class TestXmlConfigResource(TestCase):
)
open_mock.assert_called_with('/dir/conf/file.xml', 'wb')
- 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>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>\n')
\ No newline at end of file
+ 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>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>\n')
+
+ @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'})
http://git-wip-us.apache.org/repos/asf/ambari/blob/99e42776/ambari-common/src/main/python/resource_management/core/logger.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/core/logger.py b/ambari-common/src/main/python/resource_management/core/logger.py
index da64f6a..e395bd7 100644
--- a/ambari-common/src/main/python/resource_management/core/logger.py
+++ b/ambari-common/src/main/python/resource_management/core/logger.py
@@ -89,4 +89,4 @@ class Logger:
if arguments_str:
arguments_str = arguments_str[:-2]
- return "{0} {{{1}}}".format(resource, arguments_str)
\ No newline at end of file
+ return unicode("{0} {{{1}}}").format(resource, arguments_str)
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ambari/blob/99e42776/ambari-common/src/main/python/resource_management/core/providers/system.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/core/providers/system.py b/ambari-common/src/main/python/resource_management/core/providers/system.py
index 33b9ad9..c297c83 100644
--- a/ambari-common/src/main/python/resource_management/core/providers/system.py
+++ b/ambari-common/src/main/python/resource_management/core/providers/system.py
@@ -101,6 +101,7 @@ class FileProvider(Provider):
if content is not None:
with open(path, "rb") as fp:
old_content = fp.read()
+ old_content = old_content.decode(self.resource.encoding) if self.resource.encoding else old_content
if content != old_content:
write = True
reason = "contents don't match"
@@ -111,6 +112,7 @@ class FileProvider(Provider):
Logger.info("Writing %s because %s" % (self.resource, reason))
with open(path, "wb") as fp:
if content:
+ content = content.encode(self.resource.encoding) if self.resource.encoding else content
fp.write(content)
_ensure_metadata(self.resource.path, self.resource.owner,
http://git-wip-us.apache.org/repos/asf/ambari/blob/99e42776/ambari-common/src/main/python/resource_management/core/resources/system.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/core/resources/system.py b/ambari-common/src/main/python/resource_management/core/resources/system.py
index 0952c48..4fe8440 100644
--- a/ambari-common/src/main/python/resource_management/core/resources/system.py
+++ b/ambari-common/src/main/python/resource_management/core/resources/system.py
@@ -35,6 +35,7 @@ class File(Resource):
content = ResourceArgument()
# whether to replace files with different content
replace = ResourceArgument(default=True)
+ encoding = ResourceArgument()
actions = Resource.actions + ["create", "delete"]
http://git-wip-us.apache.org/repos/asf/ambari/blob/99e42776/ambari-common/src/main/python/resource_management/libraries/functions/format.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/libraries/functions/format.py b/ambari-common/src/main/python/resource_management/libraries/functions/format.py
index efbacd7..0dfc45d 100644
--- a/ambari-common/src/main/python/resource_management/libraries/functions/format.py
+++ b/ambari-common/src/main/python/resource_management/libraries/functions/format.py
@@ -66,7 +66,7 @@ class ConfigurationFormatter(Formatter):
def _convert_field(self, value, conversion, is_protected):
if conversion == 'e':
- return quote_bash_args(str(value))
+ return quote_bash_args(unicode(value))
elif conversion == 'h':
return "[PROTECTED]" if is_protected else value
elif conversion == 'p':
http://git-wip-us.apache.org/repos/asf/ambari/blob/99e42776/ambari-common/src/main/python/resource_management/libraries/providers/xml_config.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/libraries/providers/xml_config.py b/ambari-common/src/main/python/resource_management/libraries/providers/xml_config.py
index c962847..d6ef1cc 100644
--- a/ambari-common/src/main/python/resource_management/libraries/providers/xml_config.py
+++ b/ambari-common/src/main/python/resource_management/libraries/providers/xml_config.py
@@ -57,5 +57,6 @@ class XmlConfigProvider(Provider):
content = config_content,
owner = self.resource.owner,
group = self.resource.group,
- mode = self.resource.mode
+ mode = self.resource.mode,
+ encoding = self.resource.encoding
)
http://git-wip-us.apache.org/repos/asf/ambari/blob/99e42776/ambari-common/src/main/python/resource_management/libraries/resources/xml_config.py
----------------------------------------------------------------------
diff --git a/ambari-common/src/main/python/resource_management/libraries/resources/xml_config.py b/ambari-common/src/main/python/resource_management/libraries/resources/xml_config.py
index 37eb511..882d6d5 100644
--- a/ambari-common/src/main/python/resource_management/libraries/resources/xml_config.py
+++ b/ambari-common/src/main/python/resource_management/libraries/resources/xml_config.py
@@ -35,4 +35,6 @@ class XmlConfig(Resource):
owner = ResourceArgument()
group = ResourceArgument()
+ encoding = ResourceArgument(default="UTF-8")
+
actions = Resource.actions + ["create"]