Merge "Add odl configurations for odl testcase in daisy environment"
[functest.git] / functest / ci / check_deployment.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Ericsson 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 """
11 OpenStack deployment checker
12
13 Verifies that:
14  - Credentials file is given and contains the right information
15  - OpenStack endpoints are reachable
16 """
17
18 import logging
19 import logging.config
20 import os
21 import pkg_resources
22 import socket
23 import time
24 from urlparse import urlparse
25
26 from snaps.openstack.utils import glance_utils
27 from snaps.openstack.utils import keystone_utils
28 from snaps.openstack.utils import neutron_utils
29 from snaps.openstack.utils import nova_utils
30 from snaps.openstack.tests import openstack_tests
31
32 __author__ = "Jose Lausuch <jose.lausuch@ericsson.com>"
33
34 LOGGER = logging.getLogger(__name__)
35
36
37 def verify_connectivity(adress, port, timeout=10):
38     """ Returns true if an ip/port is reachable"""
39     connection = socket.socket()
40     count = 0
41     while count < timeout:
42         try:
43             connection.connect((adress, port))
44             LOGGER.debug('%s:%s is reachable!', adress, port)
45             return True
46         except socket.error:
47             count += 1
48             time.sleep(1)
49             continue
50     LOGGER.error('%s:%s is not reachable.', adress, port)
51     return False
52
53
54 class CheckDeployment(object):
55     """ Check deployment class."""
56
57     def __init__(self, rc_file='/home/opnfv/functest/conf/openstack.creds'):
58         self.rc_file = rc_file
59         self.services = ('compute', 'network', 'image')
60         self.os_creds = None
61
62     def check_rc(self):
63         """ Check if RC file exists and contains OS_AUTH_URL """
64         if not os.path.isfile(self.rc_file):
65             raise IOError('RC file {} does not exist!'.format(self.rc_file))
66         if 'OS_AUTH_URL' not in open(self.rc_file).read():
67             raise SyntaxError('OS_AUTH_URL not defined in {}.'.
68                               format(self.rc_file))
69
70     def check_auth_endpoint(self):
71         """ Verifies connectivity to the OS_AUTH_URL given in the RC file """
72         rc_endpoint = self.os_creds.auth_url
73         if not (verify_connectivity(urlparse(rc_endpoint).hostname,
74                                     urlparse(rc_endpoint).port)):
75             raise Exception("OS_AUTH_URL {} is not reachable.".
76                             format(rc_endpoint))
77         LOGGER.info("Connectivity to OS_AUTH_URL %s ...OK", rc_endpoint)
78
79     def check_public_endpoint(self):
80         """ Gets the public endpoint and verifies connectivity to it """
81         public_endpoint = keystone_utils.get_endpoint(self.os_creds,
82                                                       'identity',
83                                                       interface='public')
84         if not (verify_connectivity(urlparse(public_endpoint).hostname,
85                                     urlparse(public_endpoint).port)):
86             raise Exception("Public endpoint {} is not reachable.".
87                             format(public_endpoint))
88         LOGGER.info("Connectivity to the public endpoint %s ...OK",
89                     public_endpoint)
90
91     def check_service_endpoint(self, service):
92         """ Verifies connectivity to a given openstack service """
93         endpoint = keystone_utils.get_endpoint(self.os_creds,
94                                                service,
95                                                interface='public')
96         if not (verify_connectivity(urlparse(endpoint).hostname,
97                                     urlparse(endpoint).port)):
98             raise Exception("{} endpoint {} is not reachable.".
99                             format(service, endpoint))
100         LOGGER.info("Connectivity to endpoint '%s' %s ...OK",
101                     service, endpoint)
102
103     def check_nova(self):
104         """ checks that a simple nova operation works """
105         try:
106             client = nova_utils.nova_client(self.os_creds)
107             client.servers.list()
108             LOGGER.info("Nova service ...OK")
109         except Exception as error:
110             LOGGER.error("Nova service ...FAILED")
111             raise error
112
113     def check_neutron(self):
114         """ checks that a simple neutron operation works """
115         try:
116             client = neutron_utils.neutron_client(self.os_creds)
117             client.list_networks()
118             LOGGER.info("Neutron service ...OK")
119         except Exception as error:
120             LOGGER.error("Neutron service ...FAILED")
121             raise error
122
123     def check_glance(self):
124         """ checks that a simple glance operation works """
125         try:
126             client = glance_utils.glance_client(self.os_creds)
127             client.images.list()
128             LOGGER.info("Glance service ...OK")
129         except Exception as error:
130             LOGGER.error("Glance service ...FAILED")
131             raise error
132
133     def check_all(self):
134         """
135         Calls all the class functions and returns 0 if all of them succeed.
136         This is the method called by prepare_env or CLI
137         """
138         self.check_rc()
139         try:
140             self.os_creds = openstack_tests.get_credentials(
141                 os_env_file=self.rc_file,
142                 proxy_settings_str=None,
143                 ssh_proxy_cmd=None)
144         except:
145             raise Exception("Problem while getting credentials object.")
146         if self.os_creds is None:
147             raise Exception("Credentials is None.")
148         self.check_auth_endpoint()
149         self.check_public_endpoint()
150         for service in self.services:
151             self.check_service_endpoint(service)
152         self.check_nova()
153         self.check_neutron()
154         self.check_glance()
155         return 0
156
157
158 def main():
159     """Entry point"""
160     logging.config.fileConfig(pkg_resources.resource_filename(
161         'functest', 'ci/logging.ini'))
162     deployment = CheckDeployment()
163     return deployment.check_all()