Remove Domino and Promise testcases
[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 from urlparse import urlparse
24
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
30
31 __author__ = "Jose Lausuch <jose.lausuch@ericsson.com>"
32
33 LOGGER = logging.getLogger(__name__)
34
35
36 def verify_connectivity(adress, port):
37     """ Returns true if an ip/port is reachable"""
38     connection = socket.socket()
39     connection.settimeout(10)
40     try:
41         connection.connect((adress, port))
42         LOGGER.debug('%s:%s is reachable!', adress, port)
43         return True
44     except socket.error:
45         LOGGER.error('%s:%s is not reachable.', adress, port)
46     return False
47
48
49 class CheckDeployment(object):
50     """ Check deployment class."""
51
52     def __init__(self, rc_file='/home/opnfv/functest/conf/openstack.creds'):
53         self.rc_file = rc_file
54         self.services = ('compute', 'network', 'image')
55         self.os_creds = None
56
57     def check_rc(self):
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 {}.'.
63                               format(self.rc_file))
64
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.".
71                             format(rc_endpoint))
72         LOGGER.info("Connectivity to OS_AUTH_URL %s ...OK", rc_endpoint)
73
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,
77                                                       'identity',
78                                                       interface='public')
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",
84                     public_endpoint)
85
86     def check_service_endpoint(self, service):
87         """ Verifies connectivity to a given openstack service """
88         endpoint = keystone_utils.get_endpoint(self.os_creds,
89                                                service,
90                                                interface='public')
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",
96                     service, endpoint)
97
98     def check_nova(self):
99         """ checks that a simple nova operation works """
100         try:
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")
106             raise error
107
108     def check_neutron(self):
109         """ checks that a simple neutron operation works """
110         try:
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")
116             raise error
117
118     def check_glance(self):
119         """ checks that a simple glance operation works """
120         try:
121             client = glance_utils.glance_client(self.os_creds)
122             client.images.list()
123             LOGGER.info("Glance service ...OK")
124         except Exception as error:
125             LOGGER.error("Glance service ...FAILED")
126             raise error
127
128     def check_all(self):
129         """
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
132         """
133         self.check_rc()
134         try:
135             self.os_creds = openstack_tests.get_credentials(
136                 os_env_file=self.rc_file,
137                 proxy_settings_str=None,
138                 ssh_proxy_cmd=None)
139         except:
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)
147         self.check_nova()
148         self.check_neutron()
149         self.check_glance()
150         return 0
151
152
153 def main():
154     """Entry point"""
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()