chore: initial commit

This commit is contained in:
technofab 2025-07-15 19:28:42 +02:00
commit 7602719790
Signed by: technofab
SSH key fingerprint: SHA256:bV4h88OqS/AxjbPn66uUdvK9JsgIW4tv3vwJQ8tpMqQ
24 changed files with 1916 additions and 0 deletions

119
tests/cli_test.nix Normal file
View file

@ -0,0 +1,119 @@
{
pkgs,
nblib,
ntlib,
...
}: {
suites."CLI Tests" = {
pos = __curPos;
tests = [
{
name = "dependencies inclusion";
type = "script";
script = let
config = {pkgs, ...}: {
dependencies = [pkgs.git pkgs.curl];
playbook = [
{
name = "Test dependencies";
hosts = "localhost";
tasks = [];
}
];
};
cli = nblib.mkNixibleCli config;
in
# sh
''
${ntlib.helpers.scriptHelpers}
# check that dependencies are included in runtime inputs
assert_file_contains "${cli}/bin/nixible" "${pkgs.git}" "should include git in PATH"
assert_file_contains "${cli}/bin/nixible" "${pkgs.curl}" "should include curl in PATH"
'';
}
{
name = "CLI executable structure";
type = "script";
script = let
config = {pkgs, ...}: {
dependencies = [pkgs.git];
playbook = [
{
name = "CLI test";
hosts = "localhost";
tasks = [
{
debug.msg = "Testing CLI";
}
];
}
];
};
cli = nblib.mkNixibleCli config;
in
# sh
''
${ntlib.helpers.scriptHelpers}
# check CLI is executable
assert "-x ${cli}/bin/nixible" "CLI should be executable"
# check wrapper content
assert_file_contains "${cli}/bin/nixible" "set -euo pipefail" "should have error handling"
assert_file_contains "${cli}/bin/nixible" "ansible-playbook" "should call ansible-playbook"
assert_file_contains "${cli}/bin/nixible" "git rev-parse --show-toplevel" "should detect git repo"
'';
}
{
name = "variables setup";
type = "script";
script = let
config = {
playbook = [
{
name = "Environment test";
hosts = "localhost";
tasks = [];
}
];
};
cli = nblib.mkNixibleCli config;
in
# sh
''
${ntlib.helpers.scriptHelpers}
assert_file_contains "${cli}/bin/nixible" 'export ANSIBLE_COLLECTIONS_PATH=' "should export collections path"
assert_file_contains "${cli}/bin/nixible" '-e "pwd=$(pwd)"' "should pass pwd variable"
assert_file_contains "${cli}/bin/nixible" '-e "git_root=$git_repo"' "should pass git_root variable"
'';
}
{
name = "runtime dependencies inclusion";
type = "script";
script = let
config = {pkgs, ...}: {
dependencies = [pkgs.rsync pkgs.openssh];
playbook = [
{
name = "Dependencies test";
hosts = "localhost";
tasks = [];
}
];
};
cli = nblib.mkNixibleCli config;
in
# sh
''
${ntlib.helpers.scriptHelpers}
# check runtime dependencies are properly included
assert_file_contains "${cli}/bin/nixible" "rsync" "should include rsync from runtimeInputs"
assert_file_contains "${cli}/bin/nixible" "openssh" "should include openssh from runtimeInputs"
'';
}
];
};
}

101
tests/integration_test.nix Normal file
View file

@ -0,0 +1,101 @@
{
pkgs,
nblib,
ntlib,
...
}: {
suites."Integration Tests" = {
pos = __curPos;
tests = [
{
name = "end-to-end configuration processing";
type = "script";
script = let
config = {pkgs, ...}: {
dependencies = [pkgs.curl];
collections = {
"community-general" = {
version = "8.0.0";
hash = "sha256-dNtdCxGj72LfMqPfzOpUSXLNLj1IkaAewRmHNizh67Q=";
};
};
inventory = {
test_group = {
hosts = {
test1 = {ansible_host = "localhost";};
};
vars = {
test_var = "test_value";
};
};
};
playbook = [
{
name = "End-to-end test";
hosts = "test_group";
become = false;
tasks = [
{
name = "Test task";
debug = {
msg = "Hello from {{ inventory_hostname }}";
var = "test_var";
};
}
];
}
];
};
result = nblib.mkNixible config;
cli = nblib.mkNixibleCli config;
in
# sh
''
${ntlib.helpers.path [pkgs.jq pkgs.gnugrep]}
${ntlib.helpers.scriptHelpers}
# test that all components are generated
assert "-f ${result.config.inventoryFile}" "should generate inventory file"
assert "-f ${result.config.playbookFile}" "should generate playbook file"
assert "-d ${result.config.installedCollections}" "should create collections directory"
assert "-x ${cli}/bin/nixible" "should create CLI executable"
# test inventory content
jq -e '.test_group.hosts.test1.ansible_host' "${result.config.inventoryFile}" | grep -q "localhost"
assert_eq $? 0 "inventory should contain test host"
jq -e '.test_group.vars.test_var' "${result.config.inventoryFile}" | grep -q "test_value"
assert_eq $? 0 "inventory should contain test variable"
# test playbook content
assert_file_contains "${result.config.playbookFile}" "End-to-end test" "playbook should contain play name"
assert_file_contains "${result.config.playbookFile}" "test_group" "playbook should target test_group"
assert_file_contains "${result.config.playbookFile}" "Hello from" "playbook should contain debug message"
'';
}
{
name = "SOPS example configuration";
type = "script";
script = let
# use the actual SOPS example from the repo
sopsConfig = ../examples/sops.nix;
result = nblib.mkNixible sopsConfig;
cli = nblib.mkNixibleCli sopsConfig;
in
# sh
''
${ntlib.helpers.scriptHelpers}
assert "-f ${result.config.inventoryFile}" "SOPS example should generate inventory"
assert "-f ${result.config.playbookFile}" "SOPS example should generate playbook"
assert "-x ${cli}/bin/nixible" "SOPS example should generate CLI"
# test SOPS-specific content
assert_file_contains "${result.config.playbookFile}" "community.crypto.openssl_privatekey_pipe" "should use crypto collection"
assert_file_contains "${result.config.playbookFile}" "community.sops.sops_encrypt" "should use sops collection"
assert_file_contains "${result.config.playbookFile}" "no_log: true" "should have no_log for security"
'';
}
];
};
}

