diff --git a/proxmoxer/backends/base_ssh.py b/proxmoxer/backends/base_ssh.py index da624d9..59a27f4 100644 --- a/proxmoxer/backends/base_ssh.py +++ b/proxmoxer/backends/base_ssh.py @@ -41,14 +41,18 @@ def request(self, method, url, data=None, params=None, headers=None): data['tmpfilename'] = tmp_filename translated_data = ' '.join(["-{0} {1}".format(k, v) for k, v in chain(data.items(), params.items())]) - full_cmd = 'pvesh {0}'.format(' '.join(filter(None, (cmd, url, translated_data)))) + full_cmd = 'pvesh {0} --output-format json'.format(' '.join(filter(None, (cmd, url, translated_data)))) stdout, stderr = self._exec(full_cmd) match = lambda s: re.match('\d\d\d [a-zA-Z]', s) # sometimes contains extra text like 'trying to acquire lock...OK' - status_code = next( - (int(s.split()[0]) for s in stderr.splitlines() if match(s)), - 500) + if stderr: + status_code = next( + (int(s.split()[0]) for s in stderr.splitlines() if match(s)), + 500) + else: + status_code = 200 + if stdout: return Response(stdout, status_code) else: diff --git a/tests/base/base_ssh_suite.py b/tests/base/base_ssh_suite.py index 943b72f..57cd199 100644 --- a/tests/base/base_ssh_suite.py +++ b/tests/base/base_ssh_suite.py @@ -62,7 +62,7 @@ def test_get(self): } ]""") result = self.proxmox.nodes('proxmox').storage('local').get() - eq_(self._get_called_cmd(), self._called_cmd('pvesh get /nodes/proxmox/storage/local')) + eq_(self._get_called_cmd(), self._called_cmd('pvesh get /nodes/proxmox/storage/local --output-format json')) eq_(result[0]['subdir'], 'status') eq_(result[1]['subdir'], 'content') eq_(result[2]['subdir'], 'upload') @@ -71,16 +71,16 @@ def test_get(self): def test_delete(self): self.proxmox.nodes('proxmox').openvz(100).delete() - eq_(self._get_called_cmd(), self._called_cmd('pvesh delete /nodes/proxmox/openvz/100')) - self._set_stderr("200 OK") + eq_(self._get_called_cmd(), self._called_cmd('pvesh delete /nodes/proxmox/openvz/100 --output-format json')) + self._set_stderr("") self.proxmox.nodes('proxmox').openvz('101').delete() - eq_(self._get_called_cmd(), self._called_cmd('pvesh delete /nodes/proxmox/openvz/101')) - self._set_stderr("200 OK") + eq_(self._get_called_cmd(), self._called_cmd('pvesh delete /nodes/proxmox/openvz/101 --output-format json')) + self._set_stderr("") self.proxmox.nodes('proxmox').openvz.delete('102') - eq_(self._get_called_cmd(), self._called_cmd('pvesh delete /nodes/proxmox/openvz/102')) + eq_(self._get_called_cmd(), self._called_cmd('pvesh delete /nodes/proxmox/openvz/102 --output-format json')) def test_post(self): - self._set_stderr("200 OK") + self._set_stderr("") node = self.proxmox.nodes('proxmox') node.openvz.create(vmid=800, ostemplate='local:vztmpl/debian-6-turnkey-core_12.0-1_i386.tar.gz', @@ -105,7 +105,7 @@ def test_post(self): ok_('-swap 512' in options) ok_('-vmid 800' in options) - self._set_stderr("200 OK") + self._set_stderr("") node = self.proxmox.nodes('proxmox1') node.openvz.post(vmid=900, ostemplate='local:vztmpl/debian-7-turnkey-core_12.0-1_i386.tar.gz', @@ -131,7 +131,7 @@ def test_post(self): ok_('-vmid 900' in options) def test_put(self): - self._set_stderr("200 OK") + self._set_stderr("") node = self.proxmox.nodes('proxmox') node.openvz(101).config.set(cpus=4, memory=1024, ip_address='10.0.100.100', onboot=True) cmd, options = self._split_cmd(self._get_called_cmd()) @@ -141,7 +141,7 @@ def test_put(self): ok_('-onboot True' in options) ok_('-cpus 4' in options) - self._set_stderr("200 OK") + self._set_stderr("") node = self.proxmox.nodes('proxmox1') node.openvz('102').config.put(cpus=2, memory=512, ip_address='10.0.100.200', onboot=False) cmd, options = self._split_cmd(self._get_called_cmd()) @@ -156,8 +156,9 @@ def test_error(self): self._set_stderr("500 whoops") self.proxmox.nodes('proxmox').get() - def test_no_error_with_extra_output(self): - self._set_stderr("Extra output\n200 OK") + @raises(ResourceException) + def test_extra_output_error(self): + self._set_stderr("Extra output\n") self.proxmox.nodes('proxmox').get() @raises(ResourceException) diff --git a/tests/openssh_tests.py b/tests/openssh_tests.py index 34cf094..a2bfff1 100644 --- a/tests/openssh_tests.py +++ b/tests/openssh_tests.py @@ -16,7 +16,7 @@ class TestOpenSSHSuite(BaseSSHSuite): def setUp(self, _): self.proxmox = ProxmoxAPI('proxmox', user='root', backend='openssh', port=123) self.client = self.proxmox._store['session'].ssh_client - self._set_stderr('200 OK') + self._set_stderr('') self._set_stdout('') def _get_called_cmd(self): diff --git a/tests/paramiko_tests.py b/tests/paramiko_tests.py index e69ee79..274cf99 100644 --- a/tests/paramiko_tests.py +++ b/tests/paramiko_tests.py @@ -31,7 +31,7 @@ def setUp(self, _): self.proxmox = ProxmoxAPI('proxmox', user='root', backend='ssh_paramiko', port=123) self.client = self.proxmox._store['session'].ssh_client self.session = self.client.get_transport().open_session() - self._set_stderr('200 OK') + self._set_stderr('') self._set_stdout('') def _get_called_cmd(self): @@ -53,7 +53,7 @@ def setUp(self, _): self.proxmox = ProxmoxAPI('proxmox', user='root', backend='ssh_paramiko', port=123, sudo=True) self.client = self.proxmox._store['session'].ssh_client self.session = self.client.get_transport().open_session() - self._set_stderr('200 OK') + self._set_stderr('') self._set_stdout('') def _get_called_cmd(self):