Add processing for different output to clone_env function

because fuelclient may have different output on different servers
(dictionary or array)

Change-Id: I662781e324b44094f86c875aaee5474eab8ac53e
Closes-bug: 1603951
(cherry picked from commit 9c44fad4f4)
This commit is contained in:
Anastasiya 2016-07-27 16:05:27 +03:00 committed by Anastasia Balobashina
parent 5c443f2d3c
commit 8b65203203
4 changed files with 61 additions and 12 deletions

View File

@ -9,7 +9,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
import pytest
@ -127,6 +126,32 @@ ENV_SETTINGS = {
}
}
NORMALIZED_DATA = [
{"name": "test", "id": 2},
{"name": "test"},
]
@pytest.mark.parametrize("normalized_data,is_error",
zip(NORMALIZED_DATA, [False, True]))
def test_clone_env(mocker, normalized_data, is_error):
release = mock.Mock(data={'name': "14.04", 'id': 2})
mock_fuel_call = mocker.patch('octane.util.env.fuel2_env_call')
mock_json_loads = mocker.patch('json.loads')
mock_normalized = mocker.patch(
'octane.util.helpers.normalized_cliff_show_json'
)
mock_normalized.return_value = normalized_data
orig_id = 1
if not is_error:
seed_id = env_util.clone_env(orig_id, release)
assert seed_id == 2
mock_json_loads.assert_called_once_with(mock_fuel_call.return_value)
else:
with pytest.raises(Exception) as exc_info:
assert ("Couldn't find new environment ID in fuel CLI output:"
"\n{0}".format(normalized_data)) == exc_info.value.args[0]
def test_copy_vips(mock_subprocess):
env_id = -1

View File

@ -67,3 +67,21 @@ def test_iterate_parameters(source, parameters):
expected_result.append((line,) + params)
result = list(helpers.iterate_parameters(source))
assert result == expected_result
DATA = [
[{"Field": "id", "Value": 1}, {"Field": "name", "Value": "test"}],
{"name": "test", "id": 2}
]
NORMALIZED_DATA = [
{"name": "test", "id": 1},
{"name": "test", "id": 2}
]
@pytest.mark.parametrize('data,normalized_data',
zip(DATA, NORMALIZED_DATA))
def test_normalized_cliff_show_json(data, normalized_data):
res = helpers.normalized_cliff_show_json(data)
assert res == normalized_data

View File

@ -29,6 +29,7 @@ from octane.helpers import tasks as tasks_helpers
from octane.helpers import transformations
from octane import magic_consts
from octane.util import disk
from octane.util import helpers
from octane.util import ssh
from octane.util import subprocess
@ -74,17 +75,16 @@ def get_env_provision_method(env):
def clone_env(env_id, release):
LOG.info("Cloning env %s for release %s", env_id, release.data['name'])
res = fuel2_env_call(["clone", "-f", "json", str(env_id),
uuid.uuid4().hex, str(release.data['id'])],
output=True)
for kv in json.loads(res):
if kv['Field'] == 'id':
seed_id = kv['Value']
break
else:
raise Exception("Couldn't find new environment ID in fuel CLI output:"
"\n%s" % res)
return seed_id
res_json = fuel2_env_call(["clone", "-f", "json", str(env_id),
uuid.uuid4().hex, str(release.data['id'])],
output=True)
res = json.loads(res_json)
res = helpers.normalized_cliff_show_json(res)
if 'id' in res:
return res['id']
raise Exception("Couldn't find new environment ID in fuel CLI output:"
"\n%s" % res)
def clone_ips(orig_id, networks):

View File

@ -49,3 +49,9 @@ def iterate_parameters(fp):
yield line, section, parameter, value
continue
yield line, section, None, None
def normalized_cliff_show_json(data):
if isinstance(data, list):
return {i['Field']: i['Value'] for i in data}
return data