d494a795d006ef5a4949e52c281958a09bc7f05f
[functest-xtesting.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 # pylint: disable=missing-docstring
10
11 import unittest
12
13 import mock
14
15 from snaps.config.keypair import KeypairConfig
16 from snaps.config.network import NetworkConfig, PortConfig, SubnetConfig
17 from snaps.config.router import RouterConfig
18 from snaps.config.security_group import SecurityGroupConfig
19 from snaps.config.vm_inst import VmInstanceConfig
20
21 from snaps.openstack.create_image import OpenStackImage
22 from snaps.openstack.create_instance import OpenStackVmInstance
23 from snaps.openstack.create_keypairs import OpenStackKeypair
24 from snaps.openstack.create_network import OpenStackNetwork
25 from snaps.openstack.create_router import OpenStackRouter
26 from snaps.openstack.create_security_group import OpenStackSecurityGroup
27
28 from snaps.openstack.os_credentials import OSCreds
29
30 from functest.core.testcase import TestCase
31 from functest.opnfv_tests.openstack.vping import vping_userdata, vping_ssh
32
33
34 class VPingUserdataTesting(unittest.TestCase):
35     """
36     Ensures the VPingUserdata class can run in Functest. This test does not
37     actually connect with an OpenStack pod.
38     """
39
40     def setUp(self):
41         self.os_creds = OSCreds(
42             username='user', password='pass',
43             auth_url='http://foo.com:5000/v3', project_name='bar')
44
45         self.vping_userdata = vping_userdata.VPingUserdata(
46             os_creds=self.os_creds)
47
48     @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance')
49     @mock.patch('functest.opnfv_tests.openstack.vping.vping_base.os.'
50                 'path.exists', return_value=True)
51     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
52                 return_value=None)
53     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
54                 'get_port_ip', return_value='10.0.0.1')
55     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
56                 'vm_active', return_value=True)
57     def test_vping_userdata(self, deploy_vm, path_exists, create_flavor,
58                             get_port_ip, vm_active):
59         with mock.patch('snaps.openstack.utils.deploy_utils.create_image',
60                         return_value=OpenStackImage(self.os_creds, None)), \
61                 mock.patch('snaps.openstack.utils.deploy_utils.create_network',
62                            return_value=OpenStackNetwork(
63                                self.os_creds, NetworkConfig(name='foo'))), \
64                 mock.patch('snaps.openstack.utils.deploy_utils.'
65                            'create_vm_instance',
66                            return_value=OpenStackVmInstance(
67                                self.os_creds,
68                                VmInstanceConfig(
69                                    name='foo', flavor='bar',
70                                    port_settings=[PortConfig(
71                                        name='foo', network_name='bar')]),
72                                None)), \
73                 mock.patch('snaps.openstack.create_instance.'
74                            'OpenStackVmInstance.get_console_output',
75                            return_value='vPing OK'):
76             self.assertEquals(TestCase.EX_OK, self.vping_userdata.run())
77
78
79 class VPingSSHTesting(unittest.TestCase):
80     """
81     Ensures the VPingUserdata class can run in Functest. This test does not
82     actually connect with an OpenStack pod.
83     """
84
85     def setUp(self):
86         self.os_creds = OSCreds(
87             username='user', password='pass',
88             auth_url='http://foo.com:5000/v3', project_name='bar')
89
90         self.vping_ssh = vping_ssh.VPingSSH(
91             os_creds=self.os_creds)
92
93     @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance')
94     @mock.patch('functest.opnfv_tests.openstack.vping.vping_base.os.'
95                 'path.exists', return_value=True)
96     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
97                 return_value=None)
98     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
99                 'get_port_ip', return_value='10.0.0.1')
100     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
101                 'vm_active', return_value=True)
102     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
103                 'vm_ssh_active', return_value=True)
104     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
105                 'ssh_client', return_value=True)
106     @mock.patch('scp.SCPClient')
107     @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.'
108                 'VPingSSH._transfer_ping_script', return_value=True)
109     @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.'
110                 'VPingSSH._do_vping_ssh', return_value=TestCase.EX_OK)
111     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
112                 'get_ext_net_name', return_value='foo')
113     def test_vping_ssh(self, create_vm, path_exists,
114                        flavor_create, get_port_ip, vm_active, ssh_active,
115                        ssh_client, scp_client, trans_script, do_vping_ssh,
116                        ext_net_name):
117         os_vm_inst = mock.MagicMock(name='get_console_output')
118         os_vm_inst.get_console_output.return_value = 'vPing OK'
119         ssh_client = mock.MagicMock(name='get_transport')
120         ssh_client.get_transport.return_value = None
121         scp_client = mock.MagicMock(name='put')
122         scp_client.put.return_value = None
123
124         with mock.patch('snaps.openstack.utils.deploy_utils.create_image',
125                         return_value=OpenStackImage(self.os_creds, None)), \
126                 mock.patch('snaps.openstack.utils.deploy_utils.create_network',
127                            return_value=OpenStackNetwork(
128                                self.os_creds,
129                                NetworkConfig(
130                                    name='foo',
131                                    subnet_settings=[
132                                        SubnetConfig(
133                                            name='bar',
134                                            cidr='10.0.0.1/24')]))), \
135                 mock.patch('snaps.openstack.utils.deploy_utils.'
136                            'create_vm_instance',
137                            return_value=OpenStackVmInstance(
138                                self.os_creds,
139                                VmInstanceConfig(
140                                    name='foo', flavor='bar',
141                                    port_settings=[PortConfig(
142                                        name='foo', network_name='bar')]),
143                                None)), \
144                 mock.patch('snaps.openstack.utils.deploy_utils.create_keypair',
145                            return_value=OpenStackKeypair(
146                                self.os_creds, KeypairConfig(name='foo'))), \
147                 mock.patch('snaps.openstack.utils.deploy_utils.create_router',
148                            return_value=OpenStackRouter(
149                                self.os_creds, RouterConfig(name='foo'))), \
150                 mock.patch('snaps.openstack.utils.deploy_utils.'
151                            'create_security_group',
152                            return_value=OpenStackSecurityGroup(
153                                self.os_creds,
154                                SecurityGroupConfig(name='foo'))), \
155                 mock.patch('snaps.openstack.create_instance.'
156                            'OpenStackVmInstance.'
157                            'get_vm_inst', return_value=os_vm_inst), \
158                 mock.patch('snaps.openstack.create_instance.'
159                            'OpenStackVmInstance.'
160                            'ssh_client', return_value=ssh_client):
161             self.assertEquals(TestCase.EX_OK, self.vping_ssh.run())