|
| 1 | +import { reconnectServicesToTraefik } from "@dokploy/server/services/settings"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +const mocks = vi.hoisted(() => ({ |
| 5 | + findMany: vi.fn(), |
| 6 | + execAsync: vi.fn(), |
| 7 | + execAsyncRemote: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("@dokploy/server/db", () => ({ |
| 11 | + db: { |
| 12 | + query: { |
| 13 | + compose: { |
| 14 | + findMany: mocks.findMany, |
| 15 | + }, |
| 16 | + }, |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("@dokploy/server/utils/process/execAsync", () => ({ |
| 21 | + execAsync: mocks.execAsync, |
| 22 | + execAsyncRemote: mocks.execAsyncRemote, |
| 23 | +})); |
| 24 | + |
| 25 | +describe("reconnectServicesToTraefik", () => { |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + mocks.findMany.mockResolvedValue([]); |
| 29 | + }); |
| 30 | + |
| 31 | + it("does not execute an empty local command when no isolated deployments exist", async () => { |
| 32 | + await reconnectServicesToTraefik(); |
| 33 | + |
| 34 | + expect(mocks.execAsync).not.toHaveBeenCalled(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("does not execute an empty remote command when no isolated deployments exist", async () => { |
| 38 | + await reconnectServicesToTraefik("server-id"); |
| 39 | + |
| 40 | + expect(mocks.execAsyncRemote).not.toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("reconnects isolated deployments to the local Traefik network", async () => { |
| 44 | + mocks.findMany.mockResolvedValue([ |
| 45 | + { appName: "first-compose" }, |
| 46 | + { appName: "second-compose" }, |
| 47 | + ]); |
| 48 | + |
| 49 | + await reconnectServicesToTraefik(); |
| 50 | + |
| 51 | + expect(mocks.execAsync).toHaveBeenCalledOnce(); |
| 52 | + expect(mocks.execAsync).toHaveBeenCalledWith( |
| 53 | + 'docker network connect first-compose $(docker ps --filter "name=dokploy-traefik" -q) >/dev/null 2>&1\n' + |
| 54 | + 'docker network connect second-compose $(docker ps --filter "name=dokploy-traefik" -q) >/dev/null 2>&1\n', |
| 55 | + ); |
| 56 | + expect(mocks.execAsyncRemote).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("reconnects isolated deployments on a remote server", async () => { |
| 60 | + mocks.findMany.mockResolvedValue([{ appName: "remote-compose" }]); |
| 61 | + |
| 62 | + await reconnectServicesToTraefik("server-id"); |
| 63 | + |
| 64 | + expect(mocks.execAsyncRemote).toHaveBeenCalledOnce(); |
| 65 | + expect(mocks.execAsyncRemote).toHaveBeenCalledWith( |
| 66 | + "server-id", |
| 67 | + 'docker network connect remote-compose $(docker ps --filter "name=dokploy-traefik" -q) >/dev/null 2>&1\n', |
| 68 | + ); |
| 69 | + expect(mocks.execAsync).not.toHaveBeenCalled(); |
| 70 | + }); |
| 71 | +}); |
0 commit comments