Skip to content

Commit daf0970

Browse files
feat(commerce): add maintenance:disable fixes #466 (#467)
1 parent 2534463 commit daf0970

File tree

2 files changed

+149
-0
lines changed
  • src/commands/cloudmanager/commerce/bin-magento/maintenance
  • test/commands/commerce/bin-magento/maintenance

2 files changed

+149
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 BaseCommerceCliCommand = require('../../../../../base-commerce-cli-command')
14+
const { getProgramId } = require('../../../../../cloudmanager-helpers')
15+
const commonFlags = require('../../../../../common-flags')
16+
const commonArgs = require('../../../../../common-args')
17+
18+
class MaintenanceDisableCommand extends BaseCommerceCliCommand {
19+
async run () {
20+
const { args, flags } = this.parse(MaintenanceDisableCommand)
21+
22+
const programId = getProgramId(flags)
23+
24+
const result = await this.runSync(programId, args.environmentId,
25+
{
26+
type: 'bin/magento',
27+
command: 'maintenance:disable',
28+
},
29+
1000, 'maintenance:disable')
30+
31+
return result
32+
}
33+
}
34+
35+
MaintenanceDisableCommand.description = 'commerce maintenance disable'
36+
37+
MaintenanceDisableCommand.flags = {
38+
...commonFlags.global,
39+
...commonFlags.programId,
40+
}
41+
42+
MaintenanceDisableCommand.args = [
43+
commonArgs.environmentId,
44+
]
45+
46+
MaintenanceDisableCommand.aliases = [
47+
'cloudmanager:commerce:maintenance-disable',
48+
]
49+
50+
module.exports = MaintenanceDisableCommand
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 MaintenanceDisableCommand = require('../../../../../src/commands/cloudmanager/commerce/bin-magento/maintenance/disable')
17+
18+
beforeEach(() => {
19+
resetCurrentOrgId()
20+
})
21+
22+
test('maintenance:disable - missing arg', async () => {
23+
expect.assertions(2)
24+
25+
const runResult = MaintenanceDisableCommand.run([])
26+
await expect(runResult instanceof Promise).toBeTruthy()
27+
await expect(runResult).rejects.toThrow(/^Missing 1 required arg/)
28+
})
29+
30+
test('maintenance:disable - missing config', async () => {
31+
expect.assertions(2)
32+
33+
const runResult = MaintenanceDisableCommand.run(['--programId', '5', '10'])
34+
await expect(runResult instanceof Promise).toBeTruthy()
35+
await expect(runResult).rejects.toThrow('[CloudManagerCLI:NO_IMS_CONTEXT] Unable to find IMS context aio-cli-plugin-cloudmanager.')
36+
})
37+
38+
test('maintenance:disable', async () => {
39+
let counter = 0
40+
setCurrentOrgId('good')
41+
mockSdk.postCommerceCommandExecution = jest.fn(() =>
42+
Promise.resolve({
43+
id: '5000',
44+
}),
45+
)
46+
mockSdk.getCommerceCommandExecution = jest.fn(() => {
47+
counter++
48+
if (counter === 1) {
49+
return Promise.resolve({
50+
status: 'PENDING',
51+
message: 'running maintenance disable',
52+
})
53+
} else if (counter < 3) {
54+
return Promise.resolve({
55+
status: 'RUNNING',
56+
message: 'running maintenance disable',
57+
})
58+
}
59+
return Promise.resolve({
60+
status: 'COMPLETE',
61+
message: 'maintenance disabled',
62+
})
63+
})
64+
65+
expect.assertions(11)
66+
67+
const runResult = MaintenanceDisableCommand.run(['--programId', '5', '10'])
68+
await expect(runResult instanceof Promise).toBeTruthy()
69+
await runResult
70+
await expect(init.mock.calls.length).toEqual(1)
71+
await expect(init).toHaveBeenCalledWith(
72+
'good',
73+
'test-client-id',
74+
'fake-token',
75+
'https://cloudmanager.adobe.io',
76+
)
77+
await expect(mockSdk.postCommerceCommandExecution.mock.calls.length).toEqual(1)
78+
await expect(mockSdk.postCommerceCommandExecution).toHaveBeenCalledWith('5', '10', {
79+
type: 'bin/magento',
80+
command: 'maintenance:disable',
81+
})
82+
await expect(mockSdk.getCommerceCommandExecution).toHaveBeenCalledWith('5', '10', '5000')
83+
await expect(mockSdk.getCommerceCommandExecution).toHaveBeenCalledTimes(3)
84+
await expect(cli.action.start.mock.calls[0][0]).toEqual('Starting maintenance:disable')
85+
await expect(cli.action.start.mock.calls[1][0]).toEqual('Starting maintenance:disable')
86+
await expect(cli.action.start.mock.calls[2][0]).toEqual('Running maintenance:disable')
87+
await expect(cli.action.stop.mock.calls[0][0]).toEqual('maintenance disabled')
88+
})
89+
90+
test('maintenance:disable - api error', async () => {
91+
setCurrentOrgId('good')
92+
mockSdk.postCommerceCommandExecution = jest.fn(() =>
93+
Promise.reject(new Error('Command failed.')),
94+
)
95+
mockSdk.getCommerceCommandExecution = jest.fn()
96+
const runResult = MaintenanceDisableCommand.run(['--programId', '5', '10'])
97+
await expect(runResult instanceof Promise).toBeTruthy()
98+
await expect(runResult).rejects.toEqual(new Error('Command failed.'))
99+
})

0 commit comments

Comments
 (0)