|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {expect} from 'chai'; |
| 8 | +import {afterEach, beforeEach} from 'mocha'; |
| 9 | +import sinon from 'sinon'; |
| 10 | +import JobLog from '../../../src/commands/job/log.js'; |
| 11 | +import {createIsolatedConfigHooks, createTestCommand, runSilent} from '../../helpers/test-setup.js'; |
| 12 | + |
| 13 | +describe('job log', () => { |
| 14 | + const hooks = createIsolatedConfigHooks(); |
| 15 | + |
| 16 | + beforeEach(hooks.beforeEach); |
| 17 | + |
| 18 | + afterEach(hooks.afterEach); |
| 19 | + |
| 20 | + async function createCommand(flags: Record<string, unknown>, args: Record<string, unknown>) { |
| 21 | + return createTestCommand(JobLog, hooks.getConfig(), flags, args); |
| 22 | + } |
| 23 | + |
| 24 | + function stubCommon(command: any) { |
| 25 | + const instance = {config: {hostname: 'example.com'}}; |
| 26 | + sinon.stub(command, 'requireOAuthCredentials').returns(void 0); |
| 27 | + sinon.stub(command, 'resolvedConfig').get(() => ({values: {hostname: 'example.com'}})); |
| 28 | + sinon.stub(command, 'instance').get(() => instance); |
| 29 | + sinon.stub(command, 'log').returns(void 0); |
| 30 | + return instance; |
| 31 | + } |
| 32 | + |
| 33 | + it('fetches log for a specific execution', async () => { |
| 34 | + const command: any = await createCommand({}, {jobId: 'my-job', executionId: 'exec-1'}); |
| 35 | + const instance = stubCommon(command); |
| 36 | + sinon.stub(command, 'jsonEnabled').returns(false); |
| 37 | + |
| 38 | + const execution = {id: 'exec-1', job_id: 'my-job', is_log_file_existing: true, exit_status: {code: 'OK'}}; |
| 39 | + const getJobExecutionStub = sinon.stub().resolves(execution); |
| 40 | + const getJobLogStub = sinon.stub().resolves('log content here'); |
| 41 | + command.operations = {...command.operations, getJobExecution: getJobExecutionStub, getJobLog: getJobLogStub}; |
| 42 | + |
| 43 | + const result = await runSilent(() => command.run()); |
| 44 | + |
| 45 | + expect(getJobExecutionStub.calledOnce).to.equal(true); |
| 46 | + expect(getJobExecutionStub.getCall(0).args[0]).to.equal(instance); |
| 47 | + expect(getJobExecutionStub.getCall(0).args[1]).to.equal('my-job'); |
| 48 | + expect(getJobExecutionStub.getCall(0).args[2]).to.equal('exec-1'); |
| 49 | + expect(getJobLogStub.calledOnce).to.equal(true); |
| 50 | + expect(result.log).to.equal('log content here'); |
| 51 | + expect(result.execution).to.equal(execution); |
| 52 | + }); |
| 53 | + |
| 54 | + it('searches for most recent execution with log', async () => { |
| 55 | + const command: any = await createCommand({}, {jobId: 'my-job'}); |
| 56 | + const instance = stubCommon(command); |
| 57 | + sinon.stub(command, 'jsonEnabled').returns(false); |
| 58 | + |
| 59 | + const execWithoutLog = {id: 'exec-1', job_id: 'my-job', is_log_file_existing: false}; |
| 60 | + const execWithLog = {id: 'exec-2', job_id: 'my-job', is_log_file_existing: true, exit_status: {code: 'OK'}}; |
| 61 | + const searchStub = sinon.stub().resolves({total: 2, hits: [execWithoutLog, execWithLog]}); |
| 62 | + const getJobLogStub = sinon.stub().resolves('log from exec-2'); |
| 63 | + command.operations = {...command.operations, searchJobExecutions: searchStub, getJobLog: getJobLogStub}; |
| 64 | + |
| 65 | + const result = await runSilent(() => command.run()); |
| 66 | + |
| 67 | + expect(searchStub.calledOnce).to.equal(true); |
| 68 | + expect(searchStub.getCall(0).args[0]).to.equal(instance); |
| 69 | + expect(searchStub.getCall(0).args[1]).to.deep.include({jobId: 'my-job'}); |
| 70 | + expect(getJobLogStub.calledOnce).to.equal(true); |
| 71 | + expect(getJobLogStub.getCall(0).args[1]).to.equal(execWithLog); |
| 72 | + expect(result.log).to.equal('log from exec-2'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('searches for most recent failed execution with --failed', async () => { |
| 76 | + const command: any = await createCommand({failed: true}, {jobId: 'my-job'}); |
| 77 | + stubCommon(command); |
| 78 | + sinon.stub(command, 'jsonEnabled').returns(false); |
| 79 | + |
| 80 | + const execution = {id: 'exec-3', job_id: 'my-job', is_log_file_existing: true, exit_status: {code: 'ERROR'}}; |
| 81 | + const searchStub = sinon.stub().resolves({total: 1, hits: [execution]}); |
| 82 | + const getJobLogStub = sinon.stub().resolves('error log'); |
| 83 | + command.operations = {...command.operations, searchJobExecutions: searchStub, getJobLog: getJobLogStub}; |
| 84 | + |
| 85 | + const result = await runSilent(() => command.run()); |
| 86 | + |
| 87 | + expect(searchStub.getCall(0).args[1]).to.deep.include({status: ['ERROR']}); |
| 88 | + expect(result.log).to.equal('error log'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('errors when specific execution has no log file', async () => { |
| 92 | + const command: any = await createCommand({}, {jobId: 'my-job', executionId: 'exec-1'}); |
| 93 | + stubCommon(command); |
| 94 | + |
| 95 | + const execution = {id: 'exec-1', job_id: 'my-job', is_log_file_existing: false}; |
| 96 | + sinon.stub().resolves(execution); |
| 97 | + command.operations = {...command.operations, getJobExecution: sinon.stub().resolves(execution)}; |
| 98 | + |
| 99 | + try { |
| 100 | + await command.run(); |
| 101 | + expect.fail('should have thrown'); |
| 102 | + } catch (error: any) { |
| 103 | + expect(error.message).to.include('No log file exists'); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + it('errors when no executions with log found', async () => { |
| 108 | + const command: any = await createCommand({}, {jobId: 'my-job'}); |
| 109 | + stubCommon(command); |
| 110 | + |
| 111 | + const searchStub = sinon.stub().resolves({total: 0, hits: []}); |
| 112 | + command.operations = {...command.operations, searchJobExecutions: searchStub}; |
| 113 | + |
| 114 | + try { |
| 115 | + await command.run(); |
| 116 | + expect.fail('should have thrown'); |
| 117 | + } catch (error: any) { |
| 118 | + expect(error.message).to.include('No execution with a log file found'); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + it('returns structured result in json mode', async () => { |
| 123 | + const command: any = await createCommand({json: true}, {jobId: 'my-job', executionId: 'exec-1'}); |
| 124 | + stubCommon(command); |
| 125 | + sinon.stub(command, 'jsonEnabled').returns(true); |
| 126 | + |
| 127 | + const execution = {id: 'exec-1', job_id: 'my-job', is_log_file_existing: true, exit_status: {code: 'OK'}}; |
| 128 | + command.operations = { |
| 129 | + ...command.operations, |
| 130 | + getJobExecution: sinon.stub().resolves(execution), |
| 131 | + getJobLog: sinon.stub().resolves('json log content'), |
| 132 | + }; |
| 133 | + |
| 134 | + const result = await command.run(); |
| 135 | + |
| 136 | + expect(result).to.have.property('execution'); |
| 137 | + expect(result).to.have.property('log', 'json log content'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments