blob: 78bbc86bfd955d6491c689d90719e5da5109b20c [file] [log] [blame]
Joey Armstrong888f1ee2023-01-11 17:14:30 -05001# Copyright 2020-2023 Open Networking Foundation (ONF) and the ONF Contributors
uwe ottrembka504ca3e2020-11-23 12:02:20 +01002# delivered by ADTRAN, Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from __future__ import absolute_import
17from __future__ import print_function
18import inspect
19import os
TorstenThiemeff9c9142021-04-08 07:21:34 +000020import operator
TorstenThieme7e8ccb92022-09-29 09:12:38 +000021import requests
uwe ottrembka504ca3e2020-11-23 12:02:20 +010022
23# global definition of keys (find in given 'inventory_data')
24_NAME = 'name'
25_CHILDREN = 'children'
26_SENSOR_DATA = 'sensor_data'
27_ROOT = 'root'
28_INVENTORY = 'inventory'
29_UUID = 'uuid'
30
31
32def test(success):
33 if success is True:
34 return True
35 return False
36
37
38def unique():
39 """Returns the current filename and line number in our program."""
40 trace = str(os.path.basename(__file__) +
41 "[" + str(inspect.currentframe().f_back.f_lineno) + "]:")
42 return trace
43
44
45# check if given paramter exist in inventory data, search recursive
46def check_in_inventory_Component_data(inventory_data, name, element, value):
47 print(unique(), str(inventory_data), str(name), str(element), str(value))
48 if inventory_data.get(_NAME) == name and inventory_data.get(element) == value:
49 return True
50
51 for child in inventory_data[_CHILDREN]:
52 print(unique(), str(child))
53 if child.get(_NAME) == name and child.get(element) == value:
54 return True
55 if _SENSOR_DATA in child:
56 for sensor_data in child[_SENSOR_DATA]:
57 print(unique(), str(sensor_data))
58 if sensor_data.get(element) == value:
59 return True
60 if _CHILDREN in child:
61 result = check_in_inventory_Component_data(child, name, element, value)
62 if result is True:
63 return result
64 return False
65
66
67# get uuid out of inventory data, search recursive
68def get_uuid_from_inventory_Component_data(inventory_data, searchFor):
69 print(unique(), str(inventory_data), ', ', str(searchFor))
70 if inventory_data.get(_NAME) == searchFor:
71 return inventory_data.get(_UUID)
72 for child in inventory_data[_CHILDREN]:
73 print(unique(), str(child))
74 result = None
75 if child.get(_NAME) == searchFor:
76 print(unique(), str(child[_NAME]))
77 result = child.get(_UUID)
78 print(unique(), child.keys())
79 if result is None and _CHILDREN in child:
80 result = get_uuid_from_inventory_Component_data(child, searchFor)
81 if result is not None:
82 return result
83 return None
84
85
86def get_uuid_from_Inventory_Element(inventory, searchFor):
87 for children in inventory[_INVENTORY][_ROOT][_CHILDREN]:
88 return get_uuid_from_inventory_Component_data(children, searchFor)
89 return None
90
91
92def check_Inventory_Element(inventory, name, element, value):
93 for childrens in inventory[_INVENTORY][_ROOT][_CHILDREN]:
94 return check_in_inventory_Component_data(childrens, name, element, value)
95 return False
96
97
98def getWord(line, number):
99 line_in_list = line.split()
100 if len(line_in_list) >= number-1:
101 return line_in_list[number-1]
102 return ""
103
104
105def decode(data):
106 decoded_data = data
107 print(unique(), str(decoded_data))
TorstenThiemeff9c9142021-04-08 07:21:34 +0000108
109
110# Compares two values using a given operator. The values are converted to float first so that
111# numbers as strings are also accepted. Returns True or False.
112# operator: ==, !=, <, <=, >, >=
113# Example:
114# | ${result} | Compare | 100 | > | 5 | # True |
115def compare(value1, op, value2):
116 ops = {"==": operator.eq,
117 "!=": operator.ne,
118 "<": operator.lt,
119 "<=": operator.le,
120 ">": operator.gt,
121 ">=": operator.ge}
122 return ops[op](float(value1), float(value2))
123
124
125# Validates two values using a given operator.
126# The values are converted to float first so that numbers as strings are also accepted.
127# Second value has to be a list in case of operator is 'in' or 'range'
128# Returns True or False.
129# operator: in, range, ==, !=, <, <=, >, >=
130# Example:
131# | ${result} | validate | 100 | > | 5 | # True |
132# | ${result} | validate | 11 | in | ['11','264','329'] | # True |
133# | ${result} | validate | 1 | range | ['0','1'] | # True |
134def validate(value1, op, value2):
135 if op == "in":
136 return (float(value1) in [float(i) for i in value2])
137 if op == "range":
138 return ((compare(value1, ">=", value2[0])) and (compare(value1, "<=", value2[1])))
139 return compare(value1, op, value2)
TorstenThieme7e8ccb92022-09-29 09:12:38 +0000140
141
142def get_memory_consumptions(address, container, namespace="default"):
143 """
144 Query Prometheus and generate instantaneous memory consumptions for given pods under test
145 :param address: string The address of the Prometheus instance to query
146 :container: string The pod name
147 :param namespace: string The pod namespace
148 :return: memory consumtion value
149 """
150 container_mem_query = ('container_memory_working_set_bytes{namespace="%s",container="%s"}' %
151 (namespace, container))
152 mem_params = {
153 "query": container_mem_query,
154 }
155 r = requests.get("http://%s/api/v1/query" % address, mem_params)
156 container_cpu = r.json()["data"]["result"]
157 if len(container_cpu) > 0:
158 return container_cpu[0]["value"][1]
159 else:
160 return -1