Set image_url as filename
[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.create_image.OpenStackImage.create',
64                         return_value=OpenStackImage(self.os_creds, None)), \
65                 mock.patch(
66                     'snaps.openstack.create_network.OpenStackNetwork.create',
67                     return_value=OpenStackNetwork(
68                         self.os_creds, NetworkConfig(name='foo'))), \
69                 mock.patch(
70                     'snaps.openstack.create_router.OpenStackRouter.create',
71                     return_value=OpenStackRouter(
72                         self.os_creds, RouterConfig(name='foo'))), \
73                 mock.patch('snaps.openstack.utils.deploy_utils.'
74                            'create_vm_instance',
75                            return_value=OpenStackVmInstance(
76                                self.os_creds,
77                                VmInstanceConfig(
78                                    name='foo', flavor='bar',
79                                    port_settings=[PortConfig(
80                                        name='foo', network_name='bar')]),
81                                None)), \
82                 mock.patch('snaps.openstack.create_instance.'
83                            'OpenStackVmInstance.get_console_output',
84                            return_value='vPing OK'):
85             self.assertEquals(
86                 testcase.TestCase.EX_OK, self.vping_userdata.run())
87
88
89 class VPingSSHTesting(unittest.TestCase):
90     """
91     Ensures the VPingUserdata class can run in Functest. This test does not
92     actually connect with an OpenStack pod.
93     """
94
95     def setUp(self):
96         self.os_creds = OSCreds(
97             username='user', password='pass',
98             auth_url='http://foo.com:5000/v3', project_name='bar')
99
100         self.vping_ssh = vping_ssh.VPingSSH(
101             os_creds=self.os_creds)
102
103     @mock.patch('snaps.openstack.utils.deploy_utils.create_vm_instance')
104     @mock.patch('os.path.exists', return_value=True)
105     @mock.patch('snaps.openstack.utils.keystone_utils.keystone_client')
106     @mock.patch('snaps.openstack.utils.keystone_utils.get_project')
107     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create',
108                 return_value=None)
109     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
110                 'get_port_ip', return_value='10.0.0.1')
111     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
112                 'vm_active', return_value=True)
113     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
114                 'vm_ssh_active', return_value=True)
115     @mock.patch('snaps.openstack.create_instance.OpenStackVmInstance.'
116                 'ssh_client', return_value=True)
117     @mock.patch('scp.SCPClient')
118     @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.'
119                 'VPingSSH._transfer_ping_script', return_value=True)
120     @mock.patch('functest.opnfv_tests.openstack.vping.vping_ssh.'
121                 'VPingSSH._do_vping_ssh', return_value=testcase.TestCase.EX_OK)
122     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
123                 'get_ext_net_name', return_value='foo')
124     def test_vping_ssh(self, *args):
125         # pylint: disable=unused-argument
126         os_vm_inst = mock.MagicMock(name='get_console_output')
127         os_vm_inst.get_console_output.return_value = 'vPing OK'
128         ssh_client = mock.MagicMock(name='get_transport')
129         ssh_client.get_transport.return_value = None
130         scp_client = mock.MagicMock(name='put')
131         scp_client.put.return_value = None
132
133         subnet_config = SubnetConfig(
134             name='bar',
135             cidr='10.0.0.1/24',
136             dns_nameservers=[env.get('NAMESERVER')])
137
138         with mock.patch('snaps.openstack.create_image.OpenStackImage.create',
139                         return_value=OpenStackImage(self.os_creds, None)), \
140                 mock.patch(
141                     'snaps.openstack.create_network.OpenStackNetwork.create',
142                     return_value=OpenStackNetwork(
143                         self.os_creds,
144                         NetworkConfig(
145                             name='foo',
146                             subnet_settings=[subnet_config]))), \
147                 mock.patch('snaps.openstack.utils.deploy_utils.'
148                            'create_vm_instance',
149                            return_value=OpenStackVmInstance(
150                                self.os_creds,
151                                VmInstanceConfig(
152                                    name='foo', flavor='bar',
153                                    port_settings=[PortConfig(
154                                        name='foo', network_name='bar')]),
155                                None)), \
156                 mock.patch('snaps.openstack.utils.deploy_utils.create_keypair',
157                            return_value=OpenStackKeypair(
158                                self.os_creds, KeypairConfig(name='foo'))), \
159                 mock.patch(
160                     'snaps.openstack.create_router.OpenStackRouter.create',
161                     return_value=OpenStackRouter(
162                         self.os_creds, RouterConfig(name='foo'))), \
163                 mock.patch('snaps.openstack.utils.deploy_utils.'
164                            'create_security_group',
165                            return_value=OpenStackSecurityGroup(
166                                self.os_creds,
167                                SecurityGroupConfig(name='foo'))), \
168                 mock.patch('snaps.openstack.create_instance.'
169                            'OpenStackVmInstance.'
170                            'get_vm_inst', return_value=os_vm_inst), \
171                 mock.patch('snaps.openstack.create_instance.'
172                            'OpenStackVmInstance.'
173                            'ssh_client', return_value=ssh_client):
174             self.assertEquals(testcase.TestCase.EX_OK, self.vping_ssh.run())
175
176
177 if __name__ == "__main__":
178     logging.disable(logging.CRITICAL)
179     unittest.main(verbosity=2)