Skip to content

Commit

Permalink
Merge pull request #691 from Green-Software-Foundation/dump-logs-errors
Browse files Browse the repository at this point in the history
Dump logs errors
  • Loading branch information
MariamKhalatova committed May 12, 2024
2 parents 2eb5d3d + 6185ee0 commit cbfadfd
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 142 deletions.
2 changes: 2 additions & 0 deletions src/__mocks__/builtins/export-csv.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {Context} from '../../types/manifest';

export const tree = {
Expand Down Expand Up @@ -117,6 +118,7 @@ export const tree = {
},
};

// @ts-ignore
export const context: Context = {
name: 'demo',
description: '',
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/unit/builtins/export-csv-raw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ERRORS} from '../../../util/errors';

import {tree, context, outputs} from '../../../__mocks__/builtins/export-csv';

const {CliInputError, WriteFileError} = ERRORS;
const {ExhaustError} = ERRORS;

jest.mock('fs/promises', () => ({
__esModule: true,
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('builtins/export-csv-raw: ', () => {
await expect(
exportCSVRaw.execute(tree, context, outputPath)
).rejects.toThrow(
new WriteFileError(
new ExhaustError(
'Failed to write CSV to output#carbon: Could not write CSV file.'
)
);
Expand All @@ -65,8 +65,8 @@ describe('builtins/export-csv-raw: ', () => {
try {
await exportCSVRaw.execute(tree, context, outputPath);
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error).toEqual(new CliInputError('Output path is required.'));
expect(error).toBeInstanceOf(ExhaustError);
expect(error).toEqual(new ExhaustError('Output path is required.'));
}
});
});
Expand Down
14 changes: 7 additions & 7 deletions src/__tests__/unit/builtins/export-csv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
aggregation,
} from '../../../__mocks__/builtins/export-csv';

const {CliInputError} = ERRORS;
const {ExhaustError} = ERRORS;

jest.mock('fs/promises', () => ({
writeFile: jest.fn<() => Promise<void>>().mockResolvedValue(),
Expand Down Expand Up @@ -197,8 +197,8 @@ describe('builtins/export-csv: ', () => {
try {
await exportCSV.execute(tree, context, outputPath);
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error).toEqual(new CliInputError('Output path is required.'));
expect(error).toBeInstanceOf(ExhaustError);
expect(error).toEqual(new ExhaustError('Output path is required.'));
}
});

Expand All @@ -210,9 +210,9 @@ describe('builtins/export-csv: ', () => {
try {
await exportCSV.execute(tree, context, outputPath);
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error).toBeInstanceOf(ExhaustError);
expect(error).toEqual(
new CliInputError('Output path should contains `#`.')
new ExhaustError('Output path should contain `#`.')
);
}
});
Expand All @@ -225,9 +225,9 @@ describe('builtins/export-csv: ', () => {
try {
await exportCSV.execute(tree, context, outputPath);
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error).toBeInstanceOf(ExhaustError);
expect(error).toEqual(
new CliInputError(
new ExhaustError(
'CSV export criteria is not found in output path. Please append it after --output <path>#.'
)
);
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/unit/builtins/export-yaml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jest.mock('../../../util/yaml', () => ({
saveYamlFileAs: jest.fn(),
}));

const {CliInputError} = ERRORS;
const {ExhaustError} = ERRORS;

describe('builtins/export-yaml: ', () => {
describe('ExportYaml: ', () => {
Expand Down Expand Up @@ -38,8 +38,8 @@ describe('builtins/export-yaml: ', () => {
try {
await exportYaml.execute({}, context, '');
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error).toEqual(new CliInputError('Output path is required.'));
expect(error).toBeInstanceOf(ExhaustError);
expect(error).toEqual(new ExhaustError('Output path is required.'));
}
});
});
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/unit/lib/compute.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {compute} from '../../../lib/compute';

import {ComputeParams} from '../../../types/compute';
Expand Down Expand Up @@ -28,6 +29,7 @@ describe('lib/compute: ', () => {
* Compute params.
*/
const paramsExecute: ComputeParams = {
// @ts-ignore
context: {
name: 'mock-name',
initialize: {
Expand All @@ -42,6 +44,7 @@ describe('lib/compute: ', () => {
pluginStorage: pluginStorage().set('mock', mockExecutePlugin()),
};
const params: ComputeParams = {
// @ts-ignore
context: {
name: 'mock-name',
initialize: {
Expand Down
10 changes: 5 additions & 5 deletions src/__tests__/unit/lib/exhaust.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ERRORS} from '../../../util/errors';

import {STRINGS} from '../../../config';

const {CliInputError, ModuleInitializationError} = ERRORS;
const {ExhaustError} = ERRORS;
const {INVALID_EXHAUST_PLUGIN} = STRINGS;

describe('lib/exhaust: ', () => {
Expand Down Expand Up @@ -58,9 +58,9 @@ describe('lib/exhaust: ', () => {
// @ts-ignore
await exhaust(tree, context, {});
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error).toBeInstanceOf(ExhaustError);

if (error instanceof CliInputError) {
if (error instanceof ExhaustError) {
expect(error.message).toEqual(expectedMessage);
}
}
Expand All @@ -83,9 +83,9 @@ describe('lib/exhaust: ', () => {
// @ts-ignore
await exhaust(tree, context, {});
} catch (error) {
expect(error).toBeInstanceOf(ModuleInitializationError);
expect(error).toBeInstanceOf(ExhaustError);

if (error instanceof ModuleInitializationError) {
if (error instanceof ExhaustError) {
expect(error.message).toEqual(expectedMessage);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jest.mock('../../../util/log-memoize', () => ({
memoizedLog: mockLog,
}));

import {initalize} from '../../../lib/initialize';
import {initialize} from '../../../lib/initialize';

import {ERRORS} from '../../../util/errors';

Expand All @@ -25,7 +25,7 @@ describe('lib/initalize: ', () => {
describe('initalize(): ', () => {
it('creates instance with get and set methods.', async () => {
const plugins = {};
const response = await initalize(plugins);
const response = await initialize(plugins);

expect(response).toHaveProperty('get');
expect(response).toHaveProperty('set');
Expand All @@ -40,7 +40,7 @@ describe('lib/initalize: ', () => {
method: 'Mockavizta',
},
};
const storage = await initalize(plugins);
const storage = await initialize(plugins);

const pluginName = Object.keys(plugins)[0];
const module = storage.get(pluginName);
Expand All @@ -59,7 +59,7 @@ describe('lib/initalize: ', () => {
},
},
};
const storage = await initalize(plugins);
const storage = await initialize(plugins);

const pluginName = Object.keys(plugins)[0];
const module = storage.get(pluginName);
Expand All @@ -79,7 +79,7 @@ describe('lib/initalize: ', () => {
};

try {
await initalize(plugins);
await initialize(plugins);
} catch (error) {
expect(error).toBeInstanceOf(PluginCredentialError);

Expand All @@ -101,7 +101,7 @@ describe('lib/initalize: ', () => {
};

try {
await initalize(plugins);
await initialize(plugins);
} catch (error) {
expect(error).toBeInstanceOf(PluginCredentialError);

Expand All @@ -121,7 +121,7 @@ describe('lib/initalize: ', () => {
},
},
};
const storage = await initalize(plugins);
const storage = await initialize(plugins);

const pluginName = Object.keys(plugins)[0];
const module = storage.get(pluginName);
Expand All @@ -139,7 +139,7 @@ describe('lib/initalize: ', () => {
},
},
};
const storage = await initalize(plugins);
const storage = await initialize(plugins);

const pluginName = Object.keys(plugins)[0];
const module = storage.get(pluginName);
Expand All @@ -159,7 +159,7 @@ describe('lib/initalize: ', () => {
};

try {
await initalize(plugins);
await initialize(plugins);
} catch (error) {
expect(error).toBeInstanceOf(ModuleInitializationError);

Expand Down
129 changes: 64 additions & 65 deletions src/__tests__/unit/lib/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,7 @@ describe('lib/load: ', () => {
const result = await load(inputPath, paramPath);

const expectedValue = {
tree: {
children: {
'front-end': {
pipeline: ['boavizta-cpu'],
config: {
'boavizta-cpu': {
'core-units': 24,
processor: 'Intel® Core™ i7-1185G7',
},
},
inputs: [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 18.392,
},
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cpu/utilization': 16,
},
],
},
},
},
rawContext: {
rawManifest: {
name: 'gsf-demo',
description: 'Hello',
tags: {
Expand All @@ -69,6 +44,31 @@ describe('lib/load: ', () => {
},
},
},
tree: {
children: {
'front-end': {
pipeline: ['boavizta-cpu'],
config: {
'boavizta-cpu': {
'core-units': 24,
processor: 'Intel® Core™ i7-1185G7',
},
},
inputs: [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 18.392,
},
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cpu/utilization': 16,
},
],
},
},
},
},
parameters: PARAMETERS,
};
Expand All @@ -82,45 +82,8 @@ describe('lib/load: ', () => {

const result = await load(inputPath, paramPath);

const expectedParameters = {
'mock-carbon': {
description: 'an amount of carbon emitted into the atmosphere',
unit: 'gCO2e',
aggregation: 'sum',
},
'mock-cpu': {
description: 'number of cores available',
unit: 'cores',
aggregation: 'none',
},
};
const expectedValue = {
tree: {
children: {
'front-end': {
pipeline: ['boavizta-cpu'],
config: {
'boavizta-cpu': {
'core-units': 24,
processor: 'Intel® Core™ i7-1185G7',
},
},
inputs: [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 18.392,
},
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cpu/utilization': 16,
},
],
},
},
},
rawContext: {
rawManifest: {
name: 'gsf-demo',
description: 'Hello',
tags: {
Expand All @@ -136,8 +99,44 @@ describe('lib/load: ', () => {
},
},
},
tree: {
children: {
'front-end': {
pipeline: ['boavizta-cpu'],
config: {
'boavizta-cpu': {
'core-units': 24,
processor: 'Intel® Core™ i7-1185G7',
},
},
inputs: [
{
timestamp: '2023-07-06T00:00',
duration: 3600,
'cpu/utilization': 18.392,
},
{
timestamp: '2023-08-06T00:00',
duration: 3600,
'cpu/utilization': 16,
},
],
},
},
},
},
parameters: {
'mock-carbon': {
description: 'an amount of carbon emitted into the atmosphere',
unit: 'gCO2e',
aggregation: 'sum',
},
'mock-cpu': {
description: 'number of cores available',
unit: 'cores',
aggregation: 'none',
},
},
parameters: expectedParameters,
};

expect(result).toEqual(expectedValue);
Expand Down
Loading

0 comments on commit cbfadfd

Please sign in to comment.