-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmock.go
149 lines (140 loc) · 4.3 KB
/
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package backend
import (
"fmt"
"math/rand"
"time"
"github.com/spf13/viper"
)
// unitTestRunning lets use know if a unit test is running
// currently, it will disable introduced delays and chance of mocked errors
var unitTestRunning = false
// mock of shutdownEnv
func mockShutdownEnv(envID string) (response []byte, err error) {
// introduce delays and possible error
err = mockDelayWithPossibleError()
if err != nil {
response = []byte(fmt.Sprintf(`{"error":"%v"}`, err))
log.Errorf("mock error envID: %s: %s", envID, err)
return
}
instanceIds := getInstanceIDs(envID, "running")
if len(instanceIds) > maxInstancesToShutdown {
err = fmt.Errorf("SAFETY: env [%s] has too many associated instances to shutdown %d", envID, len(instanceIds))
log.Debugf("SAFETY: instances: %v", instanceIds)
} else if len(instanceIds) > 0 {
// set all instances to stopped
for e, env := range cachedTable {
if env.ID == envID {
for i := range cachedTable[e].Instances {
cachedTable[e].Instances[i].State = "stopped"
}
break
}
}
// MOCK BILLING: update toggled off instances map
if experimentalEnabled {
putToggledOffInstanceIDs(instanceIds)
}
response = []byte(`{"mock": "OK"}`)
log.Infof("MOCK: successfully stopped env %s", envID)
} else {
err = fmt.Errorf("MOCK: env [%s] has no associated instances", envID)
log.Errorf("env [%s] has no associated instances", envID)
}
return
}
// mock of startupEnv
func mockStartupEnv(envID string) (response []byte, err error) {
// introduce delays and possible error
err = mockDelayWithPossibleError()
if err != nil {
response = []byte(fmt.Sprintf(`{"error":"%v"}`, err))
log.Errorf("mock error envID: %s: %s", envID, err)
return
}
instanceIds := getInstanceIDs(envID, "stopped")
if len(instanceIds) > 0 {
// set all instances to running
for e, env := range cachedTable {
if env.ID == envID {
for i := range cachedTable[e].Instances {
cachedTable[e].Instances[i].State = "running"
}
break
}
}
// MOCK BILLING: update toggled off instances map
if experimentalEnabled {
deleteToggledOffInstanceIDs(instanceIds)
}
response = []byte(`{"mock": "OK"}`)
log.Infof("MOCK: successfully started env %s", envID)
} else {
err = fmt.Errorf("MOCK: env [%s] has no associated instances", envID)
log.Errorf("MOCK: env [%s] has no associated instances", envID)
}
return
}
// mock of toggleInstance
func mockToggleInstance(id, desiredState string) (response []byte, err error) {
// validate desiredState
if desiredState != "start" && desiredState != "stop" {
err = fmt.Errorf("invalid desired state: %s", desiredState)
return
}
// introduce delays and possible error
err = mockDelayWithPossibleError()
if err != nil {
response = []byte(fmt.Sprintf(`{"error":"%s"}`, err))
log.Errorf("mock error instance id: %s: %s", id, err)
return
}
// get the AWS instance id
awsInstanceID := getAWSInstanceID(id)
if awsInstanceID != "" {
// set instance to desired state
for e, env := range cachedTable {
for i, instance := range env.Instances {
if instance.ID == id {
switch desiredState {
case "start":
cachedTable[e].Instances[i].State = "running"
// MOCK BILLING: update toggled off instances map
if experimentalEnabled {
deleteToggledOffInstanceIDs([]string{instance.InstanceID})
}
case "stop":
cachedTable[e].Instances[i].State = "stopped"
// MOCK BILLING: update toggled off instances map
if experimentalEnabled {
putToggledOffInstanceIDs([]string{instance.InstanceID})
}
}
break
}
}
}
response = []byte(`{"mock": "OK"}`)
} else {
err = fmt.Errorf("no mapping found between internal id (%s) and aws instance id", id)
}
return
}
// mockDelayWithPossibleError will add a delay and possibly return an error.
// This is done to simulate real world delays and issues to aid in web UI development
func mockDelayWithPossibleError() (err error) {
// if we are doing unit tests, this should be disabled
if unitTestRunning {
return
}
// random delay betwen 100-2100 ms
r := rand.Intn(2000) + 100
if viper.GetBool("mock.delay") {
time.Sleep(time.Duration(r) * time.Millisecond)
}
// 1/4 chance of producing an error
if r%4 == 0 && viper.GetBool("mock.errors") {
err = fmt.Errorf("MOCK: Fate has thrown you an error")
}
return
}