aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/create_zones_test.py89
-rw-r--r--tests/spawn_vm_test.py71
2 files changed, 160 insertions, 0 deletions
diff --git a/tests/create_zones_test.py b/tests/create_zones_test.py
new file mode 100644
index 00000000..e431a907
--- /dev/null
+++ b/tests/create_zones_test.py
@@ -0,0 +1,89 @@
+import pytest
+import mock
+from mock import Mock, MagicMock
+import os
+from func.create_zones import create_zones
+
+return_list = []
+
+
+def get_agg_mock(host):
+ agg = Mock()
+ agg.name = host
+ agg.id = host
+ return agg
+
+
+class HyperMock(MagicMock):
+ def list(self):
+ mock_hypervisor = [Mock(service={'host': '10.20.0.4'}), Mock(service={'host': '10.20.0.5'})]
+ return mock_hypervisor
+
+
+class AggMock(MagicMock):
+ def get_details(self, agg_id):
+ print "get_detail:{0}".format(agg_id)
+ return Mock(hosts=[])
+
+ def create(self, host, agg):
+ print "create:{0}:{1}".format(host, agg)
+ return agg
+
+ def list(self):
+ return return_list
+
+ def delete(self, agg_id):
+ print "delete:{0}".format(agg_id)
+ pass
+
+ def add_host(self, aggregate, host):
+ print "add_host:{0}:{1}".format(aggregate, host)
+ pass
+
+ def remove_host(self, agg_id, host):
+ pass
+
+
+class NovaMock(MagicMock):
+ hypervisors = HyperMock()
+ aggregates = AggMock()
+
+
+class TestClass:
+ @pytest.mark.parametrize("test_input, expected", [
+ ([[], ['compute1', 'compute2']],
+ ['create:10.20.0.4:compute1',
+ 'add_host:compute1:10.20.0.4',
+ 'create:10.20.0.5: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'])
+ ])
+ @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):
+ global return_list
+ return_list = test_input[0]
+ 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()
+ create = create_zones()
+ create.create_agg(test_input[1])
+ k.stop()
+ resout, reserr = capfd.readouterr()
+ for x in expected:
+ assert x in resout
diff --git a/tests/spawn_vm_test.py b/tests/spawn_vm_test.py
new file mode 100644
index 00000000..eb843ad9
--- /dev/null
+++ b/tests/spawn_vm_test.py
@@ -0,0 +1,71 @@
+import pytest
+import mock
+from mock import Mock, MagicMock
+import os
+from func.spawn_vm import SpawnVM
+
+
+class KeystoneMock(MagicMock):
+ auth_token = Mock()
+ v2_0 = Mock()
+
+
+class ImageMock(MagicMock):
+ name = 'QTIP_CentOS'
+
+
+class ImagesMock(MagicMock):
+ def list(self):
+ return [ImageMock()]
+
+
+class StackMock(MagicMock):
+ status = 'COMPLETE'
+ outputs = [{'output_key': 'availability_instance_1',
+ 'output_value': 'output_value_1'},
+ {'output_key': 'instance_ip_1',
+ "output_value": "172.10.0.154"},
+ {"output_key": "instance_PIP_1",
+ "output_value": "10.10.17.5"},
+ {'output_key': 'KeyPair_PublicKey',
+ "output_value": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpwIBAAKCAQEAqCiHcrLBXtxG0LhnKndU7VIVpYxORmv0d4tvujkWOkYuagiW\nU/MTRk0zhRvFQDVPEs0Jrj/BIecqm6fjjT6dZ/H7JLYGaqJitR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X411dToxpXRuPaRTBFmbpvnx2Ayfpp8w+pzA62rnktApzeVFSl0fy\nH3zoLfBjcJPyG8zPwNf6HRJJsQKCAIAE2S5asTaWo+r4m/bYtmXm/eDZnfa7TI/T\nsOWELbTPNp5wjOgsgyhNaAhu7MtmesXn5cxLwohP94vhoMKMNptMD8iRPqJ471Iw\n4zW62NLGeW6AyIHes3CMPMIs+AtHoR33MkotSG5sY/jRk8+HoGoYo6/qK+l+CJ5z\neR579wR5sQKCAIAvPWq+bvcPTDKUU1Fe/Y/GyWoUA+uSqmCdORBkK38lALFGphxj\nfDz9dXskimqW+A9hOPOS8dm8YcVvi/TLXVE5Vsx9VkOg6z6AZBQpgNXGfOgpju4W\nbjER7bQaASatuWQyCxbA9oNlAUdSeOhGTxeFLkLj7hNMd6tLjfd8w7A/hA==\n-----END RSA PRIVATE KEY-----\n"}]
+
+
+class HeatMock(MagicMock):
+ def list(self):
+ return []
+
+ def get(self, stackname):
+ return StackMock()
+
+ def create(self, stack_name, template):
+ pass
+
+
+class TestClass:
+ @pytest.mark.parametrize("test_input, expected", [
+ ({'availability_zone': ['compute1', 'compute1'],
+ 'OS_image': ['QTIP_CentOS', 'QTIP_CentOS'],
+ 'public_network': ['admin-floating_net', 'admin-floating_net'],
+ 'flavor': ['m1.large', 'm1.large'],
+ 'role': ['1-server', '2-host']},
+ [('172.10.0.154', '')]),
+ ])
+ @mock.patch('func.spawn_vm.Env_setup')
+ @mock.patch('func.spawn_vm.FetchImg')
+ @mock.patch('func.spawn_vm.create_zones')
+ @mock.patch('func.spawn_vm.client', autospec=True)
+ @mock.patch('func.spawn_vm.glanceclient', autospec=True)
+ @mock.patch('func.spawn_vm.keystoneclient.v2_0', autospec=True)
+ @mock.patch('func.spawn_vm.heatclient.client', autospec=True)
+ def test_create_zones_success(self, mock_heat, mock_keystone, mock_glance,
+ mock_nova_client, mock_zone, mock_fetch,
+ mock_setup, test_input, expected):
+ mock_glance.Client.return_value = Mock(images=ImagesMock())
+ mock_nova_client.Client.return_value = Mock()
+ mock_heat.Client.return_value = Mock(stacks=HeatMock())
+ k = mock.patch.dict(os.environ, {'INSTALLER_TYPE': 'fuel'})
+ k.start()
+ SpawnVM(test_input)
+ k.stop()
+ mock_setup.ip_pw_list.append.assert_called_with(expected[0])