Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure the crc daemon is running before execute start (#205)" #219

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions __mocks__/@podman-desktop/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**********************************************************************
* Copyright (C) 2022 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

/**
* Mock the extension API for vitest.
* This file is referenced from vitest.config.js file.
*/
const plugin = {};
module.exports = plugin;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@
"format:fix": "prettier --write src/**",
"desk:build": "ts-node-esm ./scripts/run.mts build",
"desk:prepare": "ts-node-esm ./scripts/run.mts prepare",
"desk:run": "ts-node-esm ./scripts/run.mts run"
"desk:run": "ts-node-esm ./scripts/run.mts run",
"test": "vitest run --coverage --passWithNoTests"
},
"dependencies": {},
"devDependencies": {
"7zip-min": "^1.4.4",
"@podman-desktop/api": "next",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
Expand All @@ -79,6 +79,8 @@
"@types/node": "^18.14.6",
"@typescript-eslint/eslint-plugin": "^5.55.0",
"@typescript-eslint/parser": "^5.55.0",
"@vitest/coverage-v8": "^1.6.0",
"7zip-min": "^1.4.4",
"compare-versions": "^6.0.0-rc.1",
"eslint": "^8.36.0",
"got": "^12.5.3",
Expand All @@ -89,6 +91,7 @@
"ts-node": "^10.9.1",
"tslib": "^2.5.0",
"typescript": "^4.9.5",
"vitest": "^1.6.0",
"which": "^3.0.0",
"zip-local": "^0.3.5"
}
Expand Down
33 changes: 33 additions & 0 deletions src/crc-setup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import { expect, test, vi } from 'vitest';
import * as crcCli from './crc-cli';
import { needSetup } from './crc-setup';

test('needSetup should return true if setup --check-only command fails', async () => {
vi.spyOn(crcCli, 'execPromise').mockRejectedValue('daemon not running');
const isNeeded = await needSetup();
expect(isNeeded).toBeTruthy();
});

test('needSetup should return false if setup --check-only command succeed', async () => {
vi.spyOn(crcCli, 'execPromise').mockResolvedValue('');
const isNeeded = await needSetup();
expect(isNeeded).toBeFalsy();
});
6 changes: 1 addition & 5 deletions src/crc-setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
* Copyright (C) 2023-2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,15 +20,11 @@ import * as extensionApi from '@podman-desktop/api';
import { execPromise, getCrcCli } from './crc-cli';
import { defaultSetUpPreset, productName } from './util';

export let isNeedSetup = false;

export async function needSetup(): Promise<boolean> {
try {
await execPromise(getCrcCli(), ['setup', '--check-only']);
isNeedSetup = false;
return false;
} catch (e) {
isNeedSetup = true;
return true;
}
}
Expand Down
73 changes: 73 additions & 0 deletions src/crc-start.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import type * as extensionApi from '@podman-desktop/api';
import { expect, test, vi } from 'vitest';
import * as crcCli from './crc-cli';
import * as crcSetup from './crc-setup';
import { startCrc } from './crc-start';
import * as logProvider from './log-provider';
import * as daemon from './daemon-commander';
import type { StartInfo } from './types';

vi.mock('@podman-desktop/api', async () => {
return {
EventEmitter: vi.fn(),
};
});

test('setUpCRC is skipped if already setup, it just perform the daemon start command', async () => {
vi.spyOn(crcCli, 'execPromise').mockResolvedValue('');
vi.spyOn(logProvider.crcLogProvider, 'startSendingLogs').mockImplementation(() => {
return Promise.resolve();
});
const startDaemon = vi.spyOn(daemon.commander, 'start').mockResolvedValue({
Status: 'Running',
} as unknown as StartInfo);
const setUpMock = vi.spyOn(crcSetup, 'setUpCrc');
await startCrc(
{
updateStatus: vi.fn(),
} as unknown as extensionApi.Provider,
{} as extensionApi.Logger,
{ logUsage: vi.fn() } as unknown as extensionApi.TelemetryLogger,
);
expect(setUpMock).not.toBeCalled();
expect(startDaemon).toBeCalled();
});

