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