mirror of
https://github.com/TECHNOFAB11/kubenix.git
synced 2025-12-13 00:20:07 +01:00
WIP: test refactoring
This commit is contained in:
parent
8ad3b90a52
commit
bbc5e3d477
17 changed files with 714 additions and 66 deletions
|
|
@ -1,48 +1,60 @@
|
|||
{ pkgs ? import <nixpkgs> {}
|
||||
, lib ? pkgs.lib
|
||||
, kubenix ? import ../. { inherit pkgs lib; }
|
||||
, k8sVersion ? "1.21"
|
||||
|
||||
, nixosPath ? toString <nixpkgs/nixos>
|
||||
|
||||
# whether any testing error should throw an error
|
||||
, throwError ? true
|
||||
, e2e ? true }:
|
||||
, k8sVersion ? "1.18"
|
||||
, registryUrl ? throw "Registry url not defined"
|
||||
, throwError ? true # whether any testing error should throw an error
|
||||
, enabledTests ? null }:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
images = pkgs.callPackage ./images.nix {};
|
||||
|
||||
test = (kubenix.evalModules {
|
||||
config = (kubenix.evalModules {
|
||||
modules = [
|
||||
kubenix.modules.testing
|
||||
|
||||
{
|
||||
testing.name = "k8s-${k8sVersion}";
|
||||
testing.throwError = throwError;
|
||||
testing.e2e = e2e;
|
||||
testing.tests = [
|
||||
./k8s/simple.nix
|
||||
./k8s/deployment.nix
|
||||
./k8s/deployment-k3s.nix
|
||||
# ./k8s/crd.nix # flaky
|
||||
./k8s/defaults.nix
|
||||
./k8s/order.nix
|
||||
./k8s/submodule.nix
|
||||
./k8s/imports.nix
|
||||
# ./legacy/k8s.nix
|
||||
# ./legacy/crd.nix
|
||||
# ./legacy/modules.nix
|
||||
./helm/simple.nix
|
||||
# ./istio/bookinfo.nix # infinite recusion
|
||||
./submodules/simple.nix
|
||||
./submodules/defaults.nix
|
||||
./submodules/versioning.nix
|
||||
./submodules/exports.nix
|
||||
./submodules/passthru.nix
|
||||
];
|
||||
testing.args = {
|
||||
inherit images k8sVersion;
|
||||
testing = {
|
||||
name = "kubenix-${k8sVersion}";
|
||||
throwError = throwError;
|
||||
enabledTests = enabledTests;
|
||||
tests = [
|
||||
./k8s/simple.nix
|
||||
./k8s/deployment.nix
|
||||
./k8s/deployment-k3s.nix
|
||||
# ./k8s/crd.nix # flaky
|
||||
./k8s/defaults.nix
|
||||
./k8s/order.nix
|
||||
./k8s/submodule.nix
|
||||
./k8s/imports.nix
|
||||
#./legacy/k8s.nix
|
||||
#./legacy/crd.nix
|
||||
#./legacy/modules.nix
|
||||
./helm/simple.nix
|
||||
# ./istio/bookinfo.nix # infinite recursion
|
||||
./submodules/simple.nix
|
||||
./submodules/defaults.nix
|
||||
./submodules/versioning.nix
|
||||
./submodules/exports.nix
|
||||
./submodules/passthru.nix
|
||||
];
|
||||
args = {
|
||||
inherit images;
|
||||
};
|
||||
docker.registryUrl = registryUrl;
|
||||
defaults = [
|
||||
{
|
||||
features = ["k8s"];
|
||||
default = {
|
||||
kubernetes.version = k8sVersion;
|
||||
};
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
];
|
||||
|
|
@ -53,4 +65,4 @@ let
|
|||
inherit kubenix nixosPath;
|
||||
};
|
||||
}).config;
|
||||
in pkgs.recurseIntoAttrs test.testing
|
||||
in pkgs.recurseIntoAttrs config.testing
|
||||
|
|
|
|||
|
|
@ -3,6 +3,13 @@
|
|||
with lib;
|
||||
|
||||
{
|
||||
curl = dockerTools.buildLayeredImage {
|
||||
name = "curl";
|
||||
tag = "latest";
|
||||
config.Cmd = [ "${pkgs.bash}" "-c" "sleep infinity" ];
|
||||
contents = [ pkgs.bash pkgs.curl pkgs.cacert ];
|
||||
};
|
||||
|
||||
nginx = let
|
||||
nginxPort = "80";
|
||||
nginxConf = pkgs.writeText "nginx.conf" ''
|
||||
|
|
|
|||
|
|
@ -1,10 +1,26 @@
|
|||
{ config, lib, pkgs, kubenix, images, k8sVersion, ... }:
|
||||
{ config, lib, pkgs, kubenix, images, ... }:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.kubernetes.api.resources.deployments.nginx;
|
||||
image = images.nginx;
|
||||
|
||||
clientPod = builtins.toFile "client.json" (builtins.toJSON {
|
||||
apiVersion = "v1";
|
||||
kind = "Pod";
|
||||
metadata = {
|
||||
namespace = config.kubernetes.namespace;
|
||||
name = "curl";
|
||||
};
|
||||
spec.containers = [{
|
||||
name = "curl";
|
||||
image = config.docker.images.curl.path;
|
||||
args = ["curl" "--retry" "20" "--retry-connrefused" "http://nginx"];
|
||||
}];
|
||||
spec.restartPolicy = "Never";
|
||||
});
|
||||
|
||||
in {
|
||||
imports = [ kubenix.modules.test kubenix.modules.k8s kubenix.modules.docker ];
|
||||
|
||||
|
|
@ -24,34 +40,55 @@ in {
|
|||
assertion = cfg.kind == "Deployment";
|
||||
} {
|
||||
message = "should have replicas set";
|
||||
assertion = cfg.spec.replicas == 10;
|
||||
assertion = cfg.spec.replicas == 3;
|
||||
}];
|
||||
extraConfiguration = {
|
||||
environment.systemPackages = [ pkgs.curl ];
|
||||
services.kubernetes.kubelet.seedDockerImages = config.docker.export;
|
||||
};
|
||||
testScript = ''
|
||||
kube.wait_until_succeeds("kubectl apply -f ${config.kubernetes.result}")
|
||||
driver = "kubetest";
|
||||
script = ''
|
||||
import time
|
||||
|
||||
kube.succeed("kubectl get deployment | grep -i nginx")
|
||||
kube.wait_until_succeeds("kubectl get deployment -o go-template nginx --template={{.status.readyReplicas}} | grep 10")
|
||||
kube.wait_until_succeeds("curl http://nginx.default.svc.cluster.local | grep -i hello")
|
||||
@pytest.mark.applymanifest('${config.kubernetes.resultYAML}')
|
||||
def test_deployment(kube):
|
||||
"""Tests whether deployment gets successfully created"""
|
||||
|
||||
kube.wait_for_registered(timeout=30)
|
||||
|
||||
deployments = kube.get_deployments()
|
||||
nginx_deploy = deployments.get('nginx')
|
||||
assert nginx_deploy is not None
|
||||
|
||||
pods = nginx_deploy.get_pods()
|
||||
assert len(pods) == 3
|
||||
|
||||
client_pod = kube.load_pod('${clientPod}')
|
||||
client_pod.create()
|
||||
|
||||
client_pod.wait_until_ready(timeout=30)
|
||||
client_pod.wait_until_containers_start()
|
||||
|
||||
container = client_pod.get_container('curl')
|
||||
|
||||
time.sleep(5)
|
||||
|
||||
logs = container.get_logs()
|
||||
|
||||
assert "Hello from NGINX" in logs
|
||||
'';
|
||||
};
|
||||
|
||||
docker.images.nginx.image = image;
|
||||
|
||||
kubernetes.version = k8sVersion;
|
||||
docker.images = {
|
||||
nginx.image = image;
|
||||
curl.image = images.curl;
|
||||
};
|
||||
|
||||
kubernetes.resources.deployments.nginx = {
|
||||
spec = {
|
||||
replicas = 10;
|
||||
replicas = 3;
|
||||
selector.matchLabels.app = "nginx";
|
||||
template.metadata.labels.app = "nginx";
|
||||
template.spec = {
|
||||
containers.nginx = {
|
||||
image = config.docker.images.nginx.path;
|
||||
imagePullPolicy = "Never";
|
||||
imagePullPolicy = "IfNotPresent";
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue