forked from docker-flow/docker-flow-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args_test.go
117 lines (99 loc) · 2.18 KB
/
args_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
// +build !integration
package main
import (
"fmt"
"github.com/stretchr/testify/suite"
"net/http"
"os"
"testing"
)
type ArgsTestSuite struct {
suite.Suite
args args
}
func (s *ArgsTestSuite) SetupTest() {
httpListenAndServe = func(addr string, handler http.Handler) error {
return nil
}
}
// NewArgs
func (s ArgsTestSuite) Test_NewArgs_ReturnsNewStruct() {
a := newArgs()
s.IsType(args{}, a)
}
// Parse > Server
func (s ArgsTestSuite) Test_Parse_ParsesServerLongArgs() {
os.Args = []string{"myProgram", "server"}
data := []struct {
expected string
key string
value *string
}{
{"ipFromArgs", "ip", &serverImpl.IP},
{"portFromArgs", "port", &serverImpl.Port},
}
for _, d := range data {
os.Args = append(os.Args, fmt.Sprintf("--%s", d.key), d.expected)
}
args{}.parse()
for _, d := range data {
s.Equal(d.expected, *d.value)
}
}
func (s ArgsTestSuite) Test_Parse_ParsesServerShortArgs() {
os.Args = []string{"myProgram", "server"}
data := []struct {
expected string
key string
value *string
}{
{"ipFromArgs", "i", &serverImpl.IP},
{"portFromArgs", "p", &serverImpl.Port},
}
for _, d := range data {
os.Args = append(os.Args, fmt.Sprintf("-%s", d.key), d.expected)
}
args{}.parse()
for _, d := range data {
s.Equal(d.expected, *d.value)
}
}
func (s ArgsTestSuite) Test_Parse_ServerHasDefaultValues() {
os.Args = []string{"myProgram", "server"}
os.Unsetenv("IP")
os.Unsetenv("PORT")
data := []struct {
expected string
value *string
}{
{"0.0.0.0", &serverImpl.IP},
{"8080", &serverImpl.Port},
}
args{}.parse()
for _, d := range data {
s.Equal(d.expected, *d.value)
}
}
func (s ArgsTestSuite) Test_Parse_ServerDefaultsToEnvVars() {
os.Args = []string{"myProgram", "server"}
data := []struct {
expected string
key string
value *string
}{
{"ipFromEnv", "IP", &serverImpl.IP},
{"portFromEnv", "PORT", &serverImpl.Port},
}
for _, d := range data {
os.Setenv(d.key, d.expected)
}
args{}.parse()
for _, d := range data {
s.Equal(d.expected, *d.value)
}
}
// Suite
func TestArgsUnitTestSuite(t *testing.T) {
logPrintf = func(format string, v ...interface{}) {}
suite.Run(t, new(ArgsTestSuite))
}