-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add contract test for secret contract
- Loading branch information
ibizaman
committed
Oct 24, 2024
1 parent
b342d89
commit 39c1627
Showing
6 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
{ lib }: | ||
{ pkgs, lib }: | ||
{ | ||
backup = import ./backup.nix { inherit lib; }; | ||
mount = import ./mount.nix { inherit lib; }; | ||
secret = import ./secret.nix { inherit lib; }; | ||
ssl = import ./ssl.nix { inherit lib; }; | ||
test = { | ||
secret = import ./secret/test.nix { inherit pkgs lib; }; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ pkgs, lib, ... }: | ||
let | ||
pkgs' = pkgs; | ||
|
||
testLib = pkgs.callPackage ../../../test/common.nix {}; | ||
|
||
inherit (lib) getAttrFromPath setAttrByPath; | ||
inherit (lib) mkIf; | ||
in | ||
{ name, | ||
configRoot, | ||
createContent, # config to create a secret with value "secretA". | ||
modules ? [], | ||
owner ? "root", | ||
group ? "root", | ||
mode ? "0400", | ||
restartUnits ? [ "myunit.service" ], | ||
}: pkgs.testers.runNixOSTest { | ||
name = "secret_${name}_${owner}_${group}_${mode}"; | ||
|
||
nodes.machine = { config, ... }: { | ||
imports = ( testLib.baseImports pkgs' ) ++ modules; | ||
config = lib.mkMerge [ | ||
(setAttrByPath configRoot { | ||
A = { | ||
inherit owner group mode restartUnits; | ||
} // createContent; | ||
}) | ||
(mkIf (owner != "root") { | ||
users.users.${owner}.isNormalUser = true; | ||
}) | ||
(mkIf (group != "root") { | ||
users.groups.${group} = {}; | ||
}) | ||
]; | ||
}; | ||
|
||
testScript = { nodes, ... }: | ||
let | ||
cfg = (getAttrFromPath configRoot nodes.machine)."A"; | ||
in | ||
'' | ||
owner = machine.succeed("stat -c '%U' ${cfg.path}").strip() | ||
print(f"Got owner {owner}") | ||
if owner != "${owner}": | ||
raise Exception(f"Owner should be '${owner}' but got '{owner}'") | ||
group = machine.succeed("stat -c '%G' ${cfg.path}").strip() | ||
print(f"Got group {group}") | ||
if group != "${group}": | ||
raise Exception(f"Group should be '${group}' but got '{group}'") | ||
mode = str(int(machine.succeed("stat -c '%a' ${cfg.path}").strip())) | ||
print(f"Got mode {mode}") | ||
wantedMode = str(int("${mode}")) | ||
if mode != wantedMode: | ||
raise Exception(f"Mode should be '{wantedMode}' but got '{mode}'") | ||
content = machine.succeed("cat ${cfg.path}").strip() | ||
print(f"Got content {content}") | ||
if content != "secretA": | ||
raise Exception(f"Content should be 'secretA' but got '{content}'") | ||
''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ pkgs, ... }: | ||
let | ||
contracts = pkgs.callPackage ../../modules/contracts {}; | ||
in | ||
{ | ||
hardcoded_root_root = contracts.test.secret { | ||
name = "hardcoded"; | ||
modules = [ ../../modules/blocks/hardcodedsecret.nix ]; | ||
configRoot = [ "shb" "hardcodedsecret" ]; | ||
createContent = { | ||
content = "secretA"; | ||
}; | ||
}; | ||
|
||
hardcoded_user_group = contracts.test.secret { | ||
name = "hardcoded"; | ||
modules = [ ../../modules/blocks/hardcodedsecret.nix ]; | ||
configRoot = [ "shb" "hardcodedsecret" ]; | ||
createContent = { | ||
content = "secretA"; | ||
}; | ||
owner = "user"; | ||
group = "group"; | ||
mode = "640"; | ||
}; | ||
|
||
# TODO: how to do this? | ||
# sops = contracts.test.secret { | ||
# name = "sops"; | ||
# configRoot = cfg: name: cfg.sops.secrets.${name}; | ||
# createContent = content: { | ||
# sopsFile = ./secret/sops.yaml; | ||
# }; | ||
# }; | ||
} |
Empty file.