|
| 1 | +package immediateruncommand |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Azure/run-command-handler-linux/internal/constants" |
| 10 | + "github.com/Azure/run-command-handler-linux/internal/hostgacommunicator" |
| 11 | + "github.com/Azure/run-command-handler-linux/internal/observer" |
| 12 | + "github.com/Azure/run-command-handler-linux/internal/settings" |
| 13 | + "github.com/Azure/run-command-handler-linux/internal/status" |
| 14 | + "github.com/Azure/run-command-handler-linux/internal/types" |
| 15 | + "github.com/go-kit/kit/log" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +// ---- helpers ---- |
| 20 | + |
| 21 | +func ptrString(s string) *string { return &s } |
| 22 | +func ptrInt(i int) *int { return &i } |
| 23 | +func ptrInt32(i int32) *int32 { return &i } |
| 24 | + |
| 25 | +func mkSetting(ext string, seq int, state string) settings.SettingsCommon { |
| 26 | + extCopy := ext |
| 27 | + seqCopy := seq |
| 28 | + stateCopy := state |
| 29 | + return settings.SettingsCommon{ |
| 30 | + ExtensionName: &extCopy, |
| 31 | + SeqNo: &seqCopy, |
| 32 | + ExtensionState: &stateCopy, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// ---- tests for getGoalStatesToProcess ---- |
| 37 | + |
| 38 | +func TestGetGoalStatesToProcess_ValidateSignatureError(t *testing.T) { |
| 39 | + orig := validateSignatureFn |
| 40 | + defer func() { validateSignatureFn = orig }() |
| 41 | + validateSignatureFn = func(_ hostgacommunicator.ImmediateExtensionGoalState) (bool, error) { |
| 42 | + return false, errors.New("boom") |
| 43 | + } |
| 44 | + |
| 45 | + gs := []hostgacommunicator.ImmediateExtensionGoalState{ |
| 46 | + {Settings: []settings.SettingsCommon{mkSetting("RunCommand", 1, "state")}}, |
| 47 | + } |
| 48 | + |
| 49 | + _, _, err := getGoalStatesToProcess(gs, 10) |
| 50 | + require.NotNil(t, err, "expected error, got nil") |
| 51 | +} |
| 52 | + |
| 53 | +func TestGetGoalStatesToProcess_InvalidSignatureSkipsAll(t *testing.T) { |
| 54 | + orig := validateSignatureFn |
| 55 | + defer func() { validateSignatureFn = orig }() |
| 56 | + validateSignatureFn = func(_ hostgacommunicator.ImmediateExtensionGoalState) (bool, error) { |
| 57 | + return false, nil |
| 58 | + } |
| 59 | + |
| 60 | + gs := []hostgacommunicator.ImmediateExtensionGoalState{ |
| 61 | + {Settings: []settings.SettingsCommon{ |
| 62 | + mkSetting("RunCommand", 1, "A"), |
| 63 | + mkSetting("RunCommand", 2, "B"), |
| 64 | + }}, |
| 65 | + } |
| 66 | + |
| 67 | + newOnes, skipped, err := getGoalStatesToProcess(gs, 10) |
| 68 | + require.Nil(t, err, "unexpected err: %v", err) |
| 69 | + require.True(t, len(newOnes) == 0, "expected none, got new=%d", len(newOnes)) |
| 70 | + require.True(t, len(skipped) == 0, "expected none, got skipped=%d", len(skipped)) |
| 71 | +} |
| 72 | + |
| 73 | +func TestGetGoalStatesToProcess_RespectsMaxTasksToFetch(t *testing.T) { |
| 74 | + orig := validateSignatureFn |
| 75 | + defer func() { validateSignatureFn = orig }() |
| 76 | + validateSignatureFn = func(_ hostgacommunicator.ImmediateExtensionGoalState) (bool, error) { |
| 77 | + return true, nil |
| 78 | + } |
| 79 | + |
| 80 | + gs := []hostgacommunicator.ImmediateExtensionGoalState{ |
| 81 | + {Settings: []settings.SettingsCommon{ |
| 82 | + mkSetting("RunCommand", 1, "A"), |
| 83 | + mkSetting("RunCommand", 2, "B"), |
| 84 | + mkSetting("RunCommand", 3, "C"), |
| 85 | + }}, |
| 86 | + } |
| 87 | + |
| 88 | + newOnes, skipped, err := getGoalStatesToProcess(gs, 2) |
| 89 | + require.Nil(t, err, "unexpected err: %v", err) |
| 90 | + require.Equal(t, 2, len(newOnes), "expected 2 new, got %d", len(newOnes)) |
| 91 | + require.Equal(t, 1, len(skipped), "expected 1 skipped, got %d", len(skipped)) |
| 92 | +} |
| 93 | + |
| 94 | +// ---- tests for processImmediateRunCommandGoalStates ---- |
| 95 | + |
| 96 | +func TestProcessImmediateRunCommandGoalStates_WhenAtCapacity_DoesNotFetch(t *testing.T) { |
| 97 | + // Arrange |
| 98 | + executingTasks = 0 |
| 99 | + for i := int32(0); i < maxConcurrentTasks; i++ { |
| 100 | + executingTasks.Increment() |
| 101 | + } |
| 102 | + defer func() { |
| 103 | + // reset counter |
| 104 | + for executingTasks.Get() > 0 { |
| 105 | + executingTasks.Decrement() |
| 106 | + } |
| 107 | + }() |
| 108 | + |
| 109 | + origGet := getImmediateGoalStatesFn |
| 110 | + defer func() { getImmediateGoalStatesFn = origGet }() |
| 111 | + getImmediateGoalStatesFn = func(_ *log.Context, _ hostgacommunicator.IHostGACommunicator, _ string) ([]hostgacommunicator.ImmediateExtensionGoalState, string, error) { |
| 112 | + t.Fatalf("should not be called when at capacity") |
| 113 | + return nil, "", nil |
| 114 | + } |
| 115 | + |
| 116 | + ctx := log.NewContext(log.NewSyncLogger(log.NewLogfmtLogger(os.Stdout))).With("time", log.DefaultTimestamp) |
| 117 | + var comm hostgacommunicator.HostGACommunicator // zero value ok for this test |
| 118 | + |
| 119 | + etag, err := processImmediateRunCommandGoalStates(ctx, comm, "etag-old") |
| 120 | + require.Nil(t, err, "unexpected err: %v", err) |
| 121 | + require.Equal(t, "etag-old", etag, "expected etag unchanged, got %q", etag) |
| 122 | +} |
| 123 | + |
| 124 | +func TestProcessImmediateRunCommandGoalStates_WhenEtagUnchanged_NoWork(t *testing.T) { |
| 125 | + // Arrange |
| 126 | + for executingTasks.Get() > 0 { |
| 127 | + executingTasks.Decrement() |
| 128 | + } |
| 129 | + |
| 130 | + origGet := getImmediateGoalStatesFn |
| 131 | + defer func() { getImmediateGoalStatesFn = origGet }() |
| 132 | + getImmediateGoalStatesFn = func(_ *log.Context, _ hostgacommunicator.IHostGACommunicator, last string) ([]hostgacommunicator.ImmediateExtensionGoalState, string, error) { |
| 133 | + return nil, last, nil // unchanged |
| 134 | + } |
| 135 | + |
| 136 | + ctx := log.NewContext(log.NewSyncLogger(log.NewLogfmtLogger(os.Stdout))).With("time", log.DefaultTimestamp) |
| 137 | + var comm hostgacommunicator.HostGACommunicator |
| 138 | + |
| 139 | + etag, err := processImmediateRunCommandGoalStates(ctx, comm, "same") |
| 140 | + require.Nil(t, err, "unexpected err: %v", err) |
| 141 | + require.Equal(t, "same", etag, "expected same etag, got %q", etag) |
| 142 | +} |
| 143 | + |
| 144 | +func TestProcessImmediateRunCommandGoalStates_LaunchesAndReportsSkipped(t *testing.T) { |
| 145 | + // Make deterministic: run "goroutines" inline. |
| 146 | + origSpawn := spawnFn |
| 147 | + defer func() { spawnFn = origSpawn }() |
| 148 | + spawnFn = func(f func()) { f() } |
| 149 | + |
| 150 | + // Make deterministic time. |
| 151 | + origNow := nowFn |
| 152 | + defer func() { nowFn = origNow }() |
| 153 | + fixed := time.Date(2025, 12, 23, 10, 0, 0, 0, time.UTC) |
| 154 | + nowFn = func() time.Time { return fixed } |
| 155 | + |
| 156 | + // Signature validation: true |
| 157 | + origValidate := validateSignatureFn |
| 158 | + defer func() { validateSignatureFn = origValidate }() |
| 159 | + validateSignatureFn = func(_ hostgacommunicator.ImmediateExtensionGoalState) (bool, error) { return true, nil } |
| 160 | + |
| 161 | + // Return 3 goal states; max tasks should be 5 in empty case, |
| 162 | + // but we’ll artificially fill executingTasks to force maxTasksToFetch=1. |
| 163 | + for executingTasks.Get() > 0 { |
| 164 | + executingTasks.Decrement() |
| 165 | + } |
| 166 | + // executing=4 => maxTasksToFetch=1 |
| 167 | + for i := 0; i < 4; i++ { |
| 168 | + executingTasks.Increment() |
| 169 | + } |
| 170 | + defer func() { |
| 171 | + for executingTasks.Get() > 0 { |
| 172 | + executingTasks.Decrement() |
| 173 | + } |
| 174 | + }() |
| 175 | + |
| 176 | + gs := []hostgacommunicator.ImmediateExtensionGoalState{ |
| 177 | + {Settings: []settings.SettingsCommon{ |
| 178 | + mkSetting("RunCommand", 1, "A"), |
| 179 | + mkSetting("RunCommand", 2, "B"), |
| 180 | + mkSetting("RunCommand", 3, "C"), |
| 181 | + }}, |
| 182 | + } |
| 183 | + |
| 184 | + origGet := getImmediateGoalStatesFn |
| 185 | + defer func() { getImmediateGoalStatesFn = origGet }() |
| 186 | + getImmediateGoalStatesFn = func(_ *log.Context, _ hostgacommunicator.IHostGACommunicator, _ string) ([]hostgacommunicator.ImmediateExtensionGoalState, string, error) { |
| 187 | + return gs, "etag-new", nil |
| 188 | + } |
| 189 | + |
| 190 | + // HandleImmediateGoalState called exactly once (maxTasksToFetch=1), |
| 191 | + // and we’ll return success (no final report for success). |
| 192 | + handleCalls := 0 |
| 193 | + origHandle := handleImmediateGoalStateFn |
| 194 | + defer func() { handleImmediateGoalStateFn = origHandle }() |
| 195 | + handleImmediateGoalStateFn = func(_ *log.Context, _ settings.SettingsCommon, _ *observer.Notifier) (int, error) { |
| 196 | + handleCalls++ |
| 197 | + return 0, nil |
| 198 | + } |
| 199 | + |
| 200 | + // ReportFinalStatus called for skipped items (2 of them). |
| 201 | + reportCalls := 0 |
| 202 | + origReport := reportFinalStatusFn |
| 203 | + defer func() { reportFinalStatusFn = origReport }() |
| 204 | + reportFinalStatusFn = func(_ *log.Context, _ *observer.Notifier, _ types.GoalStateKey, statusType types.StatusType, instView *types.RunCommandInstanceView) error { |
| 205 | + require.Equal(t, types.StatusSkipped, statusType, "expected StatusSkipped report, got %v", statusType) |
| 206 | + require.Equal(t, constants.ImmediateRC_CommandSkipped, instView.ExitCode, "expected skipped exit code, got %d", instView.ExitCode) |
| 207 | + reportCalls++ |
| 208 | + return nil |
| 209 | + } |
| 210 | + |
| 211 | + ctx := log.NewContext(log.NewSyncLogger(log.NewLogfmtLogger(os.Stdout))).With("time", log.DefaultTimestamp) |
| 212 | + goalStateEventObserver.Initialize(ctx) |
| 213 | + goalStateEventObserver.ReportImmediateStatusFn = func(s status.ImmediateTopLevelStatus) error { |
| 214 | + return nil |
| 215 | + } |
| 216 | + |
| 217 | + tmpDir := t.TempDir() |
| 218 | + t.Setenv(constants.ExtensionPathEnvName, tmpDir) |
| 219 | + |
| 220 | + var comm hostgacommunicator.HostGACommunicator |
| 221 | + |
| 222 | + newEtag, err := processImmediateRunCommandGoalStates(ctx, comm, "etag-old") |
| 223 | + require.Nil(t, err, "unexpected err: %v", err) |
| 224 | + require.Equal(t, "etag-new", newEtag, "expected etag-new, got %q", newEtag) |
| 225 | + require.Equal(t, 1, handleCalls, "expected 1 handled call, got %d", handleCalls) |
| 226 | + require.Equal(t, 2, reportCalls, "expected 2 skipped reports, got %d", reportCalls) |
| 227 | +} |
0 commit comments