Skip to content

Commit

Permalink
nixos/testing/matrix: Add module indirection to avoid ambiguous `name…
Browse files Browse the repository at this point in the history
…` module arg
  • Loading branch information
roberth committed Jun 15, 2022
1 parent 15281ff commit 7071f1d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 55 deletions.
57 changes: 37 additions & 20 deletions nixos/lib/testing/matrix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@ let

extend = module: (extendModule module).config.result;

inMatrixModule = choiceName:
{
config = {
_matrixAttrsDone.${choiceName} = { };
_matrixRoot = false;
};
};

choice = choice@{ name, ... }: {
decisionModule = decision@{ name, ... }: {
options = {
after = mkOption {
description = ''
Expand Down Expand Up @@ -62,15 +54,40 @@ let
'';
};
value = mkOption {
type = types.lazyAttrsOf (extendModules { modules = [ (inMatrixModule choice.name) ]; attrArgName = choice.name; }).type;
type = types.lazyAttrsOf (types.submodule (choiceModule decision.name extendModules));
};
};
};

choiceModule = decisionName: extendModules: { name, ... }: {
options = {
module = mkOption {
description = ''
The effects of making a choice. You can specify any test-level option here.
NixOS-level options can be specified in the {option}`nodes.<name>` and {option}`defaults` sub-options.
'';
example = { defaults.services.foo.backend = "xyz"; };
type = (extendModules { modules = [ (inMatrixModule decisionName name) ]; }).type;
default = { };
};
};
};

inMatrixModule = decisionName: choiceName:
{
config = {
_module.args.${decisionName} = choiceName;
matrixDecisionsMade.${decisionName} = choiceName;
matrixIsRoot = false;
};
};

in
{
options = {
matrix = mkOption {
type = types.lazyAttrsOf (types.submodule choice);
type = types.lazyAttrsOf (types.submodule decisionModule);
description = ''
Wake up, Neo...
Expand All @@ -79,7 +96,7 @@ in
You define
```nix
matrix.pill.value.blue = { config, ... }: {
matrix.pill.value.blue.module = { config, ... }: {
defaults.networking.domain = "bed.home.lan";
};
```
Expand All @@ -89,7 +106,7 @@ in
You define
```nix
matrix.pill.value.red = { config, ... }: {
matrix.pill.value.red.module = { config, ... }: {
defaults.networking.domain = "wonderland.example.com";
};
```
Expand All @@ -102,8 +119,8 @@ in
'';
default = { };
};
_matrixAttrsDone = mkOption {
type = types.attrsOf (types.enum [{ }]);
matrixDecisionsMade = mkOption {
type = types.attrsOf types.str;
default = { };
};
result = mkOption {
Expand All @@ -124,14 +141,14 @@ in
We remove these when run on Hydra.
'';
};
_matrixRoot = mkOption {
matrixIsRoot = mkOption {
default = true;
internal = true;
};
};
config = {
_module.args =
if config._matrixRoot then
if config.matrixIsRoot then
lib.mapAttrs
(k: _: lib.mkOptionDefault (throw ''
Test matrix module parameter `${k}` has not been decided yet.
Expand All @@ -145,7 +162,7 @@ in
by adding the module parameter to the submodule definition itself.
For example:
matrix.foo.value.bar = { config, lib, ${k}, ... }: {
matrix.foo.value.bar.module = { config, lib, ${k}, ... }: {
xyz = f ${k};
}
Expand All @@ -159,7 +176,7 @@ in

result =
let
choicesRemaining = lib.filterAttrs (choice: v: ! config._matrixAttrsDone?${choice}) config.matrix;
choicesRemaining = lib.filterAttrs (choice: v: ! config.matrixDecisionsMade?${choice}) config.matrix;
isBefore = a: b:
if lib.elem a.name b.value.after
then true
Expand Down Expand Up @@ -200,7 +217,7 @@ in
else
lib.recurseIntoAttrs (
optionalAttrs (!config.minimalResult) { inherit extend; }
// lib.mapAttrs' (k: v: lib.nameValuePair "${nextChoice}-${k}" v.result) config.matrix.${nextChoice}.value
// lib.mapAttrs' (k: v: lib.nameValuePair "${nextChoice}-${k}" v.module.result) config.matrix.${nextChoice}.value
);
};
}
2 changes: 1 addition & 1 deletion nixos/lib/testing/params.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ in
params = mkOption {
description = ''
A namespace for ad-hoc definitions that you can override in options
such as `interactive` and `matrix.<choice>.values.<name>`.
such as `interactive` and `matrix.<choice>.values.<name>.module`.
These definitions are available in the `params` module argument.
'';
Expand Down
4 changes: 2 additions & 2 deletions nixos/tests/avahi.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
maintainers = [ eelco ];
};

matrix.variation.value.plain = { };
matrix.variation.value.networkd = {
matrix.variation.value.plain.module = { };
matrix.variation.value.networkd.module = {
defaults.networking = {
useNetworkd = true;
useDHCP = false;
Expand Down
8 changes: 4 additions & 4 deletions nixos/tests/cassandra.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ in
};

matrix.version.value = {
cassandra_2_1 = { params.testPackage = pkgs.cassandra_2_1; };
cassandra_2_2 = { params.testPackage = pkgs.cassandra_2_2; };
cassandra_3_0 = { params.testPackage = pkgs.cassandra_3_0; };
cassandra_3_11 = { params.testPackage = pkgs.cassandra_3_11; };
cassandra_2_1.module = { params.testPackage = pkgs.cassandra_2_1; };
cassandra_2_2.module = { params.testPackage = pkgs.cassandra_2_2; };
cassandra_3_0.module = { params.testPackage = pkgs.cassandra_3_0; };
cassandra_3_11.module = { params.testPackage = pkgs.cassandra_3_11; };
};

defaults = {
Expand Down
4 changes: 2 additions & 2 deletions nixos/tests/hostname.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ in

name = "hostname-${fqdn}";

matrix.domain.value.explicit.params.domain = "adomain";
matrix.domain.value.implicit.params.domain = null;
matrix.domain.value.explicit.module.params.domain = "adomain";
matrix.domain.value.implicit.module.params.domain = null;

nodes.machine = { lib, pkgs, ... }: {
networking.hostName = hostName;
Expand Down
42 changes: 21 additions & 21 deletions nixos/tests/networking.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ in {
defaults.networking.useNetworkd = lib.mkForce networkd;
name = "networking-${testcase}-${implementation}";

matrix.implementation.value.networkd = { ... }: {
matrix.implementation.value.networkd.module = { ... }: {
_module.args.networkd = true;
};
matrix.implementation.value.scripted = { ... }: {
matrix.implementation.value.scripted.module = { ... }: {
_module.args.networkd = false;
};
matrix.testcase.value = {
loopback = {
loopback.module = {
nodes.client = { pkgs, ... }: {
networking.useDHCP = false;
};
Expand All @@ -90,7 +90,7 @@ in {
assert "inet6 ::1/128" in loopback_addresses
'';
};
static = {
static.module = {
nodes.router = router;
nodes.client = { pkgs, ... }: {
virtualisation.vlans = [ 1 2 ];
Expand Down Expand Up @@ -140,7 +140,7 @@ in {
client.wait_until_succeeds("ping -c 1 192.168.3.1")
'';
};
routeType = {
routeType.module = {
nodes.client = { pkgs, ... }: {
networking = {
useDHCP = false;
Expand All @@ -157,7 +157,7 @@ in {
client.succeed("ip -4 route list table local | grep 'local 192.168.1.127'")
'';
};
dhcpDefault = {
dhcpDefault.module = {
nodes.router = router;
nodes.client = { lib, ... }: {
# Disable test driver default config
Expand All @@ -175,7 +175,7 @@ in {
client.succeed("ping -c 1 192.168.1.2")
'';
};
dhcpSimple = {
dhcpSimple.module = {
nodes.router = router;
nodes.client = { pkgs, ... }: {
virtualisation.vlans = [ 1 2 ];
Expand Down Expand Up @@ -229,7 +229,7 @@ in {
router.wait_until_succeeds("ping -c 1 fd00:1234:5678:2::2")
'';
};
dhcpOneIf = {
dhcpOneIf.module = {
nodes.router = router;
nodes.client = { pkgs, ... }: {
virtualisation.vlans = [ 1 2 ];
Expand Down Expand Up @@ -272,7 +272,7 @@ in {
router.fail("ping -c 1 192.168.2.2")
'';
};
bond = let
bond.module = let
node = address: { pkgs, ... }: {
virtualisation.vlans = [ 1 2 ];
networking = {
Expand Down Expand Up @@ -310,7 +310,7 @@ in {
client.succeed('grep -q "Bonding Mode: IEEE 802.3ad Dynamic link aggregation" /proc/net/bonding/bond0')
'';
};
bridge = let
bridge.module = let
node = { address, vlan }: { pkgs, ... }: {
virtualisation.vlans = [ vlan ];
networking = {
Expand Down Expand Up @@ -355,7 +355,7 @@ in {
router.wait_until_succeeds("ping -c 1 192.168.1.3")
'';
};
macvlan = {
macvlan.module = {
nodes.router = router;
nodes.client = { pkgs, ... }: {
environment.systemPackages = [ pkgs.iptables ]; # to debug firewall rules
Expand Down Expand Up @@ -409,7 +409,7 @@ in {
router.wait_until_succeeds("ping -c 1 192.168.1.3")
'';
};
fou = {
fou.module = {
nodes.machine = { ... }: {
virtualisation.vlans = [ 1 ];
networking = {
Expand Down Expand Up @@ -453,7 +453,7 @@ in {
} in fous, "fou4 exists"
'';
};
sit = let
sit.module = let
node = { address4, remote, address6 }: { pkgs, ... }: {
virtualisation.vlans = [ 1 ];
networking = {
Expand Down Expand Up @@ -514,7 +514,7 @@ in {
client2.wait_until_succeeds("ping -c 1 fc00::2")
'';
};
gre = let
gre.module = let
node = { pkgs, ... }: {
networking = {
useDHCP = false;
Expand Down Expand Up @@ -624,7 +624,7 @@ in {
assert links[0]['linkinfo']['info_data']['ttl'] == 255, "ttl not set for gre6Tunnel"
'';
};
vlan = let
vlan.module = let
node = address: { pkgs, ... }: {
#virtualisation.vlans = [ 1 ];
networking = {
Expand Down Expand Up @@ -655,7 +655,7 @@ in {
client2.succeed("ip addr show dev vlan >&2")
'';
};
virtual = {
virtual.module = {
nodes.machine = {
networking.useDHCP = false;
networking.interfaces.tap0 = {
Expand Down Expand Up @@ -713,7 +713,7 @@ in {
), "Some virtual interface has not been properly cleaned:\n{}".format(residue)
'';
};
privacy = {
privacy.module = {
nodes.router = { ... }: {
virtualisation.vlans = [ 1 ];
boot.kernel.sysctl."net.ipv6.conf.all.forwarding" = true;
Expand Down Expand Up @@ -793,7 +793,7 @@ in {
)
'';
};
routes = {
routes.module = {
nodes.machine = {
networking.useDHCP = false;
networking.interfaces.eth0 = {
Expand Down Expand Up @@ -872,7 +872,7 @@ in {
), "The IPv6 routing table has not been properly cleaned:\n{}".format(ipv6Residue)
'';
};
rename = {
rename.module = {
nodes.machine = { pkgs, ... }: {
virtualisation.vlans = [ 1 ];
networking = {
Expand All @@ -896,7 +896,7 @@ in {
};
# even with disabled networkd, systemd.network.links should work
# (as it's handled by udev, not networkd)
link = {
link.module = {
nodes.client = { pkgs, ... }: {
virtualisation.vlans = [ 1 ];
networking = {
Expand All @@ -917,7 +917,7 @@ in {
assert "mtu 1442" in client.succeed("ip l show dev foo")
'';
};
wlanInterface = let
wlanInterface.module = let
testMac = "06:00:00:00:02:00";
in {
nodes.machine = { pkgs, ... }: {
Expand Down
8 changes: 3 additions & 5 deletions nixos/tests/oci-containers.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{ config, lib, params, ... }:
let inherit (params) backend;
in
{ config, lib, backend, ... }:
{
name = "oci-containers-${backend}";

meta = {
maintainers = with lib.maintainers; [ adisbladis benley ] ++ lib.teams.serokell.members;
};

matrix.backend.value.docker.params.backend = "docker";
matrix.backend.value.podman.params.backend = "podman";
matrix.backend.value.docker.module = {};
matrix.backend.value.podman.module = {};

nodes = {
${backend} = { pkgs, ... }: {
Expand Down

0 comments on commit 7071f1d

Please sign in to comment.