Skip to content

Commit

Permalink
tests: add tests for "onRetry" and "max-execution-time" parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lesyk-lesyk committed Apr 23, 2024
1 parent cc9bcd0 commit 9edd6b3
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 20 deletions.
145 changes: 131 additions & 14 deletions packages/cli/src/cms/commands/__tests__/push-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ describe('handlePushStatus()', () => {
organization: '',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
},
mockConfig
)
Expand All @@ -115,7 +114,6 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
},
mockConfig
);
Expand All @@ -135,7 +133,6 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
},
mockConfig
);
Expand Down Expand Up @@ -166,7 +163,6 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
},
mockConfig
)
Expand Down Expand Up @@ -207,7 +203,6 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
},
mockConfig
);
Expand Down Expand Up @@ -244,7 +239,6 @@ describe('handlePushStatus()', () => {
project: 'test-project',
pushId: 'test-push-id',
wait: true,
'max-execution-time': 1000,
},
mockConfig
);
Expand All @@ -265,7 +259,6 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
},
mockConfig
);
Expand Down Expand Up @@ -293,7 +286,6 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
},
mockConfig
);
Expand Down Expand Up @@ -358,7 +350,7 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
'retry-interval': 0.5, // 500 ms
wait: true,
},
mockConfig
Expand All @@ -375,7 +367,7 @@ describe('handlePushStatus()', () => {
production: null,
commit: commitStub,
});
}, 15000);
});

it('should wait for production "success" status after preview "success" status', async () => {
process.env.REDOCLY_AUTHORIZATION = 'test-api-key';
Expand Down Expand Up @@ -431,7 +423,7 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
'retry-interval': 0.5, // 500 ms
wait: true,
},
mockConfig
Expand All @@ -448,7 +440,7 @@ describe('handlePushStatus()', () => {
},
commit: commitStub,
});
}, 30000);
});
});

describe('"continue-on-deployment-failures" option', () => {
Expand All @@ -472,7 +464,6 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
'continue-on-deployment-failures': false,
},
mockConfig
Expand Down Expand Up @@ -503,7 +494,6 @@ describe('handlePushStatus()', () => {
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'max-execution-time': 1000,
'continue-on-deployment-failures': true,
},
mockConfig
Expand All @@ -518,4 +508,131 @@ describe('handlePushStatus()', () => {
});
});
});

describe('"onRetry" callback', () => {
it('should be called when command retries request to API in wait mode for preview deploy', async () => {
process.env.REDOCLY_AUTHORIZATION = 'test-api-key';

remotes.getPush.mockResolvedValueOnce({
...pushResponseStub,
status: {
preview: {
deploy: { status: 'pending', url: 'https://preview-test-url' },
scorecard: [],
},
},
});

remotes.getPush.mockResolvedValueOnce({
...pushResponseStub,
status: {
preview: {
deploy: { status: 'running', url: 'https://preview-test-url' },
scorecard: [],
},
},
});

remotes.getPush.mockResolvedValueOnce({
...pushResponseStub,
status: {
preview: {
deploy: { status: 'success', url: 'https://preview-test-url' },
scorecard: [],
},
},
});

const onRetrySpy = jest.fn();

const result = await handlePushStatus(
{
domain: 'test-domain',
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
wait: true,
'retry-interval': 0.5, // 500 ms
onRetry: onRetrySpy,
},
mockConfig
);

expect(onRetrySpy).toBeCalledTimes(2);

// first retry
expect(onRetrySpy).toHaveBeenNthCalledWith(1, {
preview: {
deploy: {
status: 'pending',
url: 'https://preview-test-url',
},
scorecard: [],
},
production: null,
commit: commitStub,
});

// second retry
expect(onRetrySpy).toHaveBeenNthCalledWith(2, {
preview: {
deploy: {
status: 'running',
url: 'https://preview-test-url',
},
scorecard: [],
},
production: null,
commit: commitStub,
});

// final result
expect(result).toEqual({
preview: {
deploy: {
status: 'success',
url: 'https://preview-test-url',
},
scorecard: [],
},
production: null,
commit: commitStub,
});
});
});

describe('"max-execution-time" option', () => {
it('should throw error in case "max-execution-time" was exceeded', async () => {
process.env.REDOCLY_AUTHORIZATION = 'test-api-key';

// Stuck deployment simulation
remotes.getPush.mockResolvedValue({
...pushResponseStub,
status: {
preview: {
deploy: { status: 'pending', url: 'https://preview-test-url' },
scorecard: [],
},
},
});

await expect(
handlePushStatus(
{
domain: 'test-domain',
organization: 'test-org',
project: 'test-project',
pushId: 'test-push-id',
'retry-interval': 2, // seconds
'max-execution-time': 1, // seconds
wait: true,
},
mockConfig
)
).rejects.toThrowErrorMatchingInlineSnapshot(`
"✗ Failed to get push status. Reason: Timeout exceeded
"
`);
});
});
});
35 changes: 29 additions & 6 deletions packages/cli/src/cms/commands/push-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export type PushStatusOptions = {
config?: string;
format?: Extract<OutputFormat, 'stylish'>;
wait?: boolean;
'max-execution-time'?: number;
'start-time'?: number;
'max-execution-time'?: number; // in seconds
'retry-interval'?: number; // in seconds
'start-time'?: number; // in milliseconds
'continue-on-deployment-failures'?: boolean;
onRetry?: (lasSummary: PushStatusSummary) => void;
};

export interface PushStatusSummary {
Expand Down Expand Up @@ -55,6 +57,9 @@ export async function handlePushStatus(

const domain = argv.domain || getDomain();
const maxExecutionTime = argv['max-execution-time'] || 600;
const retryIntervalMs = argv['retry-interval']
? argv['retry-interval'] * 1000
: RETRY_INTERVAL_MS;
const startTime = argv['start-time'] || Date.now();
const retryTimeoutMs = maxExecutionTime * 1000;
const continueOnDeploymentFailures = argv['continue-on-deployment-failures'] || false;
Expand Down Expand Up @@ -86,9 +91,18 @@ export async function handlePushStatus(
wait,
});
},
onRetry: (lastResult) => {
if (argv.onRetry) {
argv.onRetry({
preview: lastResult.status.preview,
production: lastResult.isMainBranch ? lastResult.status.production : null,
commit: lastResult.commit,
});
}
},
startTime,
retryTimeoutMs: retryTimeoutMs,
retryIntervalMs: RETRY_INTERVAL_MS,
retryTimeoutMs,
retryIntervalMs,
});

printPushStatus({
Expand Down Expand Up @@ -126,9 +140,18 @@ export async function handlePushStatus(
wait,
});
},
onRetry: (lastResult) => {
if (argv.onRetry) {
argv.onRetry({
preview: lastResult.status.preview,
production: lastResult.isMainBranch ? lastResult.status.production : null,
commit: lastResult.commit,
});
}
},
startTime,
retryTimeoutMs: retryTimeoutMs,
retryIntervalMs: RETRY_INTERVAL_MS,
retryTimeoutMs,
retryIntervalMs,
});
}

Expand Down

0 comments on commit 9edd6b3

Please sign in to comment.