forked from FeLvi-zzz/tentez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_test.go
172 lines (158 loc) · 4.09 KB
/
apply_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
package tentez
import (
"bytes"
"context"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
)
type targetsMock []targetMock
type targetMock struct {
Name string
CurrentWeight Weight
}
func (t targetMock) getName() string {
return t.Name
}
func (t targetMock) execSwitch(targetWeight Weight, isForce bool, cfg Config) error {
if isForce {
return nil
}
if t.CurrentWeight.CalcOldRatio() < targetWeight.CalcOldRatio() {
return SkipSwitchError{"the old weight target is larger than current one."}
}
if t.CurrentWeight.CalcNewRatio() > targetWeight.CalcNewRatio() {
return SkipSwitchError{"the new weight target is smaller than current one."}
}
return nil
}
func (t targetsMock) targetsSlice() []Target {
res := []Target{}
for _, v := range t {
res = append(res, v)
}
return res
}
func (t targetsMock) fetchData(cfg Config) (TargetsData, error) {
return []Target{}, nil
}
type elbv2Mock struct {
ModifyListenerError error
ModifyRuleError error
DescribeRulesError error
DescribeListenersError error
}
func NewDummyActions() []types.Action {
return []types.Action{
{
ForwardConfig: &types.ForwardActionConfig{
TargetGroups: []types.TargetGroupTuple{
{
TargetGroupArn: aws.String("oldTarget"),
Weight: aws.Int32(50),
},
{
TargetGroupArn: aws.String("newTarget"),
Weight: aws.Int32(50),
},
},
},
},
}
}
func (m elbv2Mock) ModifyListener(ctx context.Context, params *elbv2.ModifyListenerInput, optFns ...func(*elbv2.Options)) (*elbv2.ModifyListenerOutput, error) {
return &elbv2.ModifyListenerOutput{}, m.ModifyListenerError
}
func (m elbv2Mock) ModifyRule(ctx context.Context, params *elbv2.ModifyRuleInput, optFns ...func(*elbv2.Options)) (*elbv2.ModifyRuleOutput, error) {
return &elbv2.ModifyRuleOutput{}, m.ModifyRuleError
}
func (m elbv2Mock) DescribeRules(ctx context.Context, params *elbv2.DescribeRulesInput, optFns ...func(*elbv2.Options)) (*elbv2.DescribeRulesOutput, error) {
return &elbv2.DescribeRulesOutput{
Rules: []types.Rule{
{
RuleArn: ¶ms.RuleArns[0],
Actions: NewDummyActions(),
},
},
}, m.DescribeRulesError
}
func (m elbv2Mock) DescribeListeners(ctx context.Context, params *elbv2.DescribeListenersInput, optFns ...func(*elbv2.Options)) (*elbv2.DescribeListenersOutput, error) {
return &elbv2.DescribeListenersOutput{
Listeners: []types.Listener{
{
ListenerArn: ¶ms.ListenerArns[0],
DefaultActions: NewDummyActions(),
},
},
}, m.DescribeListenersError
}
func (m elbv2Mock) DescribeTargetGroups(ctx context.Context, params *elbv2.DescribeTargetGroupsInput, optFns ...func(*elbv2.Options)) (*elbv2.DescribeTargetGroupsOutput, error) {
return &elbv2.DescribeTargetGroupsOutput{}, m.DescribeListenersError
}
func TestExecSwitch(t *testing.T) {
cases := []struct {
Name string
IsForce bool
IsError bool
Targets map[TargetType]Targets
TargetWeight Weight
}{
{
Name: "success",
IsError: false,
IsForce: false,
Targets: map[TargetType]Targets{
"targets_mock": targetsMock([]targetMock{
{
Name: "target_mock",
CurrentWeight: Weight{
Old: 50,
New: 50,
},
},
}),
},
TargetWeight: Weight{
Old: 0,
New: 100,
},
},
{
Name: "skip",
IsError: false,
IsForce: false,
Targets: map[TargetType]Targets{
"targets_mock": targetsMock([]targetMock{
{
Name: "target_mock",
CurrentWeight: Weight{
Old: 0,
New: 100,
},
},
}),
},
TargetWeight: Weight{
Old: 100,
New: 0,
},
},
}
for _, c := range cases {
targets := c.Targets
err := execSwitch(targets, c.TargetWeight, c.IsForce, Config{
client: Client{
elbv2: elbv2Mock{},
},
io: IOStreams{
in: bytes.NewBufferString(""),
out: bytes.NewBufferString(""),
err: bytes.NewBufferString(""),
},
})
if err != nil {
t.Errorf("expected no error, but throw %v", err)
}
}
}