Skip to content

Commit

Permalink
Write tests for overriding resources from helm, kustomize and yamls
Browse files Browse the repository at this point in the history
  • Loading branch information
arnarg committed Aug 8, 2024
1 parent a77ba17 commit 4b7ee1c
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/helm/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ in {
"helm.sh/chart"
]);
};

# Create an application with a helm chart
# and resource override
test3 = {
helm.releases.test3 = {
chart = ./chart;
};

resources.deployments.test3-chart.spec.template.spec.containers.chart.image = lib.mkForce "nginx:2.0.0";
};
};

test = with lib; {
Expand Down Expand Up @@ -235,6 +245,59 @@ in {
spec.ca.secretName = "ca-key-pair";
};
}

{
description = "Resource override in nixidy should override resource rendered by helm.";
expression =
findFirst
(x: x.kind == "Deployment" && x.metadata.name == "test3-chart")
null
apps.test3.objects;
expected = {
apiVersion = "apps/v1";
kind = "Deployment";
metadata = {
name = "test3-chart";
namespace = "test3";
labels = {
"app.kubernetes.io/instance" = "test3";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
"app.kubernetes.io/version" = "1.16.0";
"helm.sh/chart" = "chart-0.1.0";
};
};
spec = {
replicas = 1;
selector.matchLabels = {
"app.kubernetes.io/instance" = "test3";
"app.kubernetes.io/name" = "chart";
};
template = {
metadata.labels = {
"app.kubernetes.io/instance" = "test3";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
"app.kubernetes.io/version" = "1.16.0";
"helm.sh/chart" = "chart-0.1.0";
};
spec.containers = [
{
name = "chart";
image = "nginx:2.0.0";
ports = [
{
name = "http";
containerPort = 80;
protocol = "TCP";
}
];
}
];
};
};
};
}
];
};
}
51 changes: 51 additions & 0 deletions tests/kustomize/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ in {
path = "overlay";
};
};

# Create an application with a kustomization and
# a resource override
test3 = {
kustomize.applications.test3.kustomization = {
src = ./manifests;
path = "base";
};

resources.deployments.deployment.spec.template.spec.containers.nginx.image = lib.mkForce "nginx:2.0.0";
};
};

test = with lib; {
Expand Down Expand Up @@ -193,6 +204,46 @@ in {
spec.ca.secretName = "ca-key-pair";
};
}

{
description = "Overridden deployment in nixidy should override deployment rendered by kustomize.";

expression =
findFirst
(x: x.kind == "Deployment" && x.metadata.name == "deployment")
null
apps.test3.objects;

expected = {
apiVersion = "apps/v1";
kind = "Deployment";
metadata = {
name = "deployment";
namespace = "test3";
labels."app.kubernetes.io/name" = "deployment";
};
spec = {
replicas = 1;
selector.matchLabels."app.kubernetes.io/name" = "deployment";
template = {
metadata.labels."app.kubernetes.io/name" = "deployment";
spec.containers = [
{
name = "nginx";
image = "nginx:2.0.0";
ports = [
{
name = "http";
containerPort = 80;
protocol = "TCP";
}
];
}
];
};
};
};
}
];
};
}
58 changes: 58 additions & 0 deletions tests/yamls.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ in {
secretName: ca-key-pair
''
];

# Create an application with yamls and a
# resource override
test2 = {
yamls = [
''
apiVersion: v1
kind: Service
metadata:
name: my-svc
labels:
name: my-svc
spec:
type: ClusterIP
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
name: my-svc
''
];

resources.services.my-svc.spec.ports.http.port = lib.mkForce 8080;
};
};

test = with lib; {
Expand Down Expand Up @@ -94,6 +120,38 @@ in {
spec.ca.secretName = "ca-key-pair";
};
}

{
description = "Service should be overridden correctly.";

expression =
findFirst
(x: x.kind == "Service" && x.metadata.name == "my-svc")
null
apps.test2.objects;

expected = {
apiVersion = "v1";
kind = "Service";
metadata = {
name = "my-svc";
namespace = "test2";
labels.name = "my-svc";
};
spec = {
type = "ClusterIP";
ports = [
{
port = 8080;
targetPort = "http";
protocol = "TCP";
name = "http";
}
];
selector.name = "my-svc";
};
};
}
];
};
}

0 comments on commit 4b7ee1c

Please sign in to comment.