robot tcs, test charts, robot container added
[ta/cloudtaf.git] / testcases / fluentd / tc_002_elasticsearch_storage_check.py
1 import sys
2 import os
3 import datetime
4 import json
5 import common_utils
6 from decorators_for_robot_functionalities import *
7 from robot.libraries.BuiltIn import BuiltIn
8 from robot.api import logger
9 from test_constants import *
10 from users import *
11
12 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '../common'))
13
14 ex = BuiltIn().get_library_instance('execute_command')
15
16
17 def tc_002_elasticsearch_storage_check():
18     steps = ['step1_get_elasticsearch_kubernetes_data', 'step2_check_plugins']
19     if check_if_test_should_be_run():
20         common_utils.keyword_runner(steps)
21
22
23 def check_if_test_should_be_run():
24     command = "cmcli get-property --property cloud.caas " \
25               "grep '\"infra_log_store\": \"elasticsearch\"' | wc -l"
26     if ex.execute_unix_command(command) != '1':
27         command = "cat {} | grep 'infra_log_store: elasticsearch' | wc -l".format(USER_CONFIG_PATH)
28         return ex.execute_unix_command(command) == '1'
29     return True
30
31
32 @robot_log
33 def elasticsearch_get_field(field):
34     data = '{ "size": 1, ' \
35            '"query": { ' \
36                 '"exists": { "field": "' + field + '" } }, ' \
37                 '"sort" : [ {"@timestamp" : {"order" : "desc"}} ] }'
38     header = "Content-Type: application/json"
39     es_index = "_all"
40     url = "{}/{}/_search".format(ELASTICSEARCH_URL, es_index)
41     request = "--header '{}' --request POST --data '{}' {}".format(header, data, url)
42
43     resp = ex.execute_unix_command("curl {}".format(request))
44     return json.loads(resp)
45
46
47 @robot_log
48 def elasticsearch_parse_field(msg, field):
49     if 'hits' not in msg:
50         msg = elasticsearch_get_field(field)
51         if 'hits' not in msg:
52             raise Exception('hits key not found in the following input:\n {}'.format(json.dumps(msg)))
53     msglen = len(msg['hits']['hits'])
54     output = {}
55     for i in range(msglen):
56         output['date'] = (msg['hits']['hits'][i]['_source']['@timestamp'])
57         output['tag'] = (msg['hits']['hits'][i]['_source'][field])
58     logger.info(output)
59     return output
60
61
62 def step1_get_elasticsearch_kubernetes_data():
63     field = "kubernetes"
64     resp = elasticsearch_get_field(field)
65     output = elasticsearch_parse_field(resp, field)
66     if not output:
67         raise Exception("Logs with field {} not found!".format(field))
68
69
70 def is_there_some_plugin(elastic_plugins):
71     return elastic_plugins.find("reindex") != -1
72
73
74 def step2_check_plugins():
75     command = "curl http://elasticsearch-logging.kube-system.svc.nokia.net:9200/_cat/plugins?v"
76     elastic_plugins = ex.execute_unix_command_as_root(command)
77     if is_there_some_plugin(elastic_plugins):
78         logger.info("Installed elastic search plugins:" + elastic_plugins)
79     else:
80         raise Exception("No plugin named 'reindex' is installed inside elasticsearch, something not right!")