-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathopts_test.go
113 lines (92 loc) · 2.86 KB
/
opts_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
package main
import (
"flag"
"os/user"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testOpts struct {
args []string
version, help func()
}
func TestVersionHelp(t *testing.T) {
ch := make(chan bool, 1)
checkPrintInvoked := func() {
ch <- true
}
usage := func() {
panic("should not get there")
}
// test the help
testHelps := []testOpts{
{[]string{"me", "--version"}, checkPrintInvoked, usage},
{[]string{"me", "-v"}, checkPrintInvoked, usage},
{[]string{"me", "--help"}, usage, checkPrintInvoked},
{[]string{"me", "-h"}, usage, checkPrintInvoked},
}
for _, tt := range testHelps {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
opts, err := ConfigureOptions(fs, tt.args, tt.version, tt.help)
assert.Nil(t, err)
assert.Nil(t, opts)
select {
case <-ch:
case <-time.After(time.Second):
assert.Fail(t, "should have invoked print function for args %s", tt.args)
}
}
}
func TestOptions(t *testing.T) {
// helper function
mustNotFail := func(args []string) *Options {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
opts, err := ConfigureOptions(fs, args, usage, usage)
if err != nil {
t.Fatalf("Error on config: %v", err)
}
return opts
}
mustFail := func(args []string, errContent ...string) {
fs := flag.NewFlagSet("test", flag.ContinueOnError)
opts, err := ConfigureOptions(fs, args, usage, usage)
if opts != nil || err == nil {
t.Fatalf("Expect no opts and err, got %v and %v", opts, err)
}
for _, content := range errContent {
if strings.Contains(err.Error(), content) {
return
}
}
t.Fatalf("Expect error contain any of %v, got %v", errContent, err)
}
t.Run("master options", func(t *testing.T) {
// check mode
mustFail([]string{"me", "--mode", "not existed"},
"mode must be either master, agent, or executor")
// check default master
opts := mustNotFail([]string{"me"})
assert.Equal(t, Master, opts.Mode)
assert.Equal(t, DEFAULT_PORT, opts.Port)
assert.Equal(t, DEFAULT_CLUSTER_PORT, opts.ClusterPort)
u, _ := user.Current()
assert.Equal(t, u.HomeDir+"/.gobench", opts.Dir)
opts = mustNotFail([]string{"me", "-p", "3000"})
assert.Equal(t, opts.Port, 3000)
opts = mustNotFail([]string{"me", "--dir", "/foo"})
assert.Equal(t, opts.Dir, "/foo")
opts = mustNotFail([]string{"me", "--admin-password", "apassword"})
assert.Equal(t, opts.AdminPassword, "apassword")
})
t.Run("agent options", func(t *testing.T) {
mustFail([]string{"me", "--mode", "agent"}, "must have route to a master")
opts := mustNotFail([]string{"me", "--mode", "agent",
"--route", "abc.xyz:1234"})
assert.Equal(t, "abc.xyz:1234", opts.Route)
assert.Equal(t, DEFAULT_CLUSTER_PORT, opts.ClusterPort)
opts = mustNotFail([]string{"me", "--mode", "agent",
"--route", "abc.xyz:1234", "--clusterPort", "4567"})
assert.Equal(t, 4567, opts.ClusterPort)
})
}