|
| 1 | +/********************************************************************** |
| 2 | + * Copyright (C) 2024-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 type { Configuration } from '@podman-desktop/api'; |
| 20 | +import { configuration, context } from '@podman-desktop/api'; |
| 21 | +import { beforeEach, expect, test, vi } from 'vitest'; |
| 22 | + |
| 23 | +import { DockerCompatibilitySetup } from './docker-compatibility-setup.js'; |
| 24 | +import type { DockerContextHandler } from './docker-context-handler.js'; |
| 25 | + |
| 26 | +vi.mock('@podman-desktop/api', async () => { |
| 27 | + return { |
| 28 | + context: { |
| 29 | + setValue: vi.fn(), |
| 30 | + }, |
| 31 | + configuration: { |
| 32 | + onDidChangeConfiguration: vi.fn(), |
| 33 | + getConfiguration: vi.fn(), |
| 34 | + }, |
| 35 | + env: { |
| 36 | + isLinux: false, |
| 37 | + isWindows: false, |
| 38 | + isMac: false, |
| 39 | + }, |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
| 43 | +const dockerContextHandler = { |
| 44 | + listContexts: vi.fn(), |
| 45 | + switchContext: vi.fn(), |
| 46 | +} as unknown as DockerContextHandler; |
| 47 | + |
| 48 | +let dockerCompatibilitySetup: DockerCompatibilitySetup; |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + vi.resetAllMocks(); |
| 52 | + dockerCompatibilitySetup = new DockerCompatibilitySetup(dockerContextHandler); |
| 53 | +}); |
| 54 | + |
| 55 | +test('check sending the docker cli contexts as context.setValue', async () => { |
| 56 | + // return a list of 2 contexts, second one being the current one |
| 57 | + vi.mocked(dockerContextHandler.listContexts).mockResolvedValue([ |
| 58 | + { |
| 59 | + name: 'context1', |
| 60 | + metadata: { |
| 61 | + description: 'description1', |
| 62 | + }, |
| 63 | + endpoints: { |
| 64 | + docker: { |
| 65 | + host: 'host1', |
| 66 | + }, |
| 67 | + }, |
| 68 | + isCurrentContext: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: 'context2', |
| 72 | + metadata: { |
| 73 | + description: 'description2', |
| 74 | + }, |
| 75 | + endpoints: { |
| 76 | + docker: { |
| 77 | + host: 'host2', |
| 78 | + }, |
| 79 | + }, |
| 80 | + isCurrentContext: true, |
| 81 | + }, |
| 82 | + ]); |
| 83 | + |
| 84 | + await dockerCompatibilitySetup.init(); |
| 85 | + |
| 86 | + // check we called listContexts |
| 87 | + expect(dockerContextHandler.listContexts).toHaveBeenCalled(); |
| 88 | + |
| 89 | + // check we called setValue with the expected values |
| 90 | + expect(context.setValue).toHaveBeenCalledWith( |
| 91 | + 'docker.cli.context', |
| 92 | + [ |
| 93 | + { |
| 94 | + label: 'context1 (host1)', |
| 95 | + selected: false, |
| 96 | + value: 'context1', |
| 97 | + }, |
| 98 | + { |
| 99 | + label: 'context2 (host2)', |
| 100 | + selected: true, |
| 101 | + value: 'context2', |
| 102 | + }, |
| 103 | + ], |
| 104 | + 'DockerCompatibility', |
| 105 | + ); |
| 106 | +}); |
| 107 | + |
| 108 | +test('check set the context when configuration change', async () => { |
| 109 | + // empty list of context |
| 110 | + vi.mocked(dockerContextHandler.listContexts).mockResolvedValue([]); |
| 111 | + |
| 112 | + await dockerCompatibilitySetup.init(); |
| 113 | + |
| 114 | + // capture the callback sent to onDidChangeConfiguration |
| 115 | + const callback = vi.mocked(configuration.onDidChangeConfiguration).mock.calls[0][0]; |
| 116 | + |
| 117 | + // mock configuration.getConfiguration |
| 118 | + vi.mocked(configuration.getConfiguration).mockReturnValue({ |
| 119 | + get: vi.fn(() => 'context1'), |
| 120 | + } as unknown as Configuration); |
| 121 | + |
| 122 | + // mock switchContext |
| 123 | + vi.mocked(dockerContextHandler.switchContext).mockResolvedValue(); |
| 124 | + |
| 125 | + // call the callback |
| 126 | + callback({ affectsConfiguration: vi.fn(() => true) }); |
| 127 | + |
| 128 | + // check we called switchContext |
| 129 | + expect(dockerContextHandler.switchContext).toHaveBeenCalledWith('context1'); |
| 130 | +}); |
0 commit comments