Skip to content
This repository was archived by the owner on May 9, 2020. It is now read-only.

Commit 6df0fe3

Browse files
authored
Merge pull request #63 from dustinbrown/master
fixed python3 bug in ChefAPI.from_config_file
2 parents a52b46d + e6eeb5f commit 6df0fe3

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

chef/api.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,16 @@ def _ruby_value(match):
136136
url = key_path = client_name = None
137137
proc = subprocess.Popen('ruby', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
138138
script = config_ruby_script % path.replace('\\', '\\\\').replace("'", "\\'")
139-
out, err = proc.communicate(script)
139+
out, err = proc.communicate(script.encode())
140140
if proc.returncode == 0 and out.strip():
141-
data = json.loads(out)
141+
data = json.loads(out.decode())
142142
log.debug('Ruby parse succeeded with %r', data)
143143
url = data.get('chef_server_url')
144144
client_name = data.get('node_name')
145145
key_path = data.get('client_key')
146+
if key_path and not os.path.isabs(key_path):
147+
# Relative paths are relative to the config file
148+
key_path = os.path.abspath(os.path.join(os.path.dirname(path), key_path))
146149
else:
147150
log.debug('Ruby parse failed with exit code %s: %s', proc.returncode, out.strip())
148151
if not url:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
key_name = 'client'
2+
chef_server_url "#{url}"
3+
client_key "../#{key_name}.pem"
4+
# Use both kind of quotes, also a comment for testing
5+
node_name "test_1"
6+
# test multiple line values
7+
client_name = {
8+
'dev' =>'test_1'
9+
}['dev']

chef/tests/test_api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import os
22

3+
import mock
34
import unittest2
45

56
from chef.api import ChefAPI
67

8+
79
class APITestCase(unittest2.TestCase):
810
def load(self, path):
911
path = os.path.join(os.path.dirname(__file__), 'configs', path)
1012
return ChefAPI.from_config_file(path)
1113

14+
@mock.patch('chef.api.subprocess.Popen')
15+
def test_config_with_interpolated_settings(self, mock_subproc_popen):
16+
process_mock = mock.Mock()
17+
output = b'{"chef_server_url": "http:///chef:4000", "client_key": "../client.pem",' \
18+
b'"node_name": "test_1"}'
19+
attrs = {
20+
'communicate.return_value': (output, 'error'),
21+
'returncode': 0}
22+
process_mock.configure_mock(**attrs)
23+
mock_subproc_popen.return_value = process_mock
24+
25+
api = self.load('basic_with_interpolated_values.rb')
26+
self.assertEqual(api.client, 'test_1')
27+
1228
def test_basic(self):
1329
api = self.load('basic.rb')
1430
self.assertEqual(api.url, 'http://chef:4000')

0 commit comments

Comments
 (0)