-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpapi_interface_mock.go
122 lines (101 loc) · 2.72 KB
/
httpapi_interface_mock.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
package autoscaler
import (
"fmt"
"math/rand"
"strconv"
"github.com/satori/go.uuid"
)
type AdminAPIMock struct {
internalInfo map[string]NodeInfo
internalReq map[string]Fakeplog
Health []*NodeInfo
}
type Fakeplog struct {
status string
purpose string
ip string
}
func NewAdminAPIHttpMock() AdminAPI {
ii := make(map[string]NodeInfo)
ir := make(map[string]Fakeplog)
h := []*NodeInfo{}
aam := AdminAPIMock{ii, ir, h}
return &aam
}
func (aa *AdminAPIMock) CreateHN() (string, error) {
u1 := uuid.NewV4().String()
aa.internalReq[u1] = Fakeplog{"pending", "create", ""}
return u1, nil
}
func (aa *AdminAPIMock) DeleteHN(ip string) (string, error) {
u1 := uuid.NewV4().String()
aa.internalReq[u1] = Fakeplog{"pending", "delete", ip}
return u1, nil
}
func (aa *AdminAPIMock) GetHNInfo(filter string) ([]*NodeInfo, int, int, int, error) {
return aa.Health, 0, 0, 0, nil
}
func (aa *AdminAPIMock) ReqProgress(token string) (*Progress, error) {
fl := aa.internalReq[token]
p := Progress{fl.status, ""}
return &p, nil
}
func (aa *AdminAPIMock) completeCreates() {
for k, v := range aa.internalReq {
//Pending -> Created
if v.status == "pending" && v.purpose == "create" {
v.status = "success"
iplast := rand.Intn(200)
ip := "10.0.0." + strconv.Itoa(iplast)
newNode := NodeInfo{ip, *nodeTypeRamTotal, 1000, 1000 / 256, StatusInfo{false, false, false, false, false}}
//Adding new Node
aa.Health = append(aa.Health, &newNode)
aa.internalInfo[ip] = newNode
}
aa.internalReq[k] = v
}
}
func (aa *AdminAPIMock) completeDeletes() {
for k, v := range aa.internalReq {
//Pending -> Deleted
if v.status == "pending" && v.purpose == "delete" {
v.status = "success"
delete(aa.internalInfo, v.ip)
aa.rebuildHealthMinus(v.ip)
fmt.Println(aa.Health)
}
aa.internalReq[k] = v
}
}
func (aa *AdminAPIMock) rebuildHealthMinus(ip string) {
var newHealth []*NodeInfo
for _, e := range aa.Health {
if e.NodeIp != ip {
newHealth = append(newHealth, e)
}
}
aa.Health = newHealth
}
func (aa *AdminAPIMock) modifyLoad(ip string, value int64) {
var newHealth []*NodeInfo
for _, e := range aa.Health {
if e.NodeIp == ip {
e.RamUsed = value
newHealth = append(newHealth, e)
}
}
aa.Health = newHealth
}
func (aa *AdminAPIMock) populateHealth(load int64, numNodes int) {
newHealth := []*NodeInfo{}
newInternalInfo := make(map[string]NodeInfo)
for i := 0; i < numNodes; i++ {
iplast := rand.Intn(200)
ip := "10.0.0." + strconv.Itoa(iplast)
newNode := NodeInfo{ip, *nodeTypeRamTotal, load, load / 256, StatusInfo{false, false, false, false, false}}
newHealth = append(newHealth, &newNode)
newInternalInfo[ip] = newNode
}
aa.Health = newHealth
aa.internalInfo = newInternalInfo
}