1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-08 19:46:05 +01:00

rclone: modularize subtests

This commit is contained in:
Jess 2025-08-15 01:29:10 +12:00 committed by Austin Horstman
parent e6422763eb
commit 56b8749987
9 changed files with 290 additions and 233 deletions

View file

@ -1,13 +1,13 @@
{ pkgs, ... }: {
pkgs,
lib,
config,
...
}:
let let
sshKeys = import "${pkgs.path}/nixos/tests/ssh-keys.nix" pkgs; baseMachine = {
imports = [ "${pkgs.path}/nixos/modules/installer/cd-dvd/channel.nix" ];
baseMachine = extend: {
imports = [
"${pkgs.path}/nixos/modules/installer/cd-dvd/channel.nix"
extend
];
virtualisation.memorySize = 2048; virtualisation.memorySize = 2048;
users.users.alice = { users.users.alice = {
isNormalUser = true; isNormalUser = true;
@ -18,18 +18,24 @@ let
}; };
in in
{ {
imports = [
./no-secrets.nix
./with-secrets-in-store.nix
./secrets-with-whitespace.nix
./no-type.nix
./mount.nix
];
options.script = lib.mkOption {
type = lib.types.lines;
};
config = {
name = "rclone"; name = "rclone";
nodes = { nodes = {
machine = baseMachine { }; machine = baseMachine;
remote = baseMachine;
remote = baseMachine {
services.openssh.enable = true;
users.users.alice.openssh.authorizedKeys.keys = [
sshKeys.snakeOilEd25519PublicKey
];
};
}; };
testScript = '' testScript = ''
@ -77,110 +83,9 @@ in
succeed_as_alice("cp ${./home.nix} /home/alice/.config/home-manager/home.nix") succeed_as_alice("cp ${./home.nix} /home/alice/.config/home-manager/home.nix")
with subtest("Generate with no secrets"): ${config.script}
succeed_as_alice("install -m644 ${./no-secrets.nix} /home/alice/.config/home-manager/test-remote.nix")
actual = succeed_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
succeed_as_alice("diff -u ${./no-secrets.conf} /home/alice/.config/rclone/rclone.conf")
with subtest("Generate with secrets from store"):
succeed_as_alice("install -m644 ${./with-secrets-in-store.nix} /home/alice/.config/home-manager/test-remote.nix")
actual = succeed_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
succeed_as_alice("diff -u ${./with-secrets-in-store.conf} /home/alice/.config/rclone/rclone.conf")
with subtest("Secrets with spaces"):
succeed_as_alice("install -m644 ${./secrets-with-whitespace.nix} /home/alice/.config/home-manager/test-remote.nix")
actual = succeed_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
succeed_as_alice("diff -u ${./secrets-with-whitespace.conf} /home/alice/.config/rclone/rclone.conf")
with subtest("Un-typed remote"):
succeed_as_alice("install -m644 ${./no-type.nix} /home/alice/.config/home-manager/test-remote.nix")
actual = fail_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected not in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
expected = "An attribute set containing a remote type and options."
assert expected not in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
# TODO: verify correct activation order with the agenix and sops hm modules
remote.wait_for_unit("network.target")
remote.wait_for_unit("multi-user.target")
with subtest("Mount a remote (sftp)"):
# https://rclone.org/commands/rclone_mount/#vfs-directory-cache
# Sending a SIGHUP evicts every dcache entry
def clear_vfs_dcache():
svc_name = "rclone-mount:.home.alice.files@alices-sftp-remote.service"
succeed_as_alice(f"kill -s HUP $(systemctl --user show -p MainPID --value {svc_name})")
succeed_as_alice(
"sync",
"sleep 5",
box=remote
)
succeed_as_alice(
"mkdir -p /home/alice/.ssh",
"install -m644 ${./mount.nix} /home/alice/.config/home-manager/test-remote.nix"
)
actual = succeed_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
# remote -> machine
succeed_as_alice(
"mkdir /home/alice/files",
"touch /home/alice/files/test",
"echo started > /home/alice/files/log",
box=remote
)
succeed_as_alice("ls /home/alice/remote-files/test")
test_log = succeed_as_alice("cat /home/alice/remote-files/log")
expected = "started";
assert expected in test_log, \
f"Mounted file does not have expected contents. Expected {test_log} to contain \"{expected}\""
# machine -> remote
succeed_as_alice(
"touch /home/alice/remote-files/new-file",
"echo testing this works both ways! >> /home/alice/remote-files/log",
)
clear_vfs_dcache()
succeed_as_alice("ls /home/alice/files/new-file", box=remote)
test_log = succeed_as_alice("cat /home/alice/files/log", box=remote)
expected = "testing this works both ways!"
assert expected in test_log, \
f"Mounted file does not have expected contents. Expected {test_log} to contain \"{expected}\""
expected = "started"
assert expected in test_log, \
f"Mounted file does not have expected contents. Expected {test_log} to contain \"{expected}\""
logout_alice() logout_alice()
''; '';
};
} }

View file

@ -1,20 +1,23 @@
{ pkgs, lib, ... }: { pkgs, lib, ... }:
let let
sshKeys = import "${pkgs.path}/nixos/tests/ssh-keys.nix" pkgs; sshKeys = import "${pkgs.path}/nixos/tests/ssh-keys.nix" pkgs;
in
{ # https://rclone.org/sftp/#ssh-authentication
keyPem = lib.pipe sshKeys.snakeOilEd25519PrivateKey.text [
lib.trim
(lib.replaceStrings [ "\n" ] [ "\\\\n" ])
];
module = pkgs.writeText "mount-module" ''
{ pkgs, lib, ... }: {
programs.rclone.remotes = { programs.rclone.remotes = {
alices-sftp-remote = { alices-sftp-remote = {
config = { config = {
type = "sftp"; type = "sftp";
host = "remote"; host = "remote";
user = "alice"; user = "alice";
# https://rclone.org/sftp/#ssh-authentication key_pem = "${keyPem}";
key_pem = lib.pipe sshKeys.snakeOilEd25519PrivateKey.text [ known_hosts = "${sshKeys.snakeOilEd25519PublicKey}";
lib.trim
(lib.replaceStrings [ "\n" ] [ "\\n" ])
];
known_hosts = sshKeys.snakeOilEd25519PublicKey;
}; };
mounts = { mounts = {
"/home/alice/files" = { "/home/alice/files" = {
@ -24,4 +27,76 @@ in
}; };
}; };
}; };
}
'';
in
{
nodes.remote = {
services.openssh.enable = true;
users.users.alice.openssh.authorizedKeys.keys = [
sshKeys.snakeOilEd25519PublicKey
];
};
script = ''
remote.wait_for_unit("network.target")
remote.wait_for_unit("multi-user.target")
with subtest("Mount a remote (sftp)"):
# https://rclone.org/commands/rclone_mount/#vfs-directory-cache
# Sending a SIGHUP evicts every dcache entry
def clear_vfs_dcache():
svc_name = "rclone-mount:.home.alice.files@alices-sftp-remote.service"
succeed_as_alice(f"kill -s HUP $(systemctl --user show -p MainPID --value {svc_name})")
succeed_as_alice(
"sync",
"sleep 5",
box=remote
)
succeed_as_alice(
"mkdir -p /home/alice/.ssh",
"install -m644 ${module} /home/alice/.config/home-manager/test-remote.nix"
)
actual = succeed_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
# remote -> machine
succeed_as_alice(
"mkdir /home/alice/files",
"touch /home/alice/files/test",
"echo started > /home/alice/files/log",
box=remote
)
succeed_as_alice("ls /home/alice/remote-files/test")
test_log = succeed_as_alice("cat /home/alice/remote-files/log")
expected = "started";
assert expected in test_log, \
f"Mounted file does not have expected contents. Expected {test_log} to contain \"{expected}\""
# machine -> remote
succeed_as_alice(
"touch /home/alice/remote-files/new-file",
"echo testing this works both ways! >> /home/alice/remote-files/log",
)
clear_vfs_dcache()
succeed_as_alice("ls /home/alice/files/new-file", box=remote)
test_log = succeed_as_alice("cat /home/alice/files/log", box=remote)
expected = "testing this works both ways!"
assert expected in test_log, \
f"Mounted file does not have expected contents. Expected {test_log} to contain \"{expected}\""
expected = "started"
assert expected in test_log, \
f"Mounted file does not have expected contents. Expected {test_log} to contain \"{expected}\""
'';
} }

View file

@ -1,5 +0,0 @@
[alices-cool-remote]
host=backup-server
key_file=/key/path/foo
type=sftp
user=alice

View file

@ -1,4 +1,7 @@
{ { pkgs, ... }:
let
module = pkgs.writeText "no-secrets-module" ''
{
programs.rclone.remotes = { programs.rclone.remotes = {
alices-cool-remote.config = { alices-cool-remote.config = {
type = "sftp"; type = "sftp";
@ -7,4 +10,27 @@
key_file = "/key/path/foo"; key_file = "/key/path/foo";
}; };
}; };
}
'';
expected = pkgs.writeText "no-secrets-expected" ''
[alices-cool-remote]
host=backup-server
key_file=/key/path/foo
type=sftp
user=alice
'';
in
{
script = ''
with subtest("Generate with no secrets"):
succeed_as_alice("install -m644 ${module} /home/alice/.config/home-manager/test-remote.nix")
actual = succeed_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
succeed_as_alice("diff -u ${expected} /home/alice/.config/rclone/rclone.conf")
'';
} }

View file

@ -1,4 +1,7 @@
{ { pkgs, ... }:
let
module = pkgs.writeText "no-type-module" ''
{
programs.rclone.remotes = { programs.rclone.remotes = {
alices-cool-remote-v4.config = { alices-cool-remote-v4.config = {
description = "this value does not have a type"; description = "this value does not have a type";
@ -6,4 +9,21 @@
another-key-value = "pair"; another-key-value = "pair";
}; };
}; };
}
'';
in
{
script = ''
with subtest("Un-typed remote"):
succeed_as_alice("install -m644 ${module} /home/alice/.config/home-manager/test-remote.nix")
actual = fail_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected not in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
expected = "An attribute set containing a remote type and options."
assert expected not in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
'';
} }

View file

@ -1,5 +0,0 @@
[alices-cool-remote-v3]
description = alices speeedy remote
type = memory
spaces-secret = This is a secret with spaces, it has single spaces, and lots of spaces :3

View file

@ -1,5 +1,7 @@
{ pkgs, ... }: { pkgs, ... }:
{ let
module = pkgs.writeText "secrets-with-whitespace-module" ''
{
programs.rclone.remotes = { programs.rclone.remotes = {
alices-cool-remote-v3 = { alices-cool-remote-v3 = {
config = { config = {
@ -11,4 +13,27 @@
''}"; ''}";
}; };
}; };
}
'';
expected = pkgs.writeText "secrets-with-whitespace-expected" ''
[alices-cool-remote-v3]
description = alices speeedy remote
type = memory
spaces-secret = This is a secret with spaces, it has single spaces, and lots of spaces :3
'';
in
{
script = ''
with subtest("Secrets with spaces"):
succeed_as_alice("install -m644 ${module} /home/alice/.config/home-manager/test-remote.nix")
actual = succeed_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
succeed_as_alice("diff -u ${expected} /home/alice/.config/rclone/rclone.conf")
'';
} }

View file

@ -1,6 +0,0 @@
[alices-cool-remote-v2]
hard_delete = true
type = b2
account = super-secret-account-id
key = api-key-from-file

View file

@ -1,5 +1,7 @@
{ pkgs, ... }: { pkgs, ... }:
{ let
module = pkgs.writeText "with-secrets-in-store-module" ''
{
programs.rclone.remotes = { programs.rclone.remotes = {
alices-cool-remote-v2 = { alices-cool-remote-v2 = {
config = { config = {
@ -7,13 +9,33 @@
hard_delete = true; hard_delete = true;
}; };
secrets = { secrets = {
account = "${pkgs.writeText "acc" '' account = "${pkgs.writeText "acc" "super-secret-account-id"}";
super-secret-account-id key = "${pkgs.writeText "key" "api-key-from-file"}";
''}";
key = "${pkgs.writeText "key" ''
api-key-from-file
''}";
}; };
}; };
}; };
}
'';
expected = pkgs.writeText "with-secrets-in-store-expected" ''
[alices-cool-remote-v2]
hard_delete = true
type = b2
account = super-secret-account-id
key = api-key-from-file
'';
in
{
script = ''
with subtest("Generate with secrets from store"):
succeed_as_alice("install -m644 ${module} /home/alice/.config/home-manager/test-remote.nix")
actual = succeed_as_alice("home-manager switch")
expected = "Activating createRcloneConfig"
assert expected in actual, \
f"expected home-manager switch to contain {expected}, but got {actual}"
succeed_as_alice("diff -u ${expected} /home/alice/.config/rclone/rclone.conf")
'';
} }