-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
204 lines (173 loc) · 5.31 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"testing"
)
var f5TokenResp = `{
"token": {
"token": "5C0F982A0BF37CBE5DE2CB8313102A494A4759E5704371B77D7E35ADBE4AAC33184EB3C5117D94FAFA054B7DB7F02539F6550F8D4FA25C4BFF1145287E93F70D"
}
}
`
func TestVersion(t *testing.T) {
if os.Getenv("TEST_USAGE") == "1" {
version()
// NOT REACHED
}
cmd := exec.Command(os.Args[0], "-test.run="+t.Name())
cmd.Env = append(os.Environ(), "TEST_USAGE=1")
stdoutBuf := new(bytes.Buffer)
cmd.Stdout = stdoutBuf
err := cmd.Run()
if err != nil {
t.Fatalf("version(): unexpected error %q", err.Error())
}
wantVersion := "crl2f5-connector.test - 1.0.0\n"
if gotVersion := stdoutBuf.String(); gotVersion != wantVersion {
t.Errorf("version(): got output %q; want %q", gotVersion, wantVersion)
}
}
func TestFatal(t *testing.T) {
stderrBuf := new(bytes.Buffer)
stderr = stderrBuf
exit = func(status int) {
if status != 1 {
t.Errorf("fatal(%q): got exit with status %d; want %d", "test", status, 1)
}
}
fatal("test")
want := "fatal: test\n"
if got := stderrBuf.String(); got != want {
t.Errorf("fatal(%q): got %q; want %q", "test", got, want)
}
}
func TestVerbose(t *testing.T) {
t.Run("Enabled", testVerboseWhenEnabled)
t.Run("Disabled", testVerboseWhenDisabled)
}
func testVerboseWhenEnabled(t *testing.T) {
b := true
verboseMode = &b
stdoutBuf := new(bytes.Buffer)
stdout = stdoutBuf
verbose("test")
want := "verbose: test\n"
if got := stdoutBuf.String(); got != want {
t.Errorf("verbose(%q): got %q; want %q", "test", got, want)
}
}
func testVerboseWhenDisabled(t *testing.T) {
var b bool
verboseMode = &b
stdoutBuf := new(bytes.Buffer)
stdout = stdoutBuf
verbose("test")
want := ""
if got := stdoutBuf.String(); got != want {
t.Errorf("verbose(%q): got %q; want %q", "test", got, want)
}
}
func TestInfo(t *testing.T) {
stdoutBuf := new(bytes.Buffer)
stdout = stdoutBuf
info("test")
want := "info: test\n"
if got := stdoutBuf.String(); got != want {
t.Errorf("info(%q): got %q; want %q", "test", got, want)
}
}
func TestInitF5Client(t *testing.T) {
t.Run("Happy Path With Basic Auth", testInitF5ClientHappyPathWithBasicAuth)
t.Run("Happy Path With Token Auth", testInitF5ClientHappyPathWithTokenAuth)
t.Run("Fail Token Auth", testInitF5ClientFailTokenAuth)
t.Run("Fail Unsupported Auth Method", testInitF5ClientUnsupportedAuthMethod)
}
func testInitF5ClientHappyPathWithBasicAuth(t *testing.T) {
cfg := f5Config{
AuthMethod: "basic",
URL: "http://localhost/bigip",
User: "admin",
Password: "admin",
}
f5Client, err := initF5Client(cfg)
if err != nil {
t.Fatalf("initF5Client(): unexpected error %q", err.Error())
}
req, err := f5Client.MakeRequest("GET", "/test", nil)
if err != nil {
t.Fatalf("initF5Client().MakeRequest(): unexpected error %q", err.Error())
}
if got := req.URL.String(); got != cfg.URL+"/test" {
t.Errorf("initF5Client().MakeRequest(): got url %q; want %q", got, cfg.URL+"/test")
}
wantAuthorization := "Basic YWRtaW46YWRtaW4="
if got := req.Header.Get("Authorization"); got != wantAuthorization {
t.Errorf("initF5Client().MakeRequest(): got authorization header %q; want %q", got, wantAuthorization)
}
}
func testInitF5ClientHappyPathWithTokenAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(f5TokenResp))
}))
defer ts.Close()
cfg := f5Config{
AuthMethod: "token",
URL: ts.URL,
User: "admin",
Password: "admin",
}
f5Client, err := initF5Client(cfg)
if err != nil {
t.Fatalf("initF5Client().MakeRequest(): unexpected error %q", err.Error())
}
req, err := f5Client.MakeRequest("GET", "/test", nil)
if err != nil {
t.Fatalf("initF5Client().MakeRequest(): unexpected error %q", err.Error())
}
if got := req.URL.String(); got != cfg.URL+"/test" {
t.Errorf("initF5Client().MakeRequest(): got url %q; want %q", got, cfg.URL+"/test")
}
wantToken := "5C0F982A0BF37CBE5DE2CB8313102A494A4759E5704371B77D7E35ADBE4AAC33184EB3C5117D94FAFA054B7DB7F02539F6550F8D4FA25C4BFF1145287E93F70D"
if got := req.Header.Get("X-F5-Auth-Token"); got != wantToken {
t.Fatalf("initF5Client().MakeRequest(): got X-F5-Auth-Token header %q; want %q", got, wantToken)
}
}
func testInitF5ClientFailTokenAuth(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "some error", http.StatusBadRequest)
}))
defer ts.Close()
cfg := f5Config{
AuthMethod: "token",
URL: ts.URL,
User: "admin",
Password: "admin",
}
_, err := initF5Client(cfg)
if err == nil {
t.Fatal("initF5Client(): expected error, got nil")
}
wantErr := "failed to create token client (token negociation failed): http status 400 Bad Request"
if got := err.Error(); got != wantErr {
t.Errorf("initF5Client(): got error %q; want %q", got, wantErr)
}
}
func testInitF5ClientUnsupportedAuthMethod(t *testing.T) {
authMethod := "something else that does not exist"
cfg := f5Config{
AuthMethod: authMethod,
}
_, err := initF5Client(cfg)
if err == nil {
t.Fatal("initF5Client(): expected error, got nil")
}
wantErr := fmt.Sprintf("unsupported auth method \"%s\"", authMethod)
if got := err.Error(); got != wantErr {
t.Errorf("initF5Client(): got error %q; want %q", got, wantErr)
}
}