-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildkite_gerrit_trigger_test.go
98 lines (87 loc) · 2.08 KB
/
buildkite_gerrit_trigger_test.go
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
package main
import (
"log"
"os"
"testing"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
var (
cannedCreateDatabase = []string{
"create table if not exists buildkite (id text not null primary key, sha1 text, changeid text, changenumber integer, patchset integer);",
}
)
func setupDatabase(t *testing.T, statements ...string) (*os.File, *sql.DB) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("failed in setup: %s", err)
}
fh, err := os.CreateTemp(cwd, "gepdb")
if err != nil {
t.Fatalf("failed in setup: %s", err)
}
db, err := sql.Open("sqlite3", fh.Name())
if err != nil {
t.Fatalf("failed in setup: %s", err)
}
if len(statements) > 0 {
for _, statement := range statements {
if _, err := db.Exec(statement); err != nil {
t.Fatalf("failed in setup: executing '%s': %s", statement, err)
}
log.Printf("setup: executed %s", statement)
}
}
return fh, db
}
func TestTryLatestBuildCases(t *testing.T) {
type testCase struct {
statements []string
expectation bool
input int
output string
}
testCases := []testCase{
// Should find an existing build
{
statements: append(
cannedCreateDatabase,
[]string{"insert into buildkite (id, changenumber) values ('abc-123', 1234)"}...,
),
expectation: true,
input: 1234,
output: "abc-123",
},
// Should not find a missing build
{
statements: append(
cannedCreateDatabase,
[]string{"insert into buildkite (id, changenumber) values ('abc-123', 1234)"}...,
),
expectation: false,
input: 9999,
output: "abc-123",
},
}
for id, tc := range testCases {
dbFile, db := setupDatabase(t, tc.statements...)
defer func() {
db.Close()
dbFile.Close()
os.Remove(dbFile.Name())
}()
state := &State{
DB: db,
}
buildUUID, ok := state.TryGetLatestBuild(tc.input)
if ok != tc.expectation {
t.Fatalf("expected %v for case %d but got %v", tc.expectation, id, ok)
}
if !tc.expectation {
continue
}
if buildUUID != tc.output {
t.Fatalf("expected %s buildUUID for case %d but got %s", tc.output, id, buildUUID)
}
}
}