2019-10-02 05:51:23 +00:00
|
|
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package canceler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
)
|
|
|
|
|
2021-08-26 16:21:00 +00:00
|
|
|
func TestMatchPendingBuild(t *testing.T) {
|
2019-10-02 05:51:23 +00:00
|
|
|
tests := []struct {
|
|
|
|
build *core.Build
|
|
|
|
repo *core.Repository
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
// does not match repository id
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 2},
|
|
|
|
repo: &core.Repository{ID: 1},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// does not match build number requirement that
|
|
|
|
// must be older than current build
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{Number: 3}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{Number: 2}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// does not match required status
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{Number: 1, Status: core.StatusPassing}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// does not match (one of) required event types
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPullRequest},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{
|
|
|
|
Number: 1,
|
|
|
|
Status: core.StatusPending,
|
|
|
|
Event: core.EventPush,
|
|
|
|
}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// does not match ref
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPush, Ref: "refs/heads/master"},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{
|
|
|
|
Number: 1,
|
|
|
|
Status: core.StatusPending,
|
|
|
|
Event: core.EventPush,
|
|
|
|
Ref: "refs/heads/develop",
|
|
|
|
}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// successful matches
|
|
|
|
//
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPush, Ref: "refs/heads/master"},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{
|
|
|
|
Number: 1,
|
|
|
|
Status: core.StatusPending,
|
|
|
|
Event: core.EventPush,
|
|
|
|
Ref: "refs/heads/master",
|
2021-08-26 16:21:00 +00:00
|
|
|
}, CancelRunning: false},
|
2019-10-02 05:51:23 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPullRequest, Ref: "refs/heads/master"},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{
|
|
|
|
Number: 1,
|
|
|
|
Status: core.StatusPending,
|
|
|
|
Event: core.EventPullRequest,
|
|
|
|
Ref: "refs/heads/master",
|
2021-08-26 16:21:00 +00:00
|
|
|
}, CancelRunning: false},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
if got, want := match(test.build, test.repo), test.want; got != want {
|
|
|
|
t.Errorf("Want match %v at index %d, got %v", want, i, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMatchRunningBuilds(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
build *core.Build
|
|
|
|
repo *core.Repository
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
// does not match repository id
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 2},
|
|
|
|
repo: &core.Repository{ID: 1},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// does not match build number requirement that
|
|
|
|
// must be older than current build
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{Number: 3}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{Number: 2}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// does not match required status
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{Number: 1, Status: core.StatusError}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// does not match (one of) required event types
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPullRequest},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{
|
|
|
|
Number: 1,
|
|
|
|
Status: core.StatusRunning,
|
|
|
|
Event: core.EventPush,
|
2019-10-02 05:51:23 +00:00
|
|
|
}},
|
2021-08-26 16:21:00 +00:00
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
// does not match ref
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPush, Ref: "refs/heads/master"},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{
|
|
|
|
Number: 1,
|
|
|
|
Status: core.StatusRunning,
|
|
|
|
Event: core.EventPush,
|
|
|
|
Ref: "refs/heads/develop",
|
|
|
|
}},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
//
|
|
|
|
// successful matches
|
|
|
|
//
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPullRequest, Ref: "refs/heads/master"},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{
|
|
|
|
Number: 1,
|
|
|
|
Status: core.StatusRunning,
|
|
|
|
Event: core.EventPullRequest,
|
|
|
|
Ref: "refs/heads/master",
|
|
|
|
}, CancelRunning: true},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
build: &core.Build{RepoID: 1, Number: 2, Event: core.EventPush, Ref: "refs/heads/master"},
|
|
|
|
repo: &core.Repository{ID: 1, Build: &core.Build{
|
|
|
|
Number: 1,
|
|
|
|
Status: core.StatusRunning,
|
|
|
|
Event: core.EventPush,
|
|
|
|
Ref: "refs/heads/master",
|
|
|
|
}, CancelRunning: true},
|
2019-10-02 05:51:23 +00:00
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
if got, want := match(test.build, test.repo), test.want; got != want {
|
|
|
|
t.Errorf("Want match %v at index %d, got %v", want, i, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|