Skip to content

Commit 2bc0c21

Browse files
feat(commerce): add indexer:reindex fixes #455 (#456)
1 parent 6cb278f commit 2bc0c21

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
"cloudmanager:commerce:bin-magento": {
108108
"description": "commands to work with bin-magento for commerce cli"
109109
},
110+
"cloudmanager:commerce:bin-magento:indexer": {
111+
"description": "commands to work with indexer for bin-magento"
112+
},
110113
"cloudmanager:commerce:bin-magento:maintenance": {
111114
"description": "commands to work with maintenance for bin-magento"
112115
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
17+
class IndexerReindexCommand extends BaseCommerceCliCommand {
18+
async run () {
19+
const { args, flags } = this.parse(IndexerReindexCommand)
20+
21+
const programId = getProgramId(flags)
22+
23+
const result = await this.runSync(programId, args.environmentId,
24+
{
25+
type: 'bin/magento',
26+
command: 'indexer:reindex',
27+
},
28+
1000, 'indexer:reindex')
29+
30+
return result
31+
}
32+
}
33+
34+
IndexerReindexCommand.description = 'commerce indexer reindex'
35+
36+
IndexerReindexCommand.flags = {
37+
...commonFlags.global,
38+
...commonFlags.programId,
39+
}
40+
41+
IndexerReindexCommand.args = [
42+
{ name: 'environmentId', required: true, description: 'the environment id' },
43+
]
44+
45+
IndexerReindexCommand.aliases = [
46+
'cloudmanager:commerce:indexer-reindex',
47+
]
48+
49+
module.exports = IndexerReindexCommand
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 IndexerReindexCommand = require('../../../../../src/commands/cloudmanager/commerce/bin-magento/indexer/reindex')
17+
18+
beforeEach(() => {
19+
resetCurrentOrgId()
20+
})
21+
22+
test('indexer:reindex - missing arg', async () => {
23+
expect.assertions(2)
24+
25+
const runResult = IndexerReindexCommand.run([])
26+
await expect(runResult instanceof Promise).toBeTruthy()
27+
await expect(runResult).rejects.toThrow(/^Missing 1 required arg/)
28+
})
29+
30+
test('maintenance:status - missing config', async () => {
31+
expect.assertions(2)
32+
33+
const runResult = IndexerReindexCommand.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:status', 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 status',
52+
})
53+
} else if (counter < 3) {
54+
return Promise.resolve({
55+
status: 'RUNNING',
56+
message: 'running maintenance status',
57+
})
58+
}
59+
return Promise.resolve({
60+
status: 'COMPLETE',
61+
message: 'done',
62+
})
63+
})
64+
65+
expect.assertions(11)
66+
67+
const runResult = IndexerReindexCommand.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: 'indexer:reindex',
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 indexer:reindex')
85+
await expect(cli.action.start.mock.calls[1][0]).toEqual('Starting indexer:reindex')
86+
await expect(cli.action.start.mock.calls[2][0]).toEqual('Running indexer:reindex')
87+
await expect(cli.action.stop.mock.calls[0][0]).toEqual('done')
88+
})
89+
90+
test('indexer:reindex - 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 = IndexerReindexCommand.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)