summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorricolin <rico.lin@easystack.cn>2017-08-02 17:19:59 +0800
committerZane Bitter <zbitter@redhat.com>2018-03-26 20:09:41 +0000
commitdef252eb968da2954fae93826c14e91834f8d316 (patch)
tree1ba9848381e89670673a8bd7c929f2c92980c376
parentbd91db747d4597eb86e6b58483624f660e465bf5 (diff)
Use six StringIO
On python3, sys.stdin/stdout are Text-mode streams, which can be approximated with io.StringIO. On python2, there's no real distinction between Text-mode and Binary-mode (except for line endings, which sys.stdin/stdout also treat as Text-mode). Hence, there's no need to explicitly use io.BytesIO on python2. StringIO.StringIO is equally or more representative of how the mocked streams behave. Therefore, just use six.StringIO instead of different types for python2 vs. python3. Change-Id: Ib80e10a40e68ece946b344ce7bcfba5e182ce848
Notes
Notes (review): Code-Review+2: Zane Bitter <zbitter@redhat.com> Workflow+1: Zane Bitter <zbitter@redhat.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Mon, 26 Mar 2018 20:29:31 +0000 Reviewed-on: https://review.openstack.org/489943 Project: openstack/heat-agents Branch: refs/heads/master
-rwxr-xr-xheat-config-kubelet/install.d/hook-kubelet.py5
-rw-r--r--tests/test_heat_config_notify.py6
-rw-r--r--tests/test_hook_chef.py9
3 files changed, 5 insertions, 15 deletions
diff --git a/heat-config-kubelet/install.d/hook-kubelet.py b/heat-config-kubelet/install.d/hook-kubelet.py
index 72ff0f9..ca50509 100755
--- a/heat-config-kubelet/install.d/hook-kubelet.py
+++ b/heat-config-kubelet/install.d/hook-kubelet.py
@@ -12,7 +12,6 @@
12# License for the specific language governing permissions and limitations 12# License for the specific language governing permissions and limitations
13# under the License. 13# under the License.
14 14
15import io
16import json 15import json
17import logging 16import logging
18import os 17import os
@@ -82,13 +81,13 @@ def configure_logging():
82 handler.setFormatter(formatter) 81 handler.setFormatter(formatter)
83 log.addHandler(handler) 82 log.addHandler(handler)
84 83
85 deploy_stdout = io.StringIO() 84 deploy_stdout = six.StringIO()
86 handler = logging.StreamHandler(deploy_stdout) 85 handler = logging.StreamHandler(deploy_stdout)
87 handler.setFormatter(formatter) 86 handler.setFormatter(formatter)
88 handler.setLevel('DEBUG') 87 handler.setLevel('DEBUG')
89 log.addHandler(handler) 88 log.addHandler(handler)
90 89
91 deploy_stderr = io.StringIO() 90 deploy_stderr = six.StringIO()
92 handler = logging.StreamHandler(deploy_stderr) 91 handler = logging.StreamHandler(deploy_stderr)
93 handler.setFormatter(formatter) 92 handler.setFormatter(formatter)
94 handler.setLevel('WARN') 93 handler.setLevel('WARN')
diff --git a/tests/test_heat_config_notify.py b/tests/test_heat_config_notify.py
index 15ca558..6f6b02c 100644
--- a/tests/test_heat_config_notify.py
+++ b/tests/test_heat_config_notify.py
@@ -11,7 +11,6 @@
11# License for the specific language governing permissions and limitations 11# License for the specific language governing permissions and limitations
12# under the License. 12# under the License.
13 13
14import io
15import json 14import json
16import tempfile 15import tempfile
17 16
@@ -77,10 +76,7 @@ class HeatConfigNotifyTest(common.RunScriptTest):
77 super(HeatConfigNotifyTest, self).setUp() 76 super(HeatConfigNotifyTest, self).setUp()
78 self.deployed_dir = self.useFixture(fixtures.TempDir()) 77 self.deployed_dir = self.useFixture(fixtures.TempDir())
79 hcn.init_logging = mock.MagicMock() 78 hcn.init_logging = mock.MagicMock()
80 if six.PY2: 79 self.stdin = six.StringIO()
81 self.stdin = io.BytesIO()
82 else:
83 self.stdin = io.StringIO()
84 80
85 def write_config_file(self, data): 81 def write_config_file(self, data):
86 config_file = tempfile.NamedTemporaryFile(mode='w') 82 config_file = tempfile.NamedTemporaryFile(mode='w')
diff --git a/tests/test_hook_chef.py b/tests/test_hook_chef.py
index 05968dc..2c78ac9 100644
--- a/tests/test_hook_chef.py
+++ b/tests/test_hook_chef.py
@@ -13,7 +13,6 @@
13 13
14import copy 14import copy
15import imp 15import imp
16import io
17import json 16import json
18import logging 17import logging
19import mock 18import mock
@@ -59,12 +58,8 @@ class HookChefTest(common.RunScriptTest):
59 __file__, 58 __file__,
60 '..', 59 '..',
61 'heat-config-chef/install.d/hook-chef.py') 60 'heat-config-chef/install.d/hook-chef.py')
62 if six.PY2: 61 sys.stdin = six.StringIO()
63 sys.stdin = io.BytesIO() 62 sys.stdout = six.StringIO()
64 sys.stdout = io.BytesIO()
65 else:
66 sys.stdin = io.StringIO()
67 sys.stdout = io.StringIO()
68 63
69 def tearDown(self): 64 def tearDown(self):
70 super(HookChefTest, self).tearDown() 65 super(HookChefTest, self).tearDown()