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: depends_on:
# - build - test
# - test
# - lint
when: when:
- event: push - event: push
@ -10,22 +8,21 @@ when:
matrix: matrix:
BUILD_TARGET: BUILD_TARGET:
- x86_64 - x86_64-linux
- aarch64 - aarch64-linux
steps: steps:
build: build:
image: nixos/nix:latest image: git.nikos.gg/prymn/images/nix:89bb1c18
secrets: [ ATTIC_SECRET ]
commands: 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 "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 - attic login local http://172.17.0.1:8080/ "$${ATTIC_SECRET}"
- nix build -L ".#agent.${BUILD_TARGET}" - attic use prymn
- mkdir -p "dist/${BUILD_TARGET}-unknown-linux-musl" - nix build -L ".#agent-${BUILD_TARGET}"
- cp result/bin/prymn_agent "dist/${BUILD_TARGET}-unknown-linux-musl/" - attic push prymn ./result
- nix copy --to 's3://prymn-cache?endpoint=75178f9eca227dea51b3db4db2c15a5a.r2.cloudflarestorage.com&region=auto' ".#agent.${BUILD_TARGET}" - mkdir -p "dist/${BUILD_TARGET}"
secrets: [ R2_CACHE_ACCESS_KEY, R2_CACHE_SECRET_KEY ] - cp result/bin/prymn_agent "dist/${BUILD_TARGET}/"
release: release:
image: woodpeckerci/plugin-s3 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 - event: push
branch: branch:
- ${CI_REPO_DEFAULT_BRANCH} - ${CI_REPO_DEFAULT_BRANCH}
path:
- get_prymn.sh
steps: steps:
release: 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: when:
- event: [pull_request, tag]
- event: push - 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: steps:
restore-elixir-cache: flake-check:
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
group: test group: test
secrets: [ R2_CACHE_ACCESS_KEY, R2_CACHE_SECRET_KEY ] image: git.nikos.gg/prymn/images/nix:89bb1c18
secrets: [ ATTIC_SECRET ]
commands: commands:
- export AWS_ACCESS_KEY_ID="$${R2_CACHE_ACCESS_KEY}" - echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf # TODO: Move in image
- export AWS_SECRET_ACCESS_KEY="$${R2_CACHE_SECRET_KEY}" - attic login local http://172.17.0.1:8080/ "$${ATTIC_SECRET}"
- cargo test - attic use prymn
when: - nix -L flake check
- path: # TODO: nix run jq cache inside image?
- "**/*.rs" - attic push prymn $(nix eval .#checks.aarch64-linux --json | nix run nixpkgs#jq -- -r '.|join(" ")')
- "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.*"

View file

@ -80,7 +80,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn exec_works() { 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![]; let mut outputs = vec![];
while let Some(output) = rx.recv().await { while let Some(output) = rx.recv().await {

View file

@ -20,10 +20,10 @@
outputs = { self, nixpkgs, flake-utils, rust-overlay, crane }: outputs = { self, nixpkgs, flake-utils, rust-overlay, crane }:
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem (system:
let let
pkgs = import nixpkgs { inherit system overlays; };
overlays = [ (import ./nix/overlay.nix) (import rust-overlay) ]; overlays = [ (import ./nix/overlay.nix) (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
scripts = pkgs.callPackage ./nix/scripts.nix { }; 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 in
{ {
devShells.default = with pkgs; mkShell { devShells.default = with pkgs; mkShell {
@ -41,7 +41,11 @@
''; '';
}; };
packages.agent.x86_64 = buildRust "x86_64-unknown-linux-musl"; checks.rustTest = rustBuilder.test;
packages.agent.aarch64 = buildRust "aarch64-unknown-linux-musl"; 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 { pkgs
, crane , crane
, buildTarget ? "x86_64-unknown-linux-musl" , system
}: }:
let let
craneLib = (crane.mkLib pkgs).overrideToolchain pkgs.rustToolchain; craneLib = (crane.mkLib pkgs).overrideToolchain pkgs.rustToolchain;
commonArgs = {
pname = "prymn_agent-${system}";
version = "0.1.0";
nativeBuildInputs = [ pkgs.protobuf ]; nativeBuildInputs = [ pkgs.protobuf ];
src = pkgs.lib.cleanSourceWith { src = pkgs.lib.cleanSourceWith {
src = craneLib.path ../.; src = craneLib.path ../.;
filter = path: type: filter = path: type:
(craneLib.filterCargoSources path type) || (craneLib.filterCargoSources path type) ||
(builtins.match ".*proto$" path != null); (builtins.match ".*proto$" path != null);
}; };
in
craneLib.buildPackage {
inherit src nativeBuildInputs;
pname = "prymn_agent"; CARGO_BUILD_TARGET = targetToTriple system;
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_RUSTFLAGS = "-C target-feature=+crt-static -C linker=rust-lld";
CARGO_BUILD_TARGET = buildTarget; };
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";
});
} }