ci: implement tests for rust

This commit is contained in:
Nikos Papadakis 2023-10-13 22:18:39 +03:00
parent 2955f68562
commit 2bab72a9b7
Signed by untrusted user who does not match committer: nikos
GPG key ID: 78871F9905ADFF02
8 changed files with 83 additions and 238 deletions

View file

@ -1,7 +1,5 @@
#depends_on:
# - build
# - test
# - lint
depends_on:
- test
when:
- event: push
@ -10,22 +8,21 @@ when:
matrix:
BUILD_TARGET:
- x86_64
- aarch64
- x86_64-linux
- aarch64-linux
steps:
build:
image: nixos/nix:latest
image: git.nikos.gg/prymn/images/nix:89bb1c18
secrets: [ ATTIC_SECRET ]
commands:
- export AWS_ACCESS_KEY_ID="$${R2_CACHE_ACCESS_KEY}"
- export AWS_SECRET_ACCESS_KEY="$${R2_CACHE_SECRET_KEY}"
- echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf
- echo "substituters = s3://prymn-cache?endpoint=75178f9eca227dea51b3db4db2c15a5a.r2.cloudflarestorage.com&region=auto https://cache.nixos.org/" >> /etc/nix/nix.conf
- nix build -L ".#agent.${BUILD_TARGET}"
- mkdir -p "dist/${BUILD_TARGET}-unknown-linux-musl"
- cp result/bin/prymn_agent "dist/${BUILD_TARGET}-unknown-linux-musl/"
- nix copy --to 's3://prymn-cache?endpoint=75178f9eca227dea51b3db4db2c15a5a.r2.cloudflarestorage.com&region=auto' ".#agent.${BUILD_TARGET}"
secrets: [ R2_CACHE_ACCESS_KEY, R2_CACHE_SECRET_KEY ]
- attic login local http://172.17.0.1:8080/ "$${ATTIC_SECRET}"
- attic use prymn
- nix build -L ".#agent-${BUILD_TARGET}"
- attic push prymn ./result
- mkdir -p "dist/${BUILD_TARGET}"
- cp result/bin/prymn_agent "dist/${BUILD_TARGET}/"
release:
image: woodpeckerci/plugin-s3

View file

@ -1,65 +0,0 @@
when:
- event: [pull_request, tag]
- event: push
branch:
- ${CI_REPO_DEFAULT_BRANCH}
variables:
- &rust_image 'git.nikos.gg/prymn/images/rust:33331dfc'
- &elixir_image 'git.nikos.gg/prymn/images/elixir:1794cee2'
- &cache_settings
endpoint: https://75178f9eca227dea51b3db4db2c15a5a.r2.cloudflarestorage.com
bucket: prymn-cache
region: auto
access-key:
from_secret: R2_CACHE_ACCESS_KEY
secret-key:
from_secret: R2_CACHE_SECRET_KEY
steps:
restore-elixir-cache:
image: meltwater/drone-cache
pull: true
settings:
<<: *cache_settings
cache_key: '{{ .Commit.Branch }}-{{ checksum "app/mix.lock" }}'
restore: true
mount:
- 'app/deps'
- 'app/_build/dev'
rust-build:
image: *rust_image
group: build
secrets: [ R2_CACHE_ACCESS_KEY, R2_CACHE_SECRET_KEY ]
commands:
- export AWS_ACCESS_KEY_ID="$${R2_CACHE_ACCESS_KEY}"
- export AWS_SECRET_ACCESS_KEY="$${R2_CACHE_SECRET_KEY}"
- cargo check
- ls $CARGO_HOME
when:
- path:
- "**/*.rs"
- "Cargo.*"
elixir-build:
image: *elixir_image
group: build
commands:
- cd app
- mix do deps.get, compile
when:
- path:
- "**/*.{ex,exs}"
- "mix.*"
rebuild-elixir-cache:
image: meltwater/drone-cache
pull: true
settings:
<<: *cache_settings
cache_key: '{{ .Commit.Branch }}-{{ checksum "app/mix.lock" }}'
rebuild: true
mount:
- 'app/deps'
- 'app/_build/dev'

