This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
177 lines (170 loc) · 4 KB
/
main_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
package main
import (
"os"
"testing"
"github.com/pkg/errors"
"github.com/yahoojapan/garm/config"
)
func TestParseParams(t *testing.T) {
type test struct {
name string
beforeFunc func()
checkFunc func(*params) error
checkErr bool
}
tests := []test{
func() test {
return test{
name: "check parseParams set default value",
beforeFunc: func() {
os.Args = []string{""}
},
checkFunc: func(p *params) error {
if p.configFilePath != "/etc/garm/config.yaml" {
return errors.Errorf("unexpected file path. got: %s, want: /etc/garm/config.yaml", p.configFilePath)
}
if p.showVersion != false {
return errors.Errorf("unexpected showVersion flag. got: %v, want : false", p.showVersion)
}
return nil
},
checkErr: false,
}
}(),
func() test {
return test{
name: "check parse error",
checkFunc: func(p *params) error {
return nil
},
beforeFunc: func() {
os.Args = []string{"", "-="}
},
checkErr: true,
}
}(),
func() test {
return test{
name: "check parseParams set user flags",
beforeFunc: func() {
os.Args = []string{"", "-f", "/dummy/path", "-version", "true"}
},
checkFunc: func(p *params) error {
if p.configFilePath != "/dummy/path" {
return errors.Errorf("unexpected file path. got: %s, want: /dummy/path", p.configFilePath)
}
if p.showVersion != true {
return errors.Errorf("unexpected showVersion flag. got: %v, want: true", p.showVersion)
}
return nil
},
checkErr: false,
}
}(),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.beforeFunc != nil {
tt.beforeFunc()
}
got, err := parseParams()
if err != nil && !tt.checkErr {
t.Errorf("unexpected error: %v", err)
}
if err := tt.checkFunc(got); err != nil {
t.Errorf("checkFunc() error: %v", err)
}
})
}
}
func Test_run(t *testing.T) {
type args struct {
cfg config.Config
}
type test struct {
name string
args args
checkFunc func(config.Config) error
}
tests := []test{
func() test {
return test{
name: "run error",
args: args{
cfg: config.Config{
Token: config.Token{
AthenzDomain: "domain",
ServiceName: "service",
RefreshDuration: "1h",
KeyVersion: "keyId",
Expiration: "1h",
PrivateKey: "./service/testdata/dummyServer.key",
},
Athenz: config.Athenz{
Timeout: "dummy",
},
},
},
checkFunc: func(cfg config.Config) error {
got := run(cfg)
want := "failed to instantiate daemon: athenz service instantiate failed: athenz timeout parse failed: time: invalid duration dummy"
if len(got) != 1 {
return errors.New("len(got) != 1")
}
if got[0].Error() != want {
return errors.Errorf("got: %v, want: %v", got[0], want)
}
return nil
},
}
}(),
func() test {
return test{
name: "daemon init error",
args: args{
cfg: config.Config{
Token: config.Token{
RefreshDuration: "dummy",
},
},
},
checkFunc: func(cfg config.Config) error {
got := run(cfg)
want := "failed to instantiate daemon: token service instantiate failed: invalid token refresh duration dummy: time: invalid duration dummy"
if len(got) != 1 {
return errors.New("len(got) != 1")
}
if got[0].Error() != want {
return errors.Errorf("got: %v, want: %v", got[0], want)
}
return nil
},
}
}(),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.checkFunc(tt.args.cfg); err != nil {
t.Errorf("run() error = %v", err)
}
})
}
}
func Test_getVersion(t *testing.T) {
tests := []struct {
name string
want string
}{
{
name: "default",
want: "development version",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getVersion(); got != tt.want {
t.Errorf("getVersion() = %v, want %v", got, tt.want)
}
})
}
}