-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbehave.py
166 lines (142 loc) · 4.98 KB
/
behave.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: behave
version_added: "2.3"
short_description: This is a wrapper module around behave
description:
- short_description: Handle execution of behave tests (http://pythonhosted.org/behave/)
options:
path:
description:
Absolute path of the directory containing the "features" directory.
required: true
name:
description:
name of the feature files to run. If not provided all feature
files found in path will be executed.
required: false
default: ""
language:
description: language of the feature files.
required: false
tags:
description:
List of tags to select for the execution. The list must be comma
separated without spaces.
required: false
default: None
output_format:
description:
By default the output of the features execution is stored in a file
with the default formatter. The formatter can be changed by
specifying the formatter name in output_format.
default: 'pretty'
required: false
output_name:
description:
Name of the file where the behave output will be stored.
default: "{feature}_result"
where "{feature}" will be replaced by the feature filename
required: false
output_dir:
description:
Directory where the behave output file will be written.
default: "/tmp"
required: false
keep_output:
description:
Specifies if output file should be kept after test execution.
Default is False to avoid leaving files on the remote server.
default: False
required: false
'''
EXAMPLES = '''
# run all features available under /home/foo/tests
behave:
path: /home/foo/tests
# run all features and specify the language of the files to french
behave:
path: /home/foo/tests
language: fr
# run only base.feature under /home/foo/tests
behave:
path: /home/foo/tests
name: "base.feature"
# run base.feature and store output result in "base.feature.output" as json
behave:
path: /home/foo/tests
name: "base.feature"
output_format: json.pretty
output_name: "base.feature.output"
'''
def parse_behave_output(output_file_path):
"""Return the feature status extracted from the behave output file
Argument:
output_file_path (str): behave output file encoded in JSON.
Returns dict
"""
data = json.load(open(output_file_path, 'r'))
result = {}
for item in data:
name = item['name']
result[name] = item["status"]
return result
def main():
argument_spec = dict(
path = dict(required=True, type='str'),
name = dict(required=False, type='str', default=None),
language = dict(required=False, type='str', default='fr',
choices=['fr', 'en']),
tags = dict(required=False, type='str', default=None),
output_format = dict(required=False, type='str', default='pretty',
choices=['pretty', 'json.pretty']),
output_name = dict(required=False, type='str',
default="{feature}_result"),
output_dir = dict(required=False, type='str', default="/tmp"),
keep_output = dict(required=False, type='bool', default=False),
)
module = AnsibleModule(argument_spec=argument_spec)
path = module.params.get('path')
name = module.params.get('name')
language = module.params.get('language')
tags = module.params.get('tags')
output_format = module.params.get('output_format')
output_name = module.params.get('output_name')
output_dir = module.params.get('output_dir')
keep_output = module.params.get('keep_output')
if "{feature}" not in output_name:
module.fail_json(
msg="The {feature} formatter is required in output_name string")
output_name = output_name.format(feature=os.path.basename(name))
outputfile_fullpath = os.path.join(output_dir, output_name)
FEAT = ""
if name:
FEAT = "--include %s" % name
LANG = ""
if language:
LANG = "--lang %s" % language
TAGS= ""
if tags is not None:
TAGS = "--tags=%s" % tags
FORMAT = "--format %s" % output_format
OUTPUT = "--outfile %s" % outputfile_fullpath
CMD = "behave {lang} {tags} {formatter} {output} {feature}"
rc, _, stderr = module.run_command(
CMD.format(lang=LANG, tags=TAGS, formatter=FORMAT,
output=OUTPUT.format(feature=name), feature=FEAT),
cwd=path)
statuses = "UNAVAILABLE"
if output_format in ["json.pretty", "json"]:
statuses = json.dumps(parse_behave_output(outputfile_fullpath))
if keep_output is False:
module.cleanup(outputfile_fullpath)
if rc != 0:
module.fail_json(changed=False, msg="Some test cases failed",
statuses=statuses, stderr=stderr)
module.exit_json(changed=False, msg=statuses)
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
if __name__ == "__main__":
main()