This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
list_instances_steps.go
146 lines (128 loc) · 4.5 KB
/
list_instances_steps.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
// Copyright 2016-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package e2einstancesteps
import (
"io/ioutil"
"net/http"
"github.com/blox/blox/cluster-state-service/internal/features/wrappers"
. "github.com/gucumber/gucumber"
)
const (
invalidStatus = "invalidStatus"
invalidCluster = "cluster/cluster"
badRequestHTTPResponse = "400 Bad Request"
listInstancesBadRequest = "ListInstancesBadRequest"
)
func init() {
cssWrapper := wrappers.NewCSSWrapper()
When(`^I list instances$`, func() {
cssInstances, err := cssWrapper.ListInstances()
if err != nil {
T.Errorf(err.Error())
return
}
for _, i := range cssInstances {
cssContainerInstanceList = append(cssContainerInstanceList, *i)
}
})
Then(`^the list instances response contains all the registered instances$`, func() {
// cssContainerInstanceList can have instances from other clusters too
if len(cssContainerInstanceList) < len(ecsContainerInstanceList) {
T.Errorf("Unexpected number of instances in the list instances response. ")
return
}
for _, ecsInstance := range ecsContainerInstanceList {
err := ValidateListContainsInstance(ecsInstance, cssContainerInstanceList)
if err != nil {
T.Errorf(err.Error())
return
}
}
})
When(`^I list instances with cluster filter set to the ECS cluster name$`, func() {
clusterName := wrappers.GetClusterName()
cssInstances, err := cssWrapper.FilterInstancesByClusterName(clusterName)
if err != nil {
T.Errorf(err.Error())
return
}
for _, i := range cssInstances {
cssContainerInstanceList = append(cssContainerInstanceList, *i)
}
})
Then(`^the list instances response contains all the instances registered with the cluster$`, func() {
for _, ecsInstance := range ecsContainerInstanceList {
found := false
for _, cssInstance := range cssContainerInstanceList {
// Since the CSS retains INACTIVE instances till the reconciler re-runs, we only look at the ACTIVE instances.
if *cssInstance.Entity.Status == "ACTIVE" && *ecsInstance.ContainerInstanceArn == *cssInstance.Entity.ContainerInstanceARN {
found = true
break
}
}
if !found {
T.Errorf("Cannot find container instance %s in the list instance response.", *ecsInstance.ContainerInstanceArn)
return
}
}
for _, ecsInstance := range ecsContainerInstanceList {
err := ValidateListContainsInstance(ecsInstance, cssContainerInstanceList)
if err != nil {
T.Errorf(err.Error())
return
}
}
})
When(`^I try to list instances with an invalid status filter$`, func() {
exceptionList = nil
exceptionMsg, exceptionType, err := cssWrapper.TryListInstancesWithInvalidStatus(invalidStatus)
if err != nil {
T.Errorf(err.Error())
return
}
exceptionList = append(exceptionList, Exception{exceptionType: exceptionType, exceptionMsg: exceptionMsg})
})
When(`^I try to list instances with an invalid cluster filter$`, func() {
exceptionList = nil
exceptionMsg, exceptionType, err := cssWrapper.TryListInstancesWithInvalidCluster(invalidCluster)
if err != nil {
T.Errorf(err.Error())
return
}
exceptionList = append(exceptionList, Exception{exceptionType: exceptionType, exceptionMsg: exceptionMsg})
})
When(`^I try to list instances with redundant filters$`, func() {
url := "http://localhost:3000/v1/instances?cluster=cluster1&cluster=cluster2"
resp, err := http.Get(url)
if err != nil {
T.Errorf(err.Error())
return
}
exceptionList = nil
var exceptionType string
if resp.Status == badRequestHTTPResponse {
exceptionType = listInstancesBadRequest
} else {
T.Errorf("Unknown exception type '%s' when trying to list instances with redundant filters", resp.Status)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
T.Errorf("Error reading expection message when trying to list instances with redundant filters")
return
}
exceptionMsg := string(body)
exceptionList = append(exceptionList, Exception{exceptionType: exceptionType, exceptionMsg: exceptionMsg})
})
}