Rewrite refstack testcase
[functest.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 from snaps.config.keypair import KeypairConfig
18 from snaps.config.network import NetworkConfig, PortConfig, SubnetConfig
19 from snaps.config.router import RouterConfig
20 from snaps.config.security_group import SecurityGroupConfig
21 from snaps.config.vm_inst import VmInstanceConfig
22 from snaps.openstack.create_image import OpenStackImage
23 from snaps.openstack.create_instance import OpenStackVmInstance
24 from snaps.openstack.create_keypairs import OpenStackKeypair
25 from snaps.openstack.create_network import OpenStackNetwork
26 from snaps.openstack.create_router import OpenStackRouter
27 from snaps.openstack.create_security_group import OpenStackSecurityGroup
28 from snaps.openstack.os_credentials import OSCreds
29 from xtesting.core import testcase
30
31 from functest.opnfv_tests.openstack.vping import vping_userdata, vping_ssh
32 from functest.utils import env
33
34
35 class VPingUserdataTesting(unittest.TestCase):
36     """
37     Ensures the VPingUserdata class can run in Functest. This test does not
38     actually connect with an OpenStack pod.
39     """
40
41     def setUp(self):
42         self.os_creds = OSCreds(
43             username='user', password='pass',
44             auth_url='http://foo.com:5000/v3', project_name='bar')
45
46         self.vping_userdata = vping_userdata.VPingUserdata(
47             os_creds=self.os_creds)
48
49     @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance')
50     @mock.patch('os.path.exists', return_value=True)
51     @mock.patch('snaps.openstack.utils.keystone_utils.keystone_client')
52     @mock.patch('snaps.openstack.utils.keystone_utils.get_project')
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(
84                 testcase.TestCase.EX_OK, self.vping_userdata.run())
85
86
87 class VPingSSHTesting(unittest.TestCase):
88     """
89     Ensures the VPingUserdata class can run in Functest. This test does not
90     actually connect with an OpenStack pod.
91     """
92
93     def setUp(self):
94         self.os_creds = OSCreds(
95             username='user', password='pass',
96             auth_url='http://foo.com:5000/v3', project_name='bar')
97
98         self.vping_ssh = vping_ssh.VPingSSH(
99             os_creds=self.os_creds)
100
101     @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance')
102     @mock.patch('os.path.exists', return_value=True)
103     @mock.patch('snaps.openstack.utils.keystone_utils.keystone_client')
104     @mock.patch('snaps.openstack.utils.keystone_utils.get_project')
105     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
106                 return_value=None)
107     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
108                 'get_port_ip', return_value='10.0.0.1')
109     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
110                 'vm_active', return_value=True)
111     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
112                 'vm_ssh_active', return_value=True)
113     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
114                 'ssh_client', return_value=True)
115     @mock.patch('scp.SCPClient')
116     @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.'
117                 'VPingSSH._transfer_ping_script', return_value=True)
118     @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.'
119                 'VPingSSH._do_vping_ssh', return_value=testcase.TestCase.EX_OK)
120     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
121                 'get_ext_net_name', return_value='foo')
122     def test_vping_ssh(self, *args):
123         # pylint: disable=unused-argument
124         os_vm_inst = mock.MagicMock(name='get_console_output')
125         os_vm_inst.get_console_output.return_value = 'vPing OK'
126         ssh_client = mock.MagicMock(name='get_transport')
127         ssh_client.get_transport.return_value = None
128         scp_client = mock.MagicMock(name='put')
129         scp_client.put.return_value = None
130
131         subnet_config = SubnetConfig(
132             name='bar',
133             cidr='10.0.0.1/24',
134             dns_nameservers=[env.get('NAMESERVER')])
135
136         with mock.patch('snaps.openstack.utils.deploy_utils.create_image',
137                         return_value=OpenStackImage(self.os_creds, None)), \
138                 mock.patch('snaps.openstack.utils.deploy_utils.create_network',
139                            return_value=OpenStackNetwork(
140                                self.os_creds,
141                                NetworkConfig(
142                                    name='foo',
143                                    subnet_settings=[subnet_config]))), \
144                 mock.patch('snaps.openstack.utils.deploy_utils.'
145                            'create_vm_instance',
146                            return_value=OpenStackVmInstance(
147                                self.os_creds,
148                                VmInstanceConfig(
149                                    name='foo', flavor='bar',
150                                    port_settings=[PortConfig(
151                                        name='foo', network_name='bar')]),
152                                None)), \
153                 mock.patch('snaps.openstack.utils.deploy_utils.create_keypair',
154                            return_value=OpenStackKeypair(
155                                self.os_creds, KeypairConfig(name='foo'))), \
156                 mock.patch('snaps.openstack.utils.deploy_utils.create_router',
157                            return_value=OpenStackRouter(
158                                self.os_creds, RouterConfig(name='foo'))), \
159                 mock.patch('snaps.openstack.utils.deploy_utils.'
160                            'create_security_group',
161                            return_value=OpenStackSecurityGroup(
162                                self.os_creds,
163                                SecurityGroupConfig(name='foo'))), \
164                 mock.patch('snaps.openstack.create_instance.'
165                            'OpenStackVmInstance.'
166                            'get_vm_inst', return_value=os_vm_inst), \
167                 mock.patch('snaps.openstack.create_instance.'
168                            'OpenStackVmInstance.'
169                            'ssh_client', return_value=ssh_client):
170             self.assertEquals(testcase.TestCase.EX_OK, self.vping_ssh.run())
171
172
173 if __name__ == "__main__":
174     logging.disable(logging.CRITICAL)
175     unittest.main(verbosity=2)