-
Notifications
You must be signed in to change notification settings - Fork 10
/
jtimon_test.go
141 lines (115 loc) · 2.62 KB
/
jtimon_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"testing"
"time"
js "github.com/Juniper/jtimon/jtisim"
lps "github.com/Juniper/jtimon/lpserver"
flag "github.com/spf13/pflag"
)
func TestMain(m *testing.M) {
flag.Parse()
go func() {
host := "127.0.0.1"
port := 50051
jtisim := js.NewJTISim(host, int32(port), false, "tests/simulator/desc")
if err := jtisim.Start(); err != nil {
log.Printf("can not start jti simulator: %v", err)
}
}()
go func() {
s := lps.NewLPServer("127.0.0.1", 50052)
s.StartServer()
}()
fmt.Println("TestMain - waiting 3s for JTISIM and Influx Store to come up")
time.Sleep(3 * time.Second)
fmt.Println("TestMain - done waiting")
os.Exit(m.Run())
}
func compareResults(jctx *JCtx) error {
testRes := jctx.testRes
if testRes == nil {
return fmt.Errorf("testRes is nil")
}
ret, err := testRes.Seek(0, 0)
if ret != 0 {
return fmt.Errorf("can not seek testRes")
}
if err != nil {
return err
}
testExp, err := os.Open(jctx.file + ".testexp")
if err != nil {
return err
}
defer testExp.Close()
r, err := ioutil.ReadAll(testRes)
if err != nil {
return err
}
e, err := ioutil.ReadAll(testExp)
if err != nil {
return err
}
if !bytes.Equal(r, e) {
return fmt.Errorf("test failed: content of %s does not match with content of %s",
jctx.file+".testres", jctx.file+".testexp")
}
return nil
}
type storeType int
const (
STOREOPEN = iota
STORECLOSE
)
func influxStore(host string, port int, op storeType, name string) error {
url := ""
switch op {
case STOREOPEN:
url = fmt.Sprintf("http://%s:%d/store/open", host, port)
case STORECLOSE:
url = fmt.Sprintf("http://%s:%d/store/close", host, port)
}
s := fmt.Sprintf(`{"name":"%s"}`, name)
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(s)))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
return nil
}
func prometheusCollect(host string, port int, jctx *JCtx) error {
var f *os.File
var err error
if f, err = os.Create(jctx.file + ".testres"); err != nil {
return err
}
defer f.Close()
url := fmt.Sprintf("http://%s:%d/metrics", host, port)
var resp *http.Response
if resp, err = http.Get(url); err != nil {
return err
}
defer resp.Body.Close()
var body []byte
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return err
}
strs := strings.Split(string(body), "\n")
for _, str := range strs {
if strings.HasPrefix(str, "_interfaces_interface") {
f.WriteString(str + "\n")
}
}
return nil
}