-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathsupervisor_test.go
More file actions
262 lines (224 loc) · 8.94 KB
/
Copy pathsupervisor_test.go
File metadata and controls
262 lines (224 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package commands
import (
"context"
"errors"
"os"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
wfv1 "github.com/argoproj/argo-workflows/v4/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v4/util/logging"
"github.com/argoproj/argo-workflows/v4/workflow/common"
)
func TestInputArtifactPluginNames_Empty(t *testing.T) {
t.Setenv(common.EnvVarInputArtifactPluginNames, "")
assert.Empty(t, inputArtifactPluginNames())
}
func TestInputArtifactPluginNames_Single(t *testing.T) {
t.Setenv(common.EnvVarInputArtifactPluginNames, "s3-driver")
names := inputArtifactPluginNames()
require.Len(t, names, 1)
assert.Equal(t, "s3-driver", string(names[0]))
}
func TestInputArtifactPluginNames_Multi(t *testing.T) {
t.Setenv(common.EnvVarInputArtifactPluginNames, "s3-driver, gcs-driver , oci ")
names := inputArtifactPluginNames()
require.Len(t, names, 3)
assert.Equal(t, "s3-driver", string(names[0]))
assert.Equal(t, "gcs-driver", string(names[1]))
assert.Equal(t, "oci", string(names[2]))
}
func TestInputArtifactPluginNames_SkipsBlankEntries(t *testing.T) {
t.Setenv(common.EnvVarInputArtifactPluginNames, "s3,,gcs, ,oci")
names := inputArtifactPluginNames()
require.Len(t, names, 3)
}
func TestInputArtifactPluginNames_TrailingComma(t *testing.T) {
t.Setenv(common.EnvVarInputArtifactPluginNames, "s3-driver,")
names := inputArtifactPluginNames()
require.Len(t, names, 1)
assert.Equal(t, "s3-driver", string(names[0]))
}
func TestStatusMarker_RunningHeartbeat(t *testing.T) {
dir := t.TempDir()
statusPath := filepath.Join(dir, "status")
require.NoError(t, writeRunningStatusAt(statusPath))
body, err := os.ReadFile(statusPath)
require.NoError(t, err)
token, _ := parseSupervisorStatus(body)
assert.Equal(t, statusRunning, token)
assert.NoFileExists(t, statusPath+".tmp", "tmp file should be renamed away")
}
func TestStatusMarker_Success(t *testing.T) {
dir := t.TempDir()
statusPath := filepath.Join(dir, "status")
require.NoError(t, writeSuccessStatusAt(statusPath))
body, err := os.ReadFile(statusPath)
require.NoError(t, err)
token, _ := parseSupervisorStatus(body)
assert.Equal(t, statusReady, token)
assert.NoFileExists(t, statusPath+".tmp", "tmp file should be renamed away")
}
func TestStatusMarker_FailureCapturesCause(t *testing.T) {
ctx := logging.WithLogger(context.Background(), logging.NewTestLogger(logging.Info, logging.Text))
dir := t.TempDir()
statusPath := filepath.Join(dir, "status")
writeFailureStatusAt(ctx, statusPath, errors.New("boom"))
body, err := os.ReadFile(statusPath)
require.NoError(t, err)
token, message := parseSupervisorStatus(body)
assert.Equal(t, statusFailed, token)
assert.Equal(t, "boom", message)
assert.NoFileExists(t, statusPath+".tmp", "tmp file should be renamed away")
}
func TestStatusMarker_FailureWithEmptyCauseStillSignalsFailure(t *testing.T) {
ctx := logging.WithLogger(context.Background(), logging.NewTestLogger(logging.Info, logging.Text))
dir := t.TempDir()
statusPath := filepath.Join(dir, "status")
// A cause that stringifies to "" must still produce the FAILED token, so
// the emissary can't misread the failure as success.
writeFailureStatusAt(ctx, statusPath, errors.New(""))
body, err := os.ReadFile(statusPath)
require.NoError(t, err)
token, _ := parseSupervisorStatus(body)
assert.Equal(t, statusFailed, token)
}
func TestStatusMarker_BestEffortOnUnwritable(t *testing.T) {
ctx := logging.WithLogger(context.Background(), logging.NewTestLogger(logging.Info, logging.Text))
// Path under a directory that doesn't exist — write fails, but the
// helper must not panic; supervisor's pre-main error still propagates
// via PostMain even if the marker write itself fails.
writeFailureStatusAt(ctx, "/does/not/exist/status", errors.New("boom"))
}
// fakeStages is a controllable preMainStages implementation. Each
// function field, if non-nil, is invoked; if nil, the method returns nil.
// Counters and the loadStarted/loadCancelled channels let tests assert
// orchestration order and errgroup cancellation behavior.
type fakeStages struct {
writeTemplate func() error
stageFiles func(ctx context.Context) error
loadWithoutPlugins func(ctx context.Context) error
loadFromPlugin func(ctx context.Context, name wfv1.ArtifactPluginName) error
writeTemplateCalls atomic.Int32
stageFilesCalls atomic.Int32
loadWithoutPluginsCalls atomic.Int32
loadFromPluginCalls atomic.Int32
loadFromPluginNamesMu sync.Mutex
loadFromPluginNames []wfv1.ArtifactPluginName
loadFromPluginCancelledMu sync.Mutex
loadFromPluginCancelled []wfv1.ArtifactPluginName
}
func (f *fakeStages) WriteTemplate() error {
f.writeTemplateCalls.Add(1)
if f.writeTemplate != nil {
return f.writeTemplate()
}
return nil
}
func (f *fakeStages) StageFiles(ctx context.Context) error {
f.stageFilesCalls.Add(1)
if f.stageFiles != nil {
return f.stageFiles(ctx)
}
return nil
}
func (f *fakeStages) LoadArtifactsWithoutPlugins(ctx context.Context) error {
f.loadWithoutPluginsCalls.Add(1)
if f.loadWithoutPlugins != nil {
return f.loadWithoutPlugins(ctx)
}
return nil
}
func (f *fakeStages) LoadArtifactsFromPlugin(ctx context.Context, name wfv1.ArtifactPluginName) error {
f.loadFromPluginCalls.Add(1)
f.loadFromPluginNamesMu.Lock()
f.loadFromPluginNames = append(f.loadFromPluginNames, name)
f.loadFromPluginNamesMu.Unlock()
if f.loadFromPlugin != nil {
err := f.loadFromPlugin(ctx, name)
if errors.Is(ctx.Err(), context.Canceled) {
f.loadFromPluginCancelledMu.Lock()
f.loadFromPluginCancelled = append(f.loadFromPluginCancelled, name)
f.loadFromPluginCancelledMu.Unlock()
}
return err
}
return nil
}
func TestRunSupervisorPreMain_HappyPath(t *testing.T) {
ctx := logging.TestContext(t.Context())
stages := &fakeStages{}
err := runSupervisorPreMain(ctx, stages, []wfv1.ArtifactPluginName{"s3", "gcs"})
require.NoError(t, err)
assert.EqualValues(t, 1, stages.writeTemplateCalls.Load())
assert.EqualValues(t, 1, stages.stageFilesCalls.Load())
assert.EqualValues(t, 1, stages.loadWithoutPluginsCalls.Load())
assert.EqualValues(t, 2, stages.loadFromPluginCalls.Load())
}
func TestRunSupervisorPreMain_NoPlugins(t *testing.T) {
ctx := logging.TestContext(t.Context())
stages := &fakeStages{}
err := runSupervisorPreMain(ctx, stages, nil)
require.NoError(t, err)
assert.EqualValues(t, 1, stages.loadWithoutPluginsCalls.Load())
assert.EqualValues(t, 0, stages.loadFromPluginCalls.Load())
}
func TestRunSupervisorPreMain_WriteTemplateFailsShortCircuits(t *testing.T) {
ctx := logging.TestContext(t.Context())
stages := &fakeStages{writeTemplate: func() error { return errors.New("write fail") }}
err := runSupervisorPreMain(ctx, stages, []wfv1.ArtifactPluginName{"s3"})
require.ErrorContains(t, err, "failed to write template")
// Subsequent stages must not run.
assert.EqualValues(t, 0, stages.stageFilesCalls.Load())
assert.EqualValues(t, 0, stages.loadWithoutPluginsCalls.Load())
assert.EqualValues(t, 0, stages.loadFromPluginCalls.Load())
}
func TestRunSupervisorPreMain_StageFilesFailsShortCircuits(t *testing.T) {
ctx := logging.TestContext(t.Context())
stages := &fakeStages{stageFiles: func(ctx context.Context) error { return errors.New("stage fail") }}
err := runSupervisorPreMain(ctx, stages, []wfv1.ArtifactPluginName{"s3"})
require.ErrorContains(t, err, "failed to stage files")
assert.EqualValues(t, 1, stages.writeTemplateCalls.Load())
assert.EqualValues(t, 0, stages.loadWithoutPluginsCalls.Load())
}
func TestRunSupervisorPreMain_OnePluginFailureCancelsSiblings(t *testing.T) {
ctx := logging.TestContext(t.Context())
// "fail" plugin returns an error immediately. Other goroutines block
// on ctx.Done() — they must observe cancellation and return.
stages := &fakeStages{
loadWithoutPlugins: func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
},
loadFromPlugin: func(ctx context.Context, name wfv1.ArtifactPluginName) error {
if name == "fail" {
return errors.New("plugin boom")
}
<-ctx.Done()
return ctx.Err()
},
}
done := make(chan error, 1)
go func() { done <- runSupervisorPreMain(ctx, stages, []wfv1.ArtifactPluginName{"fail", "ok1", "ok2"}) }()
select {
case err := <-done:
require.ErrorContains(t, err, "plugin boom",
"the failing plugin's error should surface; sibling cancellations should not mask it")
case <-time.After(5 * time.Second):
t.Fatal("runSupervisorPreMain did not return; siblings did not observe context cancellation")
}
// All three plugin goroutines must have been entered.
assert.EqualValues(t, 3, stages.loadFromPluginCalls.Load())
}
func TestRunSupervisorPreMain_NonPluginLoadFailure(t *testing.T) {
ctx := logging.TestContext(t.Context())
stages := &fakeStages{
loadWithoutPlugins: func(ctx context.Context) error { return errors.New("non-plugin boom") },
}
err := runSupervisorPreMain(ctx, stages, nil)
require.ErrorContains(t, err, "non-plugin boom")
}