Skip to content

Commit 31001fe

Browse files
author
John Duarte
committed
ASC-1627 Add get_cinder_major_version helper
This commit adds a helper method called `get_cinder_major_version` that returns the major version of cinder as inspected on the utility container of an RPC-O installation. Supporting tests are included as well. This helper makes use of the [packaging](https://packaging.pypa.io/en/latest/) python module. So, the requirements and constraints files are updated with this dependency.
1 parent 56a974a commit 31001fe

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

constraints.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
pip<10.0.0
2-
pytest~=3.6
1+
packaging~=18.0
2+
pip<10.0.0
3+
pytest~=3.6

pytest_rpc/helpers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pprint import pformat
1010
from platform import system
1111
from subprocess import call
12+
from packaging.version import Version, InvalidVersion
1213

1314

1415
# ==============================================================================
@@ -252,3 +253,26 @@ def parse_swift_ring_builder(ring_builder_output):
252253
swift_data[k] = float(v)
253254

254255
return swift_data
256+
257+
258+
def get_cinder_major_version(run_on_host):
259+
""" Retrieve cinder version number from utility container
260+
261+
Args:
262+
run_on_host (testinfra.host.Host): Testinfra host fixture
263+
264+
Return:
265+
int: cinder major version, -1 if lookup fails
266+
267+
"""
268+
269+
cmd = '. openrc ; cinder --version'
270+
result = run_on_container(cmd, 'utility', run_on_host)
271+
272+
try:
273+
v = Version(result.stdout)
274+
major = v.release[0]
275+
except (InvalidVersion):
276+
major = -1
277+
278+
return major

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ bumpversion
33
configparser
44
flake8
55
ipython
6+
packaging
67
paramiko
78
pytest
89
pytest-cov
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest_rpc.helpers
3+
import testinfra.backend.base
4+
import testinfra.host
5+
6+
"""Test cases for the 'get_cinder_major_version' helper function."""
7+
8+
9+
def test_valid_version(mocker):
10+
"""Verify get_cinder_major_version returns the major version when the
11+
cinder version is set to a valid semantic version.
12+
13+
relies on mocked objects from testinfra
14+
"""
15+
16+
fake_backend = mocker.Mock(spec=testinfra.backend.base.BaseBackend)
17+
myhost = testinfra.host.Host(fake_backend)
18+
cr = mocker.Mock(spec=testinfra.backend.base.CommandResult)
19+
20+
cr.rc = 0
21+
cr.stdout = '3.2.1'
22+
mocker.patch('testinfra.host.Host.run', return_value=cr)
23+
24+
assert pytest_rpc.helpers.get_cinder_major_version(myhost) == 3
25+
26+
27+
def test_invalid_version(mocker):
28+
"""Verify get_cinder_major_version returns -1 when the cinder version is
29+
set to an invalid semantic version.
30+
31+
relies on mocked objects from testinfra
32+
"""
33+
34+
fake_backend = mocker.Mock(spec=testinfra.backend.base.BaseBackend)
35+
myhost = testinfra.host.Host(fake_backend)
36+
cr = mocker.Mock(spec=testinfra.backend.base.CommandResult)
37+
38+
cr.rc = 0
39+
cr.stdout = 'foobar'
40+
mocker.patch('testinfra.host.Host.run', return_value=cr)
41+
42+
assert pytest_rpc.helpers.get_cinder_major_version(myhost) == -1
43+
44+
45+
def test_error(mocker):
46+
"""Verify get_cinder_major_version returns -1 when the query for the
47+
cinder version results in an error.
48+
49+
relies on mocked objects from testinfra
50+
"""
51+
52+
fake_backend = mocker.Mock(spec=testinfra.backend.base.BaseBackend)
53+
myhost = testinfra.host.Host(fake_backend)
54+
cr = mocker.Mock(spec=testinfra.backend.base.CommandResult)
55+
56+
cr.rc = 1
57+
cr.stdout = ''
58+
mocker.patch('testinfra.host.Host.run', return_value=cr)
59+
60+
assert pytest_rpc.helpers.get_cinder_major_version(myhost) == -1

0 commit comments

Comments
 (0)