我们有以下 API 将结果获取到 to res
,并且 res
是字典类型
res = api_req.ambari_request("/api/v1/clusters/HDP/host_components?HostRoles/component_name=NAMENODE&metrics/dfs/FSNamesystem/HAState=active")
字典res
看起来像这样:
。 。
"display_name": "NameNode",
"init_count": 0,
"install_failed_count": 0,
"installed_count": 0,
"recovery_enabled": "true",
"repository_state": "CURRENT",
"service_name": "HDFS",
"started_count": 2,
"state": "STARTED",
"total_count": 2,
"unknown_count": 0
},
我们需要验证字典中的所有started_count
内容是否相等2
感谢为该测试找到正确的方法
答案1
python 字典值可以使用下标运算符 [] 来访问,给定一个字符串,该字符串是与您希望获取的值相对应的键。这获取与“started_count”键关联的值。
api_data = {
"display_name": "NameNode",
"init_count": 0,
"install_failed_count": 0,
"installed_count": 0,
"recovery_enabled": "true",
"repository_state": "CURRENT",
"service_name": "HDFS",
"started_count": 2,
"state": "STARTED",
"total_count": 2,
"unknown_count": 0
}
if api_data["started_count"] == 2:
print("started_count is 2")
else:
print("started_count is not 2")