diff options
Diffstat (limited to 'functest/tests/unit/openstack/vping')
-rw-r--r-- | functest/tests/unit/openstack/vping/__init__.py | 0 | ||||
-rw-r--r-- | functest/tests/unit/openstack/vping/test_vping.py | 159 | ||||
-rw-r--r-- | functest/tests/unit/openstack/vping/test_vping_ssh.py | 147 |
3 files changed, 147 insertions, 159 deletions
diff --git a/functest/tests/unit/openstack/vping/__init__.py b/functest/tests/unit/openstack/vping/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/functest/tests/unit/openstack/vping/__init__.py diff --git a/functest/tests/unit/openstack/vping/test_vping.py b/functest/tests/unit/openstack/vping/test_vping.py deleted file mode 100644 index dbfb679f2..000000000 --- a/functest/tests/unit/openstack/vping/test_vping.py +++ /dev/null @@ -1,159 +0,0 @@ -# Copyright (c) 2017 Cable Television Laboratories, Inc. and others. -# -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# -# http://www.apache.org/licenses/LICENSE-2.0 - -import unittest - -import mock - -from snaps.config.keypair import KeypairConfig -from snaps.config.network import NetworkConfig, PortConfig, SubnetConfig -from snaps.config.router import RouterConfig -from snaps.config.security_group import SecurityGroupConfig -from snaps.config.vm_inst import VmInstanceConfig - -from snaps.openstack.create_image import OpenStackImage -from snaps.openstack.create_instance import OpenStackVmInstance -from snaps.openstack.create_keypairs import OpenStackKeypair -from snaps.openstack.create_network import OpenStackNetwork -from snaps.openstack.create_router import OpenStackRouter -from snaps.openstack.create_security_group import OpenStackSecurityGroup - -from snaps.openstack.os_credentials import OSCreds - -from functest.core.testcase import TestCase -from functest.opnfv_tests.openstack.vping import vping_userdata, vping_ssh - - -class VPingUserdataTesting(unittest.TestCase): - """ - Ensures the VPingUserdata class can run in Functest. This test does not - actually connect with an OpenStack pod. - """ - - def setUp(self): - self.os_creds = OSCreds( - username='user', password='pass', - auth_url='http://foo.com:5000/v3', project_name='bar') - - self.vping_userdata = vping_userdata.VPingUserdata( - os_creds=self.os_creds) - - @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance') - @mock.patch('functest.opnfv_tests.openstack.vping.vping_base.os.' - 'path.exists', return_value=True) - @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create', - return_value=None) - @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.' - 'get_port_ip', return_value='10.0.0.1') - @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.' - 'vm_active', return_value=True) - def test_vping_userdata(self, deploy_vm, path_exists, create_flavor, - get_port_ip, vm_active): - with mock.patch('snaps.openstack.utils.deploy_utils.create_image', - return_value=OpenStackImage(self.os_creds, None)), \ - mock.patch('snaps.openstack.utils.deploy_utils.create_network', - return_value=OpenStackNetwork( - self.os_creds, NetworkConfig(name='foo'))), \ - mock.patch('snaps.openstack.utils.deploy_utils.' - 'create_vm_instance', - return_value=OpenStackVmInstance( - self.os_creds, - VmInstanceConfig( - name='foo', flavor='bar', - port_settings=[PortConfig( - name='foo', network_name='bar')]), - None)), \ - mock.patch('snaps.openstack.create_instance.' - 'OpenStackVmInstance.get_console_output', - return_value='vPing OK'): - self.assertEquals(TestCase.EX_OK, self.vping_userdata.run()) - - -class VPingSSHTesting(unittest.TestCase): - """ - Ensures the VPingUserdata class can run in Functest. This test does not - actually connect with an OpenStack pod. - """ - - def setUp(self): - self.os_creds = OSCreds( - username='user', password='pass', - auth_url='http://foo.com:5000/v3', project_name='bar') - - self.vping_ssh = vping_ssh.VPingSSH( - os_creds=self.os_creds) - - @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance') - @mock.patch('functest.opnfv_tests.openstack.vping.vping_base.os.' - 'path.exists', return_value=True) - @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create', - return_value=None) - @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.' - 'get_port_ip', return_value='10.0.0.1') - @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.' - 'vm_active', return_value=True) - @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.' - 'vm_ssh_active', return_value=True) - @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.' - 'ssh_client', return_value=True) - @mock.patch('scp.SCPClient') - @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.' - 'VPingSSH._transfer_ping_script', return_value=True) - @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.' - 'VPingSSH._do_vping_ssh', return_value=TestCase.EX_OK) - @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.' - 'get_ext_net_name', return_value='foo') - def test_vping_ssh(self, create_vm, path_exists, - flavor_create, get_port_ip, vm_active, ssh_active, - ssh_client, scp_client, trans_script, do_vping_ssh, - ext_net_name): - os_vm_inst = mock.MagicMock(name='get_console_output') - os_vm_inst.get_console_output.return_value = 'vPing OK' - ssh_client = mock.MagicMock(name='get_transport') - ssh_client.get_transport.return_value = None - scp_client = mock.MagicMock(name='put') - scp_client.put.return_value = None - - with mock.patch('snaps.openstack.utils.deploy_utils.create_image', - return_value=OpenStackImage(self.os_creds, None)), \ - mock.patch('snaps.openstack.utils.deploy_utils.create_network', - return_value=OpenStackNetwork( - self.os_creds, - NetworkConfig( - name='foo', - subnet_settings=[ - SubnetConfig( - name='bar', - cidr='10.0.0.1/24')]))), \ - mock.patch('snaps.openstack.utils.deploy_utils.' - 'create_vm_instance', - return_value=OpenStackVmInstance( - self.os_creds, - VmInstanceConfig( - name='foo', flavor='bar', - port_settings=[PortConfig( - name='foo', network_name='bar')]), - None)), \ - mock.patch('snaps.openstack.utils.deploy_utils.create_keypair', - return_value=OpenStackKeypair( - self.os_creds, KeypairConfig(name='foo'))), \ - mock.patch('snaps.openstack.utils.deploy_utils.create_router', - return_value=OpenStackRouter( - self.os_creds, RouterConfig(name='foo'))), \ - mock.patch('snaps.openstack.utils.deploy_utils.' - 'create_security_group', - return_value=OpenStackSecurityGroup( - self.os_creds, - SecurityGroupConfig(name='foo'))), \ - mock.patch('snaps.openstack.create_instance.' - 'OpenStackVmInstance.' - 'get_vm_inst', return_value=os_vm_inst), \ - mock.patch('snaps.openstack.create_instance.' - 'OpenStackVmInstance.' - 'ssh_client', return_value=ssh_client): - self.assertEquals(TestCase.EX_OK, self.vping_ssh.run()) diff --git a/functest/tests/unit/openstack/vping/test_vping_ssh.py b/functest/tests/unit/openstack/vping/test_vping_ssh.py new file mode 100644 index 000000000..a07148aab --- /dev/null +++ b/functest/tests/unit/openstack/vping/test_vping_ssh.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python + +# Copyright (c) 2018 Orange and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 + +# pylint: disable=missing-docstring + +import logging +import unittest + +from paramiko import ssh_exception +import mock +import munch +import shade + +from functest.opnfv_tests.openstack.vping import vping_ssh +from functest.utils import config + + +class VpingSSHTesting(unittest.TestCase): + + def setUp(self): + with mock.patch('functest.core.singlevm.SingleVm2.__init__'): + self.vping = vping_ssh.VPingSSH() + self.vping.cloud = mock.Mock() + self.vping.case_name = 'vping' + self.vping.guid = '1' + + @mock.patch('functest.core.singlevm.SingleVm2.prepare', + side_effect=Exception) + def test_prepare_exc1(self, *args): + with self.assertRaises(Exception): + self.vping.prepare() + args[0].assert_called_once_with() + + @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.VPingSSH.' + 'boot_vm', + side_effect=Exception) + @mock.patch('functest.core.singlevm.SingleVm2.prepare') + def test_prepare_exc2(self, *args): + self.vping.sec = munch.Munch(id='foo') + with self.assertRaises(Exception): + self.vping.prepare() + args[0].assert_called_once_with() + args[1].assert_called_once_with( + f'{self.vping.case_name}-vm2_{self.vping.guid}', + security_groups=[self.vping.sec.id]) + + @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.VPingSSH.' + 'boot_vm') + @mock.patch('functest.core.singlevm.SingleVm2.prepare') + def test_prepare(self, *args): + self.vping.sec = munch.Munch(id='foo') + self.vping.prepare() + args[0].assert_called_once_with() + args[1].assert_called_once_with( + f'{self.vping.case_name}-vm2_{self.vping.guid}', + security_groups=[self.vping.sec.id]) + + @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.VPingSSH.' + 'check_regex_in_console', return_value=True) + def test_execute_exc(self, *args): + self.vping.vm2 = munch.Munch(private_v4='127.0.0.1', name='foo') + self.vping.ssh = mock.Mock() + self.vping.ssh.exec_command.side_effect = ssh_exception.SSHException + with self.assertRaises(ssh_exception.SSHException): + self.vping.execute() + self.vping.ssh.exec_command.assert_called_once_with( + f'ping -c 1 {self.vping.vm2.private_v4}') + args[0].assert_called_once_with('foo') + + @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.VPingSSH.' + 'check_regex_in_console', return_value=False) + def test_execute_exc2(self, *args): + self.vping.vm2 = munch.Munch(private_v4='127.0.0.1', name='foo') + self.vping.ssh = mock.Mock() + self.vping.execute() + self.vping.ssh.exec_command.assert_not_called() + args[0].assert_called_once_with('foo') + + def _test_execute(self, ret=0): + self.vping.vm2 = munch.Munch(private_v4='127.0.0.1', name='foo') + self.vping.ssh = mock.Mock() + stdout = mock.Mock() + stdout.channel.recv_exit_status.return_value = ret + self.vping.ssh.exec_command.return_value = (None, stdout, mock.Mock()) + with mock.patch( + 'functest.opnfv_tests.openstack.vping.vping_ssh.VPingSSH.' + 'check_regex_in_console', return_value=True) as mock_check: + self.assertEqual(self.vping.execute(), ret) + mock_check.assert_called_once_with('foo') + self.vping.ssh.exec_command.assert_called_once_with( + f'ping -c 1 {self.vping.vm2.private_v4}') + + def test_execute1(self): + self._test_execute() + + def test_execute2(self): + self._test_execute(1) + + def test_clean_exc1(self): + self.vping.cloud = None + with self.assertRaises(AssertionError): + self.vping.clean() + + def test_clean_exc2(self): + self.vping.vm2 = munch.Munch(id='vm2') + mdelete_server = self.vping.cloud.delete_server + mdelete_server.side_effect = shade.OpenStackCloudException(None) + with self.assertRaises(shade.OpenStackCloudException): + self.vping.clean() + + @mock.patch('functest.core.singlevm.SingleVm2.clean', + side_effect=Exception) + def test_clean_exc3(self, *args): + self.vping.vm2 = munch.Munch(id='vm2') + with self.assertRaises(Exception): + self.vping.clean() + self.vping.cloud.delete_server.assert_called_once_with( + self.vping.vm2, wait=True, + timeout=getattr(config.CONF, 'vping_vm_delete_timeout')) + args[0].assert_called_once_with() + + @mock.patch('functest.core.singlevm.SingleVm2.clean') + def test_clean1(self, *args): + self.vping.vm2 = None + self.vping.clean() + self.vping.cloud.delete_server.assert_not_called() + args[0].assert_called_once_with() + + @mock.patch('functest.core.singlevm.SingleVm2.clean') + def test_clean2(self, *args): + self.vping.vm2 = munch.Munch(id='vm2') + self.vping.clean() + self.vping.cloud.delete_server.assert_called_once_with( + self.vping.vm2, wait=True, + timeout=getattr(config.CONF, 'vping_vm_delete_timeout')) + args[0].assert_called_once_with() + + +if __name__ == '__main__': + logging.disable(logging.CRITICAL) + unittest.main(verbosity=2) |