Skip to content
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
62 changes: 62 additions & 0 deletions tools/integration/src/__tests__/handlers/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,67 @@ describe('Init Handler', () => {
const packageJson = JSON.parse(writeCall[1]);
expect(packageJson.publishConfig).toEqual({ access: 'public' });
});

it('should call generateCollectorFiles when collector is selected', async () => {
(input as jest.Mock)
.mockResolvedValueOnce('test-package')
.mockResolvedValueOnce('1.0.0')
.mockResolvedValueOnce('Description')
.mockResolvedValueOnce('')
.mockResolvedValueOnce('Author')
.mockResolvedValueOnce('MIT');

(checkbox as jest.Mock).mockResolvedValue(['dashboards', 'collector']);
(utils.generateReadme as jest.Mock).mockImplementation(() => {});
(utils.generateCollectorFiles as jest.Mock).mockImplementation(() => {});
(utils.printDirectoryTree as jest.Mock).mockImplementation(() => {});

await handleInit();

expect(utils.generateCollectorFiles).toHaveBeenCalledWith(
'/test/dir/test-package',
'test-package',
['dashboards', 'collector']
);
});

it('should not call generateCollectorFiles when collector is not selected', async () => {
(input as jest.Mock)
.mockResolvedValueOnce('test-package')
.mockResolvedValueOnce('1.0.0')
.mockResolvedValueOnce('Description')
.mockResolvedValueOnce('')
.mockResolvedValueOnce('Author')
.mockResolvedValueOnce('MIT');

(checkbox as jest.Mock).mockResolvedValue(['dashboards', 'events']);
(utils.generateReadme as jest.Mock).mockImplementation(() => {});
(utils.generateCollectorFiles as jest.Mock).mockImplementation(() => {});
(utils.printDirectoryTree as jest.Mock).mockImplementation(() => {});

await handleInit();

expect(utils.generateCollectorFiles).not.toHaveBeenCalled();
});

it('should create collector folder when collector is selected', async () => {
(input as jest.Mock)
.mockResolvedValueOnce('test-package')
.mockResolvedValueOnce('1.0.0')
.mockResolvedValueOnce('Description')
.mockResolvedValueOnce('')
.mockResolvedValueOnce('Author')
.mockResolvedValueOnce('MIT');

(checkbox as jest.Mock).mockResolvedValue(['collector']);
(utils.generateReadme as jest.Mock).mockImplementation(() => {});
(utils.generateCollectorFiles as jest.Mock).mockImplementation(() => {});
(utils.printDirectoryTree as jest.Mock).mockImplementation(() => {});

await handleInit();

expect(fs.mkdirSync).toHaveBeenCalledWith('/test/dir/test-package/collector', { recursive: true });
expect(logger.info).toHaveBeenCalledWith('Created the integration package sub-folder: /test/dir/test-package/collector');
});
});
});
44 changes: 44 additions & 0 deletions tools/integration/src/__tests__/handlers/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ describe('Lint Handler', () => {
(validators.validateDashboardFiles as jest.Mock).mockImplementation(() => {});
(validators.validateEventFiles as jest.Mock).mockImplementation(() => {});
(validators.validateSmartAlertFiles as jest.Mock).mockImplementation(() => {});
(validators.validateCollectorFiles as jest.Mock).mockImplementation(() => {});
(fs.existsSync as jest.Mock).mockReturnValue(true);

await expect(handleLint(argv)).rejects.toThrow('process.exit(0)');
Expand All @@ -360,6 +361,49 @@ describe('Lint Handler', () => {
expect(validators.validateDashboardFiles).toHaveBeenCalled();
expect(validators.validateEventFiles).toHaveBeenCalled();
expect(validators.validateSmartAlertFiles).toHaveBeenCalled();
expect(validators.validateCollectorFiles).toHaveBeenCalled();
});

it('should validate collector when collector folder exists', async () => {
const argv = { debug: false, 'strict-mode': false };
const packageData = { name: 'test-package', private: false };
const readmeContent = '# Test';

(utils.readPackageJson as jest.Mock).mockReturnValue(packageData);
(utils.isPrivatePackage as jest.Mock).mockReturnValue(false);
(utils.readReadmeFile as jest.Mock).mockReturnValue(readmeContent);
(validators.validatePackageJson as jest.Mock).mockResolvedValue(undefined);
(validators.validateReadmeContent as jest.Mock).mockImplementation(() => {});
(validators.validateCollectorFiles as jest.Mock).mockImplementation(() => {});
(fs.existsSync as jest.Mock).mockImplementation((path: string) => {
return path.includes('collector');
});

await expect(handleLint(argv)).rejects.toThrow('process.exit(0)');

expect(validators.validateCollectorFiles).toHaveBeenCalledWith(
'/test/package/collector',
expect.any(Array),
expect.any(Array),
expect.any(Array)
);
});

it('should log info when no collector folder found', async () => {
const argv = { debug: false, 'strict-mode': false };
const packageData = { name: 'test-package', private: false };
const readmeContent = '# Test';

(utils.readPackageJson as jest.Mock).mockReturnValue(packageData);
(utils.isPrivatePackage as jest.Mock).mockReturnValue(false);
(utils.readReadmeFile as jest.Mock).mockReturnValue(readmeContent);
(validators.validatePackageJson as jest.Mock).mockResolvedValue(undefined);
(validators.validateReadmeContent as jest.Mock).mockImplementation(() => {});
(fs.existsSync as jest.Mock).mockReturnValue(false);

await expect(handleLint(argv)).rejects.toThrow('process.exit(0)');

expect(logger.info).toHaveBeenCalledWith('No collector folder found for this package.');
});
});
});
74 changes: 74 additions & 0 deletions tools/integration/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as utils from '../utils';
import { beforeEach, describe, expect, it, jest } from '@jest/globals';

import fs from 'fs';
import logger from '../logger';
import path from 'path';

// Mock dependencies
Expand Down Expand Up @@ -573,5 +574,78 @@ describe('Utils Module', () => {
const result = utils.filterElementsBy(objects, ['unknown=value']);
expect(result).toHaveLength(0);
});

describe('generateCollectorFiles', () => {
it('should create all 4 collector files with content', () => {
const packagePath = '/test/package';
const packageName = '@instana-integration/test';
const configTypes = ['collector'];

utils.generateCollectorFiles(packagePath, packageName, configTypes);

// Verify all 4 files were created with some content
expect(mockedFs.writeFileSync).toHaveBeenCalledTimes(4);

const calls = (mockedFs.writeFileSync as jest.Mock).mock.calls;
calls.forEach((call: any) => {
expect(call[1]).toBeTruthy(); // Has content
expect(call[1].length).toBeGreaterThan(0); // Content is not empty
});
});

it('should create Dockerfile', () => {
utils.generateCollectorFiles('/test/package', '@instana-integration/test', ['collector']);

expect(mockedFs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('Dockerfile'),
expect.any(String)
);
});

it('should create Python collector file with normalized name', () => {
utils.generateCollectorFiles('/test/package', '@instana-integration/my-test', ['collector']);

expect(mockedFs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('my-test_collector.py'),
expect.any(String)
);
});

it('should create requirements.txt', () => {
utils.generateCollectorFiles('/test/package', '@instana-integration/test', ['collector']);

expect(mockedFs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('requirements.txt'),
expect.any(String)
);
});

it('should create config.json', () => {
utils.generateCollectorFiles('/test/package', '@instana-integration/test', ['collector']);

expect(mockedFs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('config.json'),
expect.any(String)
);
});

it('should normalize package name by replacing slashes with underscores', () => {
utils.generateCollectorFiles('/test/package', '@instana-integration/sub/package/name', ['collector']);

expect(mockedFs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('sub_package_name_collector.py'),
expect.any(String)
);
});

it('should handle package names without @instana-integration prefix', () => {
utils.generateCollectorFiles('/test/package', 'custom-package', ['collector']);

expect(mockedFs.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('custom-package_collector.py'),
expect.any(String)
);
});
});
});
});
Loading