2022-08-17 14:32:49 +08:00
|
|
|
from yaml import full_load
|
2022-08-19 12:54:54 +08:00
|
|
|
import json
|
|
|
|
from utils.util_log import test_log as log
|
2022-08-17 14:32:49 +08:00
|
|
|
|
|
|
|
def gen_experiment_config(yaml):
|
|
|
|
"""load the yaml file of chaos experiment"""
|
|
|
|
with open(yaml) as f:
|
|
|
|
_config = full_load(f)
|
|
|
|
f.close()
|
|
|
|
return _config
|
2022-02-25 18:41:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
def findkeys(node, kv):
|
|
|
|
# refer to https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-dictionaries-and-lists
|
|
|
|
if isinstance(node, list):
|
|
|
|
for i in node:
|
|
|
|
for x in findkeys(i, kv):
|
|
|
|
yield x
|
|
|
|
elif isinstance(node, dict):
|
|
|
|
if kv in node:
|
|
|
|
yield node[kv]
|
|
|
|
for j in node.values():
|
|
|
|
for x in findkeys(j, kv):
|
|
|
|
yield x
|
|
|
|
|
|
|
|
|
2022-05-20 09:31:57 +08:00
|
|
|
def update_key_value(node, modify_k, modify_v):
|
|
|
|
# update the value of modify_k to modify_v
|
|
|
|
if isinstance(node, list):
|
|
|
|
for i in node:
|
|
|
|
update_key_value(i, modify_k, modify_v)
|
|
|
|
elif isinstance(node, dict):
|
|
|
|
if modify_k in node:
|
|
|
|
node[modify_k] = modify_v
|
|
|
|
for j in node.values():
|
|
|
|
update_key_value(j, modify_k, modify_v)
|
|
|
|
return node
|
|
|
|
|
2022-02-25 18:41:53 +08:00
|
|
|
|
2022-08-17 14:32:49 +08:00
|
|
|
def update_key_name(node, modify_k, modify_k_new):
|
|
|
|
# update the name of modify_k to modify_k_new
|
|
|
|
if isinstance(node, list):
|
|
|
|
for i in node:
|
|
|
|
update_key_name(i, modify_k, modify_k_new)
|
|
|
|
elif isinstance(node, dict):
|
|
|
|
if modify_k in node:
|
|
|
|
value_backup = node[modify_k]
|
|
|
|
del node[modify_k]
|
|
|
|
node[modify_k_new] = value_backup
|
|
|
|
for j in node.values():
|
|
|
|
update_key_name(j, modify_k, modify_k_new)
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
2023-04-18 16:06:34 +08:00
|
|
|
def get_collections(file_name="all_collections.json"):
|
2022-08-19 12:54:54 +08:00
|
|
|
try:
|
2023-04-18 16:06:34 +08:00
|
|
|
with open(f"/tmp/ci_logs/{file_name}", "r") as f:
|
2022-08-19 12:54:54 +08:00
|
|
|
data = json.load(f)
|
|
|
|
collections = data["all"]
|
|
|
|
except Exception as e:
|
|
|
|
log.error(f"get_all_collections error: {e}")
|
|
|
|
return []
|
|
|
|
return collections
|
|
|
|
|
2023-04-18 16:06:34 +08:00
|
|
|
def get_deploy_test_collections():
|
|
|
|
try:
|
|
|
|
with open("/tmp/ci_logs/deploy_test_all_collections.json", "r") as f:
|
|
|
|
data = json.load(f)
|
|
|
|
collections = data["all"]
|
|
|
|
except Exception as e:
|
|
|
|
log.error(f"get_all_collections error: {e}")
|
|
|
|
return []
|
|
|
|
return collections
|
|
|
|
|
|
|
|
def get_chaos_test_collections():
|
|
|
|
try:
|
|
|
|
with open("/tmp/ci_logs/chaos_test_all_collections.json", "r") as f:
|
|
|
|
data = json.load(f)
|
|
|
|
collections = data["all"]
|
|
|
|
except Exception as e:
|
|
|
|
log.error(f"get_all_collections error: {e}")
|
|
|
|
return []
|
|
|
|
return collections
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-19 12:54:54 +08:00
|
|
|
|
2022-02-25 18:41:53 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
d = { "id" : "abcde",
|
|
|
|
"key1" : "blah",
|
|
|
|
"key2" : "blah blah",
|
|
|
|
"nestedlist" : [
|
|
|
|
{ "id" : "qwerty",
|
|
|
|
"nestednestedlist" : [
|
|
|
|
{ "id" : "xyz", "keyA" : "blah blah blah" },
|
|
|
|
{ "id" : "fghi", "keyZ" : "blah blah blah" }],
|
|
|
|
"anothernestednestedlist" : [
|
|
|
|
{ "id" : "asdf", "keyQ" : "blah blah" },
|
|
|
|
{ "id" : "yuiop", "keyW" : "blah" }] } ] }
|
2022-05-20 09:31:57 +08:00
|
|
|
print(list(findkeys(d, 'id')))
|
|
|
|
update_key_value(d, "none_id", "ccc")
|
|
|
|
print(d)
|