Added unit tests for vping.
[functest.git] / functest / tests / unit / openstack / vping / test_vping.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. and others.
2 #
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8
9 import unittest
10
11 import mock
12
13 from snaps.openstack.create_image import OpenStackImage
14 from snaps.openstack.create_instance import OpenStackVmInstance, \
15     VmInstanceSettings
16 from snaps.openstack.create_keypairs import OpenStackKeypair, KeypairSettings
17 from snaps.openstack.create_network import OpenStackNetwork, NetworkSettings, \
18     SubnetSettings, PortSettings
19 from snaps.openstack.create_router import OpenStackRouter, RouterSettings
20 from snaps.openstack.create_security_group import OpenStackSecurityGroup, \
21     SecurityGroupSettings
22 from snaps.openstack.os_credentials import OSCreds
23
24 from functest.core.testcase import TestCase
25 from functest.opnfv_tests.openstack.vping import vping_userdata, vping_ssh
26
27
28 class VPingUserdataTesting(unittest.TestCase):
29     """
30     Ensures the VPingUserdata class can run in Functest. This test does not
31     actually connect with an OpenStack pod.
32     """
33
34     def setUp(self):
35         self.os_creds = OSCreds(
36             username='user', password='pass',
37             auth_url='http://foo.com:5000/v3', project_name='bar')
38
39         self.vping_userdata = vping_userdata.VPingUserdata(
40             os_creds=self.os_creds)
41
42     @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance')
43     @mock.patch('functest.opnfv_tests.openstack.vping.vping_base.os.'
44                 'path.exists', return_value=True)
45     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
46                 return_value=None)
47     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
48                 'get_port_ip', return_value='10.0.0.1')
49     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
50                 'vm_active', return_value=True)
51     def test_vping_userdata(self, deploy_vm, path_exists, create_flavor,
52                             get_port_ip, vm_active):
53         os_vm_inst = mock.MagicMock(name='get_console_output')
54         os_vm_inst.get_console_output.return_value = 'vPing OK'
55         with mock.patch('snaps.openstack.utils.deploy_utils.create_image',
56                         return_value=OpenStackImage(self.os_creds, None)), \
57                 mock.patch('snaps.openstack.utils.deploy_utils.create_network',
58                            return_value=OpenStackNetwork(
59                                self.os_creds, NetworkSettings(name='foo'))), \
60                 mock.patch('snaps.openstack.utils.deploy_utils.'
61                            'create_vm_instance',
62                            return_value=OpenStackVmInstance(
63                                self.os_creds,
64                                VmInstanceSettings(
65                                    name='foo', flavor='bar',
66                                    port_settings=[PortSettings(
67                                        name='foo', network_name='bar')]),
68                                None)), \
69                 mock.patch('snaps.openstack.create_instance.'
70                            'OpenStackVmInstance.get_os_vm_server_obj',
71                            return_value=os_vm_inst):
72             self.assertEquals(TestCase.EX_OK, self.vping_userdata.run())
73
74
75 class VPingSSHTesting(unittest.TestCase):
76     """
77     Ensures the VPingUserdata class can run in Functest. This test does not
78     actually connect with an OpenStack pod.
79     """
80
81     def setUp(self):
82         self.os_creds = OSCreds(
83             username='user', password='pass',
84             auth_url='http://foo.com:5000/v3', project_name='bar')
85
86         self.vping_ssh = vping_ssh.VPingSSH(
87             os_creds=self.os_creds)
88
89     @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance')
90     @mock.patch('functest.opnfv_tests.openstack.vping.vping_base.os.'
91                 'path.exists', return_value=True)
92     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
93                 return_value=None)
94     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
95                 'get_port_ip', return_value='10.0.0.1')
96     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
97                 'vm_active', return_value=True)
98     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
99                 'vm_ssh_active', return_value=True)
100     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
101                 'ssh_client', return_value=True)
102     @mock.patch('scp.SCPClient')
103     @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.'
104                 'VPingSSH._transfer_ping_script', return_value=True)
105     @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.'
106                 'VPingSSH._do_vping_ssh', return_value=TestCase.EX_OK)
107     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
108                 'get_ext_net_name', return_value='foo')
109     def test_vping_ssh(self, create_vm, path_exists,
110                        flavor_create, get_port_ip, vm_active, ssh_active,
111                        ssh_client, scp_client, trans_script, do_vping_ssh,
112                        ext_net_name):
113         os_vm_inst = mock.MagicMock(name='get_console_output')
114         os_vm_inst.get_console_output.return_value = 'vPing OK'
115         ssh_client = mock.MagicMock(name='get_transport')
116         ssh_client.get_transport.return_value = None
117         scp_client = mock.MagicMock(name='put')
118         scp_client.put.return_value = None
119
120         with mock.patch('snaps.openstack.utils.deploy_utils.create_image',
121                         return_value=OpenStackImage(self.os_creds, None)), \
122                 mock.patch('snaps.openstack.utils.deploy_utils.create_network',
123                            return_value=OpenStackNetwork(
124                                self.os_creds,
125                                NetworkSettings(
126                                    name='foo',
127                                    subnet_settings=[
128                                        SubnetSettings(
129                                            name='bar',
130                                            cidr='10.0.0.1/24')]))), \
131                 mock.patch('snaps.openstack.utils.deploy_utils.'
132                            'create_vm_instance',
133                            return_value=OpenStackVmInstance(
134                                self.os_creds,
135                                VmInstanceSettings(
136                                    name='foo', flavor='bar',
137                                    port_settings=[PortSettings(
138                                        name='foo', network_name='bar')]),
139                                None)), \
140                 mock.patch('snaps.openstack.utils.deploy_utils.create_keypair',
141                            return_value=OpenStackKeypair(
142                                self.os_creds, KeypairSettings(name='foo'))), \
143                 mock.patch('snaps.openstack.utils.deploy_utils.create_router',
144                            return_value=OpenStackRouter(
145                                self.os_creds, RouterSettings(name='foo'))), \
146                 mock.patch('snaps.openstack.utils.deploy_utils.'
147                            'create_security_group',
148                            return_value=OpenStackSecurityGroup(
149                                self.os_creds,
150                                SecurityGroupSettings(name='foo'))), \
151                 mock.patch('snaps.openstack.create_instance.'
152                            'OpenStackVmInstance.'
153                            'get_vm_inst', return_value=os_vm_inst), \
154                 mock.patch('snaps.openstack.create_instance.'
155                            'OpenStackVmInstance.'
156                            'ssh_client', return_value=ssh_client):
157             self.assertEquals(TestCase.EX_OK, self.vping_ssh.run())