-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
161 lines (145 loc) · 4 KB
/
client_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
package api_test
import (
"context"
"crypto/tls"
"flag"
"github.com/myENA/vsz-api"
"log"
"net"
"net/http"
"os"
"sync"
"testing"
"time"
)
var (
host string
port int
username string
password string
)
func init() {
flag.StringVar(&host, "host", "", "Host of VSZ to hit")
flag.IntVar(&port, "port", 7443, "Port # to use for API")
flag.StringVar(&username, "username", "", "Username of user to use for tests")
flag.StringVar(&password, "password", "", "Password of user to use for tests")
flag.Parse()
if host == "" {
log.Println("host cannot be empty")
os.Exit(1)
}
if 0 >= port {
log.Println("port must be >= 0")
os.Exit(1)
}
if username == "" {
log.Println("username cannot be empty")
os.Exit(1)
}
if password == "" {
log.Println("password cannot be empty")
os.Exit(1)
}
api.EnableDebug()
}
func testClient(t *testing.T) *api.Client {
client, err := api.NewClient(
api.DefaultConfig(host),
api.NewPasswordAuthenticator(username, password, 30*time.Minute),
&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableKeepAlives: true,
MaxIdleConnsPerHost: -1,
},
})
if err != nil {
t.Logf("Error creating client: %s", err)
t.FailNow()
}
return client
}
func TestClient(t *testing.T) {
var ctx context.Context
var cancel context.CancelFunc
client := testClient(t)
t.Run("Synchronous", func(t *testing.T) {
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
_, _, err := client.RuckusZones().RuckusWirelessApZoneRetrieveListGet(ctx, nil)
cancel()
if err != nil {
t.Logf("Error: %s", err)
t.FailNow()
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
_, _, err = client.Session().LoginSessionLogoffDelete(ctx)
cancel()
if err != nil {
t.Logf("Error: %s", err)
t.FailNow()
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
_, _, err = client.Session().LoginSessionRetrieveGet(ctx)
cancel()
if err != nil {
t.Logf("Error: %s", err)
t.FailNow()
}
})
// This is mostly here to find issues traceable with -race, and to test the robustness of your vsz setup :)
t.Run("Asynchronous", func(t *testing.T) {
start := make(chan struct{})
wg := new(sync.WaitGroup)
wg.Add(10)
for i := 0; i < 10; i++ {
go func(routine int, client *api.Client, start <-chan struct{}) {
var ctx context.Context
var cancel context.CancelFunc
var zones *api.RuckusWirelessApZoneRetrieveListGet200Response
var err error
<-start
for i := 0; i < 5; i++ {
if t.Failed() {
break
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
_, zones, err = client.RuckusZones().RuckusWirelessApZoneRetrieveListGet(ctx, nil)
cancel()
if err != nil {
t.Logf("Routine %d loop %d query 1 saw error: %+v", routine, i, err)
} else if zones == nil || len(zones.List) == 0 || *zones.TotalCount == 0 {
t.Logf("Routine %d loop %d returned an empty zone list, expected at least 1: %+v", routine, i, zones)
}
if routine == 0 {
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
_, _, err = client.Session().LoginSessionLogoffDelete(ctx)
cancel()
if err != nil {
t.Logf("Routine %d loop %d query 2 saw error: %+v", routine, i, err)
}
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
_, _, err = client.Session().LoginSessionRetrieveGet(ctx)
cancel()
if err != nil {
t.Logf("Routine %d loop %d query 3 saw error: %+v", routine, i, err)
}
}
wg.Done()
}(i, client, start)
}
close(start)
wg.Wait()
})
}