From 9c5468b5c14730b2e72fac736115eb2184a402c6 Mon Sep 17 00:00:00 2001 From: "wu.zhihui" Date: Sat, 8 Oct 2016 16:16:33 +0800 Subject: code refactor: create_zones.py 1. simply the process of creating aggregates in create_zones.py. 2. before creating aggregates, it will clean all aggregates firstly. TODO: It should clean up the created aggregates. It needs a big structure modification in args_handler.py. Let's do it step by step. Change-Id: I31e09e917ba83d4676f7f95d9f5186bce8d2a449 Signed-off-by: wu.zhihui --- tests/create_zones_test.py | 58 +++++++++++++++++++++++++++++++--------------- tests/spawn_vm_test.py | 2 +- 2 files changed, 40 insertions(+), 20 deletions(-) (limited to 'tests') diff --git a/tests/create_zones_test.py b/tests/create_zones_test.py index e431a907..39e71c17 100644 --- a/tests/create_zones_test.py +++ b/tests/create_zones_test.py @@ -2,7 +2,7 @@ import pytest import mock from mock import Mock, MagicMock import os -from func.create_zones import create_zones +from func.create_zones import AvailabilityZone return_list = [] @@ -22,7 +22,7 @@ class HyperMock(MagicMock): class AggMock(MagicMock): def get_details(self, agg_id): - print "get_detail:{0}".format(agg_id) + print "get_details:{0}".format(agg_id) return Mock(hosts=[]) def create(self, host, agg): @@ -41,6 +41,7 @@ class AggMock(MagicMock): pass def remove_host(self, agg_id, host): + print "remove_host:{0}:{1}".format(agg_id, host) pass @@ -51,29 +52,48 @@ class NovaMock(MagicMock): class TestClass: @pytest.mark.parametrize("test_input, expected", [ - ([[], ['compute1', 'compute2']], - ['create:10.20.0.4:compute1', + (['compute1', 'compute2'], + ['create:compute1:compute1', 'add_host:compute1:10.20.0.4', - 'create:10.20.0.5:compute2', + 'create:compute2:compute2', 'add_host:compute2:10.20.0.5']), - ([[get_agg_mock('10.20.0.4'), get_agg_mock('10.20.0.5')], ['compute1', 'compute2']], - ['delete:10.20.0.4', - 'create:10.20.0.4:compute1', - 'get_detail:10.20.0.4', - 'add_host:10.20.0.4:10.20.0.4', - 'delete:10.20.0.5', - 'create:10.20.0.5:compute2', - 'get_detail:10.20.0.5', - 'add_host:10.20.0.5:10.20.0.5']), - ([[], ['compute1', 'compute5']], - ['The specified compute node doesnt exist. using compute 1']) + (['compute1', 'compute1'], + ['create:compute1:compute1', + 'add_host:compute1:10.20.0.4']), ]) @mock.patch('func.create_zones.client', autospec=True) @mock.patch('func.create_zones.v2', autospec=True) @mock.patch('func.create_zones.session') def test_create_zones_success(self, mock_keystone_session, mock_keystone_v2, mock_nova_client, test_input, expected, capfd): + nova_obj = NovaMock() + mock_nova_client.Client.return_value = nova_obj() + k = mock.patch.dict(os.environ, {'OS_AUTH_URL': 'http://172.10.0.5:5000', + 'OS_USERNAME': 'admin', + 'OS_PASSWORD': 'admin', + 'OS_TENANT_NAME': 'admin'}) + k.start() + azone = AvailabilityZone() + azone.create_agg(test_input) + k.stop() + resout, reserr = capfd.readouterr() + for x in expected: + assert x in resout + + @pytest.mark.parametrize("test_input, expected", [ + ([get_agg_mock('10.20.0.4'), get_agg_mock('10.20.0.5')], + ['get_details:10.20.0.4', + 'delete:10.20.0.4', + 'get_details:10.20.0.5', + 'delete:10.20.0.5']), + ([], + []), + ]) + @mock.patch('func.create_zones.client', autospec=True) + @mock.patch('func.create_zones.v2', autospec=True) + @mock.patch('func.create_zones.session') + def test_clean_all_aggregates(self, mock_keystone_session, mock_keystone_v2, mock_nova_client, test_input, expected, capfd): global return_list - return_list = test_input[0] + return_list = test_input nova_obj = NovaMock() mock_nova_client.Client.return_value = nova_obj() k = mock.patch.dict(os.environ, {'OS_AUTH_URL': 'http://172.10.0.5:5000', @@ -81,8 +101,8 @@ class TestClass: 'OS_PASSWORD': 'admin', 'OS_TENANT_NAME': 'admin'}) k.start() - create = create_zones() - create.create_agg(test_input[1]) + azone = AvailabilityZone() + azone.clean_all_aggregates() k.stop() resout, reserr = capfd.readouterr() for x in expected: diff --git a/tests/spawn_vm_test.py b/tests/spawn_vm_test.py index 0ec5c902..7890abd1 100644 --- a/tests/spawn_vm_test.py +++ b/tests/spawn_vm_test.py @@ -43,7 +43,7 @@ class TestClass: [('172.10.0.154', '')]), ]) @mock.patch('func.spawn_vm.Env_setup') - @mock.patch('func.spawn_vm.create_zones') + @mock.patch('func.spawn_vm.AvailabilityZone') @mock.patch('func.spawn_vm.client', autospec=True) @mock.patch('func.spawn_vm.keystoneclient.v2_0', autospec=True) @mock.patch('func.spawn_vm.heatclient.client', autospec=True) -- cgit 1.2.3-korg