Skip to content

Commit 2221b8a

Browse files
committed
fix: preserve stack log selection on fetch failures
1 parent c77f6d8 commit 2221b8a

2 files changed

Lines changed: 74 additions & 5 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {
2+
getContainersByAppNameMatch,
3+
getStackContainersByAppName,
4+
} from "@dokploy/server/services/docker";
5+
import {
6+
execAsync,
7+
execAsyncRemote,
8+
} from "@dokploy/server/utils/process/execAsync";
9+
import { beforeEach, describe, expect, it, vi } from "vitest";
10+
11+
vi.mock("@dokploy/server/utils/process/execAsync", async (importOriginal) => ({
12+
...(await importOriginal<
13+
typeof import("@dokploy/server/utils/process/execAsync")
14+
>()),
15+
execAsync: vi.fn(),
16+
execAsyncRemote: vi.fn(),
17+
}));
18+
19+
const execAsyncMock = vi.mocked(execAsync);
20+
const execAsyncRemoteMock = vi.mocked(execAsyncRemote);
21+
22+
describe("stack container lookup results", () => {
23+
beforeEach(() => {
24+
vi.resetAllMocks();
25+
});
26+
27+
it("returns undefined when the native stack lookup fails", async () => {
28+
execAsyncMock.mockRejectedValueOnce(new Error("Docker is unavailable"));
29+
30+
await expect(
31+
getContainersByAppNameMatch("farmgate-odoo", "stack"),
32+
).resolves.toBeUndefined();
33+
});
34+
35+
it("returns an empty list for a successful native lookup with no tasks", async () => {
36+
execAsyncMock.mockResolvedValueOnce({ stdout: "", stderr: "" });
37+
38+
await expect(
39+
getContainersByAppNameMatch("farmgate-odoo", "stack"),
40+
).resolves.toEqual([]);
41+
});
42+
43+
it("returns undefined when the remote native stack lookup fails", async () => {
44+
execAsyncRemoteMock.mockRejectedValueOnce(new Error("SSH is unavailable"));
45+
46+
await expect(
47+
getContainersByAppNameMatch("farmgate-odoo", "stack", "server-1"),
48+
).resolves.toBeUndefined();
49+
});
50+
51+
it("returns undefined when the Swarm stack lookup reports an error", async () => {
52+
execAsyncRemoteMock.mockResolvedValueOnce({
53+
stdout: "",
54+
stderr: "node is unavailable",
55+
});
56+
57+
await expect(
58+
getStackContainersByAppName("farmgate-odoo", "server-1"),
59+
).resolves.toBeUndefined();
60+
});
61+
62+
it("returns an empty list for a successful Swarm lookup with no tasks", async () => {
63+
execAsyncMock.mockResolvedValueOnce({ stdout: "", stderr: "" });
64+
65+
await expect(getStackContainersByAppName("farmgate-odoo")).resolves.toEqual(
66+
[],
67+
);
68+
});
69+
});

‎packages/server/src/services/docker.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export const getContainersByAppNameMatch = async (
167167
return containers || [];
168168
} catch {}
169169

170-
return [];
170+
return appType === "stack" ? undefined : [];
171171
};
172172

173173
const getStackTaskContainers = async (appName: string, serverId?: string) => {
@@ -227,7 +227,7 @@ const getStackTaskContainers = async (appName: string, serverId?: string) => {
227227
return containers;
228228
} catch {}
229229

230-
return [];
230+
return undefined;
231231
};
232232

233233
export const getStackContainersByAppName = async (
@@ -243,7 +243,7 @@ export const getStackContainersByAppName = async (
243243
const { stdout, stderr } = await execAsyncRemote(serverId, command);
244244

245245
if (stderr) {
246-
return [];
246+
return undefined;
247247
}
248248

249249
if (!stdout) return [];
@@ -252,7 +252,7 @@ export const getStackContainersByAppName = async (
252252
const { stdout, stderr } = await execAsync(command);
253253

254254
if (stderr) {
255-
return [];
255+
return undefined;
256256
}
257257

258258
if (!stdout) return [];
@@ -292,7 +292,7 @@ export const getStackContainersByAppName = async (
292292
return containers || [];
293293
} catch {}
294294

295-
return [];
295+
return undefined;
296296
};
297297

298298
export const getServiceContainersByAppName = async (

0 commit comments

Comments
 (0)