From 499ec3235433cbdefabcc86930d08ec94426c9ea Mon Sep 17 00:00:00 2001 From: Bulat Gaifullin Date: Thu, 14 Apr 2016 11:52:05 +0300 Subject: [PATCH] Fixed building patch for saving deployment info Since 9.0 the deployment info is serialized only per node. Change-Id: I15dc8d8d7685043b33ae891380dc1de2002e1139 Closes-Bug: 1570234 --- fuelclient/objects/environment.py | 13 +- fuelclient/tests/functional/v1/test_client.py | 12 +- .../tests/unit/common/test_environments.py | 127 ++++++++++++++++++ 3 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 fuelclient/tests/unit/common/test_environments.py diff --git a/fuelclient/objects/environment.py b/fuelclient/objects/environment.py index 9202e8b7..fc3745be 100644 --- a/fuelclient/objects/environment.py +++ b/fuelclient/objects/environment.py @@ -356,13 +356,20 @@ class Environment(BaseObject): (serializer or self.serializer).write_to_path( engine_file_path, facts["engine"]) facts = facts["nodes"] - name_template = u"{name}" + + def name_builder(fact): + return fact['name'] else: - name_template = "{role}_{uid}" + def name_builder(fact): + if 'role' in fact: + # from 9.0 the deployment info is serialized only per node + return "{role}_{uid}".format(**fact) + return fact['uid'] + for _fact in facts: fact_path = os.path.join( dir_name, - name_template.format(**_fact) + name_builder(_fact) ) (serializer or self.serializer).write_to_path(fact_path, _fact) return dir_name diff --git a/fuelclient/tests/functional/v1/test_client.py b/fuelclient/tests/functional/v1/test_client.py index a7affe94..a1fc45ce 100644 --- a/fuelclient/tests/functional/v1/test_client.py +++ b/fuelclient/tests/functional/v1/test_client.py @@ -322,9 +322,9 @@ class TestFiles(base.CLIv1TestCase): "--env 1 deployment --default", ( "deployment_1", - "deployment_1/primary-controller_1.yaml", - "deployment_1/compute_2.yaml", - "deployment_1/compute_3.yaml" + "deployment_1/1.yaml", + "deployment_1/2.yaml", + "deployment_1/3.yaml" ) ), ( @@ -340,9 +340,9 @@ class TestFiles(base.CLIv1TestCase): ( "--env 1 deployment --default --json", ( - "deployment_1/primary-controller_1.json", - "deployment_1/compute_2.json", - "deployment_1/compute_3.json" + "deployment_1/1.json", + "deployment_1/2.json", + "deployment_1/3.json" ) ), ( diff --git a/fuelclient/tests/unit/common/test_environments.py b/fuelclient/tests/unit/common/test_environments.py new file mode 100644 index 00000000..da319639 --- /dev/null +++ b/fuelclient/tests/unit/common/test_environments.py @@ -0,0 +1,127 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2016 Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# 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 os + +from fuelclient import objects + +from fuelclient.tests.unit.v1 import base + + +class TestEnvironmentObject(base.UnitTestCase): + + def setUp(self): + super(TestEnvironmentObject, self).setUp() + self.env_object = objects.Environment(1) + + def _setup_os_mock(self, os_mock): + os_mock.path.exists.return_value = False + os_mock.path.join = os.path.join + os_mock.path.abspath = lambda x: x + + @mock.patch("fuelclient.objects.environment.os") + def test_write_facts_to_dir_for_legacy_envs(self, os_mock): + facts = [ + { + "uid": "1", + "role": "controller", + "data": "data1" + }, + { + "uid": "2", + "role": "compute", + "data": "data2" + }, + ] + + self._setup_os_mock(os_mock) + serializer = mock.MagicMock() + + self.env_object.write_facts_to_dir( + "deployment", facts, serializer=serializer + ) + + serializer.write_to_path.assert_has_calls( + [ + mock.call("./deployment_1/controller_1", facts[0]), + mock.call("./deployment_1/compute_2", facts[1]) + ] + ) + + @mock.patch("fuelclient.objects.environment.os") + def test_write_facts_to_dir_for_new_envs(self, os_mock): + facts = [ + { + "uid": "1", + "roles": ["controller"], + "data": "data1" + }, + { + "uid": "2", + "roles": ["compute"], + "data": "data2" + }, + ] + + self._setup_os_mock(os_mock) + serializer = mock.MagicMock() + + self.env_object.write_facts_to_dir( + "deployment", facts, serializer=serializer + ) + + serializer.write_to_path.assert_has_calls( + [ + mock.call("./deployment_1/1", facts[0]), + mock.call("./deployment_1/2", facts[1]) + ] + ) + + @mock.patch("fuelclient.objects.environment.os") + def test_write_facts_to_dir_if_facts_is_dict(self, os_mock): + facts = { + "engine": "test_engine", + "nodes": [ + { + "uid": "1", + "name": "node-1", + "roles": ["controller"], + "data": "data1" + }, + { + "uid": "2", + "name": "node-2", + "roles": ["compute"], + "data": "data2" + }, + ] + } + + self._setup_os_mock(os_mock) + serializer = mock.MagicMock() + + self.env_object.write_facts_to_dir( + "deployment", facts, serializer=serializer + ) + + serializer.write_to_path.assert_has_calls( + [ + mock.call("./deployment_1/engine", facts['engine']), + mock.call("./deployment_1/node-1", facts['nodes'][0]), + mock.call("./deployment_1/node-2", facts['nodes'][1]) + ] + )