aboutsummaryrefslogtreecommitdiffstats
path: root/tests/run_tests.py
blob: eb914a452165da84403e7874acf6513a4d39b1ca (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python

# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the
# 'Apache-2.0'license which can be found in the file 'LICENSE' in this
# package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

import argparse
import functest.utils.functest_logger as ft_logger
import functest.utils.functest_utils as functest_utils
import os
import sys
import time
import yaml
import subprocess
import json
try:
    import http.client as client
except ImportError:
    import httplib as client

PORT_ODL = 8181
HOST_ODL = "localhost"

parser = argparse.ArgumentParser()

parser.add_argument("-r", "--report",
                    help="Create json result file",
                    action="store_true")
args = parser.parse_args()

with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
    functest_yaml = yaml.safe_load(f)

dirs = functest_yaml.get('general').get('directories')
FUNCTEST_REPO = dirs.get('dir_repo_functest')
COPPER_REPO = dirs.get('dir_repo_moon')
TEST_DB_URL = functest_yaml.get('results').get('test_db_url')

logger = ft_logger.Logger("moon").getLogger()
try:
    # Python3 version
    from urllib.request import urlopen, HTTPBasicAuthHandler, build_opener, install_opener
except ImportError:
    # Python2 version
    from urllib import urlopen
    from urllib2 import HTTPBasicAuthHandler, build_opener, install_opener


def __get_endpoint_url(name="keystone"):
    proc = subprocess.Popen(["openstack", "endpoint", "show", name, "-f", "yaml"], stdout=subprocess.PIPE)
    y = yaml.load(proc.stdout.read())
    url = y['publicurl']
    url = url.replace("http://", "")
    url = url.replace("https://", "")
    host, port = url.split(":")
    port = port.split("/")[0]
    return host, port


def test_federation():

    username = "test_fede"
    password = "pass_fede"

    # Create a new user in OpenStack
    proc = subprocess.Popen(["openstack", "user", "create", "--password", password, username, "-f", "yaml"], stdout=subprocess.PIPE)
    logger.info("Create new user ({})".format(proc.stdout.read()))

    # Retrieve Moon token
    nhost, nport = __get_endpoint_url()
    auth_data = {'username': username, 'password': password}
    conn = client.HTTPConnection(nhost, nport)
    headers = {"Content-type": "application/json"}
    conn.request("POST", "/moon/auth/tokens", json.dumps(auth_data).encode('utf-8'), headers=headers)
    resp = conn.getresponse()
    if resp.status not in (200, 201, 202, 204):
        return False, "Not able to retrieve Moon token on {}:{} (error code: {}).".format(nhost, nport, resp.status)


    # Retrieve ODL token
    nhost, nport = __get_endpoint_url(name="neutron")
    auth_data = {'username': 'admin', 'password': 'console'}
    conn = client.HTTPConnection(nhost, "8181")
    headers = {"Content-type": "application/json"}
    conn.request("POST", "/auth/v1/domains", json.dumps(auth_data).encode('utf-8'), headers=headers)
    resp = conn.getresponse()
    if resp.status not in (200, 201, 202, 204):
        return False, "Not able to retrieve ODL token on {}:{} (error code: {}).".format(nhost, nport, resp.status)
    # auth_handler = HTTPBasicAuthHandler()
    # auth_handler.add_password(realm='Moon',
    #                           uri='http://{host}:{port}/auth/v1/domains'.format(host=HOST_ODL, port=PORT_ODL),
    #                           user='admin',
    #                           passwd='console')
    # opener = build_opener(auth_handler)
    # install_opener(opener)
    # url = urlopen('http://{host}:{port}/auth/v1/domains'.format(host=HOST_ODL, port=PORT_ODL))
    # code = url.getcode()
    # if code not in (200, 201, 202, 204):
    #     return False, "Not able to retrieve ODL token (error code: {}).".format(code)
    return True, ""


def test_moon_openstack():
    log_filename = "moonclient_selftest.log"
    cmd = "moon test --password console --self --logfile {}".format(log_filename)

    ret_val = functest_utils.execute_command(cmd, exit_on_error=False)

    return ret_val, open(log_filename, "rt").read()


def main():
    start_time = time.time()

    result_os = test_moon_openstack()
    result_odl = test_federation()

    stop_time = time.time()
    duration = round(stop_time - start_time, 1)
    if result_os == 0 and result_odl[0]:
        logger.info("OS MOON PASSED")
        test_status = 'PASS'
    else:
        logger.info("OS MOON ERROR")
        test_status = 'FAIL'
        logger.info("Errors from OpenStack tests:")
        logger.info(result_os[1])
        logger.info("Errors from Federation tests:")
        logger.info(result_odl[1])

    details = {
        'timestart': start_time,
        'duration': duration,
        'status': test_status,
        'results': {
            'openstack': result_os,
            'opendaylight': result_odl
        }
    }

    functest_utils.logger_test_results("moon",
                                       "moon_authentication",
                                       test_status, details)
    if args.report:
        functest_utils.push_results_to_db("moon",
                                          "moon_authentication",
                                          start_time,
                                          stop_time,
                                          test_status,
                                          details)
        logger.info("Moon results pushed to DB")

    if result_os != 0 or not result_odl[0]:
        return False
    return True


if __name__ == '__main__':
    ret = main()
    if ret:
        sys.exit(0)
    sys.exit(1)