-
Notifications
You must be signed in to change notification settings - Fork 23
/
main_test.go
177 lines (149 loc) · 4.58 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
// PUBLIC DOMAIN NOTICE
// National Center for Biotechnology Information
//
// This software/database is a "United States Government Work" under the
// terms of the United States Copyright Act. It was written as part of
// the author's official duties as a United States Government employee and
// thus cannot be copyrighted. This software/database is freely available
// to the public for use. The National Library of Medicine and the U.S.
// Government have not placed any restriction on its use or reproduction.
//
// Although all reasonable efforts have been taken to ensure the accuracy
// and reliability of the software and data, the NLM and the U.S.
// Government do not and cannot warrant the performance or results that
// may be obtained by using this software or data. The NLM and the U.S.
// Government disclaim all warranties, express or implied, including
// warranties of performance, merchantability or fitness for any particular
// purpose.
//
// Please cite the author in any work or product based on this material.
package main
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)
func Test_createConfig_args_narg1(t *testing.T) {
// given
args := cli.Args{"server"}
flags := &appFlags{}
// when
config, err := createConfig(flags, args)
// then
assert.NoError(t, err)
assert.Equal(t, "server", config.serverAddress)
assert.Empty(t, config.serviceName)
}
func Test_createConfig_args_narg2(t *testing.T) {
// given
args := cli.Args{"server", "svc"}
flags := &appFlags{}
// when
config, err := createConfig(flags, args)
// then
assert.NoError(t, err)
assert.Equal(t, "server", config.serverAddress)
assert.Equal(t, "svc", config.serviceName)
}
func Test_createConfig_args_narg3(t *testing.T) {
// given
args := cli.Args{"foo", "bar", "baz"}
flags := &appFlags{}
// when
_, err := createConfig(flags, args)
// then
assert.Error(t, err)
}
func Test_createConfig_args_narg0(t *testing.T) {
// given
args := cli.Args{}
flags := &appFlags{}
// when
_, err := createConfig(flags, args)
// then
assert.Error(t, err)
}
func Test_createConfig_flags_empty(t *testing.T) {
// given
args := cli.Args{"foo"}
flags := &appFlags{}
// when
config, err := createConfig(flags, args)
// then
assert.NoError(t, err)
assert.Nil(t, config.creds)
assert.False(t, config.noFail)
}
func Test_createConfig_flags(t *testing.T) {
// given
args := cli.Args{"foo"}
flags := &appFlags{
tls: true,
noFail: true,
timeout: time.Minute,
}
// when
config, err := createConfig(flags, args)
// then
assert.NoError(t, err)
assert.NotNil(t, config.creds)
assert.Equal(t, time.Minute, config.timeout)
assert.True(t, config.noFail)
}
func Test_parseCredentials_tls(t *testing.T) {
// given
dataset := []struct {
flags *appFlags
credsReturned bool
errorReturned bool
message string
}{
// success
{&appFlags{tls: true}, true, false, ""},
{&appFlags{tlsInsecure: true}, true, false, ""},
{&appFlags{tlsCAFile: "acctest/x509/certificate.pem"}, true, false, ""},
{&appFlags{tlsCAPath: "acctest/x509"}, true, false, ""},
// fail
{&appFlags{tlsCAFile: "acctest/key.pem"}, false, true, "should fail, acctest/key.pem is not a valid certificate"},
{&appFlags{tlsCAFile: "123098.pem"}, false, true, "should fail, 123098.pem does not exist"},
{&appFlags{tls: true, tlsInsecure: true}, false, true, "only one tls option should be specified"},
{&appFlags{tls: true, tlsCAFile: "acctest/x509/certificate.pem"}, false, true, "only one tls option should be specified"},
{&appFlags{tlsInsecure: true, tlsCAFile: "acctest/x509/certificate.pem"}, false, true, "only one tls option should be specified"},
{&appFlags{tlsCAFile: "acctest/x509/certificate.pem", tlsCAPath: "acctest/x509"}, false, true, "only one tls option should be specified"},
}
for _, tt := range dataset {
// when
creds, err := parseCredentials(tt.flags)
// then
if tt.credsReturned {
assert.NotNil(t, creds, tt.message)
} else {
assert.Nil(t, creds, tt.message)
}
if tt.errorReturned {
assert.Error(t, err, tt.message)
} else {
assert.NoError(t, err, tt.message)
}
}
}
func Test_countTLSFlags(t *testing.T) {
// given
dataset := []struct {
flags *appFlags
count int
}{
{&appFlags{}, 0},
{&appFlags{tls: true}, 1},
{&appFlags{tls: true, tlsInsecure: true}, 2},
{&appFlags{tls: true, tlsInsecure: true, tlsCAFile: "file"}, 3},
{&appFlags{tls: true, tlsInsecure: true, tlsCAFile: "file", tlsCAPath: "path"}, 4},
}
for _, tt := range dataset {
// when
cnt := countTLSFlags(tt.flags)
// then
assert.Equal(t, tt.count, cnt)
}
}