readYaml.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. 读yaml文件
  5. Author: songjian at <songjian12@baidu.com>
  6. Created: 2020/4/21
  7. """
  8. import sys
  9. import yaml
  10. import json
  11. import logging
  12. import os
  13. work_path = os.path.dirname(os.path.abspath(__file__))
  14. sdk_path = os.path.dirname(os.path.dirname(work_path))
  15. reload(sys)
  16. sys.setdefaultencoding('utf-8')
  17. def join_path(yamlPath, paraPath):
  18. """
  19. 相对路径替换成绝对路径
  20. :param yamlPath:
  21. :param paraPath:
  22. :return:
  23. """
  24. join_path = os.path.join(os.path.dirname(yamlPath), paraPath)
  25. if not os.path.isfile(paraPath) and os.path.isfile(join_path):
  26. return join_path
  27. return None
  28. def read_yaml(path):
  29. """
  30. 读yaml文件
  31. """
  32. conf = {}
  33. with open(sdk_path + '/conf/conf.yaml', 'r') as fc:
  34. conf = yaml.load(fc.read(), Loader=yaml.FullLoader)
  35. with open(path, 'r') as f:
  36. data = yaml.load(f.read(), Loader=yaml.FullLoader)
  37. logging.info('[%s]test_info:%s' % (os.path.basename(path), data['test_info']))
  38. logging.info('[%s]yaml_data:%s' % (os.path.basename(path), json.dumps(data, ensure_ascii=False)))
  39. if 'input' in data.keys():
  40. if 'type' not in data['input'].keys() or not data['input']['type']:
  41. data['input']['type'] = 0
  42. elif data['input']['type'] == 1 and 'parameter_path' in data['input'].keys():
  43. abs_path = join_path(path, str(data['input']['parameter_path']))
  44. if abs_path:
  45. data['input']['parameter_path'] = abs_path
  46. for case in data['test_case']:
  47. try:
  48. if 'parameter_path' in case.keys():
  49. abs_path = join_path(path, str(case['parameter_path']))
  50. if abs_path:
  51. case['parameter_path'] = abs_path
  52. if 'check' in case.keys() and 'assert_words' in case['check'].keys():
  53. abs_path = join_path(path, str(case['check']['assert_words']))
  54. if abs_path:
  55. case['check']['assert_words'] = abs_path
  56. if 'parameter_type' in case.keys() and case['parameter_type'] == 'form-data':
  57. lines = []
  58. with open(case['parameter_path'], 'r') as fr:
  59. lines = fr.readlines()
  60. with open(case['parameter_path'], 'w') as fw:
  61. fw.write('')
  62. with open(case['parameter_path'], 'a') as fa:
  63. path_para = case['parameter_path']
  64. for line in lines:
  65. line = line.decode('utf-8').encode('utf-8').strip()
  66. if os.path.isfile(line):
  67. abs_path = line
  68. elif os.path.isabs(line):
  69. abs_path = join_path(path_para, os.path.basename(line))
  70. else:
  71. abs_path = join_path(path_para, line)
  72. if abs_path:
  73. fa.write(abs_path)
  74. fa.write('\n')
  75. if 'host' in case.keys():
  76. case['host'] = conf['hosts'][case['host']]['hostname']
  77. else:
  78. case['host'] = conf['hosts']['host_1']['hostname']
  79. if 'invalid' not in case.keys():
  80. case['invalid'] = False
  81. if 'custom' not in case.keys():
  82. case['custom'] = False
  83. if 'request_type' not in case.keys():
  84. case['request_type'] = 'post'
  85. if 'headers' not in case.keys():
  86. case['headers'] = None
  87. if 'parameter_type' not in case.keys():
  88. case['parameter_type'] = 'raw'
  89. if 'para_file' not in case.keys():
  90. case['para_file'] = False
  91. if 'parameter' not in case.keys():
  92. case['parameter'] = None
  93. if 'parameter_path' not in case.keys():
  94. case['para_file'] = None
  95. if 'file_key' not in case.keys():
  96. case['file_key'] = 'file'
  97. if 'level' not in case.keys():
  98. case['level'] = []
  99. elif not isinstance(case['level'], list):
  100. case['level'] = []
  101. if 'cookie' in case.keys():
  102. case['cookie'] = conf['users'][case['cookie']]['cookie']
  103. else:
  104. case['cookie'] = conf['users']['user_1']['cookie']
  105. if 'waittime' not in case.keys():
  106. case['waittime'] = 1
  107. elif not case['waittime'] or type(case['waittime']) != int:
  108. case['waittime'] = 0
  109. if case['cookie']:
  110. if case['headers']:
  111. case['headers']['cookie'] = case['cookie']
  112. else:
  113. case['headers'] = {'cookie': case['cookie']}
  114. except Exception as e:
  115. logging.error(e)
  116. sys.exit(-1)
  117. return data
  118. if __name__ == "__main__":
  119. path = sdk_path + '/test_data/standard/test_homepage/4_homepage.yaml'
  120. print read_yaml(path)