1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4from __future__ import absolute_import
5
6import os
7
8from logger.logger import RaptorLogger
9
10
11here = os.path.abspath(os.path.dirname(__file__))
12webext_dir = os.path.join(os.path.dirname(here), "webext", "raptor")
13LOG = RaptorLogger(component="raptor-gen-test-config")
14
15FILE_CONTENTS = """// this file is auto-generated by raptor, do not edit directly
16function getTestConfig() {{
17  return {{
18    "cs_port": "{control_server_port}",
19    "test_name": "{test}",
20    "test_settings_url": "http://{host}:{control_server_port}/json/{test}.json",
21    "post_startup_delay": "{post_startup_delay}",
22    "benchmark_port": "{benchmark_port}",
23    "host": "{host}",
24    "debug_mode": "{debug_mode}",
25    "browser_cycle": "{browser_cycle}"
26  }};
27}}
28
29"""
30
31
32def gen_test_config(
33    test,
34    cs_port,
35    post_startup_delay,
36    host="127.0.0.1",
37    b_port=0,
38    debug_mode=0,
39    browser_cycle=1,
40):
41    LOG.info("writing test settings into background js, so webext can get it")
42
43    if host is None or cs_port is None:
44        raise ValueError(
45            "Invalid URL for control server: http://{}:{}".format(host, cs_port)
46        )
47
48    config = FILE_CONTENTS.format(
49        benchmark_port=b_port,
50        browser_cycle=browser_cycle,
51        control_server_port=cs_port,
52        debug_mode=debug_mode,
53        host=host,
54        post_startup_delay=post_startup_delay,
55        test=test,
56    )
57
58    webext_background_script = os.path.join(webext_dir, "auto_gen_test_config.js")
59    with open(webext_background_script, "w") as f:
60        f.write(config)
61
62    LOG.info("finished writing test config to %s" % webext_background_script)
63