Skip to content

Commit 4e3b1bb

Browse files
tharunbhuk-dbIsaac
andcommitted
Keep fake pipeline updates that end or stop in their real state
In the test server, an update that was never polled is active only if its first state is not terminal. A plain update completes at once, so a second Start Update no longer gets 409, and Stop leaves it COMPLETED. A stopped update now hides the events planned for states it never reached, so a stop before RUNNING shows no case results or summary. The test of a finite update polls a fixed number of times instead of looping until COMPLETED, and the rootdir rule notes that the CLI uses the same rule, so only a run on a real workspace checks it. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent e1a0a6d commit 4e3b1bb

2 files changed

Lines changed: 52 additions & 10 deletions

File tree

‎libs/testserver/pipelines_testing.go‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,39 @@ type fakePipelineUpdate struct {
2727
pipelineID string
2828
states []pipelines.UpdateInfoState
2929
polls int
30+
31+
// canceled is set by cancel. CANCELED then holds the index of a state the update never reached.
32+
canceled bool
3033
}
3134

3235
// stateIndex returns the index of the state returned by the last Get Update call, or -1 before the
33-
// first call. Events emitted for a later state are not visible yet.
36+
// first call.
3437
func (u *fakePipelineUpdate) stateIndex() int {
3538
return min(u.polls, len(u.states)) - 1
3639
}
3740

38-
// active reports whether the update has not ended yet: it was not polled yet, or the state of the
39-
// last Get Update call is not terminal. Like the service, an update that will end on its own is
40-
// active until it does.
41+
// eventIndex returns the index of the last state whose events are visible. Events emitted for a
42+
// later state are not visible yet, and never are for a state that a canceled update did not reach.
43+
func (u *fakePipelineUpdate) eventIndex() int {
44+
if u.canceled {
45+
return len(u.states) - 2
46+
}
47+
return u.stateIndex()
48+
}
49+
50+
// active reports whether the update has not ended yet: the state of the last Get Update call, or
51+
// the first state before that call, is not terminal. Like the service, an update that will end on
52+
// its own is active until it does.
4153
func (u *fakePipelineUpdate) active() bool {
42-
i := u.stateIndex()
43-
return i < 0 || !isTerminalUpdateState(u.states[i])
54+
return !isTerminalUpdateState(u.states[max(u.stateIndex(), 0)])
4455
}
4556

4657
// cancel ends the update at its current state, as Stop does. CANCELED is the update's state from
4758
// now on, even before the next Get Update call.
4859
func (u *fakePipelineUpdate) cancel() {
4960
u.states = append(u.states[:u.stateIndex()+1], pipelines.UpdateInfoStateCanceled)
5061
u.polls = len(u.states)
62+
u.canceled = true
5163
}
5264

5365
func isTerminalUpdateState(state pipelines.UpdateInfoState) bool {
@@ -94,7 +106,8 @@ var pipelineTestFunctionRegexp = regexp.MustCompile(`(?m)^def (test_\w+)\(`)
94106

95107
// pipelineTestRootDir returns pytest's rootdir for a test run, as the service sets it: the deepest
96108
// folder that holds rootPath and every test source or, without rootPath, the folder of every test
97-
// source. It is "" when there is neither.
109+
// source. It is "" when there is neither. The CLI applies the same rule (testIDBase in
110+
// cmd/pipelines), so acceptance tests cannot catch a wrong rule; only a run on a real workspace checks it.
98111
func pipelineTestRootDir(rootPath string, sources []string) string {
99112
var folders [][]string
100113
if rootPath != "" {
@@ -367,7 +380,7 @@ func (s *FakeWorkspace) PipelineEvents(pipelineId string, query url.Values) Resp
367380
var events []fakePipelineEvent
368381
for _, event := range s.pipelineEvents {
369382
update := s.PipelineUpdates[event.Origin.UpdateId]
370-
if update.pipelineID != pipelineId || event.visibleAt > update.stateIndex() {
383+
if update.pipelineID != pipelineId || event.visibleAt > update.eventIndex() {
371384
continue
372385
}
373386
if updateID != "" && event.Origin.UpdateId != updateID {

‎libs/testserver/pipelines_testing_test.go‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,40 @@ func TestPipelineFiniteUpdateIsActiveUntilItEnds(t *testing.T) {
169169

170170
// Like the service, a second update is rejected until the first one ends, even if it will end on its own.
171171
assert.Equal(t, 409, workspace.PipelineStartUpdate(pipelineID, []byte(selection)).StatusCode)
172-
for getUpdateState(workspace, pipelineID, updateID) != pipelines.UpdateInfoStateCompleted {
173-
}
172+
assert.Equal(t, pipelines.UpdateInfoStateWaitingForResources, getUpdateState(workspace, pipelineID, updateID))
173+
assert.Equal(t, pipelines.UpdateInfoStateRunning, getUpdateState(workspace, pipelineID, updateID))
174+
require.Equal(t, pipelines.UpdateInfoStateCompleted, getUpdateState(workspace, pipelineID, updateID))
174175
startTestUpdate(t, workspace, pipelineID, selection)
175176
}
176177

178+
func TestPipelineUpdateThatIsNeverPolledEndsWithItsFirstState(t *testing.T) {
179+
workspace := NewFakeWorkspace("http://test", "dbapi123")
180+
pipelineID := createTestPipelineWithTests(t, workspace)
181+
182+
// A plain update completes at once, so a second one can start without polling the first.
183+
updateID := startTestUpdate(t, workspace, pipelineID, `{}`)
184+
startTestUpdate(t, workspace, pipelineID, `{}`)
185+
186+
// Stop leaves an update that already completed as it is.
187+
require.Equal(t, 0, workspace.PipelineStop(pipelineID).StatusCode)
188+
assert.Equal(t, pipelines.UpdateInfoStateCompleted, getUpdateState(workspace, pipelineID, updateID))
189+
}
190+
191+
func TestPipelineStopHidesEventsOfStatesNotReached(t *testing.T) {
192+
workspace := NewFakeWorkspace("http://test", "dbapi123")
193+
pipelineID := createTestPipelineWithTests(t, workspace)
194+
updateID := startTestUpdate(t, workspace, pipelineID, `{"test_only": true, "test_options": {"test_selection": ["`+testRootPath+`/tests/test_orders.py"]}}`)
195+
query := url.Values{"filter": {"update_id='" + updateID + "'"}}
196+
197+
assert.Equal(t, pipelines.UpdateInfoStateWaitingForResources, getUpdateState(workspace, pipelineID, updateID))
198+
require.Equal(t, 0, workspace.PipelineStop(pipelineID).StatusCode)
199+
assert.Equal(t, pipelines.UpdateInfoStateCanceled, getUpdateState(workspace, pipelineID, updateID))
200+
201+
// The update was stopped before RUNNING, so its case results and summary never show.
202+
events, _ := listEvents(t, workspace, pipelineID, query)
203+
assert.Empty(t, events)
204+
}
205+
177206
func TestPipelineStopCancelsAFiniteUpdate(t *testing.T) {
178207
workspace := NewFakeWorkspace("http://test", "dbapi123")
179208
pipelineID := createTestPipelineWithTests(t, workspace)

0 commit comments

Comments
 (0)