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