aboutsummaryrefslogtreecommitdiffstats
path: root/functest/tests/unit/utils/test_openstack_snapshot.py
blob: 52744db1caa8c9c15a4c3ebefb8d1b60e3e59737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python

# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

import logging
import mock
import unittest

from functest.utils import openstack_snapshot


class OSTackerTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def _get_instance(self, key):
        mock_obj = mock.Mock()
        attrs = {'id': 'id' + str(key), 'name': 'name' + str(key),
                 'ip': 'ip' + str(key)}
        mock_obj.configure_mock(**attrs)
        return mock_obj

    def setUp(self):
        self.client = mock.Mock()
        self.test_list = [self._get_instance(1), self._get_instance(2)]
        self.update_list = {'id1': 'name1', 'id2': 'name2'}
        self.update_floatingips = {'id1': 'ip1', 'id2': 'ip2'}
        self.test_dict_list = [{'id': 'id1', 'name': 'name1', 'ip': 'ip1'},
                               {'id': 'id2', 'name': 'name2', 'ip': 'ip2'}]

    @mock.patch('functest.utils.openstack_snapshot.logger.info')
    def test_separator(self, mock_logger_info):
        openstack_snapshot.separator()
        mock_logger_info.assert_called_once_with("-----------------"
                                                 "-----------------"
                                                 "---------")

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_instances(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_instances', return_value=self.test_list):
            resp = openstack_snapshot.get_instances(self.client)
            mock_logger_debug.assert_called_once_with("Getting instances...")
            self.assertDictEqual(resp, {'instances': self.update_list})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_instances_missing_instances(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_instances', return_value=[]):
            resp = openstack_snapshot.get_instances(self.client)
            mock_logger_debug.assert_called_once_with("Getting instances...")
            self.assertDictEqual(resp, {'instances': {}})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_images(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_images', return_value=self.test_list):
            resp = openstack_snapshot.get_images(self.client)
            mock_logger_debug.assert_called_once_with("Getting images...")
            self.assertDictEqual(resp, {'images': self.update_list})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_images_missing_images(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_images', return_value=[]):
            resp = openstack_snapshot.get_images(self.client)
            mock_logger_debug.assert_called_once_with("Getting images...")
            self.assertDictEqual(resp, {'images': {}})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_volumes(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_volumes', return_value=self.test_list):
            resp = openstack_snapshot.get_volumes(self.client)
            mock_logger_debug.assert_called_once_with("Getting volumes...")
            self.assertDictEqual(resp, {'volumes': self.update_list})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_volumes_missing_volumes(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_volumes', return_value=[]):
            resp = openstack_snapshot.get_volumes(self.client)
            mock_logger_debug.assert_called_once_with("Getting volumes...")
            self.assertDictEqual(resp, {'volumes': {}})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_networks(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_network_list', return_value=self.test_dict_list):
            resp = openstack_snapshot.get_networks(self.client)
            mock_logger_debug.assert_called_once_with("Getting networks")
            self.assertDictEqual(resp, {'networks': self.update_list})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_networks_missing_networks(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_network_list', return_value=[]):
            resp = openstack_snapshot.get_networks(self.client)
            mock_logger_debug.assert_called_once_with("Getting networks")
            self.assertDictEqual(resp, {'networks': {}})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_routers(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_router_list', return_value=self.test_dict_list):
            resp = openstack_snapshot.get_routers(self.client)
            mock_logger_debug.assert_called_once_with("Getting routers")
            self.assertDictEqual(resp, {'routers': self.update_list})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_routers_missing_routers(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_router_list', return_value=[]):
            resp = openstack_snapshot.get_routers(self.client)
            mock_logger_debug.assert_called_once_with("Getting routers")
            self.assertDictEqual(resp, {'routers': {}})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_secgroups(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_security_groups',
                        return_value=self.test_dict_list):
            resp = openstack_snapshot.get_security_groups(self.client)
            mock_logger_debug.assert_called_once_with("Getting Security "
                                                      "groups...")
            self.assertDictEqual(resp, {'secgroups': self.update_list})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_secgroups_missing_secgroups(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_security_groups', return_value=[]):
            resp = openstack_snapshot.get_security_groups(self.client)
            mock_logger_debug.assert_called_once_with("Getting Security "
                                                      "groups...")
            self.assertDictEqual(resp, {'secgroups': {}})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_floatingips(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_floating_ips', return_value=self.test_list):
            resp = openstack_snapshot.get_floatinips(self.client)
            mock_logger_debug.assert_called_once_with("Getting Floating "
                                                      "IPs...")
            self.assertDictEqual(resp, {'floatingips':
                                        self.update_floatingips})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_floatingips_missing_floatingips(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_floating_ips', return_value=[]):
            resp = openstack_snapshot.get_floatinips(self.client)
            mock_logger_debug.assert_called_once_with("Getting Floating "
                                                      "IPs...")
            self.assertDictEqual(resp, {'floatingips': {}})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_users(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_users', return_value=self.test_list):
            resp = openstack_snapshot.get_users(self.client)
            mock_logger_debug.assert_called_once_with("Getting users...")
            self.assertDictEqual(resp, {'users': self.update_list})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_users_missing_users(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_users', return_value=[]):
            resp = openstack_snapshot.get_users(self.client)
            mock_logger_debug.assert_called_once_with("Getting users...")
            self.assertDictEqual(resp, {'users': {}})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_tenants(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_tenants', return_value=self.test_list):
            resp = openstack_snapshot.get_tenants(self.client)
            mock_logger_debug.assert_called_once_with("Getting tenants...")
            self.assertDictEqual(resp, {'tenants': self.update_list})

    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_get_tenants_missing_tenants(self, mock_logger_debug):
        with mock.patch('functest.utils.openstack_snapshot.os_utils'
                        '.get_tenants', return_value=[]):
            resp = openstack_snapshot.get_tenants(self.client)
            mock_logger_debug.assert_called_once_with("Getting tenants...")
            self.assertDictEqual(resp, {'tenants': {}})

    @mock.patch('functest.utils.openstack_snapshot.os_utils.get_cinder_client')
    @mock.patch('functest.utils.openstack_snapshot.os_utils'
                '.get_keystone_client')
    @mock.patch('functest.utils.openstack_snapshot.os_utils'
                '.get_neutron_client')
    @mock.patch('functest.utils.openstack_snapshot.os_utils.get_nova_client')
    @mock.patch('functest.utils.openstack_snapshot.os_utils.check_credentials')
    @mock.patch('functest.utils.openstack_snapshot.logger.info')
    @mock.patch('functest.utils.openstack_snapshot.logger.debug')
    def test_main_default(self, mock_logger_debug, mock_logger_info,
                          mock_creds, mock_nova, mock_neutron,
                          mock_keystone, mock_cinder):
        with mock.patch('functest.utils.openstack_snapshot.get_instances',
                        return_value=self.update_list), \
            mock.patch('functest.utils.openstack_snapshot.get_images',
                       return_value=self.update_list), \
            mock.patch('functest.utils.openstack_snapshot.get_images',
                       return_value=self.update_list), \
            mock.patch('functest.utils.openstack_snapshot.get_volumes',
                       return_value=self.update_list), \
            mock.patch('functest.utils.openstack_snapshot.get_networks',
                       return_value=self.update_list), \
            mock.patch('functest.utils.openstack_snapshot.get_routers',
                       return_value=self.update_list), \
            mock.patch('functest.utils.openstack_snapshot.get_security_groups',
                       return_value=self.update_list), \
            mock.patch('functest.utils.openstack_snapshot.get_floatinips',
                       return_value=self.update_floatingips), \
            mock.patch('functest.utils.openstack_snapshot.get_users',
                       return_value=self.update_list), \
            mock.patch('functest.utils.openstack_snapshot.get_tenants',
                       return_value=self.update_list), \
                mock.patch('__builtin__.open', mock.mock_open()) as m:
            openstack_snapshot.main()
            mock_logger_info.assert_called_once_with("Generating OpenStack "
                                                     "snapshot...")
            m.assert_called_once_with(openstack_snapshot.OS_SNAPSHOT_FILE,
                                      'w+')
            mock_logger_debug.assert_any_call("NOTE: These objects will "
                                              "NOT be deleted after " +
                                              "running the test.")


if __name__ == "__main__":
    unittest.main(verbosity=2)