Skip to content

Commit

Permalink
Separate out wpt-specific parts of config from wptserve
Browse files Browse the repository at this point in the history
  • Loading branch information
jgraham committed May 6, 2018
1 parent 5824152 commit 9f77a66
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 123 deletions.
30 changes: 0 additions & 30 deletions config.default.json

This file was deleted.

3 changes: 1 addition & 2 deletions tools/ci/make_hosts_file.py
Expand Up @@ -11,7 +11,6 @@ def create_parser():
return parser

def run(**kwargs):
config = load_config(os.path.join(repo_root, "config.default.json"),
os.path.join(repo_root, "config.json"))
config = load_config(os.path.join(repo_root, "config.json"))

print(make_hosts_file(config, kwargs["address"]))
63 changes: 52 additions & 11 deletions tools/serve/serve.py
Expand Up @@ -621,16 +621,10 @@ def iter_procs(servers):
yield server.proc


def load_config(default_path, override_path=None, **kwargs):
if os.path.exists(default_path):
with open(default_path) as f:
base_obj = json.load(f)
else:
raise ValueError("Config path %s does not exist" % default_path)
def load_config(override_path=None, **kwargs):
rv = Config()

rv = Config(**base_obj)

if os.path.exists(override_path):
if override_path and os.path.exists(override_path):
with open(override_path) as f:
override_obj = json.load(f)
rv.update(override_obj)
Expand Down Expand Up @@ -665,10 +659,47 @@ def load_config(default_path, override_path=None, **kwargs):

_not_subdomains = {u"nonexistent-origin"}


class Config(config.Config):
"""serve config
this subclasses wptserve.config.Config to add serve config options"""

_default = {
"browser_host": "web-platform.test",
"alternate_hosts": {
"alt": "not-web-platform.test"
},
"doc_root": repo_root,
"ws_doc_root": os.path.join(repo_root, "websockets", "handlers"),
"server_host": None,
"ports": {
"http": [8000, "auto"],
"https": [8443],
"ws": ["auto"],
"wss": ["auto"]
},
"check_subdomains": True,
"log_level": "debug",
"bind_address": True,
"ssl": {
"type": "pregenerated",
"encrypt_after_connect": False,
"openssl": {
"openssl_binary": "openssl",
"base_path": "_certs",
"force_regenerate": False,
"base_conf_path": None
},
"pregenerated": {
"host_key_path": os.path.join(repo_root, "tools", "certs", "web-platform.test.key"),
"host_cert_path": os.path.join(repo_root, "tools", "certs", "web-platform.test.pem")
},
"none": {}
},
"aliases": []
}

def __init__(self, *args, **kwargs):
super(Config, self).__init__(
subdomains=_subdomains,
Expand All @@ -677,6 +708,17 @@ def __init__(self, *args, **kwargs):
**kwargs
)

@property
def ws_doc_root(self):
if self._ws_doc_root is not None:
return self._ws_doc_root
else:
return os.path.join(self.doc_root, "websockets", "handlers")

@ws_doc_root.setter
def ws_doc_root(self, v):
self._ws_doc_root = v


def get_parser():
parser = argparse.ArgumentParser()
Expand All @@ -692,8 +734,7 @@ def get_parser():


