forked from stojg/possum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_test.go
More file actions
189 lines (159 loc) · 4.96 KB
/
db_test.go
File metadata and controls
189 lines (159 loc) · 4.96 KB
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
package possum
import (
"context"
"testing"
"time"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/rds/rdsiface"
)
func TestGetDBInstances(t *testing.T) {
tests := []struct {
instance *rds.DBInstance
hasSchema bool
}{
{&rds.DBInstance{DBInstanceStatus: aws.String("available"), DBInstanceIdentifier: aws.String("a")}, true},
{&rds.DBInstance{DBInstanceStatus: aws.String("available"), DBInstanceIdentifier: aws.String("b")}, true},
{&rds.DBInstance{DBInstanceStatus: aws.String("available"), DBInstanceIdentifier: aws.String("c")}, false},
{nil, false},
}
for _, test := range tests {
client := &mockRDSClient{}
if test.instance != nil {
client.describeDBInstancesResult = []*rds.DBInstance{test.instance}
}
if test.hasSchema {
client.listTagsForResource = []*rds.Tag{{Key: aws.String(scheduleTag), Value: aws.String("value")}}
}
list, err := getDBInstances(context.Background(), client)
if err != nil {
t.Error(err)
continue
}
if test.instance == nil {
if len(list) > 0 {
t.Errorf("Did not expect to get non empty list on empty search result")
}
continue
}
if !test.hasSchema {
if len(list) > 0 {
t.Errorf("Didnt expect schemaless dbinstances to be in list")
continue
}
continue
}
if test.instance.DBInstanceIdentifier != list[0].resource.DBInstanceIdentifier {
t.Errorf("expected %s, got %s", *test.instance.DBInstanceIdentifier, *list[0].resource.DBInstanceIdentifier)
}
//for i := range output {
// if *list[i].resource.DBName != *output[i].DBName {
// t.Errorf("Expected db changes with ID %s, got %s", *output[i].DBName, *list[i].resource.DBName)
// return
// }
//}
}
}
func TestGetDBInstanceChanges(t *testing.T) {
chkTime := newWeekday(time.Monday, 12, 0)
always, err := NewPeriod("0:0", "23:59", AllWeekdays())
if err != nil {
t.Error(err)
return
}
never, err := NewPeriod("0:00", "0:00", []time.Weekday{})
if err != nil {
t.Error(err)
return
}
tests := []struct {
status string
period *Period
schedule string
expected ScheduledAction
}{
{"available", always, "n", NoopAction},
{"stopping", always, "n", NoopAction},
{"stopped", always, "n", StartAction},
{"available", never, "n", StopAction},
{"stopping", never, "n", NoopAction},
{"stopped", never, "n", NoopAction},
{"stopped", always, "x", NoopAction},
}
for i, test := range tests {
schedule := NewSchedule("n")
schedule.AddPeriod(time.Local.String(), test.period)
schedules := Schedules{schedule}
action := &dbInstanceSchedule{
resource: &rds.DBInstance{
DBInstanceIdentifier: aws.String(fmt.Sprintf("test-%d", i)),
DBInstanceStatus: aws.String(test.status),
},
schedule: test.schedule,
}
list := []*dbInstanceSchedule{action}
changes := getDBInstanceChanges(list, chkTime, schedules)
for _, change := range changes {
if change.Action != test.expected {
t.Errorf("case %d. expected %s, got %s", i+1, test.expected, change.Action)
}
}
}
}
func TestPerformDBInstanceChanges(t *testing.T) {
tests := []struct {
action ScheduledAction
expectedStarted int
expectedStopped int
}{
{StartAction, 1, 0},
{StopAction, 0, 1},
}
for i, test := range tests {
client := &mockRDSClient{}
change := Change{
ID: aws.String(fmt.Sprintf("test-%d", i)),
Action: test.action,
}
changes := Changes{change}
if err := performDBInstanceChanges(client, changes); err != nil {
t.Error(err)
return
}
if test.expectedStarted != client.startedInstances {
t.Errorf("expected %d db instances started, got %d", test.expectedStarted, client.startedInstances)
}
if test.expectedStopped != client.stoppedInstances {
t.Errorf("expected %d db instances stopped, got %d", test.expectedStopped, client.stoppedInstances)
}
}
}
type mockRDSClient struct {
rdsiface.RDSAPI
describeDBInstancesResult []*rds.DBInstance
listTagsForResource []*rds.Tag
startedInstances int
stoppedInstances int
}
func (m *mockRDSClient) DescribeDBInstancesPagesWithContext(ctx aws.Context, input *rds.DescribeDBInstancesInput, fnc func(*rds.DescribeDBInstancesOutput, bool) bool, options ...request.Option) error {
res := &rds.DescribeDBInstancesOutput{
DBInstances: m.describeDBInstancesResult,
}
fnc(res, true)
return nil
}
func (m *mockRDSClient) ListTagsForResourceWithContext(ctx aws.Context, input *rds.ListTagsForResourceInput, options ...request.Option) (*rds.ListTagsForResourceOutput, error) {
return &rds.ListTagsForResourceOutput{
TagList: m.listTagsForResource,
}, nil
}
func (m *mockRDSClient) StartDBInstance(*rds.StartDBInstanceInput) (*rds.StartDBInstanceOutput, error) {
m.startedInstances += 1
return &rds.StartDBInstanceOutput{}, nil
}
func (m *mockRDSClient) StopDBInstance(*rds.StopDBInstanceInput) (*rds.StopDBInstanceOutput, error) {
m.stoppedInstances += 1
return &rds.StopDBInstanceOutput{}, nil
}