This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
219 lines (180 loc) · 6.26 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import argparse
import os
import sys
import zipfile
from mozilla_bitbar_devicepool import (
TESTDROID,
configuration,
logger,
modulepath,
)
from mozilla_bitbar_devicepool.bitbar.runs import run_test_for_project
from mozilla_bitbar_devicepool.test_run_manager import TestRunManager
from mozilla_bitbar_devicepool.util.network import download_file
testdroid_apk_url = "https://github.com/bitbar/test-samples/raw/master/apps/android/testdroid-sample-app.apk"
def download_testdroid_apk(args):
if args.filename:
testdroid_apk = os.path.join(args.files, args.filename)
else:
testdroid_apk = os.path.join(args.files, os.path.basename(testdroid_apk_url))
if os.path.exists(testdroid_apk) and not args.force:
logger.warning("%s exists. Skipping download." % testdroid_apk)
else:
# Add ?raw=true to force download.
download_file(testdroid_apk_url + "?raw=true", testdroid_apk)
def empty_test_zip(args):
# Create an empty zip file
test_zip = os.path.join(args.files, args.filename)
with zipfile.ZipFile(test_zip, mode="w"):
pass
def test_run_manager(args):
if not TESTDROID:
logger.error(
"The environment variabels TESTDROID_URL, TESTDROID_APIKEY both need to be set."
)
sys.exit(1)
if args.bitbar_config is None:
bitbar_configpath = os.path.join(modulepath, "config", "config.yml")
else:
bitbar_configpath = args.bitbar_config
try:
configuration.configure(
bitbar_configpath, filespath=args.files, update_bitbar=args.update_bitbar
)
except configuration.DuplicateProjectException as e:
logger.error(
"Duplicate project found! Please archive all but one and restart. Exiting..."
)
logger.error(e)
sys.exit(1)
manager = TestRunManager(wait=args.wait)
manager.run()
def run_test(args):
if not TESTDROID:
logger.error(
"The environment variabels TESTDROID_URL, TESTDROID_APIKEY both need to be set."
)
sys.exit(1)
if args.bitbar_config is None:
bitbar_configpath = os.path.join(modulepath, "config", "config.yml")
else:
bitbar_configpath = args.bitbar_config
configuration.configure(
bitbar_configpath, filespath=args.files, update_bitbar=args.update_bitbar
)
run_test_for_project(args.project_name)
logger.info("run started for project '%s'" % args.project_name)
def main():
parser = argparse.ArgumentParser(
description="Mozilla Android Hardware testing at Bitbar.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Environment Variables:
The following environment variables must be set prior to attempting to
start the test_run_manager.
TESTDROID_URL
TESTDROID_APIKEY
To get additional help for each positional sub-command, add
--help to the sub-command.
Controlling the test_run_manager via signals:
Stop Now
kill -USR2 <pid>
Stop process while leaving test containers running.
Terminate Now
kill -TERM <pid>
Abort any running test containers and exit immediately.
""",
)
parser.add_argument(
"--files",
default=os.path.join(modulepath, "files"),
help="Directory where downloaded files are saved. "
"Defaults to %s/files" % modulepath,
)
parser.add_argument(
"--log-level",
dest="log_level",
choices=["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"],
default="INFO",
help="Logging level. Defaults to INFO.",
)
subparsers = parser.add_subparsers(
help="Specify one of the positional arguments to select the command to execute."
)
### download-test-apk ###
subparser = subparsers.add_parser(
"download-testdroid-apk",
help="Download Testdroid.apk from %s to files/ then exit." % testdroid_apk_url,
)
subparser.add_argument(
"--filename",
help="Specify filename to save Testdroid.apk to in the files directory.",
)
subparser.add_argument(
"--force", action="store_true", default=False, help="Overwrite existing file."
)
subparser.set_defaults(func=download_testdroid_apk)
### empty-test-zip ###
subparser = subparsers.add_parser(
"empty-test-zip",
help="Generate empty test zip file in files/ directory then exit.",
)
subparser.add_argument(
"--filename",
default="empty-test.zip",
help="Specify filename to save in the files directory. "
"Defaults to empty-test.zip.",
)
subparser.set_defaults(func=empty_test_zip)
### test-run-manager ###
subparser = subparsers.add_parser(
"start-test-run-manager", help="Run the test run manager."
)
subparser.add_argument(
"--bitbar-config", help="Path to Bitbar yaml configuration file."
)
subparser.add_argument(
"--wait",
dest="wait",
type=int,
default=20,
help="Seconds to wait between checks. Defaults to 20.",
)
subparser.add_argument(
"--update-bitbar",
action="store_true",
default=False,
help="Update the remote bitbar configuration to reflect the config file.",
)
subparser.set_defaults(func=test_run_manager)
### run-test ###
subparser = subparsers.add_parser(
"run-test", help="Run test for a project then exit."
)
subparser.add_argument(
"--bitbar-config", help="Path to Bitbar yaml configuration file."
)
subparser.add_argument(
"--update-bitbar",
action="store_true",
default=False,
help="Update the remote bitbar configuration to reflect the config file.",
)
subparser.add_argument(
"--project-name",
required=True,
help="Specify a project name for which to start a test.",
)
subparser.set_defaults(func=run_test)
args = parser.parse_args()
logger.setLevel(level=args.log_level)
try:
func = args.func
except AttributeError:
parser.error("too few arguments")
func(args)
if __name__ == "__main__":
main()