This commit is contained in:
GTrunSec 2022-04-02 12:40:35 -07:00
parent a0ce293db8
commit 60592d3096
No known key found for this signature in database
GPG key ID: 2368FAFA4ABDD2A0
55 changed files with 23668 additions and 30925 deletions

View file

@ -1,11 +1,15 @@
{ config, pkgs, lib, kubenix, ... }:
with lib;
let
{
config,
pkgs,
lib,
kubenix,
...
}:
with lib; let
cfg = config.testing;
testModule = {
imports = [ ./evalTest.nix ];
imports = [./evalTest.nix];
# passthru testing configuration
config._module.args = {
@ -16,9 +20,7 @@ let
isTestEnabled = test:
(cfg.enabledTests == null || elem test.name cfg.enabledTests) && test.enable;
in
{
in {
imports = [
./docker.nix
./driver/kubetest.nix
@ -41,28 +43,28 @@ in
common = mkOption {
description = "List of common options to apply to tests";
type = types.listOf (types.submodule ({ config, ... }: {
type = types.listOf (types.submodule ({config, ...}: {
options = {
features = mkOption {
description = "List of features that test has to have to apply options";
type = types.listOf types.str;
default = [ ];
default = [];
};
options = mkOption {
description = "Options to apply to test";
type = types.unspecified;
default = { };
apply = default: { _file = "testing.common"; } // default;
default = {};
apply = default: {_file = "testing.common";} // default;
};
};
}));
default = [ ];
default = [];
};
tests = mkOption {
description = "List of test cases";
default = [ ];
default = [];
type = types.listOf (types.coercedTo types.path
(module: {
inherit module;
@ -86,7 +88,7 @@ in
args = mkOption {
description = "Attribute set of extra args passed to tests";
type = types.attrs;
default = { };
default = {};
};
success = mkOption {

View file

@ -1,16 +1,17 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:
with lib;
with import ../../lib/docker { inherit lib pkgs; };
let
with import ../../lib/docker {inherit lib pkgs;}; let
testing = config.testing;
allImages = unique (flatten (map (t: t.evaled.config.docker.export or [ ]) testing.tests));
allImages = unique (flatten (map (t: t.evaled.config.docker.export or []) testing.tests));
cfg = config.testing.docker;
in
{
in {
options.testing.docker = {
registryUrl = mkOption {
description = "Docker registry url";
@ -37,11 +38,13 @@ in
};
};
config.testing.common = [{
features = [ "docker" ];
options = {
_file = "testing.docker.registryUrl";
docker.registry.url = cfg.registryUrl;
};
}];
config.testing.common = [
{
features = ["docker"];
options = {
_file = "testing.docker.registryUrl";
docker.registry.url = cfg.registryUrl;
};
}
];
}

View file

@ -1,15 +1,14 @@
{ pkgs ? import <nixpkgs> { } }:
{pkgs ? import <nixpkgs> {}}:
with pkgs;
with pkgs.python38Packages;
with pkgs.python38;
pkgs.python38Packages.buildPythonPackage rec {
pname = "kubetest";
version = "0.9.5";
src = fetchPypi {
inherit pname version;
sha256 = "sha256-TqDHMciAEXv4vMWLJY1YdtXsP4ho+INgdFB3xQQNoZU=";
};
propagatedBuildInputs = [ pytest kubernetes ];
doCheck = false;
}
pkgs.python38Packages.buildPythonPackage rec {
pname = "kubetest";
version = "0.9.5";
src = fetchPypi {
inherit pname version;
sha256 = "sha256-TqDHMciAEXv4vMWLJY1YdtXsP4ho+INgdFB3xQQNoZU=";
};
propagatedBuildInputs = [pytest kubernetes];
doCheck = false;
}

View file

@ -1,7 +1,11 @@
{ lib, config, testing, kubenix, ... }:
with lib;
let
{
lib,
config,
testing,
kubenix,
...
}:
with lib; let
modules = [
# testing module
config.module
@ -13,10 +17,12 @@ let
{
config = {
kubenix.project = mkDefault config.name;
_module.args = {
inherit kubenix;
test = evaled.config;
} // testing.args;
_module.args =
{
inherit kubenix;
test = evaled.config;
}
// testing.args;
};
}
];
@ -36,28 +42,29 @@ let
# common options that can be applied on this test
commonOpts =
filter
(d:
(intersectLists d.features testFeatures) == d.features ||
(length d.features) == 0
)
testing.common;
(
d:
(intersectLists d.features testFeatures)
== d.features
|| (length d.features) == 0
)
testing.common;
# add common options modules to all modules
modulesWithCommonOptions = modules ++ (map (d: d.options) commonOpts);
# evaled test
evaled =
let
evaled' = kubenix.evalModules {
modules = modulesWithCommonOptions;
};
in
if testing.doThrowError then evaled'
evaled = let
evaled' = kubenix.evalModules {
modules = modulesWithCommonOptions;
};
in
if testing.doThrowError
then evaled'
else if (builtins.tryEval evaled'.config.test.assertions).success
then evaled' else null;
in
{
then evaled'
else null;
in {
options = {
module = mkOption {
description = "Module defining kubenix test";
@ -100,7 +107,7 @@ in
description = "Test result";
type = types.unspecified;
internal = true;
default = [ ];
default = [];
};
script = mkOption {
@ -108,7 +115,6 @@ in
type = types.nullOr (types.either types.lines types.path);
internal = true;
};
};
config = mkMerge [

View file

@ -1,7 +1,10 @@
{ lib, config, pkgs, ... }:
with lib;
let
{
lib,
config,
pkgs,
...
}:
with lib; let
testing = config.testing;
script = pkgs.writeScript "run-local-k8s-tests-${testing.name}.sh" ''
@ -31,8 +34,7 @@ let
echo "--> running tests"
${testing.testScript} --kube-config=$KUBECONFIG
'';
in
{
in {
options.testing.runtime.local = {
script = mkOption {
type = types.package;

View file

@ -1,29 +1,32 @@
# nixos-k8s implements nixos kubernetes testing runtime
{ config
, pkgs
, lib
, ...
{
config,
pkgs,
lib,
...
}:
with lib;
let
with lib; let
testing = config.testing;
# kubeconfig = "/etc/${config.services.kubernetes.pki.etcClusterAdminKubeconfig}";
kubeconfig = "/etc/kubernetes/cluster-admin.kubeconfig";
kubecerts = "/var/lib/kubernetes/secrets";
# how we differ from the standard configuration of mkKubernetesBaseTest
extraConfiguration = { config, pkgs, lib, nodes, ... }: {
extraConfiguration = {
config,
pkgs,
lib,
nodes,
...
}: {
virtualisation = {
memorySize = 2048;
};
networking = {
nameservers = [ "10.0.0.254" ];
nameservers = ["10.0.0.254"];
firewall = {
trustedInterfaces = [ "docker0" "cni0" ];
trustedInterfaces = ["docker0" "cni0"];
};
};
@ -32,22 +35,26 @@ let
kubelet = {
seedDockerImages = testing.docker.images;
networkPlugin = "cni";
cni.config = [{
name = "mynet";
type = "bridge";
bridge = "cni0";
addIf = true;
ipMasq = true;
isGateway = true;
ipam = {
type = "host-local";
subnet = "10.1.0.0/16";
gateway = "10.1.0.1";
routes = [{
dst = "0.0.0.0/0";
}];
};
}];
cni.config = [
{
name = "mynet";
type = "bridge";
bridge = "cni0";
addIf = true;
ipMasq = true;
isGateway = true;
ipam = {
type = "host-local";
subnet = "10.1.0.0/16";
gateway = "10.1.0.1";
routes = [
{
dst = "0.0.0.0/0";
}
];
};
}
];
};
};
@ -57,32 +64,30 @@ let
services.copy-certs = {
description = "Share k8s certificates with host";
script = "cp -rf ${kubecerts} /tmp/xchg/; cp -f ${kubeconfig} /tmp/xchg/;";
after = [ "kubernetes.target" ];
wantedBy = [ "multi-user.target" ];
after = ["kubernetes.target"];
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
};
};
};
script = ''
machine1.succeed("${testing.testScript} --kube-config=${kubeconfig}")
'';
test =
with import "${pkgs.path}/nixos/tests/kubernetes/base.nix" { inherit pkgs; inherit (pkgs) system; };
test = with import "${pkgs.path}/nixos/tests/kubernetes/base.nix" {
inherit pkgs;
inherit (pkgs) system;
};
mkKubernetesSingleNodeTest {
inherit extraConfiguration;
inherit (config.testing) name;
test = script;
};
in
{
in {
options.testing.runtime.nixos-k8s = {
driver = mkOption {
description = "Test driver";

View file

@ -1,11 +1,12 @@
{ lib, config, pkgs, ... }:
with lib;
let
cfg = config.test;
in
{
lib,
config,
pkgs,
...
}:
with lib; let
cfg = config.test;
in {
options.test = {
name = mkOption {
description = "Test name";
@ -38,8 +39,13 @@ in
};
};
});
default = [ ];
example = [{ assertion = false; message = "you can't enable this for some reason"; }];
default = [];
example = [
{
assertion = false;
message = "you can't enable this for some reason";
}
];
description = ''
This option allows modules to express conditions that must
hold for the evaluation of the system configuration to
@ -52,6 +58,5 @@ in
type = types.nullOr (types.either types.lines types.path);
default = null;
};
};
}