|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +Describe 'apt_get_install budget timeout' |
| 4 | + apt_install_precheck() { |
| 5 | + CSE_STARTTIME_SECONDS=$(date +%s) |
| 6 | + } |
| 7 | + BeforeEach apt_install_precheck |
| 8 | + |
| 9 | + Include "./parts/linux/cloud-init/artifacts/cse_helpers.sh" |
| 10 | + Include "./parts/linux/cloud-init/artifacts/ubuntu/cse_helpers_ubuntu.sh" |
| 11 | + |
| 12 | + Describe '_apt_get_install with budget' |
| 13 | + # Mock apt-related commands and timeout to isolate budget logic |
| 14 | + wait_for_apt_locks() { :; } |
| 15 | + dpkg() { :; } |
| 16 | + apt_get_update() { :; } |
| 17 | + # Pass-through timeout mock: skip the timeout value, run the rest |
| 18 | + timeout() { shift; "$@"; } |
| 19 | + apt-get() { |
| 20 | + # Simulate install failure by default so retries are exercised |
| 21 | + if [ "$1" = "install" ]; then |
| 22 | + return 1 |
| 23 | + fi |
| 24 | + # apt-get clean, etc. |
| 25 | + return 0 |
| 26 | + } |
| 27 | + |
| 28 | + It "returns 0 when install succeeds within budget" |
| 29 | + apt-get() { |
| 30 | + if [ "$1" = "install" ]; then |
| 31 | + return 0 |
| 32 | + fi |
| 33 | + return 0 |
| 34 | + } |
| 35 | + When call _apt_get_install 3 1 "-y" 60 fake-package |
| 36 | + The status should eq 0 |
| 37 | + The stdout should include 'Executed apt-get install "fake-package"' |
| 38 | + End |
| 39 | + |
| 40 | + It "logs all package names when installing multiple packages" |
| 41 | + apt-get() { |
| 42 | + if [ "$1" = "install" ]; then |
| 43 | + return 0 |
| 44 | + fi |
| 45 | + return 0 |
| 46 | + } |
| 47 | + When call _apt_get_install 1 0 "-y" 0 pkg-one pkg-two pkg-three |
| 48 | + The status should eq 0 |
| 49 | + The stdout should include 'Executed apt-get install "pkg-one pkg-two pkg-three"' |
| 50 | + End |
| 51 | + |
| 52 | + It "returns 1 when install fails and retries exhausted (no budget)" |
| 53 | + When call _apt_get_install 2 0 "-y" 0 fake-package |
| 54 | + The status should eq 1 |
| 55 | + End |
| 56 | + |
| 57 | + It "returns 2 when per-operation budget is exceeded" |
| 58 | + # Mock timeout to sleep so elapsed time exceeds the 1s budget |
| 59 | + timeout() { |
| 60 | + sleep 2 |
| 61 | + return 1 |
| 62 | + } |
| 63 | + When call _apt_get_install 5 0 "-y" 1 fake-package |
| 64 | + The status should eq 2 |
| 65 | + The stderr should include "apt_get_install budget of 1s exceeded" |
| 66 | + End |
| 67 | + |
| 68 | + It "returns 2 when CSE timeout is already exceeded before first attempt" |
| 69 | + CSE_STARTTIME_SECONDS=$(( $(date +%s) - 800 )) |
| 70 | + When call _apt_get_install 3 1 "-y" 600 fake-package |
| 71 | + The status should eq 2 |
| 72 | + The stderr should include "CSE timeout approaching" |
| 73 | + End |
| 74 | + |
| 75 | + It "does not apply budget when CSE_STARTTIME_SECONDS is unset" |
| 76 | + unset CSE_STARTTIME_SECONDS |
| 77 | + apt-get() { |
| 78 | + if [ "$1" = "install" ]; then |
| 79 | + return 0 |
| 80 | + fi |
| 81 | + return 0 |
| 82 | + } |
| 83 | + # maxBudget=1 but since CSE_STARTTIME_SECONDS is unset, budget is ignored by apt_get_install wrapper |
| 84 | + # Here we test _apt_get_install directly with budget=0 (what the wrapper passes when unset) |
| 85 | + When call _apt_get_install 1 0 "-y" 0 fake-package |
| 86 | + The status should eq 0 |
| 87 | + The stdout should include 'Executed apt-get install "fake-package"' |
| 88 | + The stderr should include "Warning: CSE_STARTTIME_SECONDS environment variable is not set." |
| 89 | + End |
| 90 | + End |
| 91 | + |
| 92 | + Describe 'apt_get_install wrapper' |
| 93 | + wait_for_apt_locks() { :; } |
| 94 | + dpkg() { :; } |
| 95 | + apt_get_update() { :; } |
| 96 | + timeout() { shift; "$@"; } |
| 97 | + apt-get() { |
| 98 | + if [ "$1" = "install" ]; then |
| 99 | + return 0 |
| 100 | + fi |
| 101 | + return 0 |
| 102 | + } |
| 103 | + |
| 104 | + It "passes timeout as budget during CSE run" |
| 105 | + CSE_STARTTIME_SECONDS=$(date +%s) |
| 106 | + When call apt_get_install 1 0 60 fake-package |
| 107 | + The status should eq 0 |
| 108 | + The stdout should include 'Executed apt-get install "fake-package"' |
| 109 | + End |
| 110 | + |
| 111 | + It "does not apply budget during VHD build (CSE_STARTTIME_SECONDS unset)" |
| 112 | + unset CSE_STARTTIME_SECONDS |
| 113 | + # Override timeout mock to fail if called — proves budget was not applied |
| 114 | + timeout() { |
| 115 | + echo "ERROR: timeout should not be called during VHD build" >&2 |
| 116 | + return 1 |
| 117 | + } |
| 118 | + When call apt_get_install 1 0 60 fake-package |
| 119 | + The status should eq 0 |
| 120 | + The stdout should include 'Executed apt-get install "fake-package"' |
| 121 | + The stderr should include "Warning: CSE_STARTTIME_SECONDS environment variable is not set." |
| 122 | + End |
| 123 | + End |
| 124 | +End |
0 commit comments