From 953caf7e6658ac9ff8e45e43c8a92b9d141fb346 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 8 May 2020 08:24:40 +0300 Subject: Change: let port be specified by config Have tests specify a random port. --- Cargo.lock | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++ ewww.py | 43 +++++++++---- src/main.rs | 27 ++++++++- 4 files changed, 260 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bacf618..7e7d8c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,31 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi 0.3.8", +] + +[[package]] +name = "anyhow" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9a60d744a80c30fcb657dfe2c1b22bcb3e814c1a1e3674f32bf5820b570fbff" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.8", +] + [[package]] name = "autocfg" version = "0.1.7" @@ -106,6 +132,21 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -134,6 +175,10 @@ checksum = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3" name = "ewww" version = "0.1.0" dependencies = [ + "anyhow", + "serde", + "serde_yaml", + "structopt", "tokio", "warp", ] @@ -327,6 +372,24 @@ dependencies = [ "http", ] +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4" +dependencies = [ + "libc", +] + [[package]] name = "http" version = "0.2.1" @@ -453,6 +516,12 @@ version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" +[[package]] +name = "linked-hash-map" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a" + [[package]] name = "log" version = "0.3.9" @@ -675,6 +744,32 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" +[[package]] +name = "proc-macro-error" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check 0.9.1", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f5444ead4e9935abd7f27dc51f7e852a0569ac888096d5ec2499470794e2e53" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "syn-mid", + "version_check 0.9.1", +] + [[package]] name = "proc-macro-hack" version = "0.5.15" @@ -943,6 +1038,20 @@ name = "serde" version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "serde_json" @@ -967,6 +1076,18 @@ dependencies = [ "url", ] +[[package]] +name = "serde_yaml" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "691b17f19fc1ec9d94ec0b5864859290dff279dbd7b03f017afda54eb36c3c35" +dependencies = [ + "dtoa", + "linked-hash-map", + "serde", + "yaml-rust", +] + [[package]] name = "sha-1" version = "0.8.2" @@ -1003,6 +1124,36 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "structopt" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863246aaf5ddd0d6928dfeb1a9ca65f505599e4e1b399935ef7e75107516b4ef" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d239ca4b13aee7a2142e6795cbd69e457665ff8037aed33b3effdc430d2f927a" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "1.0.18" @@ -1014,6 +1165,17 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "syn-mid" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tempfile" version = "3.1.0" @@ -1028,6 +1190,15 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "time" version = "0.1.43" @@ -1188,6 +1359,18 @@ dependencies = [ "smallvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" + +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" + [[package]] name = "unicode-xid" version = "0.2.0" @@ -1223,6 +1406,12 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version_check" version = "0.1.5" @@ -1395,3 +1584,12 @@ dependencies = [ "winapi 0.2.8", "winapi-build", ] + +[[package]] +name = "yaml-rust" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d" +dependencies = [ + "linked-hash-map", +] diff --git a/Cargo.toml b/Cargo.toml index 3af5cc9..ffcdfb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,7 @@ edition = "2018" [dependencies] warp = { version = "0.2", features = ["tls"] } tokio = { version = "0.2", features = ["macros"] } +serde_yaml = "0.8" +serde = { version = "1", features = ["derive"] } +structopt = "0.3" +anyhow = "1" \ No newline at end of file diff --git a/ewww.py b/ewww.py index 22c6507..8e179be 100644 --- a/ewww.py +++ b/ewww.py @@ -3,6 +3,7 @@ import json import os +import random import re import subprocess import time @@ -23,9 +24,16 @@ def _run(ctx, argv): # Check that latest call of _run ended with an specific exit code. def _run_exit(ctx, expected): + if ctx['exit'] != expected: + print('ctx:', ctx.as_dict()) assert_eq(ctx['exit'], expected) +# Name of Rust binary, debug-build. +def _binary(name): + return os.path.abspath(os.path.join(srcdir, 'target', 'debug', name)) + + # Write a file with given content. def _write(filename, content): open(filename, 'w').write(content) @@ -34,9 +42,10 @@ def _write(filename, content): # Construct a URL that points to server running on localhost by # replacing the actual scheme and host with ones that work for test. def _url(ctx, url): + port = ctx['config']['port'] c = urllib.parse.urlparse(url) host = c[1] - c = ('http', 'localhost:{}'.format(ctx['port'])) + c[2:] + c = ('http', 'localhost:{}'.format(port)) + c[2:] return urllib.parse.urlunparse(c), host @@ -57,15 +66,25 @@ def create_file(ctx, filename=None, content=None): # Start server using named configuration file. def start_server(ctx, filename=None): config = get_file(filename).decode('UTF-8') - ctx['config'] = yaml.safe_load(config) + config = yaml.safe_load(config) + config['port'] = random.randint(2000, 30000) + ctx['config'] = config + config = yaml.safe_dump(config) _write(filename, config) - pid = os.path.abspath('ewww.pid') - bin = os.path.join(srcdir, 'target', 'debug', 'ewww') - _run(ctx, ['/usr/sbin/daemonize', '-p', pid, bin, filename]) - ctx['pid'] = open(pid).read() - ctx['port'] = 3030 + + _run(ctx, [ + '/usr/sbin/daemonize', + '-c', os.getcwd(), + '-p', 'ewww.pid', + '-o', 'ewww.stdout', + '-e', 'ewww.stderr', + _binary('ewww'), + filename + ]) _run_exit(ctx, 0) + ctx['pid'] = open('ewww.pid').read() + # Make a HTTP request. def request(ctx, method=None, url=None): url, host = _url(ctx, url) @@ -81,14 +100,16 @@ def status_code_is(ctx, code=None): def http_header_is(ctx, header=None, value=None): s = ctx['stderr'] pattern = '\n< {}: {}'.format(header, value) - print('stderr:', repr(s)) - print('pattern:', repr(pattern)) + if pattern not in s: + print('stderr:', repr(s)) + print('pattern:', repr(pattern)) assert_eq(pattern in s, True) # Check a HTTP body response for latest request has a given value. def http_body_is(ctx, body=None): s = ctx['stdout'] body = body.encode('UTF8').decode('unicode-escape') - print('stdout:', repr(s)) - print('pattern:', repr(body)) + if body != s: + print('stdout:', repr(s)) + prin('pattern:', repr(body)) assert_eq(body, s) diff --git a/src/main.rs b/src/main.rs index 7fa1bda..35728a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,36 @@ +use std::path::{Path, PathBuf}; +use serde::Deserialize; +use structopt::StructOpt; use warp::Filter; +#[derive(Debug, Deserialize)] +struct Config { + port: u16, +} + +#[derive(Debug, StructOpt)] +#[structopt(name = "ewww", about = "Web server for static sites")] +struct Opt { + #[structopt(parse(from_os_str))] + config: PathBuf, +} + #[tokio::main] async fn main() { + let opt = Opt::from_args(); + let config = read_config(&opt.config).unwrap(); + let hello = warp::any() .map(|| "hello, world\n".to_string()); + eprintln!("starting server: {:?}", config); warp::serve(hello) - .run(([127, 0, 0, 1], 3030)) + .run(([127, 0, 0, 1], config.port)) .await; } + +fn read_config(filename: &Path) -> anyhow::Result { + let config = std::fs::read_to_string(filename)?; + let config: Config = serde_yaml::from_str(&config)?; + Ok(config) +} -- cgit v1.2.1