test('set up CRC and then start the daemon', async () => {
vi.spyOn(crcCli, 'execPromise').mockRejectedValue('daemon not running');

vi.spyOn(logProvider.crcLogProvider, 'startSendingLogs').mockImplementation(() => {
return Promise.resolve();
});
const startDaemon = vi.spyOn(daemon.commander, 'start').mockResolvedValue({
Status: 'Running',
} as unknown as StartInfo);
const setUpMock = vi.spyOn(crcSetup, 'setUpCrc').mockImplementation(() => Promise.resolve(true));
await startCrc(
{
updateStatus: vi.fn(),
} as unknown as extensionApi.Provider,
{} as extensionApi.Logger,
{ logUsage: vi.fn() } as unknown as extensionApi.TelemetryLogger,
);
expect(setUpMock).toBeCalled();
expect(startDaemon).toBeCalled();
});
6 changes: 3 additions & 3 deletions src/crc-start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
* Copyright (C) 2023-2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
***********************************************************************/

import * as extensionApi from '@podman-desktop/api';
import { isNeedSetup, needSetup, setUpCrc } from './crc-setup';
import { needSetup, setUpCrc } from './crc-setup';
import { crcStatus } from './crc-status';
import { commander } from './daemon-commander';
import { crcLogProvider } from './log-provider';
Expand All @@ -44,11 +44,11 @@ export async function startCrc(
});
try {
// call crc setup to prepare bundle, before start
const isNeedSetup = await needSetup();
if (isNeedSetup) {
try {
crcStatus.setSetupRunning(true);
await setUpCrc(logger);
await needSetup();
} catch (error) {
logger.error(error);
provider.updateStatus('stopped');
Expand Down
5 changes: 3 additions & 2 deletions src/crc-status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
* Copyright (C) 2023-2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@
import * as extensionApi from '@podman-desktop/api';
import type { Status, CrcStatus as CrcStatusApi } from './types';
import { commander } from './daemon-commander';
import { isNeedSetup } from './crc-setup';
import { needSetup } from './crc-setup';

const defaultStatus: Status = { CrcStatus: 'Unknown', Preset: 'openshift' };
const setupStatus: Status = { CrcStatus: 'Need Setup', Preset: 'Unknown' };
Expand Down Expand Up @@ -75,6 +75,7 @@ export class CrcStatus {
}

async initialize(): Promise<void> {
const isNeedSetup = await needSetup();
if (isNeedSetup) {
this._status = setupStatus;
return;
Expand Down
7 changes: 4 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**********************************************************************
* Copyright (C) 2022 Red Hat, Inc.
* Copyright (C) 2022-2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@ import { CrcInstall } from './install/crc-install';

import { crcStatus } from './crc-status';
import { startCrc } from './crc-start';
import { isNeedSetup, needSetup, setUpCrc } from './crc-setup';
import { needSetup, setUpCrc } from './crc-setup';
import { deleteCrc, registerDeleteCommand } from './crc-delete';
import { presetChangedEvent, syncPreferences } from './preferences';
import { stopCrc } from './crc-stop';
Expand Down Expand Up @@ -58,8 +58,9 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
const detectionChecks: extensionApi.ProviderDetectionCheck[] = [];
let status: extensionApi.ProviderStatus = 'not-installed';
let hasDaemonRunning = false;
let isNeedSetup = false;
if (crcVersion) {
await needSetup();
isNeedSetup = await needSetup();

status = 'installed';
if (!isNeedSetup) {
Expand Down
37 changes: 37 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import path from 'node:path';

const config = {
test: {
include: ['**/*.{test,spec}.?(c|m)[jt]s?(x)'],
coverage: {
provider: 'v8',
reporter: ['lcov', 'text'],
extension: '.ts',
},
},
resolve: {
alias: {
'@podman-desktop/api': path.resolve(__dirname, '__mocks__/@podman-desktop/api.js'),
},
},
};

export default config;
Loading
Loading