-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_test.go
123 lines (109 loc) · 2.66 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bytes"
"net/url"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/tactycal/agent/stubUtils"
)
func TestNewConfig_Valid(t *testing.T) {
fixtures := []struct {
title string
data []byte
expected *Config
}{
{
title: "minimal config",
data: []byte(`token=TOKEN`),
expected: &Config{
Token: "TOKEN",
Uri: "https://api.tactycal.com",
Labels: []string{},
StatePath: "/default/path",
ClientTimeout: time.Second,
},
},
{
title: "full config",
data: []byte(`token=TOKEN
uri=API_URL/
labels=$PATH,label
timeout=10s
state=/path/to/state`),
expected: &Config{
Token: "TOKEN",
Uri: "API_URL",
Labels: []string{os.Getenv("PATH"), "label"},
ClientTimeout: time.Second * 10,
StatePath: "/path/to/state",
},
},
}
for _, fixture := range fixtures {
t.Run(fixture.title, func(t *testing.T) {
s := stubUtils.NewStubs(t, &stubUtils.ReadFileStub{Path: "path", Output: fixture.data})
defer s.Close()
// parse
c, err := NewConfig("path", "/default/path", time.Second)
if err != nil {
t.Error("Error should be nil; got", err)
}
if !reflect.DeepEqual(fixture.expected, c) {
t.Errorf("Expected\n%+v\nto equal\n%+v", fixture.expected, c)
}
})
}
}
func TestNewConfig_Errors(t *testing.T) {
fixtures := []struct {
title string
data []byte
expected string
}{
{
title: "file missing",
data: []byte("missing"),
expected: "configuration: oh no",
},
{
title: "no token",
data: []byte(``),
expected: "configuration: No token provided",
},
{
title: "broken proxy URL",
data: []byte(`token=TOKEN
proxy=%gh`),
expected: "configuration: unable to parse proxy URL",
},
}
for _, fixture := range fixtures {
t.Run(fixture.title, func(t *testing.T) {
// // create a temp file
s := stubUtils.NewStubs(t)
if bytes.Equal(fixture.data, []byte("missing")) {
s.Add(&stubUtils.ReadFileStub{Err: stubUtils.OhNoErr})
} else {
s.Add(&stubUtils.ReadFileStub{Output: fixture.data})
}
// parse
c, err := NewConfig("path", "/default/path", time.Second)
if c != nil {
t.Errorf("Expected Config to be nil; got %+v", c)
}
if err == nil {
t.Error("Expected err to not be nil")
}
if err != nil && !strings.Contains(err.Error(), fixture.expected) {
t.Errorf("Expected error to contain \"%s\"; got \"%s\"", fixture.expected, err.Error())
}
})
}
}
func mustParseUrl(rawUrl string) *url.URL {
parsed, _ := url.Parse(rawUrl)
return parsed
}