This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
/
get_instance.go
118 lines (102 loc) · 3.7 KB
/
get_instance.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
// 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 tests
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/blox/blox/cluster-state-service/canary/pkg/tests/util"
"github.com/blox/blox/cluster-state-service/canary/pkg/wrappers"
"github.com/blox/blox/cluster-state-service/swagger/v1/generated/client/operations"
log "github.com/cihub/seelog"
"github.com/pkg/errors"
)
const (
getInstanceMetric = "getInstance"
getInstanceClusterNamePrefix = canaryClusterNamePrefix + "GetInstance_"
)
// GetInstance defines the test for CSS GetInstance API
func (c canary) GetInstance() error {
clusterName := getInstanceClusterNamePrefix + util.TimeString()
err := c.ecsWrapper.CreateCluster(&clusterName)
if err != nil {
return err
}
log.Infof("Get instance - created cluster")
// TODO: Launch more than 1 instance for this test
numInstances := int64(1)
ec2InstanceIDs, err := c.ec2Wrapper.LaunchInstances(&numInstances, &clusterName)
if err != nil {
return err
}
log.Infof("Get instance - launched EC2 instance")
instanceARN, err := util.ValidateECSContainerInstanceAndGetInstanceARN(c.ecsWrapper, clusterName)
if err != nil {
return err
}
log.Infof("Get instance - instance registered with ECS")
err = validateGetInstanceCall(c.cssWrapper, clusterName, instanceARN)
if err != nil {
return err
}
log.Infof("Get instance - instance in CSS GetInstance response")
err = util.DeleteCluster(c.ecsWrapper, clusterName)
if err != nil {
return err
}
err = util.TerminateInstances(c.ec2Wrapper, ec2InstanceIDs)
if err != nil {
return err
}
log.Infof("Get instance - GetInstance test resources cleaned up")
return nil
}
// GetInstanceMetric returns the metric name to which the GetInstance test metric should be emitted into
func (c canary) GetInstanceMetric() string {
return getInstanceMetric
}
func validateGetInstanceCall(cssWrapper wrappers.CSSWrapper, clusterName string, instanceARN string) error {
// Takes some time for CSS to get the instance.
// Retry get call once every 10 seconds for 2 minutes.
// TODO: Change sleep and retry related numbers to constants
found := false
for i := 0; i < 12; i++ {
instance, err := cssWrapper.GetInstance(&clusterName, &instanceARN)
if err != nil {
rootErr := errors.Cause(err)
if _, ok := rootErr.(*operations.GetInstanceNotFound); ok {
time.Sleep(10 * time.Second)
continue
}
return err
}
if instance == nil {
return errors.Errorf("Get instance call with cluster name '%s' and instance ARN '%s' returned a nil instance. ",
clusterName, instanceARN)
}
if instance.Entity == nil {
return errors.Errorf("Get instance call with cluster name '%s' and instance ARN '%s' returned a nil instance entity. ",
clusterName, instanceARN)
}
if aws.StringValue(instance.Entity.ContainerInstanceARN) == instanceARN {
found = true
break
}
return errors.Errorf("Expected instance belonging to cluster with name '%s' to have ARN '%s' but was '%s'. ",
clusterName, instanceARN, *instance.Entity.ContainerInstanceARN)
}
if !found {
return errors.Errorf("Instance with ARN '%s' belonging to cluster with name"+
" '%s' not found in CSS. ", instanceARN, clusterName)
}
return nil
}