Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing output parsing for pvesh 5.3 #68

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions proxmoxer/backends/base_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 13 additions & 12 deletions tests/base/base_ssh_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/openssh_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/paramiko_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down