Skip to content

Commit

Permalink
Add tests for plume webserver (#513)
Browse files Browse the repository at this point in the history
* begin setup front-end test environment with selenium
* run migrations before tests
* use https for tests
  • Loading branch information
trinity-1686a authored Apr 6, 2019
1 parent 1f7ff62 commit eabe73d
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 45 deletions.
91 changes: 72 additions & 19 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
version: 2.1

aliases:
- &plume-docker
image: plumeorg/plume-buildenv:v0.0.5
- &defaults
docker:
- image: plumeorg/plume-buildenv:v0.0.3
- *plume-docker
working_directory: ~/projects/Plume
- &postgresql
docker:
- image: plumeorg/plume-buildenv:v0.0.3
- *plume-docker
- image: circleci/postgres:9.6-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_DB: plume
working_directory: ~/projects/Plume
- &selenium
docker:
- *plume-docker
- image: elgalu/selenium:latest
working_directory: ~/projects/Plume
- &postgresql_selenium
docker:
- *plume-docker
- image: circleci/postgres:9.6-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_DB: plume
- image: elgalu/selenium:latest
working_directory: ~/projects/Plume

- &attach_workspace
attach_workspace:
Expand All @@ -25,13 +41,13 @@ aliases:

- &env_postgresql
environment:
MIGRATION_DIR: migrations/postgres
MIGRATION_DIRECTORY: migrations/postgres
FEATURES: postgres
DATABASE_URL: postgres://postgres@localhost/plume
RUST_TEST_THREADS: 1
- &env_sqlite
environment:
MIGRATION_DIR: migrations/sqlite
MIGRATION_DIRECTORY: migrations/sqlite
FEATURES: sqlite
DATABASE_URL: plume.sqlite3
RUST_TEST_THREADS: 1
Expand Down Expand Up @@ -101,26 +117,48 @@ aliases:
name: clippy
command: cargo clippy --no-default-features --features="${FEATURES}" --release -p plume-cli -- -D warnings
- *save_cache_cli
- &test_plume
- &test_unit
steps:
- *attach_workspace
- run:
name: Set cache key
command: echo "$FEATURES" > /FEATURES
- run:
name: Set compiler flags
# rustflags = ["-Clink-dead-code", "-Clink-args=-Xlinker --no-keep-memory -Xlinker --reduce-memory-overheads"]
command: echo "W3RhcmdldC54ODZfNjQtdW5rbm93bi1saW51eC1nbnVdCnJ1c3RmbGFncyA9IFsiLUNsaW5rLWRlYWQtY29kZSIsICItQ2xpbmstYXJncz0tWGxpbmtlciAtLW5vLWtlZXAtbWVtb3J5IC1YbGlua2VyIC0tcmVkdWNlLW1lbW9yeS1vdmVyaGVhZHMiXQoK" | base64 -d >> ~/.cargo/config
- *restore_cache_plume_dead_code
- run:
name: clippy
command: cargo clippy --no-default-features --features="${FEATURES}" --release -- -D warnings
- run:
- run:
name: compile test
command: cargo test --no-default-features --features="${FEATURES}" --all --exclude plume-front --no-run || cargo test --no-default-features --features="${FEATURES}" --all --exclude plume-front --no-run
- run:
name: run test and upload coverage
command: ./script/compute_coverage.sh
- run:
name: run test
command: ./script/run_unit_test.sh
- run:
name: upload coverage
command: ./script/upload_coverage.sh unit
- *save_cache_plume_dead_code

- &test_browser
steps:
- *attach_workspace
- run:
name: Set cache key
command: echo "$FEATURES" > /FEATURES
- *restore_cache_plume_dead_code
- run:
name: install server
command: cargo install --debug --no-default-features --features="${FEATURES}",test --path . || cargo install --debug --no-default-features --features="${FEATURES}",test --path .
- run:
name: install plm
command: cargo install --debug --no-default-features --features="${FEATURES}" --path plume-cli || cargo install --debug --no-default-features --features="${FEATURES}" --path plume-cli
- run:
name: run test
command: ./script/run_browser_test.sh
environment:
BROWSER: firefox
- run:
name: upload coverage
command: ./script/upload_coverage.sh integration
- *save_cache_plume_dead_code

jobs:
Expand Down Expand Up @@ -160,16 +198,25 @@ jobs:
<<: *env_sqlite
<<: *test_cli

test_plume_postgresql:
test_unit_postgresql:
<<: *postgresql
<<: *env_postgresql
<<: *test_plume
<<: *test_unit

test_plume_sqlite:
test_unit_sqlite:
<<: *defaults
<<: *env_sqlite
<<: *test_plume
<<: *test_unit

test_browser_postgresql:
<<: *postgresql_selenium
<<: *env_postgresql
<<: *test_browser

test_browser_sqlite:
<<: *selenium
<<: *env_sqlite
<<: *test_browser

workflows:
version: 2
Expand All @@ -188,9 +235,15 @@ workflows:
- test_cli_sqlite:
requires:
- download_deps
- test_plume_postgresql:
- test_unit_postgresql:
requires:
- download_deps
- test_plume_sqlite:
- test_unit_sqlite:
requires:
- download_deps
- test_browser_postgresql:
requires:
- build_web
- test_browser_sqlite:
requires:
- build_web
6 changes: 6 additions & 0 deletions .circleci/images/plume-buildenv/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
localhost:443 {
proxy / localhost:7878 {
transparent
}
tls self_signed
}
19 changes: 16 additions & 3 deletions .circleci/images/plume-buildenv/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ ENV PATH="/root/.cargo/bin:${PATH}"
#install native/circleci/build dependancies
RUN apt update &&\
apt install -y git ssh tar gzip ca-certificates &&\
apt install -y binutils-dev build-essential cmake curl gcc gettext git libcurl4-openssl-dev libdw-dev libelf-dev libiberty-dev libpq-dev libsqlite3-dev libssl-dev make openssl pkg-config postgresql postgresql-contrib python zlib1g-dev
apt install -y binutils-dev build-essential cmake curl gcc gettext git libcurl4-openssl-dev libdw-dev libelf-dev libiberty-dev libpq-dev libsqlite3-dev libssl-dev make openssl pkg-config postgresql postgresql-contrib python zlib1g-dev python3-pip

#install and configure rust
RUN curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly-2019-03-23 -y &&\
rustup component add rustfmt clippy &&\
rustup component add rust-std --target wasm32-unknown-unknown &&\
cargo install cargo-web &&\
rustup component add rust-std --target wasm32-unknown-unknown

#compile some deps
RUN cargo install cargo-web &&\
cargo install diesel_cli --no-default-features --features postgres,sqlite --version '=1.3.0' &&\
rm -fr ~/.cargo/registry

#install coverage tools
RUN curl -L https://github.com/SimonKagstrom/kcov/archive/master.tar.gz | tar xz &&\
mkdir -p kcov-master/build && cd kcov-master/build && cmake .. && make &&\
make install && cd ../.. && rm -rf kcov-master

#set some compilation parametters
COPY cargo_config /root/.cargo/config

#install selenium for front end tests
RUN pip3 install selenium

#install and configure caddy
RUN curl https://getcaddy.com | bash -s personal
COPY Caddyfile /Caddyfile
3 changes: 3 additions & 0 deletions .circleci/images/plume-buildenv/cargo_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[target.x86_64-unknown-linux-gnu]
# link dead code for coverage, attempt to reduce linking memory usage to not get killed
rustflags = ["-Clink-dead-code", "-Clink-args=-Xlinker --no-keep-memory -Xlinker --reduce-memory-overheads"]
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ main.css
*.wasm
*.js
.buildconfig
__pycache__
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ default = ["postgres"]
postgres = ["plume-models/postgres", "diesel/postgres"]
sqlite = ["plume-models/sqlite", "diesel/sqlite"]
debug-mailer = []
test = []

[workspace]
members = ["plume-api", "plume-cli", "plume-models", "plume-common", "plume-front"]
2 changes: 1 addition & 1 deletion plume-models/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ lazy_static! {
pub static ref CONFIG: Config = Config {
base_url: var("BASE_URL").unwrap_or_else(|_| format!(
"127.0.0.1:{}",
var("ROCKET_PORT").unwrap_or_else(|_| "8000".to_owned())
var("ROCKET_PORT").unwrap_or_else(|_| "7878".to_owned())
)),
db_name: DB_NAME,
#[cfg(feature = "postgres")]
Expand Down
Empty file added script/browser_test/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions script/browser_test/instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/python3
from utils import Browser

class InstanceName(Browser):
def test_name_in_title(self):
self.get("/")
self.assertIn("plume-test", self.driver.title)
22 changes: 22 additions & 0 deletions script/browser_test/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/python3
import unittest,os
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class Browser(unittest.TestCase):
def setUp(self):
if os.environ["BROWSER"] == "firefox":
capabilities=DesiredCapabilities.FIREFOX
elif os.environ["BROWSER"] == "chrome":
capabilities=DesiredCapabilities.CHROME
else:
raise Exception("No browser was requested")
capabilities['acceptSslCerts'] = True
self.driver = webdriver.Remote(
command_executor='http://localhost:24444/wd/hub',
desired_capabilities=capabilities)
def tearDown(self):
self.driver.close()

def get(self, url):
return self.driver.get("https://localhost" + url)
26 changes: 26 additions & 0 deletions script/run_browser_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -eo pipefail

export ROCKET_SECRET_KEY="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="

mkdir -p "target/cov/plume"
mkdir -p "target/cov/plm"
plm='kcov --exclude-pattern=/.cargo,/usr/lib --verify target/cov/plm plm'

diesel migration run
diesel migration redo
$plm instance new -d plume-test.local -n plume-test
$plm users new -n admin -N 'Admin' -e 'email@exemple.com' -p 'password'
$plm search init

kcov --exclude-pattern=/.cargo,/usr/lib --verify target/cov/plume plume &
caddy -conf /Caddyfile &

until curl http://localhost:7878/test/health -f; do sleep 1; done 2>/dev/null >/dev/null

cd $(dirname $0)/browser_test/
python3 -m unittest *.py

kill -SIGINT %1
kill -SIGKILL %2
wait
1 change: 0 additions & 1 deletion script/compute_coverage.sh → script/run_unit_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ for file in target/debug/*-*[^\.d]; do
kcov --exclude-pattern=/.cargo,/usr/lib --verify "target/cov/$filename" "$file"
fi
done
bash <(curl -s https://codecov.io/bash)
3 changes: 3 additions & 0 deletions script/upload_coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -eo pipefail
bash <(curl -s https://codecov.io/bash) -F $1
22 changes: 9 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ mod mail;
#[macro_use]
mod template_utils;
mod routes;
#[cfg(feature = "test")]
mod test_routes;

include!(concat!(env!("OUT_DIR"), "/templates.rs"));

Expand Down Expand Up @@ -122,6 +124,7 @@ Then try to restart Plume

let search_unlocker = searcher.clone();
ctrlc::set_handler(move || {
search_unlocker.commit();
search_unlocker.drop_writer();
exit(0);
})
Expand All @@ -133,7 +136,7 @@ Then try to restart Plume
println!("Please refer to the documentation to see how to configure it.");
}

rocket::custom(CONFIG.rocket.clone().unwrap())
let rocket = rocket::custom(CONFIG.rocket.clone().unwrap())
.mount(
"/",
routes![
Expand Down Expand Up @@ -258,16 +261,6 @@ Then try to restart Plume
"/@/<name>/inbox".to_owned(),
rocket::http::Method::Post,
),
(
"/login".to_owned(),
"/login".to_owned(),
rocket::http::Method::Post,
),
(
"/users/new".to_owned(),
"/users/new".to_owned(),
rocket::http::Method::Post,
),
(
"/api/<path..>".to_owned(),
"/api/<path..>".to_owned(),
Expand All @@ -276,6 +269,9 @@ Then try to restart Plume
])
.finalize()
.expect("main: csrf fairing creation error"),
)
.launch();
);

#[cfg(feature = "test")]
let rocket = rocket.mount("/test", routes![test_routes::health,]);
rocket.launch();
}
5 changes: 1 addition & 4 deletions src/routes/likes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ pub fn create(
#[post("/~/<blog>/<slug>/like", rank = 2)]
pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash<Redirect> {
utils::requires_login(
&i18n!(
i18n.catalog,
"To like a post, you need to be logged in"
),
&i18n!(i18n.catalog, "To like a post, you need to be logged in"),
uri!(create: blog = blog, slug = slug),
)
}
5 changes: 1 addition & 4 deletions src/routes/reshares.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ pub fn create(
#[post("/~/<blog>/<slug>/reshare", rank = 1)]
pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash<Redirect> {
utils::requires_login(
&i18n!(
i18n.catalog,
"To reshare a post, you need to be logged in"
),
&i18n!(i18n.catalog, "To reshare a post, you need to be logged in"),
uri!(create: blog = blog, slug = slug),
)
}
2 changes: 2 additions & 0 deletions src/test_routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[get("/health")]
pub fn health() {}

0 comments on commit eabe73d

Please sign in to comment.