|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright:: 2025 Amazon.com, Inc. and its affiliates. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the |
| 6 | +# License. A copy of the License is located at |
| 7 | +# |
| 8 | +# http://aws.amazon.com/apache2.0/ |
| 9 | +# |
| 10 | +# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 11 | +# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +require_relative '../../spec_helper' |
| 15 | +require_relative '../../../libraries/command_runner' |
| 16 | + |
| 17 | +describe ErrorHandlers::CommandRunner do |
| 18 | + let(:log_prefix) { 'TestPrefix:' } |
| 19 | + let(:runner) { described_class.new(log_prefix: log_prefix) } |
| 20 | + let(:command) { 'test command' } |
| 21 | + let(:description) { 'test operation' } |
| 22 | + let(:shell_out_result) { double('shell_out_result', exitstatus: 0, stdout: 'success', stderr: '') } |
| 23 | + |
| 24 | + before do |
| 25 | + allow(runner).to receive(:shell_out).and_return(shell_out_result) |
| 26 | + allow(runner).to receive(:sleep) |
| 27 | + end |
| 28 | + |
| 29 | + describe '#run_with_retries' do |
| 30 | + context 'when command succeeds on first attempt' do |
| 31 | + it 'returns true and does not retry' do |
| 32 | + expect(runner).to receive(:shell_out).once.and_return(shell_out_result) |
| 33 | + expect(runner).not_to receive(:sleep) |
| 34 | + expect(runner.run_with_retries(command, description: description)).to be true |
| 35 | + end |
| 36 | + |
| 37 | + it 'logs stdout and stderr' do |
| 38 | + allow(Chef::Log).to receive(:info) |
| 39 | + expect(Chef::Log).to receive(:info).with(/Command stdout: success/) |
| 40 | + expect(Chef::Log).to receive(:info).with(/Command stderr:/) |
| 41 | + runner.run_with_retries(command, description: description) |
| 42 | + end |
| 43 | + |
| 44 | + it 'logs success message' do |
| 45 | + allow(Chef::Log).to receive(:info) |
| 46 | + expect(Chef::Log).to receive(:info).with(/Successfully executed: test operation/) |
| 47 | + runner.run_with_retries(command, description: description) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context 'when command fails then succeeds' do |
| 52 | + let(:failed_result) { double('failed_result', exitstatus: 1, stdout: '', stderr: 'error') } |
| 53 | + |
| 54 | + it 'retries and returns true on success' do |
| 55 | + expect(runner).to receive(:shell_out).and_return(failed_result, shell_out_result) |
| 56 | + expect(runner).to receive(:sleep).with(90).once |
| 57 | + expect(runner.run_with_retries(command, description: description, retries: 1)).to be true |
| 58 | + end |
| 59 | + |
| 60 | + it 'logs retry message' do |
| 61 | + allow(runner).to receive(:shell_out).and_return(failed_result, shell_out_result) |
| 62 | + allow(Chef::Log).to receive(:info) |
| 63 | + allow(Chef::Log).to receive(:warn) |
| 64 | + expect(Chef::Log).to receive(:info).with(/Retrying in 90 seconds/) |
| 65 | + runner.run_with_retries(command, description: description, retries: 1) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context 'when command fails all attempts' do |
| 70 | + let(:failed_result) { double('failed_result', exitstatus: 1, stdout: '', stderr: 'error') } |
| 71 | + |
| 72 | + it 'returns false after exhausting retries' do |
| 73 | + allow(runner).to receive(:shell_out).and_return(failed_result) |
| 74 | + expect(runner.run_with_retries(command, description: description, retries: 1, retry_delay: 0)).to be false |
| 75 | + end |
| 76 | + |
| 77 | + it 'logs error after all attempts fail' do |
| 78 | + allow(runner).to receive(:shell_out).and_return(failed_result) |
| 79 | + expect(Chef::Log).to receive(:error).with(/Failed to test operation after 2 attempts/) |
| 80 | + runner.run_with_retries(command, description: description, retries: 1, retry_delay: 0) |
| 81 | + end |
| 82 | + |
| 83 | + it 'logs warning for each failed attempt' do |
| 84 | + allow(runner).to receive(:shell_out).and_return(failed_result) |
| 85 | + allow(Chef::Log).to receive(:info) |
| 86 | + allow(Chef::Log).to receive(:error) |
| 87 | + expect(Chef::Log).to receive(:warn).with(%r{Failed to test operation \(attempt 1/2\)}) |
| 88 | + expect(Chef::Log).to receive(:warn).with(%r{Failed to test operation \(attempt 2/2\)}) |
| 89 | + runner.run_with_retries(command, description: description, retries: 1, retry_delay: 0) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context 'with custom retry parameters' do |
| 94 | + it 'respects custom retries count' do |
| 95 | + failed_result = double('failed_result', exitstatus: 1, stdout: '', stderr: 'error') |
| 96 | + allow(runner).to receive(:shell_out).and_return(failed_result) |
| 97 | + expect(runner).to receive(:shell_out).exactly(3).times |
| 98 | + runner.run_with_retries(command, description: description, retries: 2, retry_delay: 0) |
| 99 | + end |
| 100 | + |
| 101 | + it 'respects custom retry delay' do |
| 102 | + failed_result = double('failed_result', exitstatus: 1, stdout: '', stderr: 'error') |
| 103 | + allow(runner).to receive(:shell_out).and_return(failed_result, shell_out_result) |
| 104 | + expect(runner).to receive(:sleep).with(30).once |
| 105 | + runner.run_with_retries(command, description: description, retries: 1, retry_delay: 30) |
| 106 | + end |
| 107 | + |
| 108 | + it 'respects custom timeout' do |
| 109 | + expect(runner).to receive(:shell_out).with(command, timeout: 60).and_return(shell_out_result) |
| 110 | + runner.run_with_retries(command, description: description, timeout: 60) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + context 'with default parameters' do |
| 115 | + it 'uses DEFAULT_RETRIES' do |
| 116 | + failed_result = double('failed_result', exitstatus: 1, stdout: '', stderr: 'error') |
| 117 | + allow(runner).to receive(:shell_out).and_return(failed_result) |
| 118 | + expect(runner).to receive(:shell_out).exactly(11).times # 10 retries + 1 initial = 11 attempts |
| 119 | + runner.run_with_retries(command, description: description, retry_delay: 0) |
| 120 | + end |
| 121 | + |
| 122 | + it 'uses DEFAULT_TIMEOUT' do |
| 123 | + expect(runner).to receive(:shell_out).with(command, timeout: 30).and_return(shell_out_result) |
| 124 | + runner.run_with_retries(command, description: description) |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments