Skip to content

LW-8949 rework cli argument parsing and relative tests #1098

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

Merged
merged 1 commit into from
Feb 19, 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
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration-unit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ jobs:
run: |
yarn test:build:verify
yarn test --forceExit
yarn workspace @cardano-sdk/cardano-services test:cli
env:
NODE_OPTIONS: '--max_old_space_size=8192'
10 changes: 9 additions & 1 deletion packages/cardano-services/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
module.exports = {
const common = {
...require('../../test/jest.config'),
globalSetup: './test/jest-setup/jest-setup.ts',
globalTeardown: './test/jest-setup/jest-teardown.ts',
setupFilesAfterEnv: ['./test/jest-setup/matchers.ts']
};

module.exports = {
...common,
projects: [
{ ...common, displayName: 'cli', testMatch: ['<rootDir>/test/cli.test.ts'] },
{ ...common, displayName: 'unit', testPathIgnorePatterns: ['cli.test'] }
]
};
3 changes: 2 additions & 1 deletion packages/cardano-services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
"preview:dev": "FILES='-f ../../compose/dev.yml -f dev.yml' yarn preview:up",
"preview:up": "NETWORK=preview SUBMIT_API_ARGS='--testnet-magic 2' yarn compose:up",
"preview:down": "NETWORK=preview yarn compose:down",
"test": "jest --runInBand -c ./jest.config.js",
"test": "jest --runInBand -c ./jest.config.js --selectProjects unit",
"test:build:verify": "tsc --build ./test",
"test:cli": "jest --runInBand -c ./jest.config.js --selectProjects cli",
"test:debug": "DEBUG=true yarn test",
"test:e2e": "echo 'test:e2e' command not implemented yet",
"test:load": "jest -c ./load.jest.config.js --runInBand",
Expand Down
25 changes: 11 additions & 14 deletions packages/cardano-services/src/Program/options/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { Seconds } from '@cardano-sdk/core';
import { BuildInfo as ServiceBuildInfo } from '../../Http';
import { addOptions, newOption } from './util';
import { buildInfoValidator, cacheTtlValidator } from '../../util/validators';
import { buildInfoValidator, floatValidator, integerValidator, urlValidator } from '../../util/validators';
import { loggerMethodNames } from '@cardano-sdk/util';

export const ENABLE_METRICS_DEFAULT = false;
Expand All @@ -21,17 +21,18 @@ export const LAST_ROS_EPOCHS_DEFAULT = 10;
enum Descriptions {
ApiUrl = 'API URL',
BuildInfo = 'Service build info',
DumpOnly = 'Dumps the input arguments and exits. Used for tests',
EnableMetrics = 'Enable Prometheus Metrics',
LastRosEpochs = 'Number of epochs over which lastRos is computed',
LoggerMinSeverity = 'Log level',
HealthCheckCacheTtl = 'Health check cache TTL in seconds between 1 and 10',
EnableMetrics = 'Enable Prometheus Metrics',
ServiceDiscoveryBackoffFactor = 'Exponential backoff factor for service discovery',
ServiceDiscoveryTimeout = 'Timeout for service discovery attempts'
}

export type CommonProgramOptions = {
apiUrl: URL;
buildInfo?: ServiceBuildInfo;
dumpOnly?: boolean;
enableMetrics?: boolean;
lastRosEpochs?: number;
loggerMinSeverity?: LogLevel;
Expand All @@ -41,27 +42,23 @@ export type CommonProgramOptions = {

export const withCommonOptions = (command: Command, apiUrl: URL) => {
addOptions(command, [
newOption('--api-url <apiUrl>', Descriptions.ApiUrl, 'API_URL', (url) => new URL(url), apiUrl),
newOption('--api-url <apiUrl>', Descriptions.ApiUrl, 'API_URL', urlValidator(Descriptions.ApiUrl), apiUrl),
newOption('--build-info <buildInfo>', Descriptions.BuildInfo, 'BUILD_INFO', buildInfoValidator),
newOption('--dump-only <true/false>', Descriptions.DumpOnly, 'DUMP_ONLY', (dumpOnly) =>
stringOptionToBoolean(dumpOnly, Programs.ProviderServer, Descriptions.DumpOnly)
),
newOption(
'--enable-metrics <true/false>',
Descriptions.EnableMetrics,
'ENABLE_METRICS',
(enableMetrics) => stringOptionToBoolean(enableMetrics, Programs.ProviderServer, Descriptions.EnableMetrics),
ENABLE_METRICS_DEFAULT
),
newOption(
'--health-check-cache-ttl <healthCheckCacheTTL>',
Descriptions.HealthCheckCacheTtl,
'HEALTH_CHECK_CACHE_TTL',
(ttl: string) => cacheTtlValidator(ttl, { lowerBound: 1, upperBound: 120 }, Descriptions.HealthCheckCacheTtl),
DEFAULT_HEALTH_CHECK_CACHE_TTL
),
newOption(
'--last-ros-epochs <lastRosEpochs>',
Descriptions.LastRosEpochs,
'LAST_ROS_EPOCHS',
(lastRosEpochs) => Number.parseInt(lastRosEpochs, 10),
integerValidator(Descriptions.LastRosEpochs),
LAST_ROS_EPOCHS_DEFAULT
),
newOption(
Expand All @@ -78,14 +75,14 @@ export const withCommonOptions = (command: Command, apiUrl: URL) => {
'--service-discovery-backoff-factor <serviceDiscoveryBackoffFactor>',
Descriptions.ServiceDiscoveryBackoffFactor,
'SERVICE_DISCOVERY_BACKOFF_FACTOR',
(factor) => Number.parseFloat(factor),
floatValidator(Descriptions.ServiceDiscoveryBackoffFactor),
SERVICE_DISCOVERY_BACKOFF_FACTOR_DEFAULT
),
newOption(
'--service-discovery-timeout <serviceDiscoveryTimeout>',
Descriptions.ServiceDiscoveryTimeout,
'SERVICE_DISCOVERY_TIMEOUT',
(interval) => Number.parseInt(interval, 10),
integerValidator(Descriptions.ServiceDiscoveryTimeout),
SERVICE_DISCOVERY_TIMEOUT_DEFAULT
)
]);
Expand Down
3 changes: 2 additions & 1 deletion packages/cardano-services/src/Program/options/ogmios.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from 'commander';
import { Ogmios } from '@cardano-sdk/ogmios';
import { addOptions, newOption } from './util';
import { urlValidator } from '../../util/validators';

const OGMIOS_URL_DEFAULT = (() => {
const connection = Ogmios.createConnectionObject();
Expand Down Expand Up @@ -28,7 +29,7 @@ export const withOgmiosOptions = (command: Command) =>
'--ogmios-url <ogmiosUrl>',
OgmiosOptionDescriptions.Url,
'OGMIOS_URL',
(url) => new URL(url),
urlValidator(OgmiosOptionDescriptions.Url),
new URL(OGMIOS_URL_DEFAULT)
).conflicts('ogmiosSrvServiceName')
]);
4 changes: 2 additions & 2 deletions packages/cardano-services/src/Program/options/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Command } from 'commander';
import { addOptions, newOption } from './util';
import { existingFileValidator } from '../../util/validators';
import { existingFileValidator, integerValidator } from '../../util/validators';

export enum PostgresOptionDescriptions {
ConnectionString = 'PostgreSQL Connection string',
Expand Down Expand Up @@ -120,7 +120,7 @@ export const withPostgresOptions = (command: Command, suffixes: ConnectionNames[
`--postgres-pool-max${cliSuffix} <postgresPoolMax${suffix}>`,
PostgresOptionDescriptions.PoolMax + descSuffix,
`POSTGRES_POOL_MAX${envSuffix}`,
(max) => Number.parseInt(max, 10)
integerValidator(PostgresOptionDescriptions.PoolMax + descSuffix)
),
newOption(
`--postgres-port${cliSuffix} <postgresPort${suffix}>`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Command } from 'commander';
import { MissingProgramOption } from '../errors';
import { STAKE_POOL_METADATA_QUEUE } from '@cardano-sdk/projection-typeorm';
import { URL } from 'url';
import { addOptions, newOption } from './util';
import { urlValidator } from '../../util/validators';

export enum StakePoolMetadataOptionDescriptions {
Mode = 'This mode governs where the stake pool metadata is fetched from',
Expand Down Expand Up @@ -32,20 +32,18 @@ export const withStakePoolMetadataOptions = (command: Command) => {
(mode: string) => StakePoolMetadataFetchMode[mode.toUpperCase() as keyof typeof StakePoolMetadataFetchMode],
'direct'
).choices(['direct', 'smash']),
newOption('--smash-url <smashUrl>', StakePoolMetadataOptionDescriptions.Url, 'SMASH_URL', (url) =>
new URL(url).toString()
newOption(
'--smash-url <smashUrl>',
StakePoolMetadataOptionDescriptions.Url,
'SMASH_URL',
urlValidator(StakePoolMetadataOptionDescriptions.Url, true)
)
]);

return command;
};

export const checkProgramOptions = (metadataFetchMode: StakePoolMetadataFetchMode, smashUrl: string | undefined) => {
if (!metadataFetchMode) throw new MissingProgramOption(STAKE_POOL_METADATA_QUEUE, 'medata-fetch-mode');

if (metadataFetchMode === StakePoolMetadataFetchMode.SMASH && !smashUrl)
throw new MissingProgramOption(
STAKE_POOL_METADATA_QUEUE,
`smash-url to be set when medata-fetch-mode is smash ${smashUrl}`
);
throw new MissingProgramOption(STAKE_POOL_METADATA_QUEUE, 'smash-url to be set when metadata-fetch-mode is smash');
};
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ export const loadBlockfrostWorker = async (args: BlockfrostWorkerArgs, deps: Loa
BlockfrostWorkerOptionDescriptions.BlockfrostApiKey
]);

if (!args.network) throw new MissingProgramOption(blockfrostWorker, BlockfrostWorkerOptionDescriptions.Network);

if (!db)
throw new MissingProgramOption(blockfrostWorker, [
PostgresOptionDescriptions.ConnectionString,
Expand Down
2 changes: 2 additions & 0 deletions packages/cardano-services/src/Program/programs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ export enum ProviderServerOptionDescriptions {
DisableStakePoolMetricApy = 'Omit this metric for improved query performance',
EpochPollInterval = 'Epoch poll interval',
HandleProviderServerUrl = 'URL for the Handle provider server',
HealthCheckCacheTtl = 'Health check cache TTL in seconds between 1 and 10',
PaginationPageSizeLimit = 'Pagination page size limit shared across all providers',
SubmitApiUrl = 'cardano-submit-api URL',
TokenMetadataCacheTtl = 'Token Metadata API cache TTL in seconds',
TokenMetadataRequestTimeout = 'Token Metadata request timeout in milliseconds',
TokenMetadataServerUrl = 'Token Metadata API server URL',
UseTypeOrmStakePoolProvider = 'Enables the TypeORM Stake Pool Provider',
UseBlockfrost = 'Enables Blockfrost cached data DB',
Expand Down
Loading