-
Notifications
You must be signed in to change notification settings - Fork 4
/
valet_test.go
101 lines (81 loc) · 2.61 KB
/
valet_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
package valet
import (
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
func TestValetRegisterTask(t *testing.T) {
v := New("internal/config/testdata/test_config.yaml")
taskrepo := v.taskService.GetTaskRepository()
if _, err := taskrepo.GetTaskFunc("dummytask"); err == nil {
t.Errorf("taskrepo did not return expected error")
}
v.RegisterTask("dummytask", func(i ...interface{}) (interface{}, error) {
return nil, nil
})
if _, err := taskrepo.GetTaskFunc("dummytask"); err != nil {
t.Errorf("taskrepo returned unexpected error: got %s want nil", err)
}
}
func TestDecodeTaskParams(t *testing.T) {
type test struct {
Name string `json:"name"`
Description string `json:"description"`
}
testStruct := test{}
taskParams := map[string]interface{}{
"name": "test_name",
"description": "test_description",
}
args := []interface{}{
taskParams,
}
if err := DecodeTaskParams(args, &testStruct); err != nil {
t.Errorf("DecodeTaskParaams returned unexpected error: got %s want nil", err)
}
if testStruct.Name != taskParams["name"] {
t.Errorf("DecodeTaskParaams decoded wrong struct name field: got %#v want #%v", testStruct.Name, taskParams["name"])
}
if testStruct.Description != taskParams["description"] {
t.Errorf("DecodeTaskParaams decoded wrong struct description field: gow %#v want %#v", testStruct.Description, taskParams["description"])
}
}
func TestDecodePreviousJobResults(t *testing.T) {
var results string
taskParams := map[string]interface{}{
"name": "test_name",
"description": "test_description",
}
args := []interface{}{
taskParams,
"some metadata",
}
if err := DecodePreviousJobResults(args, &results); err != nil {
t.Errorf("DecodePreviousJobResults returned unexpected error: got %s want nil", err)
}
if results != "some metadata" {
t.Errorf("DecodePreviousJobResults decoded wrong results: got %#v want %#v", results, "some metadata")
}
}
func TestRun(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
v := New("internal/config/testdata/test_config.yaml")
v.logger = &logrus.Logger{Out: ioutil.Discard}
v.RegisterTask("dummytask", func(i ...interface{}) (interface{}, error) {
return "some_metadata", nil
})
go v.Run()
// Give some time for the server to run.
time.Sleep(200 * time.Millisecond)
res, err := http.Get("http://localhost:8080/api/status")
if err != nil {
t.Errorf("error calling valet status endpoint: %s", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("valet server responded with wrong status code: got %v want %v", res.StatusCode, http.StatusOK)
}
v.Stop()
}