|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const { execAsync } = vi.hoisted(() => ({ execAsync: vi.fn() })); |
| 4 | + |
| 5 | +vi.mock("@dokploy/server/db", () => ({ |
| 6 | + db: {}, |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("@dokploy/server/utils/process/execAsync", () => ({ |
| 10 | + execAsync, |
| 11 | + execAsyncRemote: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +const { |
| 15 | + cleanupOldDokployImages, |
| 16 | + getAgentHitsUpdateCommand, |
| 17 | + getDokployImageCleanupCommand, |
| 18 | + getDokployImageKeepCount, |
| 19 | + getDokployImageRepositories, |
| 20 | + getDokployImages, |
| 21 | +} = await import("@dokploy/server/services/settings"); |
| 22 | + |
| 23 | +describe("Dokploy image cleanup", () => { |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks(); |
| 26 | + vi.stubEnv("DOKPLOY_KEEP_IMAGES", undefined); |
| 27 | + vi.stubEnv("DOKPLOY_AGENTHITS_UPDATE_IMAGE", undefined); |
| 28 | + vi.stubEnv("DOKPLOY_AGENTHITS_UPDATE_TAG", undefined); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + vi.unstubAllEnvs(); |
| 33 | + }); |
| 34 | + |
| 35 | + it.each([ |
| 36 | + [undefined, null], |
| 37 | + ["0", null], |
| 38 | + ["2", null], |
| 39 | + ["3", 3], |
| 40 | + ["5", 5], |
| 41 | + ["6", null], |
| 42 | + ["3.5", null], |
| 43 | + ["abc", null], |
| 44 | + ])("reads keep count %s as %s", (value, expected) => { |
| 45 | + vi.stubEnv("DOKPLOY_KEEP_IMAGES", value); |
| 46 | + expect(getDokployImageKeepCount()).toBe(expected); |
| 47 | + }); |
| 48 | + |
| 49 | + it("targets the fork and official image repositories without tags", () => { |
| 50 | + expect(getDokployImageRepositories()).toEqual([ |
| 51 | + "ghcr.io/agenthits/dokploy", |
| 52 | + "dokploy/dokploy", |
| 53 | + ]); |
| 54 | + |
| 55 | + vi.stubEnv( |
| 56 | + "DOKPLOY_AGENTHITS_UPDATE_IMAGE", |
| 57 | + "registry.local:5000/team/dokploy:dev@sha256:abc", |
| 58 | + ); |
| 59 | + expect(getDokployImageRepositories()).toEqual([ |
| 60 | + "registry.local:5000/team/dokploy", |
| 61 | + "dokploy/dokploy", |
| 62 | + ]); |
| 63 | + }); |
| 64 | + |
| 65 | + it("keeps the newest images and only removes exited dokploy task containers", () => { |
| 66 | + const command = getDokployImageCleanupCommand(4); |
| 67 | + |
| 68 | + expect(command).toContain( |
| 69 | + "--filter reference\\=ghcr.io/agenthits/dokploy --filter reference\\=dokploy/dokploy", |
| 70 | + ); |
| 71 | + expect(command).toContain("tail -n +5"); |
| 72 | + expect(command).toContain("--filter status=exited"); |
| 73 | + expect(command).toContain( |
| 74 | + "--filter label=com.docker.swarm.service.name=dokploy", |
| 75 | + ); |
| 76 | + expect(command).not.toMatch(/\brm (-f|--force)\b/); |
| 77 | + }); |
| 78 | + |
| 79 | + it("does nothing at startup when the cleanup is off", async () => { |
| 80 | + await cleanupOldDokployImages(); |
| 81 | + expect(execAsync).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("runs the cleanup at startup when a keep count is set", async () => { |
| 85 | + vi.stubEnv("DOKPLOY_KEEP_IMAGES", "3"); |
| 86 | + execAsync.mockResolvedValue({ stdout: "", stderr: "" }); |
| 87 | + |
| 88 | + await cleanupOldDokployImages(); |
| 89 | + |
| 90 | + expect(execAsync).toHaveBeenCalledWith(getDokployImageCleanupCommand(3)); |
| 91 | + }); |
| 92 | + |
| 93 | + it("does not throw when the cleanup fails", async () => { |
| 94 | + vi.stubEnv("DOKPLOY_KEEP_IMAGES", "3"); |
| 95 | + execAsync.mockRejectedValue(new Error("docker unavailable")); |
| 96 | + vi.spyOn(console, "error").mockImplementation(() => {}); |
| 97 | + |
| 98 | + await expect(cleanupOldDokployImages()).resolves.toBeUndefined(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("stores the keep count on the service only when it is given", () => { |
| 102 | + expect(getAgentHitsUpdateCommand("v0.30.6")).not.toContain( |
| 103 | + "DOKPLOY_KEEP_IMAGES", |
| 104 | + ); |
| 105 | + expect(getAgentHitsUpdateCommand("v0.30.6", null, null, 3)).toContain( |
| 106 | + "--env-add DOKPLOY_KEEP_IMAGES\\=3 \\\n\tdokploy", |
| 107 | + ); |
| 108 | + expect(getAgentHitsUpdateCommand("v0.30.6", null, null, null)).toContain( |
| 109 | + "--env-add DOKPLOY_KEEP_IMAGES\\=0", |
| 110 | + ); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe("Dokploy image list", () => { |
| 115 | + beforeEach(() => { |
| 116 | + vi.clearAllMocks(); |
| 117 | + vi.stubEnv("DOKPLOY_AGENTHITS_UPDATE_IMAGE", undefined); |
| 118 | + }); |
| 119 | + |
| 120 | + afterEach(() => { |
| 121 | + vi.unstubAllEnvs(); |
| 122 | + }); |
| 123 | + |
| 124 | + const image = (id: string, createdAt: string, fork: string) => |
| 125 | + JSON.stringify({ |
| 126 | + id: `sha256:${id}`, |
| 127 | + createdAt, |
| 128 | + size: 4455354111, |
| 129 | + tags: [`ghcr.io/agenthits/dokploy:agenthits-dev-${id}`], |
| 130 | + config: { |
| 131 | + Env: [ |
| 132 | + "PATH=/usr/bin|/bin", |
| 133 | + `DOKPLOY_FORK_VERSION=${fork}`, |
| 134 | + "DOKPLOY_OFFICIAL_VERSION=v0.30.6", |
| 135 | + ], |
| 136 | + }, |
| 137 | + }); |
| 138 | + const container = (id: string, running: boolean, service: string) => |
| 139 | + JSON.stringify({ image: `sha256:${id}`, running, service }); |
| 140 | + |
| 141 | + it("reads versions, sizes and containers newest first", async () => { |
| 142 | + execAsync.mockImplementation(async (command: string) => { |
| 143 | + if (command.startsWith("docker image ls")) { |
| 144 | + return { stdout: "sha256:aaa\nsha256:bbb\nsha256:ccc\n", stderr: "" }; |
| 145 | + } |
| 146 | + if (command.startsWith("docker image inspect")) { |
| 147 | + return { |
| 148 | + stdout: [ |
| 149 | + image("aaa", "2026-10-07T10:26:35.154227243Z", "Fork_412+aaa"), |
| 150 | + image("ccc", "2026-10-07T08:50:17.72561574Z", "Fork_400+ccc"), |
| 151 | + image("bbb", "2026-10-07T09:30:23.012650643Z", "Fork_408+bbb"), |
| 152 | + ].join("\n"), |
| 153 | + stderr: "", |
| 154 | + }; |
| 155 | + } |
| 156 | + if (command.startsWith("docker system df")) { |
| 157 | + return { |
| 158 | + stdout: JSON.stringify({ |
| 159 | + Images: [{ ID: "sha256:aaa", UniqueSize: "4.115GB" }], |
| 160 | + }), |
| 161 | + stderr: "", |
| 162 | + }; |
| 163 | + } |
| 164 | + return { |
| 165 | + stdout: [ |
| 166 | + container("aaa", true, "dokploy"), |
| 167 | + container("bbb", false, "dokploy"), |
| 168 | + container("bbb", false, "dokploy"), |
| 169 | + container("ccc", false, ""), |
| 170 | + ].join("\n"), |
| 171 | + stderr: "", |
| 172 | + }; |
| 173 | + }); |
| 174 | + |
| 175 | + const images = await getDokployImages(); |
| 176 | + |
| 177 | + expect(images.map((item) => item.forkVersion)).toEqual([ |
| 178 | + "Fork_412+aaa", |
| 179 | + "Fork_408+bbb", |
| 180 | + "Fork_400+ccc", |
| 181 | + ]); |
| 182 | + expect(images[0]).toMatchObject({ |
| 183 | + isCurrent: true, |
| 184 | + uniqueSizeBytes: 4115000000, |
| 185 | + officialVersion: "v0.30.6", |
| 186 | + }); |
| 187 | + expect(images[1]).toMatchObject({ |
| 188 | + isCurrent: false, |
| 189 | + dokployTaskContainers: 2, |
| 190 | + otherContainers: 0, |
| 191 | + uniqueSizeBytes: null, |
| 192 | + }); |
| 193 | + expect(images[2]).toMatchObject({ otherContainers: 1 }); |
| 194 | + }); |
| 195 | + |
| 196 | + it("returns nothing when there are no Dokploy images", async () => { |
| 197 | + execAsync.mockResolvedValue({ stdout: "", stderr: "" }); |
| 198 | + await expect(getDokployImages()).resolves.toEqual([]); |
| 199 | + expect(execAsync).toHaveBeenCalledTimes(1); |
| 200 | + }); |
| 201 | +}); |
0 commit comments