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 *
12 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '../common'))
14 ex = BuiltIn().get_library_instance('execute_command')
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)
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'
33 def elasticsearch_get_field(field):
34 data = '{ "size": 1, ' \
36 '"exists": { "field": "' + field + '" } }, ' \
37 '"sort" : [ {"@timestamp" : {"order" : "desc"}} ] }'
38 header = "Content-Type: application/json"
40 url = "{}/{}/_search".format(ELASTICSEARCH_URL, es_index)
41 request = "--header '{}' --request POST --data '{}' {}".format(header, data, url)
43 resp = ex.execute_unix_command("curl {}".format(request))
44 return json.loads(resp)
48 def elasticsearch_parse_field(msg, field):
50 msg = elasticsearch_get_field(field)
52 raise Exception('hits key not found in the following input:\n {}'.format(json.dumps(msg)))
53 msglen = len(msg['hits']['hits'])
55 for i in range(msglen):
56 output['date'] = (msg['hits']['hits'][i]['_source']['@timestamp'])
57 output['tag'] = (msg['hits']['hits'][i]['_source'][field])
62 def step1_get_elasticsearch_kubernetes_data():
64 resp = elasticsearch_get_field(field)
65 output = elasticsearch_parse_field(resp, field)
67 raise Exception("Logs with field {} not found!".format(field))
70 def is_there_some_plugin(elastic_plugins):
71 return elastic_plugins.find("reindex") != -1
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)
80 raise Exception("No plugin named 'reindex' is installed inside elasticsearch, something not right!")