Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# top-most EditorConfig file
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.nix]
indent_style = space
indent_width = 2
119 changes: 13 additions & 106 deletions flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ in {
tests = mkOption {
type = types.attrsOf (types.submodule {
options = {
dist = mkOption { type = types.path; };
dist = mkOption {
type = types.path;
description = ''
Path to static directory to test. Great for static site generators.
'';
};

categories = mkOption {
default = { };
Expand Down Expand Up @@ -68,112 +73,14 @@ in {

config = {
perSystem = { config, pkgs, ... }:
let
{
checks = lib.mapAttrs (name: test:
pkgs.nixosTest {
inherit name;
nodes = {
server = { pkgs, config, ... }: {
virtualisation.cores = 4;

environment.variables.CHROME_PATH =
lib.getExe pkgs.ungoogled-chromium;

services.h2o = {
enable = true;
settings = {
compress = "ON";
http2-reprioritize-blocking-assets = "ON";
ssl-offload = "kernel";
};
hosts."acme.test" = {
tls = {
recommendations = "modern";
policy = "force";
quic = { };
identity = [
{
key-file = pkgs.path + "/nixos/tests/common/acme/server/acme.test.key.pem";
certificate-file = pkgs.path + "/nixos/tests/common/acme/server/acme.test.cert.pem";
}
];
extraSettings = {
# when using common ACME certs, disable talking to CA
ocsp-update-interval = 0;
};
};
settings = {
paths."/" = {
"file.dir" = test.dist;
};
};
};
};

networking = {
firewall = {
allowedTCPPorts = [ config.services.h2o.defaultTLSListenPort ];
allowedUDPPorts = [ config.services.h2o.defaultTLSListenPort ];
};
extraHosts = ''
127.0.0.1 acme.test
'';
};
# Required to make TLS work
security.pki.certificates = [
(builtins.readFile (pkgs.path + "/nixos/tests/common/acme/server/ca.cert.pem"))
];
};
};

testScript = { nodes, ... }: ''
import json
import os

start_all()
server.wait_for_open_port(${builtins.toString nodes.server.services.h2o.defaultTLSListenPort})
server.wait_for_unit("h2o.service")

report_path = "/tmp/lighthouse-report.json"

server.succeed("CI=1 ${lib.getExe config.lighthouse.package} https://acme.test --output json --output-path {} --only-categories accessibility,best-practices,performance,seo --skip-audits valid-source-maps --chrome-flags=\"--headless --no-sandbox\"".format(report_path))

server.copy_from_vm(report_path)

with open("{}/lighthouse-report.json".format(os.environ["out"]), "r") as f:
report = json.load(f)
categories = report["categories"]

performance_score = categories["performance"]["score"]
assert performance_score >= ${
toString test.categories.performance
}, "performance score should be at least ${
toString test.categories.performance
}, but it was {}".format(performance_score)

accessibility_score = categories["accessibility"]["score"]
assert accessibility_score >= ${
toString test.categories.accessibility
}, "accessibility score should be at least ${
toString test.categories.accessibility
}, but it was {}".format(accessibility_score)

seo_score = categories["seo"]["score"]
assert seo_score >= ${
toString test.categories.seo
}, "seo score should be at least ${
toString test.categories.seo
}, but it was {}".format(seo_score)

best_practices_score = categories["best-practices"]["score"]
assert best_practices_score >= ${
toString test.categories.bestPractices
}, "best-practices score should be at least ${
toString (test.categories.bestPractices)
}, but it was {}".format(best_practices_score)
'';
}) config.lighthouse.tests;
in { inherit checks; };
pkgs.callPackage ./nixos-test.nix {
inherit name test;
google-lighthouse = config.lighthouse.package;
}
) config.lighthouse.tests;
};
};
}

98 changes: 98 additions & 0 deletions nixos-test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
lib,
nixosTest,
google-lighthouse,
name,
test,
}:

nixosTest {
inherit name;
nodes = {
server =
{ pkgs, config, ... }:
{
virtualisation.cores = 4;

environment.variables.CHROME_PATH = lib.getExe pkgs.ungoogled-chromium;

services.h2o = {
enable = true;
settings = {
compress = "ON";
http2-reprioritize-blocking-assets = "ON";
ssl-offload = "kernel";
};
hosts."acme.test" = {
tls = {
recommendations = "modern";
policy = "force";
quic = { };
identity = [
{
key-file = pkgs.path + "/nixos/tests/common/acme/server/acme.test.key.pem";
certificate-file = pkgs.path + "/nixos/tests/common/acme/server/acme.test.cert.pem";
}
];
extraSettings = {
# when using common ACME certs, disable talking to CA
ocsp-update-interval = 0;
};
};
settings = {
paths."/" = {
"file.dir" = test.dist;
};
};
};
};

networking = {
firewall = {
allowedTCPPorts = [ config.services.h2o.defaultTLSListenPort ];
allowedUDPPorts = [ config.services.h2o.defaultTLSListenPort ];
};
extraHosts = ''
127.0.0.1 acme.test
'';
};
# Required to make TLS work
security.pki.certificates = [
(builtins.readFile (pkgs.path + "/nixos/tests/common/acme/server/ca.cert.pem"))
];
};
};

testScript =
{ nodes, ... }: # python
''
import json
import os

start_all()
server.wait_for_open_port(${builtins.toString nodes.server.services.h2o.defaultTLSListenPort})
server.wait_for_unit("h2o.service")

report_path = "/tmp/lighthouse-report.json"

server.succeed("CI=1 ${lib.getExe google-lighthouse} https://acme.test --output json --output-path {} --only-categories accessibility,best-practices,performance,seo --skip-audits valid-source-maps --chrome-flags=\"--headless --no-sandbox\"".format(report_path))

server.copy_from_vm(report_path)

with open("{}/lighthouse-report.json".format(os.environ["out"]), "r") as f:
report = json.load(f)
categories = report["categories"]

performance_score = categories["performance"]["score"]
assert performance_score >= ${toString test.categories.performance}, "performance score should be at least ${toString test.categories.performance}, but it was {}".format(performance_score)

accessibility_score = categories["accessibility"]["score"]
assert accessibility_score >= ${toString test.categories.accessibility}, "accessibility score should be at least ${toString test.categories.accessibility}, but it was {}".format(accessibility_score)

seo_score = categories["seo"]["score"]
assert seo_score >= ${toString test.categories.seo}, "seo score should be at least ${toString test.categories.seo}, but it was {}".format(seo_score)

best_practices_score = categories["best-practices"]["score"]
assert best_practices_score >= ${toString test.categories.bestPractices}, "best-practices score should be at least ${toString (test.categories.bestPractices)}, but it was {}".format(best_practices_score)
'';
}