package ec2
import (
"reflect"
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
)
func TestDescribeAvailabilityZonesResponse(t *testing.T) {
data := `
e23c5a54-a29c-43ee-8b55-0c13c26e9e01
-
opt-in-not-required
us-west-2a
usw2-az1
available
us-west-2
us-west-2-lax-1
-
us-west-2
opt-in-not-required
us-west-2b
usw2-az2
available
us-west-2
us-west-2-lax-1
`
azr, err := parseAvailabilityZonesResponse([]byte(data))
if err != nil {
t.Fatalf("unexpected error when parsing data: %s", err)
}
azrExpected := &AvailabilityZonesResponse{
AvailabilityZoneInfo: AvailabilityZoneInfo{
Items: []AvailabilityZone{
{
ZoneName: "us-west-2a",
ZoneID: "usw2-az1",
},
{
ZoneName: "us-west-2b",
ZoneID: "usw2-az2",
},
},
},
}
if !reflect.DeepEqual(azr, azrExpected) {
t.Fatalf("unexpected DescribeAvailabilityZonesResponse parsed;\ngot\n%+v\nwant\n%+v", azr, azrExpected)
}
}
func TestParseInstancesResponse(t *testing.T) {
data := `
98667f8e-7fb6-441b-a612-41c6268c6399
-
r-05534f81f74ea7036
793614593844
-
i-0e730b692d9c15460
ami-0eb89db7593b5d434
16
running
ip-172-31-11-152.eu-west-2.compute.internal
ec2-3-8-232-141.eu-west-2.compute.amazonaws.com
my-laptop
0
t2.micro
2020-04-27T09:19:26.000Z
eu-west-2c
default
disabled
subnet-57044c3e
vpc-f1eaad99
172.31.11.152
3.8.232.141
true
-
sg-05d74e4e8551bd020
launch-wizard-1
x86_64
ebs
/dev/sda1
-
/dev/sda1
vol-0153ef24058482522
attached
2020-04-27T09:19:27.000Z
true
hvm
-
foo
bar
xen
-
eni-01d7b338ea037a60b
subnet-57044c3e
vpc-f1eaad99
793614593844
in-use
02:3b:63:46:13:9a
172.31.11.152
ip-172-31-11-152.eu-west-2.compute.internal
true
-
sg-05d74e4e8551bd020
launch-wizard-1
eni-attach-030cc2cdffe745682
0
attached
2020-04-27T09:19:26.000Z
true
3.8.232.141
ec2-3-8-232-141.eu-west-2.compute.amazonaws.com
amazon
-
172.31.11.152
ip-172-31-11-152.eu-west-2.compute.internal
true
3.8.232.141
ec2-3-8-232-141.eu-west-2.compute.amazonaws.com
amazon
false
spot
windows
`
ir, err := parseInstancesResponse([]byte(data))
if err != nil {
t.Fatalf("unexpected error when parsing data: %s", err)
}
irExpected := &InstancesResponse{
ReservationSet: ReservationSet{
Items: []Reservation{
{
OwnerID: "793614593844",
InstanceSet: InstanceSet{
Items: []Instance{
{
PrivateIPAddress: "172.31.11.152",
Architecture: "x86_64",
Placement: Placement{
AvailabilityZone: "eu-west-2c",
},
ID: "i-0e730b692d9c15460",
ImageID: "ami-0eb89db7593b5d434",
Lifecycle: "spot",
State: InstanceState{
Name: "running",
},
Type: "t2.micro",
Platform: "windows",
SubnetID: "subnet-57044c3e",
PrivateDNSName: "ip-172-31-11-152.eu-west-2.compute.internal",
PublicDNSName: "ec2-3-8-232-141.eu-west-2.compute.amazonaws.com",
PublicIPAddress: "3.8.232.141",
VPCID: "vpc-f1eaad99",
NetworkInterfaceSet: NetworkInterfaceSet{
Items: []NetworkInterface{
{
SubnetID: "subnet-57044c3e",
},
},
},
TagSet: TagSet{
Items: []Tag{
{
Key: "foo",
Value: "bar",
},
},
},
},
},
},
},
},
},
}
if !reflect.DeepEqual(ir, irExpected) {
t.Fatalf("unexpected InstancesResponse parsed;\ngot\n%+v\nwant\n%+v", ir, irExpected)
}
rs := ir.ReservationSet.Items[0]
ownerID := rs.OwnerID
port := 423
inst := rs.InstanceSet.Items[0]
labelss := inst.appendTargetLabels(nil, ownerID, "region-a", port, map[string]string{
"eu-west-2c": "foobar-zone",
})
var sortedLabelss [][]prompbmarshal.Label
for _, labels := range labelss {
sortedLabelss = append(sortedLabelss, discoveryutils.GetSortedLabels(labels))
}
expectedLabels := [][]prompbmarshal.Label{
discoveryutils.GetSortedLabels(map[string]string{
"__address__": "172.31.11.152:423",
"__meta_ec2_architecture": "x86_64",
"__meta_ec2_availability_zone": "eu-west-2c",
"__meta_ec2_availability_zone_id": "foobar-zone",
"__meta_ec2_ami": "ami-0eb89db7593b5d434",
"__meta_ec2_instance_id": "i-0e730b692d9c15460",
"__meta_ec2_instance_lifecycle": "spot",
"__meta_ec2_instance_state": "running",
"__meta_ec2_instance_type": "t2.micro",
"__meta_ec2_owner_id": "793614593844",
"__meta_ec2_platform": "windows",
"__meta_ec2_primary_subnet_id": "subnet-57044c3e",
"__meta_ec2_private_dns_name": "ip-172-31-11-152.eu-west-2.compute.internal",
"__meta_ec2_private_ip": "172.31.11.152",
"__meta_ec2_public_dns_name": "ec2-3-8-232-141.eu-west-2.compute.amazonaws.com",
"__meta_ec2_public_ip": "3.8.232.141",
"__meta_ec2_region": "region-a",
"__meta_ec2_subnet_id": ",subnet-57044c3e,",
"__meta_ec2_tag_foo": "bar",
"__meta_ec2_vpc_id": "vpc-f1eaad99",
}),
}
if !reflect.DeepEqual(sortedLabelss, expectedLabels) {
t.Fatalf("unexpected labels:\ngot\n%v\nwant\n%v", sortedLabelss, expectedLabels)
}
}