View file

@ -2,6 +2,8 @@ when:
- event: push
branch:
- ${CI_REPO_DEFAULT_BRANCH}
path:
- get_prymn.sh
steps:
release:

View file

@ -1,76 +0,0 @@
depends_on:
- build
- test
when:
- event: [pull_request, tag]
- event: push
branch:
- ${CI_REPO_DEFAULT_BRANCH}
variables:
- &rust_image 'git.nikos.gg/prymn/images/rust:33331dfc'
- &elixir_image 'git.nikos.gg/prymn/images/elixir:1794cee2'
- &when_elixir
- path:
- "**/*.{ex,exs}"
- "mix.*"
- &cache_settings
endpoint: https://75178f9eca227dea51b3db4db2c15a5a.r2.cloudflarestorage.com
bucket: prymn-cache
region: auto
access-key:
from_secret: R2_CACHE_ACCESS_KEY
secret-key:
from_secret: R2_CACHE_SECRET_KEY
steps:
restore-elixir-cache:
image: meltwater/drone-cache
pull: true
settings:
<<: *cache_settings
cache_key: '{{ .Commit.Branch }}-{{ checksum "app/mix.lock" }}'
restore: true
mount:
- 'app/deps'
- 'app/_build/dev'
rust-lint:
image: *rust_image
group: lint
secrets: [ R2_CACHE_ACCESS_KEY, R2_CACHE_SECRET_KEY ]
commands:
- export AWS_ACCESS_KEY_ID="$${R2_CACHE_ACCESS_KEY}"
- export AWS_SECRET_ACCESS_KEY="$${R2_CACHE_SECRET_KEY}"
- rustup component add clippy rustfmt
- cargo clippy -- -D warnings
- cargo fmt --all -- --check
when:
- path:
- "**/*.rs"
- "Cargo.*"
elixir-credo:
image: *elixir_image
group: lint
commands:
- cd app
- mix credo
when: *when_elixir
elixir-dialyzer:
image: *elixir_image
group: lint
commands:
- cd app
- mix dialyzer --format short
when: *when_elixir
elixir-format:
image: *elixir_image
group: lint
commands:
- cd app
- mix format --check-formatted
when: *when_elixir

View file

