git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #85635 from basvandijk/support-bigger-nixos-tests

Support more than 99 nodes in NixOS tests & export toHex and toBase

commit27296cc5c572da9a7ba35638a139a168a301ee14 authorBas van Dijk <v.dijk.bas@gmail.com> dateMon 20 Jul 2020, 13:39 (6 years ago) parent64d855d6e7822b
5 files changed+82−11
lib/default.nix+1−1 lib/tests/misc.nix+10 lib/trivial.nix+51 nixos/lib/qemu-flags.nix+8−3 nixos/tests/networking.nix+12−7
lib/default.nix+1−1
@@ -67,7 +67,7 @@ let
6767 inherit (trivial) id const pipe concat or and bitAnd bitOr bitXor
6868 bitNot boolToString mergeAttrs flip mapNullable inNixShell min max
6969 importJSON warn info showWarnings nixpkgsVersion version mod compare
70− splitByAndCompare functionArgs setFunctionArgs isFunction;
70+ splitByAndCompare functionArgs setFunctionArgs isFunction toHexString toBaseDigits;
7171 inherit (fixedPoints) fix fix' converge extends composeExtensions
7272 makeExtensible makeExtensibleWithCustomName;
7373 inherit (attrsets) attrByPath hasAttrByPath setAttrByPath
lib/tests/misc.nix+10
@@ -102,6 +102,16 @@ runTests {
102102 expected = 9;
103103 };
104104
105+ testToHexString = {
106+ expr = toHexString 250;
107+ expected = "FA";
108+ };
109+
110+ testToBaseDigits = {
111+ expr = toBaseDigits 2 6;
112+ expected = [ 1 1 0 ];
113+ };
114+
105115# STRINGS
106116
107117 testConcatMapStrings = {
lib/trivial.nix+51
@@ -332,4 +332,55 @@ rec {
332332 */
333333 isFunction = f: builtins.isFunction f ||
334334 (f ? __functor && isFunction (f.__functor f));
335+
336+ /* Convert the given positive integer to a string of its hexadecimal
337+ representation. For example:
338+
339+ toHexString 0 => "0"
340+
341+ toHexString 16 => "10"
342+
343+ toHexString 250 => "FA"
344+ */
345+ toHexString = i:
346+ let
347+ toHexDigit = d:
348+ if d < 10
349+ then toString d
350+ else
351+ {
352+ "10" = "A";
353+ "11" = "B";
354+ "12" = "C";
355+ "13" = "D";
356+ "14" = "E";
357+ "15" = "F";
358+ }.${toString d};
359+ in
360+ lib.concatMapStrings toHexDigit (toBaseDigits 16 i);
361+
362+ /* `toBaseDigits base i` converts the positive integer i to a list of its
363+ digits in the given base. For example:
364+
365+ toBaseDigits 10 123 => [ 1 2 3 ]
366+
367+ toBaseDigits 2 6 => [ 1 1 0 ]
368+
369+ toBaseDigits 16 250 => [ 15 10 ]
370+ */
371+ toBaseDigits = base: i:
372+ let
373+ go = i:
374+ if i < base
375+ then [i]
376+ else
377+ let
378+ r = i - ((i / base) * base);
379+ q = (i - r) / base;
380+ in
381+ [r] ++ go q;
382+ in
383+ assert (base >= 2);
384+ assert (i >= 0);
385+ lib.reverseList (go i);
335386}
nixos/lib/qemu-flags.nix+8−3
@@ -2,13 +2,18 @@
22{ pkgs }:
33
44let
5− zeroPad = n: if n < 10 then "0${toString n}" else toString n;
5+ zeroPad = n:
6+ pkgs.lib.optionalString (n < 16) "0" +
7+ (if n > 255
8+ then throw "Can't have more than 255 nets or nodes!"
9+ else pkgs.lib.toHexString n);
610in
711
8−{
12+rec {
13+ qemuNicMac = net: machine: "52:54:00:12:${zeroPad net}:${zeroPad machine}";
914
1015 qemuNICFlags = nic: net: machine:
11− [ "-device virtio-net-pci,netdev=vlan${toString nic},mac=52:54:00:12:${zeroPad net}:${zeroPad machine}"
16+ [ "-device virtio-net-pci,netdev=vlan${toString nic},mac=${qemuNicMac net machine}"
1217 "-netdev vde,id=vlan${toString nic},sock=$QEMU_VDE_SOCKET_${toString net}"
1318 ];
1419
nixos/tests/networking.nix+12−7
@@ -8,7 +8,9 @@ with import ../lib/testing-python.nix { inherit system pkgs; };
88with pkgs.lib;
99
1010let
11− router = { config, pkgs, ... }:
11+ qemu-flags = import ../lib/qemu-flags.nix { inherit pkgs; };
12+
13+ router = { config, pkgs, lib, ... }:
1214 with pkgs.lib;
1315 let
1416 vlanIfs = range 1 (length config.virtualisation.vlans);
@@ -31,16 +33,19 @@ let
3133 enable = true;
3234 interfaces = map (n: "eth${toString n}") vlanIfs;
3335 extraConfig = ''
34− authoritative;
3536 '' + flip concatMapStrings vlanIfs (n: ''
3637 subnet 192.168.${toString n}.0 netmask 255.255.255.0 {
3738 option routers 192.168.${toString n}.1;
38− # XXX: technically it's _not guaranteed_ that IP addresses will be
39− # issued from the first item in range onwards! We assume that in
40− # our tests however.
41− range 192.168.${toString n}.2 192.168.${toString n}.254;
4239 }
43− '');
40+ '')
41+ ;
42+ machines = lib.flip map vlanIfs (vlan:
43+ {
44+ hostName = "client${toString vlan}";
45+ ethernetAddress = qemu-flags.qemuNicMac vlan 1;
46+ ipAddress = "192.168.${toString vlan}.2";
47+ }
48+ );
4449 };
4550 services.radvd = {
4651 enable = true;