Skip to content

Commit 8b77e11

Browse files
feat(commerce): add get-command fixes #437 (#438)
1 parent 6baa32c commit 8b77e11

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2021 Adobe. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
const BaseCommand = require('../../../base-command')
14+
const { initSdk, formatTime, getProgramId } = require('../../../cloudmanager-helpers')
15+
const { cli } = require('cli-ux')
16+
const commonFlags = require('../../../common-flags')
17+
18+
class GetCommandExecutionCommand extends BaseCommand {
19+
async run () {
20+
const { args, flags } = this.parse(GetCommandExecutionCommand)
21+
22+
const programId = getProgramId(flags)
23+
24+
const commandRetrievalMessage = `Getting Status for Command ID# ${args.commandExecutionId}`
25+
const commandRetrievedMessage = `Status for Command ID# ${args.commandExecutionId}`
26+
27+
cli.action.start(commandRetrievalMessage)
28+
29+
const result = await this.getCommand(
30+
programId,
31+
args.environmentId,
32+
args.commandExecutionId,
33+
)
34+
35+
cli.action.start(commandRetrievedMessage)
36+
cli.action.stop('\n')
37+
const { id, type, command, startedBy, startedAt, completedAt, status } = result
38+
cli.table([{ id, type, command, startedBy, startedAt, completedAt, status }], {
39+
id: {},
40+
type: {},
41+
command: {},
42+
startedBy: {
43+
header: 'Started By',
44+
},
45+
startedAt: {
46+
header: 'Started At',
47+
get: formatTime(startedAt),
48+
},
49+
completedAt: {
50+
header: 'Completed At',
51+
get: formatTime(completedAt),
52+
},
53+
status: {},
54+
})
55+
console.log('\n')
56+
57+
return result
58+
}
59+
60+
async getCommand (
61+
programId,
62+
environmentId,
63+
commandExecutionId,
64+
imsContextName = null,
65+
) {
66+
const sdk = await initSdk(imsContextName)
67+
return sdk.getCommerceCommandExecution(programId, environmentId, commandExecutionId)
68+
}
69+
}
70+
71+
GetCommandExecutionCommand.description = 'get status of a single commerce cli command'
72+
73+
GetCommandExecutionCommand.flags = {
74+
...commonFlags.global,
75+
...commonFlags.programId,
76+
}
77+
78+
GetCommandExecutionCommand.args = [
79+
{ name: 'environmentId', required: true, description: 'the environment id' },
80+
{ name: 'commandExecutionId', required: true, description: 'the command execution id' },
81+
]
82+
83+
module.exports = GetCommandExecutionCommand
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright 2021 Adobe. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
const { cli } = require('cli-ux')
14+
const { init, mockSdk } = require('@adobe/aio-lib-cloudmanager')
15+
const { resetCurrentOrgId, setCurrentOrgId } = require('@adobe/aio-lib-ims')
16+
const GetCommandExecutionCommand = require('../../../src/commands/cloudmanager/commerce/get-command-execution')
17+
18+
beforeEach(() => {
19+
resetCurrentOrgId()
20+
})
21+
22+
test('get-command (commerce) - missing arg', async () => {
23+
expect.assertions(4)
24+
25+
const runResultOne = GetCommandExecutionCommand.run([])
26+
await expect(runResultOne instanceof Promise).toBeTruthy()
27+
await expect(runResultOne).rejects.toSatisfy(
28+
(err) => err.message.indexOf('Missing 2 required args') === 0,
29+
)
30+
const runResultTwo = GetCommandExecutionCommand.run(['12345'])
31+
await expect(runResultTwo instanceof Promise).toBeTruthy()
32+
await expect(runResultTwo).rejects.toSatisfy(
33+
(err) => err.message.indexOf('Missing 1 required arg') === 0,
34+
)
35+
})
36+
37+
test('get-command (commerce) - missing config', async () => {
38+
expect.assertions(2)
39+
40+
const runResult = GetCommandExecutionCommand.run(['--programId', '5', '10', '20'])
41+
await expect(runResult instanceof Promise).toBeTruthy()
42+
await expect(runResult).rejects.toSatisfy(err => err.message === '[CloudManagerCLI:NO_IMS_CONTEXT] Unable to find IMS context aio-cli-plugin-cloudmanager.')
43+
})
44+
45+
test('get-command (commerce)', async () => {
46+
setCurrentOrgId('good')
47+
mockSdk.getCommerceCommandExecution = jest.fn(() => {
48+
return Promise.resolve({
49+
id: 100,
50+
type: 'bin/magento',
51+
command: 'maintenance:status',
52+
options: [],
53+
startedBy: '[email protected]',
54+
startedAt: '2021-08-17T17:03:18.858+0000',
55+
completedAt: '2021-08-17T17:03:43.000+0000',
56+
name: 'magento-cli-952',
57+
status: 'COMPLETED',
58+
environmentId: 177253,
59+
})
60+
})
61+
62+
// expect.assertions(9)
63+
64+
const runResult = GetCommandExecutionCommand.run(['--programId', '5', '10', '100'])
65+
await expect(runResult instanceof Promise).toBeTruthy()
66+
await runResult
67+
await expect(init.mock.calls.length).toEqual(1)
68+
await expect(init).toHaveBeenCalledWith(
69+
'good',
70+
'test-client-id',
71+
'fake-token',
72+
'https://cloudmanager.adobe.io',
73+
)
74+
await expect(mockSdk.getCommerceCommandExecution.mock.calls.length).toEqual(1)
75+
await expect(mockSdk.getCommerceCommandExecution).toHaveBeenCalledWith('5', '10', '100')
76+
await expect(cli.action.start.mock.calls[0][0]).toEqual('Getting Status for Command ID# 100')
77+
await expect(cli.action.start.mock.calls[1][0]).toEqual('Status for Command ID# 100')
78+
await expect(cli.table.mock.calls[0][0]).toEqual([{ command: 'maintenance:status', completedAt: '2021-08-17T17:03:43.000+0000', id: 100, startedAt: '2021-08-17T17:03:18.858+0000', startedBy: '[email protected]', status: 'COMPLETED', type: 'bin/magento' }])
79+
await expect(cli.action.stop).toHaveBeenCalled()
80+
})
81+
82+
test('get-command (commerce) - api error', async () => {
83+
setCurrentOrgId('good')
84+
mockSdk.getCommerceCommandExecution = jest.fn(() =>
85+
Promise.reject(new Error('Command failed.')),
86+
)
87+
const runResult = GetCommandExecutionCommand.run(['--programId', '5', '10', '20'])
88+
await expect(runResult instanceof Promise).toBeTruthy()
89+
await expect(runResult).rejects.toEqual(new Error('Command failed.'))
90+
})

0 commit comments

Comments
 (0)