aboutsummaryrefslogtreecommitdiffstats
path: root/tools/docker/results/resultsdb/init_db.py
blob: 40bb4ee2b068e8695150634962519a0fb94c897f (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
##############################################################################
# Copyright (c) 2016 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
"""
Preload the results database with testcases.
"""

from __future__ import print_function
import json
import sys
import requests

DB_HOST_IP = sys.argv[1]
TESTAPI_PORT = sys.argv[2]

TARGET_URL = 'http://{}:{}/api/v1'.format(DB_HOST_IP, TESTAPI_PORT)


def get(url):
    """
    Get the http response.
    """
    return requests.get(url).json()


def post(url, data):
    """
    Post HTTP request.
    """
    headers = {'Content-Type': 'application/json'}
    res = requests.post(url, data=json.dumps(data), headers=headers)
    print(res.text)


def pod():
    """
    Get the PODs.
    """
    target = '{}/pods'.format(TARGET_URL)

    with open('pods.json', 'r') as podref:
        pods = json.load(podref)
    for apod in pods:
        post(target, apod)

    add_pod('master', 'metal')
    add_pod('virtual_136_2', 'virtual')


def project():
    """
    Get the Projects
    """
    target = '{}/projects'.format(TARGET_URL)
    with open('projects.json', 'r') as projref:
        projects = json.load(projref)
    for proj in projects:
        post(target, proj)


def cases():
    """
    Get the Cases
    """
    with open('cases.json', 'r') as caseref:
        for line in caseref:
            subcases = json.loads(line)
            for cas in subcases["testcases"]:
                target = '{}/projects/{}/cases'.format(TARGET_URL,
                                                       cas['project_name'])
                post(target, cas)
    add_case("functest", "tempest_custom")


def add_pod(name, mode):
    """
    Add the Pods.
    """
    data = {
        "role": "",
        "name": name,
        "details": '',
        "mode": mode,
        "creation_date": "2017-2-23 11:23:03.765581"
    }
    pod_url = '{}/pods'.format(TARGET_URL)
    post(pod_url, data)


def add_case(projectname, casename):
    """
    Add a testcase
    """
    data = {
        "project_name": projectname,
        "name": casename,
    }
    case_url = '{}/projects/{}/cases'.format(TARGET_URL, projectname)
    post(case_url, data)


if __name__ == '__main__':
    pod()
    project()
    cases()