Skip to content

Commit

Permalink
move this change to woodpecker-ci#3151
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Jan 9, 2024
1 parent 20988f7 commit eb37bf3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 46 deletions.
2 changes: 1 addition & 1 deletion server/grpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (s *RPC) Update(_ context.Context, id string, state rpc.State) error {
return err
}

if err := pipeline.UpdateStepStatus(s.store, step, state); err != nil {
if err := pipeline.UpdateStepStatus(s.store, step, state, currentPipeline.Started); err != nil {
log.Error().Err(err).Msg("rpc.update: cannot update step")
}

Expand Down
8 changes: 6 additions & 2 deletions server/pipeline/stepStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/server/model"
)

func UpdateStepStatus(store model.UpdateStepStore, step *model.Step, state rpc.State) error {
func UpdateStepStatus(store model.UpdateStepStore, step *model.Step, state rpc.State, started int64) error {
if state.Exited {
step.Stopped = state.Finished
step.ExitCode = state.ExitCode
Expand All @@ -34,10 +34,14 @@ func UpdateStepStatus(store model.UpdateStepStore, step *model.Step, state rpc.S
if state.ExitCode == 137 {
step.State = model.StatusKilled
}
} else if step.Stopped == 0 {
} else {
step.Started = state.Started
step.State = model.StatusRunning
}

if step.Started == 0 && step.Stopped != 0 {
step.Started = started
}
return store.StepUpdate(step)
}

Expand Down
100 changes: 57 additions & 43 deletions server/pipeline/stepStatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ func (m *mockUpdateStepStore) StepUpdate(_ *model.Step) error {

func TestUpdateStepStatusNotExited(t *testing.T) {
t.Parallel()
// step in db before update
step := &model.Step{}

// advertised step status
state := rpc.State{
Started: int64(42),
Exited: false,
Expand All @@ -45,47 +42,54 @@ func TestUpdateStepStatusNotExited(t *testing.T) {
ExitCode: 137,
Error: "not an error",
}

err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
step := &model.Step{}
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state, int64(1))
assert.NoError(t, err)
assert.EqualValues(t, model.StatusRunning, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 0, step.Stopped)
assert.EqualValues(t, 0, step.ExitCode)
assert.EqualValues(t, "", step.Error)

if step.State != model.StatusRunning {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusRunning, step.State)
} else if step.Started != int64(42) {
t.Errorf("Step started not equals 42 != %d", step.Started)
} else if step.Stopped != int64(0) {
t.Errorf("Step stopped not equals 0 != %d", step.Stopped)
} else if step.ExitCode != 0 {
t.Errorf("Step exit code not equals 0 != %d", step.ExitCode)
} else if step.Error != "" {
t.Errorf("Step error not equals '' != '%s'", step.Error)
}
}

func TestUpdateStepStatusNotExitedButStopped(t *testing.T) {
t.Parallel()

// step in db before update
step := &model.Step{Started: 42, Stopped: 64, State: model.StatusKilled}
step := &model.Step{Stopped: int64(64)}

// advertised step status
state := rpc.State{
Exited: false,
// Dummy data
Finished: int64(1),
ExitCode: 137,
Error: "not an error",
}

err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state, int64(42))
assert.NoError(t, err)
assert.EqualValues(t, model.StatusKilled, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 64, step.Stopped)
assert.EqualValues(t, 0, step.ExitCode)
assert.EqualValues(t, "", step.Error)

if step.State != model.StatusRunning {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusRunning, step.State)
} else if step.Started != int64(42) {
t.Errorf("Step started not equals 42 != %d", step.Started)
} else if step.Stopped != int64(64) {
t.Errorf("Step stopped not equals 64 != %d", step.Stopped)
} else if step.ExitCode != 0 {
t.Errorf("Step exit code not equals 0 != %d", step.ExitCode)
} else if step.Error != "" {
t.Errorf("Step error not equals '' != '%s'", step.Error)
}
}

func TestUpdateStepStatusExited(t *testing.T) {
t.Parallel()

// step in db before update
step := &model.Step{Started: 42}

// advertised step status
state := rpc.State{
Started: int64(42),
Exited: true,
Expand All @@ -94,42 +98,52 @@ func TestUpdateStepStatusExited(t *testing.T) {
Error: "an error",
}

err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
step := &model.Step{}
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state, int64(42))
assert.NoError(t, err)
assert.EqualValues(t, model.StatusKilled, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 34, step.Stopped)
assert.EqualValues(t, 137, step.ExitCode)
assert.EqualValues(t, "an error", step.Error)

if step.State != model.StatusKilled {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusKilled, step.State)
} else if step.Started != int64(42) {
t.Errorf("Step started not equals 42 != %d", step.Started)
} else if step.Stopped != int64(34) {
t.Errorf("Step stopped not equals 34 != %d", step.Stopped)
} else if step.ExitCode != 137 {
t.Errorf("Step exit code not equals 137 != %d", step.ExitCode)
} else if step.Error != "an error" {
t.Errorf("Step error not equals 'an error' != '%s'", step.Error)
}
}

func TestUpdateStepStatusExitedButNot137(t *testing.T) {
t.Parallel()

// step in db before update
step := &model.Step{Started: 42}

// advertised step status
state := rpc.State{
Started: int64(42),
Exited: true,
Finished: int64(34),
Error: "an error",
}

err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
step := &model.Step{}
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state, int64(42))
assert.NoError(t, err)
assert.EqualValues(t, model.StatusFailure, step.State)
assert.EqualValues(t, 42, step.Started)
assert.EqualValues(t, 34, step.Stopped)
assert.EqualValues(t, 0, step.ExitCode)
assert.EqualValues(t, "an error", step.Error)

if step.State != model.StatusFailure {
t.Errorf("Step status not equals '%s' != '%s'", model.StatusFailure, step.State)
} else if step.Started != int64(42) {
t.Errorf("Step started not equals 42 != %d", step.Started)
} else if step.Stopped != int64(34) {
t.Errorf("Step stopped not equals 34 != %d", step.Stopped)
} else if step.ExitCode != 0 {
t.Errorf("Step exit code not equals 0 != %d", step.ExitCode)
} else if step.Error != "an error" {
t.Errorf("Step error not equals 'an error' != '%s'", step.Error)
}
}

func TestUpdateStepStatusExitedWithCode(t *testing.T) {
t.Parallel()

// advertised step status
state := rpc.State{
Started: int64(42),
Exited: true,
Expand All @@ -138,7 +152,7 @@ func TestUpdateStepStatusExitedWithCode(t *testing.T) {
Error: "an error",
}
step := &model.Step{}
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state)
err := UpdateStepStatus(&mockUpdateStepStore{}, step, state, int64(42))
assert.NoError(t, err)

if step.State != model.StatusFailure {
Expand Down

0 comments on commit eb37bf3

Please sign in to comment.