-
Notifications
You must be signed in to change notification settings - Fork 0
/
protection_test.go
211 lines (197 loc) · 4.85 KB
/
protection_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Package ecstp (ecs-task-protection) provides an easy function for enabling and disabling ECS
// task termination protection and can be called from inside an ECS task. See
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-scale-in-protection.html
package ecstp
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/stretchr/testify/assert"
)
type SuccessfulTestClient struct{}
func (c *SuccessfulTestClient) UpdateTaskProtection(
ctx context.Context, params *ecs.UpdateTaskProtectionInput, optFns ...func(*ecs.Options),
) (*ecs.UpdateTaskProtectionOutput, error) {
protectedTasks := make([]types.ProtectedTask, len(params.Tasks))
for i, task := range params.Tasks {
protectedTasks[i] = types.ProtectedTask{
TaskArn: &task,
ProtectionEnabled: params.ProtectionEnabled,
}
}
return &ecs.UpdateTaskProtectionOutput{
ProtectedTasks: protectedTasks,
}, nil
}
type FailureTestClient struct{}
func (c *FailureTestClient) UpdateTaskProtection(
ctx context.Context, params *ecs.UpdateTaskProtectionInput, optFns ...func(*ecs.Options),
) (*ecs.UpdateTaskProtectionOutput, error) {
failedTasks := make([]types.Failure, len(params.Tasks))
for i, task := range params.Tasks {
failedTasks[i] = types.Failure{
Arn: &task,
Reason: aws.String("failed"),
}
}
return &ecs.UpdateTaskProtectionOutput{
Failures: failedTasks,
}, nil
}
func TestClient_UpdateTaskProtection(t *testing.T) {
type fields struct {
ECSClient ECSClient
MetadataEndpointOverride string
}
type args struct {
ctx context.Context
input *UpdateTaskProtectionInput
}
tests := []struct {
name string
fields fields
args args
want *ecs.UpdateTaskProtectionOutput
wantErr bool
}{
{
name: "should return a successful response with protection enabled",
fields: fields{
ECSClient: &SuccessfulTestClient{},
},
args: args{
ctx: context.Background(),
input: &UpdateTaskProtectionInput{
Metadata: &MetadataBody{
TaskARN: "test",
},
Protect: true,
},
},
want: &ecs.UpdateTaskProtectionOutput{
ProtectedTasks: []types.ProtectedTask{
{
TaskArn: aws.String("test"),
ProtectionEnabled: true,
},
},
},
},
{
name: "should return a successful response with protection disabled",
fields: fields{
ECSClient: &SuccessfulTestClient{},
},
args: args{
ctx: context.Background(),
input: &UpdateTaskProtectionInput{
Metadata: &MetadataBody{
TaskARN: "test",
},
Protect: false,
},
},
want: &ecs.UpdateTaskProtectionOutput{
ProtectedTasks: []types.ProtectedTask{
{
TaskArn: aws.String("test"),
ProtectionEnabled: false,
},
},
},
},
{
name: "should return a response with protection failures",
fields: fields{
ECSClient: &FailureTestClient{},
},
args: args{
ctx: context.Background(),
input: &UpdateTaskProtectionInput{
Metadata: &MetadataBody{
TaskARN: "test",
},
},
},
want: &ecs.UpdateTaskProtectionOutput{
Failures: []types.Failure{
{
Arn: aws.String("test"),
Reason: aws.String("failed"),
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Client{
ECSClient: tt.fields.ECSClient,
MetadataEndpointOverride: tt.fields.MetadataEndpointOverride,
}
got, err := c.UpdateTaskProtection(tt.args.ctx, tt.args.input)
if assert.NoError(t, err) {
assert.Equal(t, tt.want, got)
}
})
}
}
func TestClient_GetTaskArn(t *testing.T) {
tests := []struct {
name string
want *MetadataBody
wantErr bool
}{
{
name: "should return a MetadataBody with test cluster and task ARN",
want: &MetadataBody{
Cluster: "test_cluster",
TaskARN: "test_arn",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"Cluster": "test_cluster", "TaskARN": "test_arn"}`)
}))
defer ts.Close()
c := &Client{
MetadataEndpointOverride: ts.URL,
}
got, err := c.GetTaskArn(context.Background())
if assert.NoError(t, err) {
assert.Equal(t, &MetadataBody{
Cluster: "test_cluster",
TaskARN: "test_arn",
}, got)
}
})
}
}
func TestNewClient(t *testing.T) {
type args struct {
ecsClient ECSClient
}
tests := []struct {
name string
args args
}{
{
name: "should test ECS client compatibility with ECSClient interface",
args: args{
ecsClient: ecs.New(ecs.Options{}),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = NewClient(tt.args.ecsClient)
})
}
}