Separate test data vs dashboard script
[integration/test.git] / csit / scripts / push_test_data.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
5 ##############################################################################
6 # Copyright (c) 2017 Raghuram Vadapalli, Jaspreet Singh and others.
7 #
8 # All rights reserved. This program and the accompanying materials
9 # are made available under the terms of the Eclipse Public License v1.0
10 # which accompanies this distribution, and is available at
11 # http://www.eclipse.org/legal/epl-v10.html
12 ##############################################################################
13
14 """
15 This script is used to parse test logs, construct JSON BODY and push
16 it to ELK DB.
17
18 Usage: python push_test_data.py host:port
19
20 JSON body similar to following is \
21 constructed from robot files, jenkins environment
22 and plot files available in workspace available post-build.
23 {
24     "project": "opendaylight", <- fix string for ODL project
25     "subject": "test", <- fix string for ODL test
26     "test-type": "performance", <- if there are csv files, \
27                                      otherwise "functional"
28     "jenkins-silo": "releng" <- from Jenkins $SILO
29     "test-name": "openflowplugin-csit-1node-periodic \
30                 -bulkomatic-perf-daily-only-carbon", <- from Jenkins $JOB_NAME
31     "test-run": 289, <- from Jenkins $BUILD_NUMBER
32     "start-time": "20170612 16:50:04 GMT-07:00",  <- from robot log
33     "duration": "00:01:05.942", <- from robot log
34     "pass-tests": 9, <- from robot log
35     "fail-tests": 0, <- from robot log
36     "plots": {
37         "rate": { <- csv filename
38             "Config DS": 5816.99726601, <- from csv file
39             "OVS Switch": 5757.05238918, <- from csv file
40             "Operational DS": 2654.49139945 <- from csv file
41         },
42         "time": { <- csv filename
43             "Config DS": 17.191, <- from csv file
44             "OVS Switch": 17.37, <- from csv file
45             "Operational DS": 37.672 <- from csv file
46         }
47     }
48 }
49 """
50
51 # stdlib
52 import json
53 import sys
54
55 # 3rd party lib
56 from elasticsearch import Elasticsearch, RequestsHttpConnection, exceptions
57
58 # User defined libs
59 import data_generate as data_gen
60
61
62 def p(x):
63     print(json.dumps(x, indent=6, sort_keys=True))
64 # ELK DB host and port to be passed as ':' separated argument
65
66
67 if len(sys.argv) > 1:
68     if ':' in sys.argv[1]:
69         ELK_DB_HOST = sys.argv[1].split(':')[0]
70         ELK_DB_PORT = sys.argv[1].split(':')[1]
71 else:
72     print('Usage: python push_to_elk.py host:port')
73     print('Unable to publish data to ELK. Exiting.')
74     sys.exit()
75
76 # Construct json body
77
78 # BODY = {}
79
80 try:
81     es = Elasticsearch(
82         hosts=[{'host': ELK_DB_HOST, 'port': int(ELK_DB_PORT)}],
83         scheme='https',
84         connection_class=RequestsHttpConnection
85     )
86 except Exception as e:
87     print('Unexpected Error Occurred. Exiting')
88     print(e)
89 # print(es.info())
90
91
92 # get data from the user defined script
93 BODY = data_gen.generate()
94
95 print(json.dumps(BODY, indent=4))
96
97 # Try to send request to ELK DB.
98
99 try:
100     index = '{}-{}'.format(BODY['project'],
101                            BODY['subject'])
102     ES_ID = '{}:{}-{}'.format(BODY['test-type'], BODY['test-name'],
103                               BODY['test-run'])
104     res = es.index(index=index, doc_type='doc', id=ES_ID, body=BODY)
105     print(json.dumps(res, indent=4))
106 except Exception as e:
107     print(e)
108     print('Unable to push data to ElasticSearch')