feat: add tests for labels and networks

This commit is contained in:
Mauricio Siu
2024-08-17 16:10:36 -06:00
parent c0261384ca
commit 65527bc39a
4 changed files with 178 additions and 24 deletions

View File

@@ -0,0 +1,74 @@
import type { Domain } from "@/server/api/services/domain";
import { createDomainLabels } from "@/server/utils/docker/domain";
import { describe, it, expect } from "vitest";
describe("createDomainLabels", () => {
const appName = "test-app";
const baseDomain: Domain = {
host: "example.com",
port: 8080,
https: false,
uniqueConfigKey: 1,
certificateType: "none",
applicationId: "",
composeId: "",
domainType: "compose",
serviceName: "test-app",
domainId: "",
path: "/",
createdAt: "",
};
it("should create basic labels for web entrypoint", async () => {
const labels = await createDomainLabels(appName, baseDomain, "web");
expect(labels).toContain(
"traefik.http.routers.test-app-1-web.rule=Host(`example.com`)",
);
expect(labels).toContain(
"traefik.http.routers.test-app-1-web.entrypoints=web",
);
expect(labels).toContain(
"traefik.http.services.test-app-1-web.loadbalancer.server.port=8080",
);
});
it("should create labels for websecure entrypoint", async () => {
const labels = await createDomainLabels(
appName,
{ ...baseDomain, https: true },
"websecure",
);
expect(labels).toContain(
"traefik.http.routers.test-app-1-websecure.rule=Host(`example.com`)",
);
expect(labels).toContain(
"traefik.http.routers.test-app-1-websecure.entrypoints=websecure",
);
expect(labels).not.toContain(
"traefik.http.services.test-app-1-websecure.loadbalancer.server.port=8080",
);
});
it("should add redirect middleware for https on web entrypoint", async () => {
const labels = await createDomainLabels(
appName,
{ ...baseDomain, https: true },
"web",
);
expect(labels).toContain(
"traefik.http.routers.test-app-1-web.middlewares=redirect-to-https@file",
);
});
it("should add Let's Encrypt configuration for websecure with letsencrypt certificate", async () => {
const labels = await createDomainLabels(
appName,
{ ...baseDomain, https: true, certificateType: "letsencrypt" },
"websecure",
);
expect(labels).toContain(
"traefik.http.routers.test-app-1-websecure.tls.certresolver=letsencrypt",
);
});
});

View File

@@ -0,0 +1,29 @@
import { addDokployNetworkToRoot } from "@/server/utils/docker/domain";
import { describe, it, expect } from "vitest";
describe("addDokployNetworkToRoot", () => {
it("should create network object if networks is undefined", () => {
const result = addDokployNetworkToRoot(undefined);
expect(result).toEqual({ "dokploy-network": { external: true } });
});
it("should add network to an empty object", () => {
const result = addDokployNetworkToRoot({});
expect(result).toEqual({ "dokploy-network": { external: true } });
});
it("should not modify existing network configuration", () => {
const existing = { "dokploy-network": { external: false } };
const result = addDokployNetworkToRoot(existing);
expect(result).toEqual({ "dokploy-network": { external: true } });
});
it("should add network alongside existing networks", () => {
const existing = { "other-network": { external: true } };
const result = addDokployNetworkToRoot(existing);
expect(result).toEqual({
"other-network": { external: true },
"dokploy-network": { external: true },
});
});
});

View File

@@ -0,0 +1,24 @@
import { addDokployNetworkToService } from "@/server/utils/docker/domain";
import { describe, it, expect } from "vitest";
describe("addDokployNetworkToService", () => {
it("should add network to an empty array", () => {
const result = addDokployNetworkToService([]);
expect(result).toEqual(["dokploy-network"]);
});
it("should not add duplicate network to an array", () => {
const result = addDokployNetworkToService(["dokploy-network"]);
expect(result).toEqual(["dokploy-network"]);
});
it("should add network to an existing array with other networks", () => {
const result = addDokployNetworkToService(["other-network"]);
expect(result).toEqual(["other-network", "dokploy-network"]);
});
it("should add network to an object if networks is an object", () => {
const result = addDokployNetworkToService({ "other-network": {} });
expect(result).toEqual({ "other-network": {}, "dokploy-network": {} });
});
});