-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaws_test.go
195 lines (168 loc) · 5.43 KB
/
aws_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package backend
import (
"os"
"testing"
)
func TestCheckInstanceType(t *testing.T) {
// init the test config
os.Setenv("POWER_TOGGLE_AWS_IGNORE_INSTANCE_TYPES", "c5d.18xlarge c5d.9xlarge")
ConfigInit("../testdata/sampleconfig/power-toggle-config.yaml", false)
testCases := map[string]bool{
"c5d.18xlarge": false,
"c5d.9xlarge": false,
"c5.18xlarge": true,
"t2.medium": true,
}
for input, expectedResult := range testCases {
if checkInstanceType(input) != expectedResult {
t.Errorf("input(%s) != expectedResult(%v)", input, expectedResult)
}
}
}
func TestValidateEnvName(t *testing.T) {
// init the test config
os.Setenv("POWER_TOGGLE_AWS_IGNORE_ENVIRONMENTS", "ignoredEnv1 ignoredEnv2")
ConfigInit("../testdata/sampleconfig/power-toggle-config.yaml", false)
testCases := map[string]bool{
"ignoredEnv1": false,
"ignoredEnv2": false,
"env3": true,
"env4": true,
}
for input, expectedResult := range testCases {
if validateEnvName(input) != expectedResult {
t.Errorf("input(%s) != expectedResult(%v)", input, expectedResult)
}
}
}
func TestGetEnvironmentById(t *testing.T) {
if err := resetMockData(); err != nil {
t.Fatalf("mockRefreshTable failed: %v", err)
}
envID := "356f6265efcc"
if len(cachedTable) == 0 {
t.Fatalf("cachedTable is of 0 size!")
}
env, found := getEnvironmentByID(envID)
if !found {
t.Errorf("unable to retrieve test env %s", envID)
}
if env.Name != "mockenv1" && len(env.Instances) < 1 {
t.Errorf("unexpected env retrieved")
}
if _, found := getEnvironmentByID("incorrectEnvId"); found {
t.Errorf("getEnvironmentByID reported found for an invalid env id")
}
}
func TestUpdateEnvDetails(t *testing.T) {
if err := resetMockData(); err != nil {
t.Fatalf("mockRefreshTable failed: %v", err)
}
// ensure our mocked json initial state is correct
startingInstanceState := cachedTable[1].Instances[1].State
if startingInstanceState != "stopped" {
t.Error("our moc json has unexpected init values. Has it changed?")
}
startingEnvState := cachedTable[1].State
if startingEnvState != "stopped" {
t.Error("our moc json has unexpected init values. Has it changed?")
}
// set an instance in this env to running, we should see env state change
cachedTable[1].Instances[1].State = "running"
updateEnvDetails()
if cachedTable[1].State != EnvStateMixed {
t.Errorf("unexpected env state: got %s expected %s", cachedTable[1].State, EnvStateMixed)
}
// set an instance in this env to pending, we should see env state change
cachedTable[1].Instances[1].State = "pending"
updateEnvDetails()
if cachedTable[1].State != EnvStateChanging {
t.Errorf("unexpected env state: got %s expected %s", cachedTable[1].State, EnvStateChanging)
}
// set all instances to running, we should see env state change
for i := range cachedTable[1].Instances {
cachedTable[1].Instances[i].State = "running"
}
updateEnvDetails()
if cachedTable[1].State != EnvStateRunning {
t.Errorf("unexpected env state: got %s expected %s", cachedTable[1].State, EnvStateRunning)
}
// set all instances to stopped, we should see env state change
for i := range cachedTable[1].Instances {
cachedTable[1].Instances[i].State = "stopped"
}
updateEnvDetails()
if cachedTable[1].State != EnvStateStopped {
t.Errorf("unexpected env state: got %s expected %s", cachedTable[1].State, EnvStateStopped)
}
// ensure that our counts are functioning correctly
currentTotalCPUCount := cachedTable[1].TotalVCPU
cachedTable[1].Instances[1].VCPU++
updateEnvDetails()
if (currentTotalCPUCount + 1) != cachedTable[1].TotalVCPU {
t.Errorf("Total vCPU count has an unexpected value: %d", cachedTable[1].TotalVCPU)
}
}
func TestToggleInstance(t *testing.T) {
if err := resetMockData(); err != nil {
t.Fatalf("mockRefreshTable failed: %v", err)
}
// test toggleInstance
for desiredState, actualState := range map[string]string{
"stop": "stopped",
"start": "running",
} {
_, err := toggleInstance(cachedTable[1].Instances[1].ID, desiredState)
if err != nil || cachedTable[1].Instances[1].State != actualState {
t.Errorf("go %s but wanted %s. Err: %v", cachedTable[1].Instances[1].State, actualState, err)
}
}
}
func TestGetAWSInstanceId(t *testing.T) {
if err := resetMockData(); err != nil {
t.Fatalf("mockRefreshTable failed: %v", err)
}
if getAWSInstanceID(cachedTable[1].Instances[1].ID) != cachedTable[1].Instances[1].InstanceID {
t.Errorf("getAWSInstanceID is not returning correct id")
}
}
func TestEnvStartStop(t *testing.T) {
// disabled mock delays and chance of errors
unitTestRunning = true
if err := resetMockData(); err != nil {
t.Fatalf("mockRefreshTable failed: %v", err)
}
envID := "4f9f1afb29f1"
_, err := startupEnv(envID)
if err != nil {
t.Errorf("startupEnv return and error: %v", err)
}
updateEnvDetails()
state, _ := getEnvState(envID)
if state != "running" {
t.Errorf("test env is not in running state: %s", state)
}
_, err = shutdownEnv(envID)
if err != nil {
t.Errorf("startupEnv return and error: %v", err)
}
updateEnvDetails()
state, _ = getEnvState(envID)
if state != "stopped" {
t.Errorf("test env is not in stopped state: %s", state)
}
}
func resetMockData() error {
// disabled mock delays and chance of errors
unitTestRunning = true
mockEnabled = true
cachedTable = cachedTable[:0]
return mockRefreshTable()
}
func getEnvState(envID string) (string, bool) {
env, found := getEnvironmentByID(envID)
if !found {
return "", found
}
return env.State, found
}