-
Notifications
You must be signed in to change notification settings - Fork 29
/
worker_test.go
185 lines (159 loc) · 4.64 KB
/
worker_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
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
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package worker
import (
"reflect"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func TestWorker_New(t *testing.T) {
// setup types
logger := logrus.NewEntry(logrus.StandardLogger())
_sql, _mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
if err != nil {
t.Errorf("unable to create new SQL mock: %v", err)
}
defer _sql.Close()
_mock.ExpectExec(CreatePostgresTable).WillReturnResult(sqlmock.NewResult(1, 1))
_mock.ExpectExec(CreateHostnameAddressIndex).WillReturnResult(sqlmock.NewResult(1, 1))
_config := &gorm.Config{SkipDefaultTransaction: true}
_postgres, err := gorm.Open(postgres.New(postgres.Config{Conn: _sql}), _config)
if err != nil {
t.Errorf("unable to create new postgres database: %v", err)
}
_sqlite, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), _config)
if err != nil {
t.Errorf("unable to create new sqlite database: %v", err)
}
defer func() { _sql, _ := _sqlite.DB(); _sql.Close() }()
// setup tests
tests := []struct {
failure bool
name string
client *gorm.DB
key string
logger *logrus.Entry
skipCreation bool
want *engine
}{
{
failure: false,
name: "postgres",
client: _postgres,
logger: logger,
skipCreation: false,
want: &engine{
client: _postgres,
config: &config{SkipCreation: false},
logger: logger,
},
},
{
failure: false,
name: "sqlite3",
client: _sqlite,
logger: logger,
skipCreation: false,
want: &engine{
client: _sqlite,
config: &config{SkipCreation: false},
logger: logger,
},
},
}
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := New(
WithClient(test.client),
WithLogger(test.logger),
WithSkipCreation(test.skipCreation),
)
if test.failure {
if err == nil {
t.Errorf("New for %s should have returned err", test.name)
}
return
}
if err != nil {
t.Errorf("New for %s returned err: %v", test.name, err)
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("New for %s is %v, want %v", test.name, got, test.want)
}
})
}
}
// testPostgres is a helper function to create a Postgres engine for testing.
func testPostgres(t *testing.T) (*engine, sqlmock.Sqlmock) {
// create the new mock sql database
//
// https://pkg.go.dev/github.com/DATA-DOG/go-sqlmock#New
_sql, _mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
if err != nil {
t.Errorf("unable to create new SQL mock: %v", err)
}
_mock.ExpectExec(CreatePostgresTable).WillReturnResult(sqlmock.NewResult(1, 1))
_mock.ExpectExec(CreateHostnameAddressIndex).WillReturnResult(sqlmock.NewResult(1, 1))
// create the new mock Postgres database client
//
// https://pkg.go.dev/gorm.io/gorm#Open
_postgres, err := gorm.Open(
postgres.New(postgres.Config{Conn: _sql}),
&gorm.Config{SkipDefaultTransaction: true},
)
if err != nil {
t.Errorf("unable to create new postgres database: %v", err)
}
_engine, err := New(
WithClient(_postgres),
WithLogger(logrus.NewEntry(logrus.StandardLogger())),
WithSkipCreation(false),
)
if err != nil {
t.Errorf("unable to create new postgres worker engine: %v", err)
}
return _engine, _mock
}
// testSqlite is a helper function to create a Sqlite engine for testing.
func testSqlite(t *testing.T) *engine {
_sqlite, err := gorm.Open(
sqlite.Open("file::memory:?cache=shared"),
&gorm.Config{SkipDefaultTransaction: true},
)
if err != nil {
t.Errorf("unable to create new sqlite database: %v", err)
}
_engine, err := New(
WithClient(_sqlite),
WithLogger(logrus.NewEntry(logrus.StandardLogger())),
WithSkipCreation(false),
)
if err != nil {
t.Errorf("unable to create new sqlite worker engine: %v", err)
}
return _engine
}
// testWorker is a test helper function to create a library
// Worker type with all fields set to their zero values.
func testWorker() *library.Worker {
return &library.Worker{
ID: new(int64),
Hostname: new(string),
Address: new(string),
Routes: new([]string),
Active: new(bool),
Status: new(string),
LastStatusUpdateAt: new(int64),
RunningBuildIDs: new([]string),
LastBuildFinishedAt: new(int64),
LastCheckedIn: new(int64),
BuildLimit: new(int64),
}
}