github.com/nginxinc/kubernetes-ingress@v1.12.5/tests/suite/yaml_utils.py (about) 1 """Describe methods to work with yaml files""" 2 3 import yaml 4 5 6 def get_first_ingress_host_from_yaml(file) -> str: 7 """ 8 Parse yaml file and return first spec.rules[0].host appeared. 9 10 :param file: an absolute path to file 11 :return: str 12 """ 13 with open(file) as f: 14 docs = yaml.safe_load_all(f) 15 for dep in docs: 16 return dep['spec']['rules'][0]['host'] 17 18 19 def get_name_from_yaml(file) -> str: 20 """ 21 Parse yaml file and return first metadata.name appeared. 22 23 :param file: an absolute path to file 24 :return: str 25 """ 26 res = "" 27 with open(file) as f: 28 docs = yaml.safe_load_all(f) 29 for dep in docs: 30 return dep['metadata']['name'] 31 return res 32 33 34 def get_paths_from_vs_yaml(file) -> []: 35 """ 36 Parse yaml file and return all the found spec.routes.path. 37 38 :param file: an absolute path to file 39 :return: [] 40 """ 41 res = [] 42 with open(file) as f: 43 docs = yaml.safe_load_all(f) 44 for dep in docs: 45 for route in dep['spec']['routes']: 46 res.append(route['path']) 47 return res 48 49 50 def get_first_host_from_yaml(file) -> str: 51 """ 52 Parse yaml file and return first spec.host appeared. 53 54 :param file: an absolute path to file 55 :return: str 56 """ 57 with open(file) as f: 58 docs = yaml.safe_load_all(f) 59 for dep in docs: 60 return dep['spec']['host'] 61 62 63 def get_configmap_fields_from_yaml(file) -> {}: 64 """ 65 Parse yaml file and return a dict of ConfigMap data fields. 66 67 :param file: an absolute path to a file 68 :return: {} 69 """ 70 with open(file) as f: 71 dep = yaml.safe_load(f) 72 return dep['data'] 73 74 75 def get_route_namespace_from_vs_yaml(file) -> []: 76 """ 77 Parse yaml file and return namespaces of all spec.routes.route. 78 79 :param file: an absolute path to file 80 :return: [] 81 """ 82 res = [] 83 with open(file) as f: 84 docs = yaml.safe_load_all(f) 85 for dep in docs: 86 for route in dep['spec']['routes']: 87 res.append(route['route'].split('/')[0]) 88 return res 89 90 91 def get_paths_from_vsr_yaml(file) -> []: 92 """ 93 Parse yaml file and return all the found spec.subroutes.path. 94 95 :param file: an absolute path to file 96 :return: [] 97 """ 98 res = [] 99 with open(file) as f: 100 docs = yaml.safe_load_all(f) 101 for dep in docs: 102 for route in dep['spec']['subroutes']: 103 res.append(route['path']) 104 return res