Skip to content

Commit 42b75b4

Browse files
authored
test: add unit tests for PodLogs (#326)
Signed-off-by: Philippe Martin <[email protected]>
1 parent d036eb2 commit 42b75b4

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/**********************************************************************
2+
* Copyright (C) 2025 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
***********************************************************************/
18+
19+
import { render } from '@testing-library/svelte';
20+
import { RemoteMocks } from '/@/tests/remote-mocks';
21+
import { API_POD_LOGS } from '/@common/channels';
22+
import { StreamsMocks } from '/@/tests/stream-mocks';
23+
import { FakeStreamObject } from '/@/stream/util/fake-stream-object.svelte';
24+
import type { PodLogsChunk } from '/@common/model/pod-logs-chunk';
25+
import type { PodLogsApi } from '/@common/interface/pod-logs-api';
26+
import PodLogs from './PodLogs.svelte';
27+
import type { V1Pod } from '@kubernetes/client-node';
28+
import TerminalWindow from '../terminal/TerminalWindow.svelte';
29+
import type { Terminal } from '@xterm/xterm';
30+
import { EmptyScreen } from '@podman-desktop/ui-svelte';
31+
32+
vi.mock(import('../terminal/TerminalWindow.svelte'));
33+
vi.mock(import('@podman-desktop/ui-svelte'));
34+
35+
const remoteMocks = new RemoteMocks();
36+
const streamMocks = new StreamsMocks();
37+
38+
const streamPodLogsMock = new FakeStreamObject<PodLogsChunk>();
39+
40+
beforeEach(() => {
41+
vi.resetAllMocks();
42+
streamMocks.reset();
43+
streamMocks.mock<PodLogsChunk>('streamPodLogs', streamPodLogsMock);
44+
45+
remoteMocks.reset();
46+
remoteMocks.mock(API_POD_LOGS, {} as unknown as PodLogsApi);
47+
});
48+
49+
describe('pod with one container', async () => {
50+
let pod: V1Pod;
51+
beforeEach(() => {
52+
pod = {
53+
metadata: {
54+
name: 'podName',
55+
namespace: 'namespace',
56+
},
57+
spec: {
58+
containers: [
59+
{
60+
name: 'containerName',
61+
},
62+
],
63+
},
64+
} as V1Pod;
65+
});
66+
67+
test('display No Logwith no logs', async () => {
68+
render(PodLogs, { object: pod });
69+
expect(EmptyScreen).toHaveBeenCalledWith(
70+
expect.anything(),
71+
expect.objectContaining({
72+
hidden: false,
73+
}),
74+
);
75+
});
76+
77+
test('write received logs to the terminal', async () => {
78+
const mockedTerminal: Terminal = {
79+
write: vi.fn(),
80+
dispose: vi.fn(),
81+
clear: vi.fn(),
82+
} as unknown as Terminal;
83+
vi.mocked(TerminalWindow).mockImplementation((_, props) => {
84+
props.terminal = mockedTerminal;
85+
return {};
86+
});
87+
render(PodLogs, { object: pod });
88+
expect(mockedTerminal.write).not.toHaveBeenCalledWith();
89+
expect(mockedTerminal.clear).toHaveBeenCalled();
90+
expect(TerminalWindow).toHaveBeenCalled();
91+
92+
streamPodLogsMock.sendData({
93+
podName: 'podName',
94+
namespace: 'namespace',
95+
containerName: 'containerName',
96+
data: 'some logs',
97+
});
98+
expect(mockedTerminal.write).toHaveBeenCalledWith('some logs\r');
99+
expect(EmptyScreen).toHaveBeenCalledWith(
100+
expect.anything(),
101+
expect.objectContaining({
102+
hidden: true,
103+
}),
104+
);
105+
});
106+
});
107+
108+
describe('pod with two containers', async () => {
109+
let pod: V1Pod;
110+
beforeEach(() => {
111+
pod = {
112+
metadata: {
113+
name: 'podName',
114+
namespace: 'namespace',
115+
},
116+
spec: {
117+
containers: [
118+
{
119+
name: 'cnt1',
120+
},
121+
{
122+
name: 'containerName2',
123+
},
124+
],
125+
},
126+
} as V1Pod;
127+
});
128+
129+
test('display No Logwith no logs', async () => {
130+
render(PodLogs, { object: pod });
131+
expect(EmptyScreen).toHaveBeenCalledWith(
132+
expect.anything(),
133+
expect.objectContaining({
134+
hidden: false,
135+
}),
136+
);
137+
});
138+
139+
test('write received logs to the terminal v2', async () => {
140+
const mockedTerminal: Terminal = {
141+
write: vi.fn(),
142+
dispose: vi.fn(),
143+
clear: vi.fn(),
144+
} as unknown as Terminal;
145+
vi.mocked(TerminalWindow).mockImplementation((_, props) => {
146+
props.terminal = mockedTerminal;
147+
return {};
148+
});
149+
render(PodLogs, { object: pod });
150+
expect(mockedTerminal.write).not.toHaveBeenCalledWith();
151+
expect(mockedTerminal.clear).toHaveBeenCalled();
152+
expect(TerminalWindow).toHaveBeenCalled();
153+
154+
streamPodLogsMock.sendData({
155+
podName: 'podName',
156+
namespace: 'namespace',
157+
containerName: 'cnt1',
158+
data: 'some logs',
159+
});
160+
expect(mockedTerminal.write).toHaveBeenCalledWith(' \u001b[36mcnt1\u001b[0m|some logs\r');
161+
expect(EmptyScreen).toHaveBeenCalledWith(
162+
expect.anything(),
163+
expect.objectContaining({
164+
hidden: true,
165+
}),
166+
);
167+
});
168+
});

0 commit comments

Comments
 (0)