-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
106 lines (89 loc) · 2.03 KB
/
config_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
package main
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestNewConfigFromFile(t *testing.T) {
configRaw := `
jobs:
job_id:
name: job_name
steps:
- name: step
run: exit`
f, err := ioutil.TempFile("", "test")
if err != nil {
t.Fatal(err)
}
defer func() {
f.Close()
os.Remove(f.Name())
}()
io.Copy(f, strings.NewReader(configRaw))
cfg, err := NewConfig(f.Name())
if err != nil {
t.Fatal(err)
}
job, ok := cfg.Jobs["job_id"]
if !ok {
t.Fatal("expected to have job_id")
}
if job.Name != "job_name" {
t.Fatalf("expected job name to be \"job_name\", but got %s", job.Name)
}
if len(job.Steps) != 1 {
t.Fatalf("expected to have 1 step, but got %d steps", len(job.Steps))
}
step := job.Steps[0]
if step.Name != "step" {
t.Fatalf("expected step name to be \"step\", but got %s", step.Name)
}
if step.Run != "exit" {
t.Fatalf("expected step run to be \"exit\", but got %s", step.Run)
}
}
func TestNewConfigFromURL(t *testing.T) {
configRaw := `
jobs:
job_id:
name: job_name
steps:
- name: step
run: exit`
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte(configRaw))
}))
defer func() { testServer.Close() }()
cfg, err := NewConfig(testServer.URL)
if err != nil {
t.Fatal(err)
}
job, ok := cfg.Jobs["job_id"]
if !ok {
t.Fatal("expected to have job_id")
}
if job.Name != "job_name" {
t.Fatalf("expected job name to be \"job_name\", but got %s", job.Name)
}
if len(job.Steps) != 1 {
t.Fatalf("expected to have 1 step, but got %d steps", len(job.Steps))
}
step := job.Steps[0]
if step.Name != "step" {
t.Fatalf("expected step name to be \"step\", but got %s", step.Name)
}
if step.Run != "exit" {
t.Fatalf("expected step run to be \"exit\", but got %s", step.Run)
}
}
func TestNewConfigFromInvalidURL(t *testing.T) {
_, err := NewConfig("https://this-url-must-be-broken.test")
if err == nil {
t.Fatal("expected to get an error")
}
}