Skip to content
Draft
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
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
./nix/hooks.nix # pre-commit hooks
./nix/utils.nix # utility functions
./nix/shells.nix
./nix/tests.nix
./wire/cli
./wire/key_agent
./doc
./tests/nix
./runtime
./bench/runner.nix
./tests/tests.nix
];
systems = import systems;

Expand Down
2 changes: 2 additions & 0 deletions nix/shells.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
lib,
craneLib,
pkgs,
cargo-testing-exports,
...
}:
let
Expand Down Expand Up @@ -32,6 +33,7 @@
cfg.installationScript
''
export WIRE_TEST_DIR=$(realpath ./tests/rust)
${cargo-testing-exports}
''
];
};
Expand Down
38 changes: 0 additions & 38 deletions nix/tests.nix

This file was deleted.

126 changes: 126 additions & 0 deletions tests/tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{ inputs, ... }:
{
perSystem =
{
craneLib,
pkgs,
lib,
commonArgs,
system,
cargo-testing-vms,
cargo-testing-exports,
self',
...
}:
let
evalConfig = import (pkgs.path + "/nixos/lib/eval-config.nix");
tests = craneLib.buildPackage (
{
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
doCheck = false;

doNotPostBuildInstallCargoBinaries = true;

buildPhase = ''
cargo test --no-run
'';

installPhaseCommand = ''
mkdir -p $out
cp $(ls target/debug/deps/{wire,lib,key_agent}-* | grep -v "\.d") $out
'';
}
// commonArgs
);

snakeOil = import "${pkgs.path}/nixos/tests/ssh-keys.nix" pkgs;
in
{
packages.cargo-tests = pkgs.writeShellScriptBin "run-tests" ''
set -e

${cargo-testing-exports}

for item in "${tests}"/*; do
echo "running $item"
"$item"
done
'';

_module.args = {
cargo-testing-exports = ''
export WIRE_TEST_VM="${cargo-testing-vms}"
export WIRE_PUSHABLE_PATH="${self'.packages.agent}"
export WIRE_SSH_KEY="${snakeOil.snakeOilEd25519PrivateKey}"
'';

cargo-testing-vms =
let
mkVM =
index:
evalConfig {
inherit system;
modules = lib.singleton {
imports = [ "${inputs.nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix" ];

networking.hostName = "cargo-vm-${builtins.toString index}";

boot = {
loader = {
systemd-boot.enable = true;
efi.canTouchEfiVariables = true;
timeout = 0;
};

kernelParams = [ "console=ttyS0" ];
};

services = {
openssh = {
enable = true;
settings = {
PermitRootLogin = "without-password";
};
};

getty.autologinUser = "root";
};

virtualisation = {
graphics = false;

diskSize = 5024;
diskImage = null;

# testing for pushing is hard without this
# useBootLoader = true;
useNixStoreImage = true;
writableStore = true;

forwardPorts = [
{
from = "host";
host.port = 2000 + index;
guest.port = 22;
}
];
};

users.users.root.openssh.authorizedKeys.keys = [ snakeOil.snakeOilEd25519PublicKey ];

users.users.root.initialPassword = "root";

system.stateVersion = "23.11";
};
};
in
pkgs.linkFarm "vm-forest" (
builtins.map (index: {
path = (mkVM index).config.system.build.vm;
name = builtins.toString index;
# Updated with every new test that uses a VM
}) (lib.range 0 1)
);
};
};
}
58 changes: 58 additions & 0 deletions wire/lib/src/commands/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,61 @@ pub async fn evaluate_hive_attribute(
Either::Left((_, stdout)) | Either::Right((_, stdout)) => stdout,
})
}

#[cfg(test)]
mod tests {
use std::{assert_matches::assert_matches, collections::HashMap, env};

use crate::{
SubCommandModifiers,
commands::{
CommandArguments, WireCommandChip, common::push,
noninteractive::non_interactive_command_with_env,
},
errors::CommandError,
hive::node::{Context, Name, Node, Push},
test_support::test_with_vm,
};

#[tokio::test]
async fn push_to_vm() {
let vm = test_with_vm();
let mut node = Node::from_target(vm.target.clone());
let name = Name("test".into());
let mut context = Context::create_test_context(
crate::hive::HiveLocation::Flake { uri: "in-test".to_string(), prefetch: crate::hive::FlakePrefetch { hash: "AAAAA".into(), store_path: "unknown".into() } },
&name,
&mut node,
);
context.modifiers = SubCommandModifiers {
ssh_accept_host: crate::StrictHostKeyChecking::No,
..Default::default()
};

let push_path = env::var("WIRE_PUSHABLE_PATH").unwrap();
let to_push = Push::Path(&push_path);

let child = non_interactive_command_with_env(
&CommandArguments::new(format!("stat {push_path}"), context.modifiers)
.on_target(Some(&context.node.target)),
HashMap::new(),
)
.unwrap();

assert_matches!(
child.wait_till_success().await,
Err(CommandError::CommandFailed { command_ran, logs, code, reason }) if logs.contains("No such file or directory")
);

push(&context, to_push).await.unwrap();

let child = non_interactive_command_with_env(
&CommandArguments::new(format!("stat {push_path}"), context.modifiers)
.on_target(Some(&node.target)),
HashMap::new(),
)
.unwrap();

child.wait_till_success().await.unwrap();
}
}
Loading
Loading