| 
 | 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 | +import { beforeEach, expect, test, vi } from 'vitest';  | 
 | 19 | +import * as extensionApi from '@podman-desktop/api';  | 
 | 20 | +import { pushImageToCrcCluster } from './image-handler.js';  | 
 | 21 | +import { tmpdir, homedir } from 'node:os';  | 
 | 22 | +import { promises } from 'node:fs';  | 
 | 23 | +import * as utils from './util.js';  | 
 | 24 | + | 
 | 25 | +type ImageInfo = { engineId: string; name?: string; tag?: string };  | 
 | 26 | + | 
 | 27 | +vi.mock('@podman-desktop/api', async () => {  | 
 | 28 | +  return {  | 
 | 29 | +    window: {  | 
 | 30 | +      withProgress: vi.fn(),  | 
 | 31 | +      showInformationMessage: vi.fn(),  | 
 | 32 | +    },  | 
 | 33 | +    containerEngine: {  | 
 | 34 | +      saveImage: vi.fn(),  | 
 | 35 | +    },  | 
 | 36 | +    ProgressLocation: {  | 
 | 37 | +      TASK_WIDGET: 2,  | 
 | 38 | +    },  | 
 | 39 | +    configuration: {  | 
 | 40 | +      getConfiguration: vi.fn(),  | 
 | 41 | +    },  | 
 | 42 | +    EventEmitter: vi.fn(),  | 
 | 43 | +  };  | 
 | 44 | +});  | 
 | 45 | + | 
 | 46 | +vi.mock('./util.ts', async () => {  | 
 | 47 | +  return {  | 
 | 48 | +    runCliCommand: vi.fn(),  | 
 | 49 | +    isWindows: vi.fn(),  | 
 | 50 | +    isMac: vi.fn(),  | 
 | 51 | +    productName: 'OpenShift Local',  | 
 | 52 | +  };  | 
 | 53 | +});  | 
 | 54 | + | 
 | 55 | +vi.mock('node:os', () => {  | 
 | 56 | +  return {  | 
 | 57 | +    tmpdir: vi.fn(),  | 
 | 58 | +    platform: vi.fn().mockReturnValue('linux'),  | 
 | 59 | +    homedir: vi.fn(),  | 
 | 60 | +  };  | 
 | 61 | +});  | 
 | 62 | + | 
 | 63 | +vi.mock('node:fs', () => {  | 
 | 64 | +  return {  | 
 | 65 | +    promises: {  | 
 | 66 | +      rm: vi.fn(),  | 
 | 67 | +      access: vi.fn(),  | 
 | 68 | +    },  | 
 | 69 | +  };  | 
 | 70 | +});  | 
 | 71 | + | 
 | 72 | +vi.mock('./crc-status.ts', () => {  | 
 | 73 | +  return {  | 
 | 74 | +    crcStatus: {  | 
 | 75 | +      status: {  | 
 | 76 | +        CrcStatus: 'Running',  | 
 | 77 | +      },  | 
 | 78 | +    },  | 
 | 79 | +  };  | 
 | 80 | +});  | 
 | 81 | + | 
 | 82 | +beforeEach(() => {  | 
 | 83 | +  vi.resetAllMocks();  | 
 | 84 | +  vi.mocked(utils.isWindows).mockReturnValue(false);  | 
 | 85 | +  vi.mocked(utils.isMac).mockReturnValue(false);  | 
 | 86 | +  vi.mocked(utils.runCliCommand).mockResolvedValue({ exitCode: 0, stdErr: '', stdOut: 'ok' } as utils.SpawnResult);  | 
 | 87 | +  vi.mocked(extensionApi.configuration.getConfiguration).mockReturnValue({  | 
 | 88 | +    get: vi.fn(),  | 
 | 89 | +  } as unknown as extensionApi.Configuration);  | 
 | 90 | +});  | 
 | 91 | + | 
 | 92 | +test.each([  | 
 | 93 | +  { accessExists: false, keyName: 'id_ecdsa' },  | 
 | 94 | +  { accessExists: true, keyName: 'id_ed25519' },  | 
 | 95 | +])('use correct ssh key name for version $version', async ({ accessExists, keyName }) => {  | 
 | 96 | +  let progressFunction;  | 
 | 97 | + | 
 | 98 | +  const progress: extensionApi.Progress<{ message?: string; increment?: number }> = { report: vi.fn() };  | 
 | 99 | + | 
 | 100 | +  vi.mocked(extensionApi.window.withProgress).mockImplementation(  | 
 | 101 | +    (  | 
 | 102 | +      options: extensionApi.ProgressOptions,  | 
 | 103 | +      task: (  | 
 | 104 | +        progress: extensionApi.Progress<{ message?: string; increment?: number }>,  | 
 | 105 | +        token: extensionApi.CancellationToken,  | 
 | 106 | +      ) => Promise<void>,  | 
 | 107 | +    ): Promise<void> => {  | 
 | 108 | +      progressFunction = task;  | 
 | 109 | +      return;  | 
 | 110 | +    },  | 
 | 111 | +  );  | 
 | 112 | + | 
 | 113 | +  vi.mocked(tmpdir).mockReturnValue('/tmp/path');  | 
 | 114 | +  vi.mocked(homedir).mockReturnValue('/home/path');  | 
 | 115 | +  if (accessExists) {  | 
 | 116 | +    vi.mocked(promises.access).mockResolvedValue(undefined);  | 
 | 117 | +  } else {  | 
 | 118 | +    vi.mocked(promises.access).mockRejectedValue({ code: 'ENOENT' });  | 
 | 119 | +  }  | 
 | 120 | + | 
 | 121 | +  const imageInfoMock: ImageInfo = {  | 
 | 122 | +    engineId: 'imageEngine',  | 
 | 123 | +    name: 'image1',  | 
 | 124 | +    tag: 'tag1',  | 
 | 125 | +  };  | 
 | 126 | +  await pushImageToCrcCluster(imageInfoMock);  | 
 | 127 | + | 
 | 128 | +  await progressFunction(progress);  | 
 | 129 | + | 
 | 130 | +  expect(utils.runCliCommand).toHaveBeenCalledWith(  | 
 | 131 | +    'podman',  | 
 | 132 | +    expect.arrayContaining([`--identity=/home/path/.crc/machines/crc/${keyName}`]),  | 
 | 133 | +    expect.any(Object),  | 
 | 134 | +  );  | 
 | 135 | +});  | 
0 commit comments