-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathaci_util.go
134 lines (109 loc) · 2.63 KB
/
aci_util.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
package systemtests
import (
"bytes"
"encoding/json"
"errors"
log "github.com/Sirupsen/logrus"
"io/ioutil"
"net/http"
"strings"
"time"
)
// EPSpec for aci-gw
type EPSpec struct {
Tenant string `json:"tenant,omitempty"`
App string `json:"app-prof,omitempty"`
Epg string `json:"epg,omitempty"`
EpMac string `json:"epmac,omitempty"`
}
// EPResp from aci-gw
type EPResp struct {
Result string `json:"result,omitempty"`
Msg string `json:"msg,omitempty"`
IP string `json:"ip,omitempty"`
Vlan string `json:"vlan,omitempty"`
}
func aciHTTPGet(url string, jin, jout interface{}) error {
buf, err := json.Marshal(jin)
if err != nil {
return err
}
body := bytes.NewBuffer(buf)
r, err := http.Post(url, "application/json", body)
if err != nil {
return err
}
defer r.Body.Close()
switch {
case r.StatusCode == int(404):
return errors.New("Page not found!")
case r.StatusCode == int(403):
return errors.New("Access denied!")
case r.StatusCode == int(500):
response, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return errors.New(string(response))
case r.StatusCode != int(200):
log.Errorf("GET Status '%s' status code %d \n", r.Status, r.StatusCode)
return errors.New(r.Status)
}
response, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if err := json.Unmarshal(response, jout); err != nil {
return err
}
return nil
}
// GetEPFromAPIC checks learning
func GetEPFromAPIC(tenant, app, epg, mac string) (string, string, error) {
ep := &EPSpec{Tenant: tenant,
App: app,
Epg: epg,
EpMac: mac,
}
resp := &EPResp{}
err := aciHTTPGet("http://localhost:5000/getEndpoint", ep, resp)
if err == nil {
if resp.Result == "success" {
log.Infof("resp: %+v", resp)
return resp.IP, resp.Vlan, nil
}
return resp.IP, resp.Vlan, errors.New(resp.Result)
}
log.Errorf("err: %v", err)
return "", "", err
}
func (s *systemtestSuite) checkACILearning(tenant, app, epg string, containers []*container) error {
ip := ""
for _, c := range containers {
mac, err := c.node.exec.getMACAddr(c, "eth0")
mac = strings.ToUpper(mac)
if err != nil {
return err
}
containerIP, err := c.node.exec.getIPAddr(c, "eth0")
if err != nil {
return err
}
log.Infof("Checking IP %s and MAC %s learned on ACI...", containerIP, mac)
for ix := 0; ix < 20; ix++ {
ip, _, err = GetEPFromAPIC(tenant, app, epg, mac)
if err == nil {
break
}
time.Sleep(2 * time.Second)
}
if err != nil {
return err
}
if ip != containerIP {
log.Errorf("ip from apic: %s from container: %s", ip, containerIP)
return errors.New("ip mismatch")
}
}
return nil
}