-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathworker_test.go
119 lines (93 loc) · 2.71 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
// 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 (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/database/sqlite"
"github.com/go-vela/types/library"
)
func TestWorker_Retrieve(t *testing.T) {
// setup types
want := new(library.Worker)
want.SetID(1)
// setup context
gin.SetMode(gin.TestMode)
context, _ := gin.CreateTestContext(nil)
ToContext(context, want)
// run test
got := Retrieve(context)
if got != want {
t.Errorf("Retrieve is %v, want %v", got, want)
}
}
func TestWorker_Establish(t *testing.T) {
// setup types
want := new(library.Worker)
want.SetID(1)
want.SetHostname("worker_0")
want.SetAddress("localhost")
want.SetRoutes([]string{"foo", "bar", "baz"})
want.SetActive(true)
want.SetStatus("available")
want.SetLastStatusUpdateAt(12345)
want.SetRunningBuildIDs([]string{})
want.SetLastBuildFinishedAt(12345)
want.SetLastCheckedIn(12345)
want.SetBuildLimit(0)
got := new(library.Worker)
// setup database
db, _ := sqlite.NewTest()
defer func() {
db.Sqlite.Exec("delete from workers;")
_sql, _ := db.Sqlite.DB()
_sql.Close()
}()
_ = db.CreateWorker(want)
// setup context
gin.SetMode(gin.TestMode)
resp := httptest.NewRecorder()
context, engine := gin.CreateTestContext(resp)
context.Request, _ = http.NewRequest(http.MethodGet, "/workers/worker_0", nil)
// setup mock server
engine.Use(func(c *gin.Context) { database.ToContext(c, db) })
engine.Use(Establish())
engine.GET("/workers/:worker", func(c *gin.Context) {
got = Retrieve(c)
c.Status(http.StatusOK)
})
// run test
engine.ServeHTTP(resp, context.Request)
if resp.Code != http.StatusOK {
t.Errorf("Establish returned %v, want %v", resp.Code, http.StatusOK)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Establish is %v, want %v", got, want)
}
}
func TestWorker_Establish_NoWorkerParameter(t *testing.T) {
// setup database
db, _ := sqlite.NewTest()
defer func() { _sql, _ := db.Sqlite.DB(); _sql.Close() }()
// setup context
gin.SetMode(gin.TestMode)
resp := httptest.NewRecorder()
context, engine := gin.CreateTestContext(resp)
context.Request, _ = http.NewRequest(http.MethodGet, "/workers/", nil)
// setup mock server
engine.Use(func(c *gin.Context) { database.ToContext(c, db) })
engine.Use(Establish())
engine.GET("/workers/:worker", func(c *gin.Context) {
c.Status(http.StatusOK)
})
// run test
engine.ServeHTTP(context.Writer, context.Request)
if resp.Code != http.StatusBadRequest {
t.Errorf("Establish returned %v, want %v", resp.Code, http.StatusBadRequest)
}
}