def run(**kwargs):
config = load_config(os.path.join(repo_root, "config.default.json"),
os.path.join(repo_root, "config.json"),
config = load_config(os.path.join(repo_root, "config.json"),
**kwargs)

global logger
Expand Down
35 changes: 34 additions & 1 deletion tools/serve/test_serve.py
@@ -1,7 +1,15 @@
import pickle
import os

import pytest

import localpaths
from . import serve
from .serve import Config


def test_make_hosts_file():
c = serve.Config(browser_host="foo.bar", alternate_hosts={"alt": "foo2.bar"})
c = Config(browser_host="foo.bar", alternate_hosts={"alt": "foo2.bar"})
hosts = serve.make_hosts_file(c, "192.168.42.42")
lines = hosts.split("\n")
assert set(lines) == {"",
Expand All @@ -20,3 +28,28 @@ def test_make_hosts_file():
"192.168.42.42\txn--n8j6ds53lwwkrqhv28a.foo.bar",
"192.168.42.42\txn--n8j6ds53lwwkrqhv28a.foo2.bar"}
assert lines[-1] == ""


def test_ws_doc_root_default():
c = Config()
assert c.ws_doc_root == os.path.join(localpaths.repo_root, "websockets", "handlers")


def test_init_ws_doc_root():
c = Config(ws_doc_root="/")
assert c.doc_root == localpaths.repo_root # check this hasn't changed
assert c._ws_doc_root == "/"
assert c.ws_doc_root == "/"


def test_set_ws_doc_root():
c = Config()
c.ws_doc_root = "/"
assert c.doc_root == localpaths.repo_root # check this hasn't changed
assert c._ws_doc_root == "/"
assert c.ws_doc_root == "/"


def test_pickle():
# Ensure that the config object can be pickled
pickle.dumps(Config())
3 changes: 1 addition & 2 deletions tools/wpt/run.py
Expand Up @@ -96,8 +96,7 @@ def args_general(kwargs):

def check_environ(product):
if product not in ("firefox", "servo"):
config = serve.load_config(os.path.join(wpt_root, "config.default.json"),
os.path.join(wpt_root, "config.json"))
config = serve.load_config(os.path.join(wpt_root, "config.json"))
expected_hosts = set(config.all_domains_set)
missing_hosts = set(expected_hosts)
if platform.uname()[0] != "Windows":
Expand Down
13 changes: 2 additions & 11 deletions tools/wptrunner/wptrunner/environment.py
Expand Up @@ -10,12 +10,12 @@

from wptlogging import LogLevelRewriter
from wptserve.handlers import StringHandler
from wptserve import sslutils

here = os.path.split(__file__)[0]
repo_root = os.path.abspath(os.path.join(here, os.pardir, os.pardir, os.pardir))

serve = None
sslutils = None


def do_delayed_imports(logger, test_paths):
Expand All @@ -31,11 +31,6 @@ def do_delayed_imports(logger, test_paths):
except ImportError:
failed.append("serve")

try:
import sslutils
except ImportError:
failed.append("sslutils")

if failed:
logger.critical(
"Failed to import %s. Ensure that tests path %s contains web-platform-tests" %
Expand Down Expand Up @@ -134,13 +129,9 @@ def process_interrupts(self):
signal.signal(signal.SIGINT, signal.SIG_DFL)

def load_config(self):
default_config_path = os.path.join(serve_path(self.test_paths), "config.default.json")
override_path = os.path.join(serve_path(self.test_paths), "config.json")

with open(default_config_path) as f:
default_config = json.load(f)

config = serve.Config(override_ssl_env=self.ssl_env, **default_config)
config = serve.Config(override_ssl_env=self.ssl_env)

config.ports = {
"http": [8000, 8001],
Expand Down
4 changes: 1 addition & 3 deletions tools/wptrunner/wptrunner/tests/test_products.py
Expand Up @@ -9,10 +9,8 @@
from .base import all_products, active_products

sys.path.insert(0, join(dirname(__file__), "..", "..", "..", "..")) # repo root

from tools import localpaths

import sslutils
from wptserve import sslutils

from wptrunner import environment
from wptrunner import products
Expand Down
4 changes: 2 additions & 2 deletions tools/wptserve/setup.py
@@ -1,6 +1,6 @@
from setuptools import setup

PACKAGE_VERSION = '1.4.0'
PACKAGE_VERSION = '2.0'
deps = ["six>=1.8"]

setup(name='wptserve',
Expand All @@ -16,7 +16,7 @@
author_email='james@hoppipolla.co.uk',
url='http://wptserve.readthedocs.org/',
license='BSD',
packages=['wptserve'],
packages=['wptserve', 'wptserve.sslutils'],
include_package_data=True,
zip_safe=False,
install_requires=deps
Expand Down
37 changes: 2 additions & 35 deletions tools/wptserve/tests/test_config.py
@@ -1,12 +1,9 @@
import logging
import os
import pickle
from logging import handlers

import pytest

import localpaths

config = pytest.importorskip("wptserve.config")


Expand Down Expand Up @@ -207,11 +204,6 @@ def test_ports_openssl():
assert ports["wss"] == [1004]


def test_doc_root_default():
c = config.Config()
assert c.doc_root == localpaths.repo_root


def test_init_doc_root():
c = config.Config(doc_root="/")
assert c._doc_root == "/"
Expand All @@ -225,47 +217,22 @@ def test_set_doc_root():
assert c.doc_root == "/"


def test_ws_doc_root_default():
c = config.Config()
assert c.ws_doc_root == os.path.join(localpaths.repo_root, "websockets", "handlers")


def test_ws_doc_root_from_doc_root():
c = config.Config(doc_root="/foo")
assert c.ws_doc_root == os.path.join("/foo", "websockets", "handlers")


def test_init_ws_doc_root():
c = config.Config(ws_doc_root="/")
assert c.doc_root == localpaths.repo_root # check this hasn't changed
assert c._ws_doc_root == "/"
assert c.ws_doc_root == "/"


def test_set_ws_doc_root():
c = config.Config()
c.ws_doc_root = "/"
assert c.doc_root == localpaths.repo_root # check this hasn't changed
assert c._ws_doc_root == "/"
assert c.ws_doc_root == "/"


def test_server_host_from_browser_host():
c = config.Config(browser_host="foo.bar")
assert c.server_host == "foo.bar"


def test_init_server_host():
c = config.Config(server_host="foo.bar")
assert c.browser_host == "web-platform.test" # check this hasn't changed
assert c.browser_host == "localhost" # check this hasn't changed
assert c._server_host == "foo.bar"
assert c.server_host == "foo.bar"


def test_set_server_host():
c = config.Config()
c.server_host = "/"
assert c.browser_host == "web-platform.test" # check this hasn't changed
assert c.browser_host == "localhost" # check this hasn't changed
assert c._server_host == "/"
assert c.server_host == "/"

Expand Down

0 comments on commit 9f77a66

Please sign in to comment.