@ -1,66 +1,15 @@
depends_on:
- build
when:
- event: [pull_request, tag]
- event: push
branch:
- ${CI_REPO_DEFAULT_BRANCH}
services:
database:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_PASSWORD=postgres
variables:
- &rust_image 'git.nikos.gg/prymn/images/rust:33331dfc'
- &elixir_image 'git.nikos.gg/prymn/images/elixir:1794cee2'
- &cache_settings
endpoint: https://75178f9eca227dea51b3db4db2c15a5a.r2.cloudflarestorage.com
bucket: prymn-cache
region: auto
access-key:
from_secret: R2_CACHE_ACCESS_KEY
secret-key:
from_secret: R2_CACHE_SECRET_KEY
steps:
restore-elixir-cache:
image: meltwater/drone-cache
pull: true
settings:
<<: *cache_settings
cache_key: '{{ .Commit.Branch }}-{{ checksum "app/mix.lock" }}'
restore: true
mount:
- 'app/deps'
- 'app/_build/dev'
rust-test:
image: *rust_image
flake-check:
group: test
secrets: [ R2_CACHE_ACCESS_KEY, R2_CACHE_SECRET_KEY ]
image: git.nikos.gg/prymn/images/nix:89bb1c18
secrets: [ ATTIC_SECRET ]
commands:
- export AWS_ACCESS_KEY_ID="$${R2_CACHE_ACCESS_KEY}"
- export AWS_SECRET_ACCESS_KEY="$${R2_CACHE_SECRET_KEY}"
- cargo test
when:
- path:
- "**/*.rs"
- "Cargo.*"
elixir-test:
image: *elixir_image
group: test
environment:
- MIX_ENV=test
- PRYMN_TEST_DATABASE_HOST=database
commands:
- cd app
- mix test
when:
- path:
- "**/*.{ex,exs}"
- "mix.*"
- echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf # TODO: Move in image
- attic login local http://172.17.0.1:8080/ "$${ATTIC_SECRET}"
- attic use prymn
- nix -L flake check
# TODO: nix run jq cache inside image?
- attic push prymn $(nix eval .#checks.aarch64-linux --json | nix run nixpkgs#jq -- -r '.|join(" ")')

View file

@ -80,7 +80,7 @@ mod tests {
#[tokio::test]
async fn exec_works() {
let mut rx = exec(&"echo", &["1\n2\n3"]).expect("to spawn command");
let mut rx = exec("echo", &["1\n2\n3"]).expect("to spawn command");
let mut outputs = vec![];
while let Some(output) = rx.recv().await {

View file

@ -20,10 +20,10 @@
outputs = { self, nixpkgs, flake-utils, rust-overlay, crane }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system overlays; };
overlays = [ (import ./nix/overlay.nix) (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
scripts = pkgs.callPackage ./nix/scripts.nix { };
buildRust = buildTarget: pkgs.callPackage ./nix/rust.nix { inherit crane buildTarget; };
rustBuilder = import ./nix/rust.nix { inherit crane pkgs system; };
in
{
devShells.default = with pkgs; mkShell {
@ -41,7 +41,11 @@
'';
};
packages.agent.x86_64 = buildRust "x86_64-unknown-linux-musl";
packages.agent.aarch64 = buildRust "aarch64-unknown-linux-musl";
checks.rustTest = rustBuilder.test;
checks.rustClippy = rustBuilder.clippy;
checks.rustFormat = rustBuilder.format;
packages.agent-x86_64-linux = rustBuilder.package "x86_64-linux";
packages.agent-aarch64-linux = rustBuilder.package "aarch64-linux";
});
}

View file

@ -1,25 +1,59 @@
{ pkgs
, crane
, buildTarget ? "x86_64-unknown-linux-musl"
, system
}:
let
craneLib = (crane.mkLib pkgs).overrideToolchain pkgs.rustToolchain;
nativeBuildInputs = [ pkgs.protobuf ];
src = pkgs.lib.cleanSourceWith {
src = craneLib.path ../.;
filter = path: type:
(craneLib.filterCargoSources path type) ||
(builtins.match ".*proto$" path != null);
};
in
craneLib.buildPackage {
inherit src nativeBuildInputs;
pname = "prymn_agent";
version = "0.1.0";
doCheck = false; # FIXME: Tests need to run on the native architecture
strictDeps = true;
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static -C linker=rust-lld";
CARGO_BUILD_TARGET = buildTarget;
commonArgs = {
pname = "prymn_agent-${system}";
version = "0.1.0";
nativeBuildInputs = [ pkgs.protobuf ];
src = pkgs.lib.cleanSourceWith {
src = craneLib.path ../.;
filter = path: type:
(craneLib.filterCargoSources path type) ||
(builtins.match ".*proto$" path != null);
};
CARGO_BUILD_TARGET = targetToTriple system;
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static -C linker=rust-lld";
};
targetToTriple = targetSystem:
builtins.replaceStrings
[ "x86_64-linux" "aarch64-linux" ]
[ "x86_64-unknown-linux-musl" "aarch64-unknown-linux-musl" ]
targetSystem;
in
rec {
cargoArtifacts = craneLib.buildDepsOnly (commonArgs // {
strictDeps = true;
doCheck = false; # Tests are run in CI separately
});
test = craneLib.cargoTest (commonArgs // {
inherit cargoArtifacts;
});
clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- -D warnings";
});
format = craneLib.cargoFmt commonArgs;
package = targetSystem: craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;
pname = "prymn_agent-${targetSystem}";
doCheck = false;
CARGO_BUILD_TARGET = targetToTriple targetSystem;
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static -C linker=rust-lld";
});
}