mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-02 18:22:16 +00:00
refactor: keep stack log fix focused
This commit is contained in:
@@ -1,73 +0,0 @@
|
|||||||
import {
|
|
||||||
getContainersByAppNameMatch,
|
|
||||||
getStackContainersByAppName,
|
|
||||||
} from "@dokploy/server/services/docker";
|
|
||||||
import {
|
|
||||||
execAsync,
|
|
||||||
execAsyncRemote,
|
|
||||||
} from "@dokploy/server/utils/process/execAsync";
|
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
||||||
|
|
||||||
vi.mock("@dokploy/server/utils/process/execAsync", async (importOriginal) => ({
|
|
||||||
...(await importOriginal<
|
|
||||||
typeof import("@dokploy/server/utils/process/execAsync")
|
|
||||||
>()),
|
|
||||||
execAsync: vi.fn(),
|
|
||||||
execAsyncRemote: vi.fn(),
|
|
||||||
}));
|
|
||||||
|
|
||||||
const execAsyncMock = vi.mocked(execAsync);
|
|
||||||
const execAsyncRemoteMock = vi.mocked(execAsyncRemote);
|
|
||||||
|
|
||||||
describe("stack container lookup results", () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
vi.resetAllMocks();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns undefined when the native stack lookup fails", async () => {
|
|
||||||
execAsyncMock.mockRejectedValueOnce(new Error("Docker is unavailable"));
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
getContainersByAppNameMatch("farmgate-odoo", "stack"),
|
|
||||||
).resolves.toBeUndefined();
|
|
||||||
|
|
||||||
const command = execAsyncMock.mock.calls[0]?.[0];
|
|
||||||
expect(command).toBeDefined();
|
|
||||||
expect(command).not.toContain("|| true");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns an empty list for a successful native lookup with no tasks", async () => {
|
|
||||||
execAsyncMock.mockResolvedValueOnce({ stdout: "", stderr: "" });
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
getContainersByAppNameMatch("farmgate-odoo", "stack"),
|
|
||||||
).resolves.toEqual([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns undefined when the remote native stack lookup fails", async () => {
|
|
||||||
execAsyncRemoteMock.mockRejectedValueOnce(new Error("SSH is unavailable"));
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
getContainersByAppNameMatch("farmgate-odoo", "stack", "server-1"),
|
|
||||||
).resolves.toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns undefined when the Swarm stack lookup reports an error", async () => {
|
|
||||||
execAsyncRemoteMock.mockResolvedValueOnce({
|
|
||||||
stdout: "",
|
|
||||||
stderr: "node is unavailable",
|
|
||||||
});
|
|
||||||
|
|
||||||
await expect(
|
|
||||||
getStackContainersByAppName("farmgate-odoo", "server-1"),
|
|
||||||
).resolves.toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns an empty list for a successful Swarm lookup with no tasks", async () => {
|
|
||||||
execAsyncMock.mockResolvedValueOnce({ stdout: "", stderr: "" });
|
|
||||||
|
|
||||||
await expect(getStackContainersByAppName("farmgate-odoo")).resolves.toEqual(
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -167,7 +167,7 @@ export const getContainersByAppNameMatch = async (
|
|||||||
return containers || [];
|
return containers || [];
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
return appType === "stack" ? undefined : [];
|
return [];
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStackTaskContainers = async (appName: string, serverId?: string) => {
|
const getStackTaskContainers = async (appName: string, serverId?: string) => {
|
||||||
@@ -175,7 +175,7 @@ const getStackTaskContainers = async (appName: string, serverId?: string) => {
|
|||||||
const divider = "__DOKPLOY_DIVIDER__";
|
const divider = "__DOKPLOY_DIVIDER__";
|
||||||
const tasksCommand = `docker stack ps ${appName} --no-trunc --filter "desired-state=running" --format 'TASK : {{.ID}} | Name: {{.Name}} | Node: {{.Node}} | CurrentState: {{.CurrentState}} | Error: {{.Error}}'`;
|
const tasksCommand = `docker stack ps ${appName} --no-trunc --filter "desired-state=running" --format 'TASK : {{.ID}} | Name: {{.Name}} | Node: {{.Node}} | CurrentState: {{.CurrentState}} | Error: {{.Error}}'`;
|
||||||
const inspectCommand = `docker stack ps ${appName} -q --no-trunc --filter "desired-state=running" | xargs -r docker inspect --format '{{if .Status.ContainerStatus}}TASK : {{.ID}} | ContainerId: {{.Status.ContainerStatus.ContainerID}}{{end}}' 2>/dev/null`;
|
const inspectCommand = `docker stack ps ${appName} -q --no-trunc --filter "desired-state=running" | xargs -r docker inspect --format '{{if .Status.ContainerStatus}}TASK : {{.ID}} | ContainerId: {{.Status.ContainerStatus.ContainerID}}{{end}}' 2>/dev/null`;
|
||||||
const command = `${tasksCommand} && echo "${divider}" && ${inspectCommand}`;
|
const command = `${tasksCommand} && echo "${divider}" && (${inspectCommand} || true)`;
|
||||||
|
|
||||||
let stdout = "";
|
let stdout = "";
|
||||||
|
|
||||||
@@ -227,7 +227,7 @@ const getStackTaskContainers = async (appName: string, serverId?: string) => {
|
|||||||
return containers;
|
return containers;
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
return undefined;
|
return [];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getStackContainersByAppName = async (
|
export const getStackContainersByAppName = async (
|
||||||
@@ -243,7 +243,7 @@ export const getStackContainersByAppName = async (
|
|||||||
const { stdout, stderr } = await execAsyncRemote(serverId, command);
|
const { stdout, stderr } = await execAsyncRemote(serverId, command);
|
||||||
|
|
||||||
if (stderr) {
|
if (stderr) {
|
||||||
return undefined;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stdout) return [];
|
if (!stdout) return [];
|
||||||
@@ -252,7 +252,7 @@ export const getStackContainersByAppName = async (
|
|||||||
const { stdout, stderr } = await execAsync(command);
|
const { stdout, stderr } = await execAsync(command);
|
||||||
|
|
||||||
if (stderr) {
|
if (stderr) {
|
||||||
return undefined;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stdout) return [];
|
if (!stdout) return [];
|
||||||
@@ -292,7 +292,7 @@ export const getStackContainersByAppName = async (
|
|||||||
return containers || [];
|
return containers || [];
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|
||||||
return undefined;
|
return [];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getServiceContainersByAppName = async (
|
export const getServiceContainersByAppName = async (
|
||||||
|
|||||||
Reference in New Issue
Block a user