Merge lp:~gocept/landscape-client/py3-manager-keystonetoken into lp:~landscape/landscape-client/trunk

Proposed by Steffen Allner
Status: Merged
Approved by: Eric Snow
Approved revision: 987
Merged at revision: 987
Proposed branch: lp:~gocept/landscape-client/py3-manager-keystonetoken
Merge into: lp:~landscape/landscape-client/trunk
Prerequisite: lp:~gocept/landscape-client/py3-manager-shutdownmanager-aptsources
Diff against target: 108 lines (+35/-16)
3 files modified
landscape/manager/keystonetoken.py (+17/-1)
landscape/manager/tests/test_keystonetoken.py (+7/-5)
py3_ready_tests (+11/-10)
To merge this branch: bzr merge lp:~gocept/landscape-client/py3-manager-keystonetoken
Reviewer Review Type Date Requested Status
Eric Snow (community) Approve
Michael Howitz (community) Approve
Alberto Donato (community) Approve
🤖 Landscape Builder test results Approve
Review via email: mp+321258@code.launchpad.net

Commit message

This is the Py3 port of landscape.manager.keystonetoken.

All the changes fix bytes/unicode issues. Notably, ConfigParser in Python 3 is constrained to unicode, even for values. So we have to dance around that using surrogateescape.

Description of the change

This MP finalized the port of landscape.manager to Python 3.

In landscape.manager.keystonetoken, a potentially binary token is read from a config file. This file would be decoded with utf-8 per default in Python 3, which needs the "surrogateescape" error handler to work properly. Consequently, we have to read in the file and decode it manually.

To post a comment you must log in.
Revision history for this message
🤖 Landscape Builder (landscape-builder) :
review: Abstain (executing tests)
Revision history for this message
🤖 Landscape Builder (landscape-builder) wrote :

Command: TRIAL_ARGS=-j4 make ci-check
Result: Success
Revno: 987
Branch: lp:~gocept/landscape-client/py3-manager-keystonetoken
Jenkins: https://ci.lscape.net/job/latch-test-xenial/3810/

review: Approve (test results)
Revision history for this message
Alberto Donato (ack) wrote :

+1

review: Approve
Revision history for this message
Steffen Allner (sallner) wrote :

Alberto, thanks for the review.

Revision history for this message
Michael Howitz (mh-gocept) wrote :

LGTM.

review: Approve
Revision history for this message
Eric Snow (ericsnowcurrently) wrote :

LGTM

It wasn't clear at first why we need to do the whole read_binary_file() dance. However, I figured out that ConfigParser is strictly unicode-based in py3, even for values. :/

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/manager/keystonetoken.py'
2--- landscape/manager/keystonetoken.py 2017-01-23 12:14:33 +0000
3+++ landscape/manager/keystonetoken.py 2017-03-29 09:23:27 +0000
4@@ -1,9 +1,12 @@
5 import os
6 import logging
7
8+from twisted.python.compat import _PY3
9+
10 from landscape.compat import ConfigParser, NoOptionError
11 from landscape.monitor.plugin import DataWatcher
12 from landscape.lib.persist import Persist
13+from landscape.lib.fs import read_binary_file
14
15
16 KEYSTONE_CONFIG_FILE = "/etc/keystone/keystone.conf"
17@@ -47,11 +50,24 @@
18 return None
19
20 config = ConfigParser()
21- config.read(self._keystone_config_file)
22+ if _PY3:
23+ # We need to use the surrogateescape error handler as the
24+ # admin_token my contain arbitrary bytes. The ConfigParser in
25+ # Python 2 on the other hand does not support read_string.
26+ config_str = read_binary_file(
27+ self._keystone_config_file).decode("utf-8", "surrogateescape")
28+ config.read_string(config_str)
29+ else:
30+ config.read(self._keystone_config_file)
31 try:
32 admin_token = config.get("DEFAULT", "admin_token")
33 except NoOptionError:
34 logging.error("KeystoneToken: No admin_token found in %s"
35 % (self._keystone_config_file))
36 return None
37+ # There is no support for surrogateescape in Python 2, but we actually
38+ # have bytes in this case anyway.
39+ if _PY3:
40+ admin_token = admin_token.encode("utf-8", "surrogateescape")
41+
42 return admin_token
43
44=== modified file 'landscape/manager/tests/test_keystonetoken.py'
45--- landscape/manager/tests/test_keystonetoken.py 2013-07-15 09:19:02 +0000
46+++ landscape/manager/tests/test_keystonetoken.py 2017-03-29 09:23:27 +0000
47@@ -46,17 +46,19 @@
48 self.makeFile(
49 path=self.keystone_file,
50 content="[DEFAULT]\nadmin_token = foobar")
51- self.assertEqual("foobar", self.plugin.get_data())
52+ # As we allow arbitrary bytes, we also need bytes here.
53+ self.assertEqual(b"foobar", self.plugin.get_data())
54
55 def test_get_keystone_token_non_utf8(self):
56 """
57 The data can be arbitrary bytes.
58 """
59- content = "[DEFAULT]\nadmin_token = \xff"
60+ content = b"[DEFAULT]\nadmin_token = \xff"
61 self.makeFile(
62 path=self.keystone_file,
63- content=content)
64- self.assertEqual("\xff", self.plugin.get_data())
65+ content=content,
66+ mode="wb")
67+ self.assertEqual(b"\xff", self.plugin.get_data())
68
69 def test_get_message(self):
70 """
71@@ -69,7 +71,7 @@
72 self.plugin.register(self.manager)
73 message = self.plugin.get_message()
74 self.assertEqual(
75- {'type': 'keystone-token', 'data': 'foobar'},
76+ {'type': 'keystone-token', 'data': b'foobar'},
77 message)
78 message = self.plugin.get_message()
79 self.assertIs(None, message)
80
81=== modified file 'py3_ready_tests'
82--- py3_ready_tests 2017-03-29 09:23:27 +0000
83+++ py3_ready_tests 2017-03-29 09:23:27 +0000
84@@ -3,13 +3,14 @@
85 landscape.user.tests
86 landscape.broker.tests
87 landscape.package.tests
88-landscape.tests.test_configuration
89-landscape.tests.test_watchdog
90-landscape.tests.test_reactor
91-landscape.tests.test_schema
92-
93-landscape.manager.tests.test_scriptexecution
94-landscape.manager.tests.test_customgraph
95-landscape.manager.tests.test_usermanager
96-landscape.manager.tests.test_shutdownmanager
97-landscape.manager.tests.test_aptsources
98+landscape.manager.tests
99+landscape.tests
100+
101+
102+
103+
104+
105+
106+
107+
108+

Subscribers

People subscribed via source and target branches

to all changes: