-
Notifications
You must be signed in to change notification settings - Fork 0
/
nats_shared_test.go
89 lines (69 loc) · 1.45 KB
/
nats_shared_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
package discovery
import (
"fmt"
"net"
"net/url"
"testing"
"time"
)
var NatsTestURLs = []string{
"nats://nats:4222",
"nats://localhost:4222",
}
var NatsAuthTestURLs = []string{
"nats://nats-auth:4222",
"nats://localhost:4223",
}
var tokenExchangeURLs = []string{
"http://api-server:8080/api",
"http://localhost:8080/api",
}
// SkipWithoutNats Skips a test if NATS is not available
func SkipWithoutNats(t *testing.T) {
var err error
for _, url := range NatsTestURLs {
err = testURL(url)
if err == nil {
return
}
}
if err != nil {
t.Error(err)
t.Skip("NATS not available")
}
}
// SkipWithoutNatsAuth Skips a test if authenticated NATS is not available
func SkipWithoutNatsAuth(t *testing.T) {
var err error
for _, url := range NatsAuthTestURLs {
err = testURL(url)
if err == nil {
return
}
}
if err != nil {
t.Error(err)
t.Skip("NATS not available")
}
}
func GetWorkingTokenExchange() (string, error) {
var err error
for _, url := range tokenExchangeURLs {
if err = testURL(url); err == nil {
return url, nil
}
}
return "", fmt.Errorf("no working token exchanges found: %w", err)
}
func testURL(testURL string) error {
url, err := url.Parse(testURL)
if err != nil {
return fmt.Errorf("could not parse NATS URL: %v. Error: %w", testURL, err)
}
conn, err := net.DialTimeout("tcp", net.JoinHostPort(url.Hostname(), url.Port()), time.Second)
if err == nil {
conn.Close()
return nil
}
return err
}