Skip to content

Commit

Permalink
fix: ACNA-1627 - catch local save log forwarding config errors
Browse files Browse the repository at this point in the history
Co-authored-by: Shazron Abdullah <36107+shazron@users.noreply.github.com>
  • Loading branch information
purplecabbage and shazron committed Jan 12, 2024
1 parent 8133c82 commit 712e3f7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 47 deletions.
9 changes: 7 additions & 2 deletions src/commands/app/config/set/log-forwarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ governing permissions and limitations under the License.

const BaseCommand = require('../../../../BaseCommand')
const LogForwarding = require('../../../../lib/log-forwarding')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:lf:set', { provider: 'debug' })

class LogForwardingCommand extends BaseCommand {
async run () {
Expand All @@ -25,8 +26,12 @@ class LogForwardingCommand extends BaseCommand {
this.log(`Log forwarding is set to '${destination}'`)

const fullSanitizedConfig = lfConfig.getMergedConfig(lf.getConfigFromJson(res))
await lf.updateLocalConfig(fullSanitizedConfig)
this.log('Log forwarding settings are saved to the local configuration')
lf.updateLocalConfig(fullSanitizedConfig).then(() => {
this.log('Log forwarding settings are saved to the local configuration')
}).catch(e => {
this.warn('Log forwarding settings could not be saved to the local configuration.')
aioLogger.error(e.message)
})
}

async promptDestination (supportedDestinations) {
Expand Down
7 changes: 3 additions & 4 deletions src/lib/log-forwarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ class LogForwarding {
projectConfig.project.workspace.log_forwarding = {
[destination]: nonSecretSettings
}
const interactive = false
const merge = true
await writeAio(projectConfig, '', { interactive, merge })
await writeEnv({}, '', { interactive, merge }, secretSettings)
const writeOptions = { interactive: false, merge: true }
await writeAio(projectConfig, '', writeOptions)
await writeEnv({}, '', writeOptions, secretSettings)
}

isLocalConfigChanged () {
Expand Down
117 changes: 76 additions & 41 deletions test/commands/app/config/set/log-forwarding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,83 @@ beforeEach(async () => {
LogForwarding.init.mockResolvedValue(lf)
})

test('set log forwarding destination', async () => {
return new Promise(resolve => {
const destination = 'destination'
const input = {
field_one: 'val_one',
field_two: 'val_two',
secret: 'val_secret'
}
command.prompt.mockResolvedValueOnce({ type: destination })
command.prompt.mockResolvedValueOnce(input)
const serverSanitizedSettings = {
field_one: 'val_one',
field_two: 'val_two sanitized'
}
const fullSanitizedSettings = {
field_one: 'val_one',
field_two: 'val_two sanitized',
secret: 'val_secret'
}
const setCall = jest.fn().mockResolvedValue({
destination: serverSanitizedSettings
})
const localSetCall = jest.fn()
lf.updateServerConfig = setCall
lf.updateLocalConfig = localSetCall
lf.getConfigFromJson.mockReturnValue(new LogForwarding.LogForwardingConfig(destination, serverSanitizedSettings))
return command.run()
.then(() => {
expect(command.prompt).toHaveBeenNthCalledWith(1, [{
name: 'type',
message: 'select log forwarding destination',
type: 'list',
choices: [{ name: 'Destination', value: 'destination' }]
}])
expect(stdout.output).toMatch(`Log forwarding is set to '${destination}'\nLog forwarding settings are saved to the local configuration`)
expect(setCall).toHaveBeenCalledTimes(1)
expect(setCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, input))
expect(localSetCall).toHaveBeenCalledTimes(1)
expect(localSetCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, fullSanitizedSettings))
resolve()
})
test('set log forwarding destination and save local', async () => {
const destination = 'destination'
const input = {
field_one: 'val_one',
field_two: 'val_two',
secret: 'val_secret'
}
command.prompt.mockResolvedValueOnce({ type: destination })
command.prompt.mockResolvedValueOnce(input)
const serverSanitizedSettings = {
field_one: 'val_one',
field_two: 'val_two sanitized'
}
const fullSanitizedSettings = {
field_one: 'val_one',
field_two: 'val_two sanitized',
secret: 'val_secret'
}
const setCall = jest.fn().mockResolvedValue({
destination: serverSanitizedSettings
})
const localSetCall = jest.fn()
lf.updateServerConfig = setCall
lf.updateLocalConfig = localSetCall.mockResolvedValue()
lf.getConfigFromJson.mockReturnValue(new LogForwarding.LogForwardingConfig(destination, serverSanitizedSettings))

await expect(command.run()).resolves.not.toThrow()
expect(command.prompt).toHaveBeenNthCalledWith(1, [{
name: 'type',
message: 'select log forwarding destination',
type: 'list',
choices: [{ name: 'Destination', value: 'destination' }]
}])
expect(stdout.output).toMatch(`Log forwarding is set to '${destination}'\nLog forwarding settings are saved to the local configuration`)
expect(setCall).toHaveBeenCalledTimes(1)
expect(setCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, input))
expect(localSetCall).toHaveBeenCalledTimes(1)
expect(localSetCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, fullSanitizedSettings))
})

test('set log forwarding destination and fail save local', async () => {
const destination = 'destination'
const input = {
field_one: 'val_one',
field_two: 'val_two',
secret: 'val_secret'
}
command.prompt.mockResolvedValueOnce({ type: destination })
command.prompt.mockResolvedValueOnce(input)
const serverSanitizedSettings = {
field_one: 'val_one',
field_two: 'val_two sanitized'
}
const fullSanitizedSettings = {
field_one: 'val_one',
field_two: 'val_two sanitized',
secret: 'val_secret'
}
const setCall = jest.fn().mockResolvedValue({
destination: serverSanitizedSettings
})
const localSetCall = jest.fn()
lf.updateServerConfig = setCall
lf.updateLocalConfig = localSetCall.mockRejectedValue(Error('mocked error'))
lf.getConfigFromJson.mockReturnValue(new LogForwarding.LogForwardingConfig(destination, serverSanitizedSettings))
await expect(command.run()).resolves.not.toThrow('mocked error')
expect(command.prompt).toHaveBeenNthCalledWith(1, [{
name: 'type',
message: 'select log forwarding destination',
type: 'list',
choices: [{ name: 'Destination', value: 'destination' }]
}])
expect(stdout.output).toMatch(`Log forwarding is set to '${destination}'\n`)
expect(setCall).toHaveBeenCalledTimes(1)
expect(setCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, input))
expect(localSetCall).toHaveBeenCalledTimes(1)
expect(localSetCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, fullSanitizedSettings))
})

test('failed to set log forwarding settings', async () => {
Expand Down

0 comments on commit 712e3f7

Please sign in to comment.