182
tests/lib_test.nix Normal file
View file

@ -0,0 +1,182 @@
{
pkgs,
nblib,
ntlib,
...
}: {
suites."Lib Tests" = {
pos = __curPos;
tests = [
{
name = "mkNixibleCli generates executable";
type = "script";
script = let
config = {
playbook = [
{
name = "Test CLI";
hosts = "localhost";
tasks = [
{
debug.msg = "Testing CLI generation";
}
];
}
];
};
cli = nblib.mkNixibleCli config;
in
# sh
''
${ntlib.helpers.scriptHelpers}
# Check CLI contains expected content
assert_file_contains "${cli}/bin/nixible" "ansible-playbook" "should contain ansible-playbook command"
assert_file_contains "${cli}/bin/nixible" "ANSIBLE_COLLECTIONS_PATH" "should set collections path"
'';
}
{
name = "inventory JSON generation";
type = "script";
script = let
config = {
inventory = {
webservers = {
hosts = {
web1 = {ansible_host = "192.168.1.10";};
web2 = {ansible_host = "192.168.1.11";};
};
vars = {
http_port = 80;
};
};
};
playbook = [
{
name = "Test inventory";
hosts = "webservers";
tasks = [];
}
];
};
result = nblib.mkNixible config;
inventoryFile = result.config.inventoryFile;
in
# sh
''
${ntlib.helpers.path [pkgs.jq pkgs.gnugrep]}
${ntlib.helpers.scriptHelpers}
# Check inventory file exists
assert "-f ${inventoryFile}" "inventory file should exist"
# Check JSON structure
jq -e '.webservers.hosts.web1.ansible_host' "${inventoryFile}" | grep -q "192.168.1.10"
assert_eq $? 0 "should contain web1 host"
jq -e '.webservers.vars.http_port' "${inventoryFile}" | grep -q "80"
assert_eq $? 0 "should contain http_port variable"
'';
}
{
name = "playbook YAML generation";
type = "script";
script = let
config = {
playbook = [
{
name = "Test playbook generation";
hosts = "localhost";
become = true;
tasks = [
{
name = "Install package";
package = {
name = "nginx";
state = "present";
};
}
{
name = "Start service";
service = {
name = "nginx";
state = "started";
};
}
];
}
];
};
result = nblib.mkNixible config;
playbookFile = result.config.playbookFile;
in
# sh
''
${ntlib.helpers.scriptHelpers}
# Check playbook file exists
assert "-f ${playbookFile}" "playbook file should exist"
# Check YAML structure
assert_file_contains "${playbookFile}" "Test playbook generation" "should contain play name"
assert_file_contains "${playbookFile}" "become: true" "should have become enabled"
assert_file_contains "${playbookFile}" "Install package" "should contain first task"
assert_file_contains "${playbookFile}" "nginx" "should contain nginx package"
'';
}
{
name = "ansible package is configurable";
type = "script";
script = let
config = {pkgs, ...}: {
ansiblePackage = pkgs.python3Packages.ansible;
playbook = [
{
name = "Test custom ansible";
hosts = "localhost";
tasks = [];
}
];
};
cli = nblib.mkNixibleCli config;
in
# sh
''
${ntlib.helpers.scriptHelpers}
# check that custom ansible package is used
assert_file_contains "${cli}/bin/nixible" "${pkgs.python3Packages.ansible}" "should use custom ansible package"
'';
}
{
name = "installed collections directory";
type = "script";
script = let
config = {
collections = {
"amazon-aws" = {
version = "10.1.0";
hash = "sha256-w1wv0lYnuHXrpNubvePwKag4oM1k1I43HreFWYeIWgU=";
};
"community-aws" = {
version = "10.0.0";
hash = "sha256-oqsfmuztf8FLalwSDvRYcuvOVzLbWx/cEsYoUt8Dbn0=";
};
};
};
result = nblib.mkNixible config;
collections = result.config.installedCollections;
in
# sh
''
${ntlib.helpers.scriptHelpers}
assert "-d ${collections}" "collections directory should exist"
assert "-d ${collections}/ansible_collections/amazon/aws" "amazon/aws directory should exist"
assert "-d ${collections}/ansible_collections/community/aws" "community/aws directory should exist"
'';
}
];
};
}