3 # Copyright (c) 2017 Ericsson and others.
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
11 OpenStack deployment checker
14 - Credentials file is given and contains the right information
15 - OpenStack endpoints are reachable
23 from urlparse import urlparse
25 from snaps.openstack.utils import glance_utils
26 from snaps.openstack.utils import keystone_utils
27 from snaps.openstack.utils import neutron_utils
28 from snaps.openstack.utils import nova_utils
29 from snaps.openstack.tests import openstack_tests
31 __author__ = "Jose Lausuch <jose.lausuch@ericsson.com>"
33 LOGGER = logging.getLogger(__name__)
36 def verify_connectivity(adress, port):
37 """ Returns true if an ip/port is reachable"""
38 connection = socket.socket()
39 connection.settimeout(10)
41 connection.connect((adress, port))
42 LOGGER.debug('%s:%s is reachable!', adress, port)
45 LOGGER.error('%s:%s is not reachable.', adress, port)
49 class CheckDeployment(object):
50 """ Check deployment class."""
52 def __init__(self, rc_file='/home/opnfv/functest/conf/openstack.creds'):
53 self.rc_file = rc_file
54 self.services = ('compute', 'network', 'image')
58 """ Check if RC file exists and contains OS_AUTH_URL """
59 if not os.path.isfile(self.rc_file):
60 raise IOError('RC file {} does not exist!'.format(self.rc_file))
61 if 'OS_AUTH_URL' not in open(self.rc_file).read():
62 raise SyntaxError('OS_AUTH_URL not defined in {}.'.
65 def check_auth_endpoint(self):
66 """ Verifies connectivity to the OS_AUTH_URL given in the RC file """
67 rc_endpoint = self.os_creds.auth_url
68 if not (verify_connectivity(urlparse(rc_endpoint).hostname,
69 urlparse(rc_endpoint).port)):
70 raise Exception("OS_AUTH_URL {} is not reachable.".
72 LOGGER.info("Connectivity to OS_AUTH_URL %s ...OK", rc_endpoint)
74 def check_public_endpoint(self):
75 """ Gets the public endpoint and verifies connectivity to it """
76 public_endpoint = keystone_utils.get_endpoint(self.os_creds,
79 if not (verify_connectivity(urlparse(public_endpoint).hostname,
80 urlparse(public_endpoint).port)):
81 raise Exception("Public endpoint {} is not reachable.".
82 format(public_endpoint))
83 LOGGER.info("Connectivity to the public endpoint %s ...OK",
86 def check_service_endpoint(self, service):
87 """ Verifies connectivity to a given openstack service """
88 endpoint = keystone_utils.get_endpoint(self.os_creds,
91 if not (verify_connectivity(urlparse(endpoint).hostname,
92 urlparse(endpoint).port)):
93 raise Exception("{} endpoint {} is not reachable.".
94 format(service, endpoint))
95 LOGGER.info("Connectivity to endpoint '%s' %s ...OK",
99 """ checks that a simple nova operation works """
101 client = nova_utils.nova_client(self.os_creds)
102 client.servers.list()
103 LOGGER.info("Nova service ...OK")
104 except Exception as error:
105 LOGGER.error("Nova service ...FAILED")
108 def check_neutron(self):
109 """ checks that a simple neutron operation works """
111 client = neutron_utils.neutron_client(self.os_creds)
112 client.list_networks()
113 LOGGER.info("Neutron service ...OK")
114 except Exception as error:
115 LOGGER.error("Neutron service ...FAILED")
118 def check_glance(self):
119 """ checks that a simple glance operation works """
121 client = glance_utils.glance_client(self.os_creds)
123 LOGGER.info("Glance service ...OK")
124 except Exception as error:
125 LOGGER.error("Glance service ...FAILED")
130 Calls all the class functions and returns 0 if all of them succeed.
131 This is the method called by prepare_env or CLI
135 self.os_creds = openstack_tests.get_credentials(
136 os_env_file=self.rc_file,
137 proxy_settings_str=None,
140 raise Exception("Problem while getting credentials object.")
141 if self.os_creds is None:
142 raise Exception("Credentials is None.")
143 self.check_auth_endpoint()
144 self.check_public_endpoint()
145 for service in self.services:
146 self.check_service_endpoint(service)
155 logging.config.fileConfig(pkg_resources.resource_filename(
156 'functest', 'ci/logging.ini'))
157 logging.captureWarnings(True)
158 deployment = CheckDeployment()
159 return deployment.check_all()