-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2_test.go
144 lines (131 loc) · 3.67 KB
/
ec2_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
package main
import (
"reflect"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
type mockEC2Iface struct {
ec2iface.EC2API
}
type expectTag struct {
name string
value string
}
func (svc *mockEC2Iface) DescribeInstances(*ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) {
return &ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
{
InstanceId: aws.String("i-12345678"),
},
{
InstanceId: aws.String("i-abcdefgh"),
},
},
},
},
}, nil
}
func TestListInstances(t *testing.T) {
mockSvc := &mockEC2Iface{}
mockEC2Client := NewEC2Client(mockSvc)
instances, err := mockEC2Client.ListInstances("")
if err != nil {
t.Errorf("Expected no error, but got %v.", err)
}
for _, instance := range instances {
if len(*instance.InstanceId) == 0 {
t.Errorf("Expected any instance info, but got empty.")
}
}
}
func TestParseFilter(t *testing.T) {
var expectEC2Filter []*ec2.Filter
filter := "Name=tag:Foo,Values=Bar Name=instance-type,Values=m1.small"
expectEC2Filter = append(expectEC2Filter, &ec2.Filter{
Name: aws.String("tag:Foo"),
Values: []*string{
aws.String("Bar"),
},
})
expectEC2Filter = append(expectEC2Filter, &ec2.Filter{
Name: aws.String("instance-type"),
Values: []*string{
aws.String("m1.small"),
},
})
ec2Filter := ParseFilter(filter)
if !reflect.DeepEqual(ec2Filter, expectEC2Filter) {
t.Errorf("expect: %s\nactual: %s", expectEC2Filter, ec2Filter)
}
}
func TestGetTagValue(t *testing.T) {
var expectTags []expectTag
expectTags = append(expectTags, expectTag{
name: "Name",
value: "server001",
})
expectTags = append(expectTags, expectTag{
name: "Env",
value: "Production",
})
instance := &ec2.Instance{
Tags: []*ec2.Tag{
{
Key: aws.String("Name"),
Value: aws.String("server001"),
},
{
Key: aws.String("Env"),
Value: aws.String("Production"),
},
},
}
for _, expectTag := range expectTags {
actualTagValue := GetTagValue(instance, expectTag.name)
if len(actualTagValue) == 0 {
t.Errorf("Expected tag value for name: %s, but got empty.", expectTag.name)
}
if expectTag.value != actualTagValue {
t.Errorf("Expected tag name: %s and value: %s, but got tag value: %v.", expectTag.name, expectTag.value, actualTagValue)
}
}
tagValue := GetTagValue(instance, "Deploy")
if tagValue != "" {
t.Errorf("Expected no value of tag no exist name of tag, but go %v.", tagValue)
}
}
func TestEC2Info(t *testing.T) {
var instance *ec2.Instance
instance = &ec2.Instance{
PrivateIpAddress: aws.String("192.0.2.11"),
PublicIpAddress: aws.String("203.0.113.11"),
Platform: nil,
}
if actual := GetPrivateIPAddress(instance); "192.0.2.11" != actual {
t.Errorf("Expected private ip address: 192.0.2.11, but got %v", actual)
}
if actual := GetPublicIPAddress(instance); "203.0.113.11" != actual {
t.Errorf("Expected public ip address: 203.0.113.11, but got %v", actual)
}
if actual := GetPlatform(instance); "linux" != actual {
t.Errorf("Expected platform: linux, but got %v", actual)
}
instance = &ec2.Instance{
PrivateIpAddress: nil,
PublicIpAddress: nil,
Platform: aws.String("windows"),
}
if actual := GetPrivateIPAddress(instance); len(actual) != 0 {
t.Errorf("Expected no value of private ip address, but got %v", actual)
}
if actual := GetPublicIPAddress(instance); len(actual) != 0 {
t.Errorf("Expected no value of public ip address, but got %v", actual)
}
if actual := GetPlatform(instance); "windows" != actual {
t.Errorf("Expected platform: windows, but got %v", actual)
}
}