forked from nleiva/xrgrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
58 lines (54 loc) · 1.4 KB
/
config_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
package xrgrpc_test
import (
"net"
"os"
"strings"
"testing"
xr "github.com/nleiva/xrgrpc"
)
const (
defaultFile = "example/input/config.json"
wrongFile = "example/input/config2.json"
wrongFileErr = "could not open the file"
)
func TestDecodeJSONConfig(t *testing.T) {
targets := xr.NewDevices()
tt := []struct {
name string
file string
err string
}{
{name: "local file", file: defaultFile},
{name: "wrong file", file: wrongFile, err: wrongFileErr},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
err := xr.DecodeJSONConfig(targets, tc.file)
if err != nil {
if strings.Contains(err.Error(), wrongFileErr) && tc.err == wrongFileErr {
return
}
t.Fatalf("could not open the file: %s; %v", tc.file, err)
}
for _, router := range targets.Routers {
if router.User == "" {
t.Fatalf("invalid username for %v", router.Host)
}
if router.Password == "" {
t.Fatalf("invalid password for %v", router.Host)
}
_, err := net.ResolveTCPAddr("tcp", router.Host)
if err != nil {
t.Fatalf("%v is not a valid target: %v", router.Host, err)
}
creds := strings.Replace(router.Cert, "..", "example", 1)
if _, err := os.Stat(creds); os.IsNotExist(err) {
t.Fatalf("%v cert file not found for %v", router.Host, err)
}
if !(router.Timeout > 0) {
t.Fatalf("invalid timeout for %v", router.Host)
}
}
})
}
}