Skip to content

Commit 0cfa320

Browse files
author
Gabriel Monroy
committed
Merge pull request #1871 from gabrtv/fix-config-ints
Fix integer handling when encoding configuration
2 parents 6243d91 + 0920b9e commit 0cfa320

5 files changed

Lines changed: 34 additions & 12 deletions

File tree

client/deis.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ def dictify(args):
303303
return data
304304

305305

306+
def encode(obj):
307+
"""Return UTF-8 encoding for string objects."""
308+
if isinstance(obj, basestring):
309+
return obj.encode('utf-8')
310+
else:
311+
return obj
312+
313+
306314
def readable_datetime(datetime_str):
307315
"""
308316
Return a human-readable datetime string from an ECMA-262 (JavaScript)
@@ -1122,14 +1130,12 @@ def config_list(self, args):
11221130
if not oneline:
11231131
width = max(map(len, keys)) + 5
11241132
for k in keys:
1125-
v = values[k]
1126-
k, v = k.encode('utf-8'), v.encode('utf-8')
1133+
k, v = encode(k), encode(values[k])
11271134
self._logger.info(("{k:<" + str(width) + "} {v}").format(**locals()))
11281135
else:
11291136
output = []
11301137
for k in keys:
1131-
v = values[k]
1132-
k, v = k.encode('utf-8'), v.encode('utf-8')
1138+
k, v = encode(k), encode(values[k])
11331139
output.append("{k}={v}".format(**locals()))
11341140
self._logger.info(' '.join(output))
11351141
else:
@@ -1175,8 +1181,7 @@ def config_set(self, args):
11751181
self._logger.info('No configuration')
11761182
return
11771183
for k, v in values.items():
1178-
k, v = k.encode('utf-8'), v.encode('utf-8')
1179-
self._logger.info("{k}: {v}".format(**locals()))
1184+
self._logger.info("{}: {}".format(encode(k), encode(v)))
11801185
else:
11811186
raise ResponseError(response)
11821187

controller/api/tests/test_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ def test_config_set_unicode(self):
148148
self.assertEqual(response.status_code, 201)
149149
self.assertIn('POWERED_BY', json.loads(response.data['values']))
150150
self.assertEqual(json.loads(response.data['values'])['POWERED_BY'], 'Кроликов')
151+
# set an integer to test unicode regression
152+
body = {'values': json.dumps({'INTEGER': 1})}
153+
response = self.client.post(url, json.dumps(body), content_type='application/json')
154+
self.assertEqual(response.status_code, 201)
155+
self.assertIn('INTEGER', json.loads(response.data['values']))
156+
self.assertEqual(json.loads(response.data['values'])['INTEGER'], 1)
151157

152158
@mock.patch('requests.post', mock_import_repository_task)
153159
def test_config_str(self):

controller/api/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ def fingerprint(key):
108108
return ':'.join(a + b for a, b in zip(fp_plain[::2], fp_plain[1::2]))
109109

110110

111+
def encode(obj):
112+
"""Return UTF-8 encoding for string objects."""
113+
if isinstance(obj, basestring):
114+
return obj.encode('utf-8')
115+
else:
116+
return obj
117+
118+
111119
if __name__ == "__main__":
112120
import doctest
113121
doctest.testmod()

controller/registry/private.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import urlparse
77
import uuid
88

9+
from django.conf import settings
910
from docker.utils import utils
1011

11-
from django.conf import settings
12+
from api.utils import encode
1213

1314

1415
def publish_release(source, config, target):
@@ -158,7 +159,6 @@ def _put_tag(image_id, repository_path, tag):
158159

159160
# utility functions
160161

161-
162162
def _construct_env(env, config):
163163
"Update current environment with latest config"
164164
new_env = []
@@ -168,10 +168,10 @@ def _construct_env(env, config):
168168
if k in config:
169169
# update values defined by config
170170
v = config.pop(k)
171-
new_env.append("{}={}".format(k.encode('utf-8'), v.encode('utf-8')))
171+
new_env.append("{}={}".format(encode(k), encode(v)))
172172
# add other config ENV items
173173
for k, v in config.items():
174-
new_env.append("{}={}".format(k.encode('utf-8'), v.encode('utf-8')))
174+
new_env.append("{}={}".format(encode(k), encode(v)))
175175
return new_env
176176

177177

tests/config_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
var (
1212
configListCmd = "config:list --app={{.AppName}}"
1313
configSetCmd = "config:set FOO=讲台 --app={{.AppName}}"
14+
configSet2Cmd = "config:set FOO=10 --app={{.AppName}}"
1415
configUnsetCmd = "config:unset FOO --app={{.AppName}}"
1516
)
1617

@@ -52,11 +53,13 @@ func configListTest(
5253
}
5354

5455
func configSetTest(t *testing.T, params *utils.DeisTestConfig) {
55-
utils.Execute(t, configSetCmd, params, false, "")
56+
utils.Execute(t, configSetCmd, params, false, "讲台")
5657
utils.CheckList(t, appsInfoCmd, params, "(v3)", false)
58+
utils.Execute(t, configSet2Cmd, params, false, "10")
59+
utils.CheckList(t, appsInfoCmd, params, "(v4)", false)
5760
}
5861

5962
func configUnsetTest(t *testing.T, params *utils.DeisTestConfig) {
6063
utils.Execute(t, configUnsetCmd, params, false, "")
61-
utils.CheckList(t, appsInfoCmd, params, "(v4)", false)
64+
utils.CheckList(t, appsInfoCmd, params, "(v5)", false)
6265
}

0 commit comments

Comments
 (0)