f3b35e9d91048b6dea3728187bc9e1f90ed74b1e
[functest.git] / functest / opnfv_tests / openstack / api / connection_check.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2018 Orange 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 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """Verify the connection to OpenStack Services"""
11
12 import logging
13 import time
14
15 import os_client_config
16 import shade
17 from xtesting.core import testcase
18
19 from functest.utils import env
20 from functest.utils import functest_utils
21
22
23 class ConnectionCheck(testcase.TestCase):
24     """Perform simplest queries"""
25     __logger = logging.getLogger(__name__)
26
27     func_list = [
28         "get_network_extensions", "list_aggregates", "list_domains",
29         "list_endpoints", "list_floating_ip_pools", "list_floating_ips",
30         "list_hypervisors", "list_keypairs", "list_networks", "list_ports",
31         "list_role_assignments", "list_roles", "list_routers", "list_servers",
32         "list_subnets"]
33
34     def __init__(self, **kwargs):
35         if "case_name" not in kwargs:
36             kwargs["case_name"] = 'connection_check'
37         super(ConnectionCheck, self).__init__(**kwargs)
38         self.output_log_name = 'functest.log'
39         self.output_debug_log_name = 'functest.debug.log'
40         try:
41             cloud_config = os_client_config.get_config()
42             self.cloud = shade.OpenStackCloud(cloud_config=cloud_config)
43         except Exception:  # pylint: disable=broad-except
44             self.cloud = None
45
46     def run(self, **kwargs):
47         # pylint: disable=protected-access
48         """Run all read operations to check connections"""
49         status = testcase.TestCase.EX_RUN_ERROR
50         try:
51             assert self.cloud
52             self.start_time = time.time()
53             self.__logger.debug(
54                 "list_services: %s", functest_utils.list_services(self.cloud))
55             if env.get('NO_TENANT_NETWORK').lower() == 'true':
56                 self.func_list.remove("list_floating_ip_pools")
57                 self.func_list.remove("list_floating_ips")
58                 self.func_list.remove("list_routers")
59             for func in self.func_list:
60                 self.__logger.debug(
61                     "%s: %s", func, getattr(self.cloud, func)())
62             data = self.cloud._network_client.get("/service-providers.json")
63             self.__logger.debug(
64                 "list_service_providers: %s",
65                 self.cloud._get_and_munchify('service_providers', data))
66             functest_utils.get_openstack_version(self.cloud)
67             self.result = 100
68             status = testcase.TestCase.EX_OK
69         except Exception:  # pylint: disable=broad-except
70             self.__logger.exception('Cannot run %s', self.case_name)
71         finally:
72             self.stop_time = time.time()
73         return status