Skip to content

Commit

Permalink
test: improve coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Ilona Shishov <[email protected]>
  • Loading branch information
IlonaShishov committed Jul 10, 2024
1 parent 9cb102b commit 48e58de
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 52 deletions.
63 changes: 57 additions & 6 deletions test/exhortServices.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { describe, it, expect, vi } from 'vitest';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { execSync } from 'child_process';
import exhort from '@rhecosystemappeng/exhort-javascript-api';

import { UTM_SOURCE } from '../src/constants';
import { imageAnalysisService } from '../src/exhortServices';
import { imageAnalysisService, stackAnalysisService } from '../src/exhortServices';

vi.mock('child_process', () => ({
execSync: vi.fn(),
}));

vi.mock('@rhecosystemappeng/exhort-javascript-api', async (importOriginal) => {
const actual: any = await importOriginal();
return {
...actual,
default: {
...actual.default,
stackAnalysis: vi.fn(),
},
};
});

describe('imageAnalysisService', () => {
const options = {
// RHDA_TOKEN: string;
RHDA_SOURCE: UTM_SOURCE,
EXHORT_SYFT_PATH: '/path/to/syft',
EXHORT_SYFT_CONFIG_PATH: '/path/to/syft_config',
Expand All @@ -20,7 +31,11 @@ describe('imageAnalysisService', () => {
EXHORT_PODMAN_PATH: '/path/to/podman',
EXHORT_IMAGE_PLATFORM: 'platform',
};
const images = [{ image: 'image1', platform: 'platform1' }];
const images = [{ image: 'image1', platform: 'platform1' }, { image: 'image2', platform: '' }];

beforeEach(() => {
vi.clearAllMocks();
});

it('should execute image analysis and return HTML result', async () => {
const mockExecSyncResult = 'Mock HTML result';
Expand All @@ -29,7 +44,7 @@ describe('imageAnalysisService', () => {
const result = await imageAnalysisService(images, options);

const expectedJarPath = `${process.cwd()}/javaApiAdapter/exhort-java-api-adapter-1.0-SNAPSHOT-jar-with-dependencies.jar`;
const expectedCommand = `java -DRHDA_SOURCE=github-actions -DEXHORT_SYFT_PATH=/path/to/syft -DEXHORT_SYFT_CONFIG_PATH=/path/to/syft_config -DEXHORT_SKOPEO_PATH=/path/to/skopeo -DEXHORT_SKOPEO_CONFIG_PATH=/path/to/skopeo_config -DEXHORT_DOCKER_PATH=/path/to/docker -DEXHORT_PODMAN_PATH=/path/to/podman -DEXHORT_IMAGE_PLATFORM=platform -jar ${expectedJarPath} json image1^^platform1`;
const expectedCommand = `java -DRHDA_SOURCE=github-actions -DEXHORT_SYFT_PATH=/path/to/syft -DEXHORT_SYFT_CONFIG_PATH=/path/to/syft_config -DEXHORT_SKOPEO_PATH=/path/to/skopeo -DEXHORT_SKOPEO_CONFIG_PATH=/path/to/skopeo_config -DEXHORT_DOCKER_PATH=/path/to/docker -DEXHORT_PODMAN_PATH=/path/to/podman -DEXHORT_IMAGE_PLATFORM=platform -jar ${expectedJarPath} json image1^^platform1 image2`;
expect(execSync).toHaveBeenCalledWith(expectedCommand, {
maxBuffer: 1000 * 1000 * 10,
});
Expand All @@ -47,4 +62,40 @@ describe('imageAnalysisService', () => {
});
});

// TODO: stackAnalysisService Unit Test
describe('stackAnalysisService', () => {
const mockPathToManifest = 'path/to/manifest';
const mockOptions = { someOption: 'someValue' };
const mockReport: exhort.AnalysisReport = { analysis: 'report' } as exhort.AnalysisReport;
const mockError: Error = new Error('Analysis failed');

beforeEach(() => {
vi.clearAllMocks();
});

it('should return the stack analysis report in JSON format', async () => {
vi.mocked(exhort.stackAnalysis).mockResolvedValue(mockReport);

const result = await stackAnalysisService(mockPathToManifest, mockOptions);

expect(result).toEqual(mockReport);
expect(exhort.stackAnalysis).toHaveBeenCalledWith(
mockPathToManifest,
false,
mockOptions
);
});

it('should throw an error if the stack analysis fails', async () => {
vi.mocked(exhort.stackAnalysis).mockRejectedValue(mockError);

await expect(stackAnalysisService(mockPathToManifest, mockOptions)).rejects.toThrow(
mockError
);

expect(exhort.stackAnalysis).toHaveBeenCalledWith(
mockPathToManifest,
false,
mockOptions
);
});
});
4 changes: 2 additions & 2 deletions test/imageAnalysis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ vi.mock('../src/exhortServices', () => ({
}));

describe('executeDockerImageAnalysis', () => {
const mockFileContent = 'ARG TEST_ARG=14\nFROM node:${TEST_ARG}\nFROM --platform=linux python:3.8 as app\nFROM scratch\n# hello world';
const mockFileContent = 'ARG TEST_ARG1=node\nARG TEST_ARG2=14\nFROM ${TEST_ARG1}:$TEST_ARG2\nFROM --platform=linux python:3.8 as app\nFROM scratch\n# hello world';
const filePath = '/mock/path/to/Dockerfile';

beforeEach(() => {
vi.resetModules();
vi.clearAllMocks();

vi.mocked(fs.readFileSync).mockReturnValue(mockFileContent);
});
Expand Down
20 changes: 10 additions & 10 deletions test/rhda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ describe('generateRHDAReport', () => {
const rhdaReportJsonFilePath = `${process.cwd()}/report.json`;

beforeEach(() => {
vi.resetModules();
vi.clearAllMocks();

vi.spyOn(ghCore, 'getInput').mockImplementation((name) => {
vi.mocked(ghCore.getInput).mockImplementation((name) => {
const inputs = {
[Inputs.MATCH_MANIFEST_VERSION]: 'true',
[Inputs.USE_PYTHON_VIRTUAL_ENVIRONMENT]: 'false',
Expand All @@ -54,20 +54,15 @@ describe('generateRHDAReport', () => {
return inputs[name];
});

vi.spyOn(exhortServices, 'stackAnalysisService').mockResolvedValue(rhdaReportJson);
vi.mocked(exhortServices.stackAnalysisService).mockResolvedValue(rhdaReportJson);

vi.spyOn(imageAnalysis, 'executeDockerImageAnalysis').mockResolvedValue(rhdaReportJson);

vi.spyOn(utils, 'writeToFile').mockResolvedValue(undefined);

vi.spyOn(ghCore, 'info').mockImplementation(() => {});

vi.spyOn(ghCore, 'setOutput').mockImplementation(() => {});
vi.mocked(imageAnalysis.executeDockerImageAnalysis).mockResolvedValue(rhdaReportJson);
});

it('should generate the RHDA report for non-Docker ecosystem', async () => {
const result = await generateRHDAReport(manifestFilePath, MAVEN);

expect(ghCore.info).toBeCalledWith(`⏳ Analysing dependencies...`);
expect(exhortServices.stackAnalysisService).toHaveBeenCalledWith(manifestFilePath, {
RHDA_SOURCE: UTM_SOURCE,
MATCH_MANIFEST_VERSIONS: 'true',
Expand All @@ -88,7 +83,9 @@ describe('generateRHDAReport', () => {
JSON.stringify(rhdaReportJson, null, 4),
rhdaReportJsonFilePath,
);
expect(ghCore.info).toBeCalledWith(`✍️ Setting output "${Outputs.RHDA_REPORT_JSON}" to ${rhdaReportJsonFilePath}`);
expect(ghCore.setOutput).toHaveBeenCalledWith(Outputs.RHDA_REPORT_JSON, rhdaReportJson);
expect(ghCore.info).toBeCalledWith(`✅ Successfully generated Red Had Dependency Analytics report`);
expect(result).toEqual({
rhdaReportJson: rhdaReportJson,
rhdaReportJsonFilePath: rhdaReportJsonFilePath,
Expand All @@ -98,12 +95,15 @@ describe('generateRHDAReport', () => {
it('should generate the RHDA report for Docker ecosystem', async () => {
const result = await generateRHDAReport(manifestFilePath, DOCKER);

expect(ghCore.info).toBeCalledWith(`⏳ Analysing dependencies...`);
expect(imageAnalysis.executeDockerImageAnalysis).toHaveBeenCalledWith(manifestFilePath);
expect(utils.writeToFile).toHaveBeenCalledWith(
JSON.stringify(rhdaReportJson, null, 4),
rhdaReportJsonFilePath,
);
expect(ghCore.info).toBeCalledWith(`✍️ Setting output "${Outputs.RHDA_REPORT_JSON}" to ${rhdaReportJsonFilePath}`);
expect(ghCore.setOutput).toHaveBeenCalledWith(Outputs.RHDA_REPORT_JSON, rhdaReportJson);
expect(ghCore.info).toBeCalledWith(`✅ Successfully generated Red Had Dependency Analytics report`);
expect(result).toEqual({
rhdaReportJson: rhdaReportJson,
rhdaReportJsonFilePath: rhdaReportJsonFilePath,
Expand Down
Loading

0 comments on commit 48e58de

Please sign in to comment.