@ -18,10 +18,11 @@ import (
"errors"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
@ -39,7 +40,7 @@ func boolptr(b bool) *bool {
return & b
}
func int64ptr ( i int64 ) * int64 {
func int32ptr ( i int32 ) * int32 {
return & i
}
@ -51,7 +52,7 @@ type ec2DataStore struct {
ownerID string
instances [ ] * ec2 . Instance
instances [ ] ec2Types . Instance
}
// The tests itself.
@ -121,7 +122,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
"azname-b" : "azid-2" ,
"azname-c" : "azid-3" ,
} ,
instances : [ ] * ec2 . Instance {
instances : [ ] ec2Types . Instance {
{
InstanceId : strptr ( "instance-id-noprivateip" ) ,
} ,
@ -143,26 +144,26 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
"azname-c" : "azid-3" ,
} ,
ownerID : "owner-id-novpc" ,
instances : [ ] * ec2 . Instance {
instances : [ ] ec2Types . Instance {
{
// set every possible options and test them here
Architecture : strptr ( "architecture-novpc" ) ,
Architecture : "architecture-novpc" ,
ImageId : strptr ( "ami-novpc" ) ,
InstanceId : strptr ( "instance-id-novpc" ) ,
InstanceLifecycle : strptr ( "instance-lifecycle-novpc" ) ,
InstanceType : strptr ( "instance-type-novpc" ) ,
Placement : & ec2 . Placement { AvailabilityZone : strptr ( "azname-b" ) } ,
Platform : strptr ( "platform-novpc" ) ,
InstanceLifecycle : "instance-lifecycle-novpc" ,
InstanceType : "instance-type-novpc" ,
Placement : & ec2Types . Placement { AvailabilityZone : strptr ( "azname-b" ) } ,
Platform : "platform-novpc" ,
PrivateDnsName : strptr ( "private-dns-novpc" ) ,
PrivateIpAddress : strptr ( "1.2.3.4" ) ,
PublicDnsName : strptr ( "public-dns-novpc" ) ,
PublicIpAddress : strptr ( "42.42.42.2" ) ,
State : & ec2 . InstanceState { Name : strptr ( "running" ) } ,
State : & ec2Types . InstanceState { Name : "running" } ,
// test tags once and for all
Tags : [ ] * ec2 . Tag {
Tags : [ ] ec2Types . Tag {
{ Key : strptr ( "tag-1-key" ) , Value : strptr ( "tag-1-value" ) } ,
{ Key : strptr ( "tag-2-key" ) , Value : strptr ( "tag-2-value" ) } ,
nil ,
{ } ,
{ Value : strptr ( "tag-4-value" ) } ,
{ Key : strptr ( "tag-5-key" ) } ,
} ,
@ -206,22 +207,22 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
"azname-b" : "azid-2" ,
"azname-c" : "azid-3" ,
} ,
instances : [ ] * ec2 . Instance {
instances : [ ] ec2Types . Instance {
{
// just the minimum needed for the refresh work
ImageId : strptr ( "ami-ipv4" ) ,
InstanceId : strptr ( "instance-id-ipv4" ) ,
InstanceType : strptr ( "instance-type-ipv4" ) ,
Placement : & ec2 . Placement { AvailabilityZone : strptr ( "azname-c" ) } ,
InstanceType : "instance-type-ipv4" ,
Placement : & ec2Types . Placement { AvailabilityZone : strptr ( "azname-c" ) } ,
PrivateIpAddress : strptr ( "5.6.7.8" ) ,
State : & ec2 . InstanceState { Name : strptr ( "running" ) } ,
State : & ec2Types . InstanceState { Name : "running" } ,
SubnetId : strptr ( "azid-3" ) ,
VpcId : strptr ( "vpc-ipv4" ) ,
// network interfaces
NetworkInterfaces : [ ] * ec2 . InstanceNetworkInterface {
NetworkInterfaces : [ ] ec2Types . InstanceNetworkInterface {
// interface without subnet -> should be ignored
{
Ipv6Addresses : [ ] * ec2 . InstanceIpv6Address {
Ipv6Addresses : [ ] ec2Types . InstanceIpv6Address {
{
Ipv6Address : strptr ( "2001:db8:1::1" ) ,
IsPrimaryIpv6 : boolptr ( true ) ,
@ -230,12 +231,12 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
} ,
// interface with subnet, no IPv6
{
Ipv6Addresses : [ ] * ec2 . InstanceIpv6Address { } ,
Ipv6Addresses : [ ] ec2Types . InstanceIpv6Address { } ,
SubnetId : strptr ( "azid-3" ) ,
} ,
// interface with another subnet, no IPv6
{
Ipv6Addresses : [ ] * ec2 . InstanceIpv6Address { } ,
Ipv6Addresses : [ ] ec2Types . InstanceIpv6Address { } ,
SubnetId : strptr ( "azid-1" ) ,
} ,
} ,
@ -274,25 +275,25 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
"azname-b" : "azid-2" ,
"azname-c" : "azid-3" ,
} ,
instances : [ ] * ec2 . Instance {
instances : [ ] ec2Types . Instance {
{
// just the minimum needed for the refresh work
ImageId : strptr ( "ami-ipv6" ) ,
InstanceId : strptr ( "instance-id-ipv6" ) ,
InstanceType : strptr ( "instance-type-ipv6" ) ,
Placement : & ec2 . Placement { AvailabilityZone : strptr ( "azname-b" ) } ,
InstanceType : "instance-type-ipv6" ,
Placement : & ec2Types . Placement { AvailabilityZone : strptr ( "azname-b" ) } ,
PrivateIpAddress : strptr ( "9.10.11.12" ) ,
State : & ec2 . InstanceState { Name : strptr ( "running" ) } ,
State : & ec2Types . InstanceState { Name : "running" } ,
SubnetId : strptr ( "azid-2" ) ,
VpcId : strptr ( "vpc-ipv6" ) ,
// network interfaces
NetworkInterfaces : [ ] * ec2 . InstanceNetworkInterface {
NetworkInterfaces : [ ] ec2Types . InstanceNetworkInterface {
// interface without primary IPv6, index 2
{
Attachment : & ec2 . InstanceNetworkInterfaceAttachment {
DeviceIndex : int64 ptr ( 3 ) ,
Attachment : & ec2Types . InstanceNetworkInterfaceAttachment {
DeviceIndex : int32 ptr ( 3 ) ,
} ,
Ipv6Addresses : [ ] * ec2 . InstanceIpv6Address {
Ipv6Addresses : [ ] ec2Types . InstanceIpv6Address {
{
Ipv6Address : strptr ( "2001:db8:2::1:1" ) ,
IsPrimaryIpv6 : boolptr ( false ) ,
@ -302,10 +303,10 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
} ,
// interface with primary IPv6, index 1
{
Attachment : & ec2 . InstanceNetworkInterfaceAttachment {
DeviceIndex : int64 ptr ( 1 ) ,
Attachment : & ec2Types . InstanceNetworkInterfaceAttachment {
DeviceIndex : int32 ptr ( 1 ) ,
} ,
Ipv6Addresses : [ ] * ec2 . InstanceIpv6Address {
Ipv6Addresses : [ ] ec2Types . InstanceIpv6Address {
{
Ipv6Address : strptr ( "2001:db8:2::2:1" ) ,
IsPrimaryIpv6 : boolptr ( false ) ,
@ -319,10 +320,10 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
} ,
// interface with primary IPv6, index 3
{
Attachment : & ec2 . InstanceNetworkInterfaceAttachment {
DeviceIndex : int64 ptr ( 3 ) ,
Attachment : & ec2Types . InstanceNetworkInterfaceAttachment {
DeviceIndex : int32 ptr ( 3 ) ,
} ,
Ipv6Addresses : [ ] * ec2 . InstanceIpv6Address {
Ipv6Addresses : [ ] ec2Types . InstanceIpv6Address {
{
Ipv6Address : strptr ( "2001:db8:2::3:1" ) ,
IsPrimaryIpv6 : boolptr ( true ) ,
@ -332,10 +333,10 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
} ,
// interface without primary IPv6, index 0
{
Attachment : & ec2 . InstanceNetworkInterfaceAttachment {
DeviceIndex : int64 ptr ( 0 ) ,
Attachment : & ec2Types . InstanceNetworkInterfaceAttachment {
DeviceIndex : int32 ptr ( 0 ) ,
} ,
Ipv6Addresses : [ ] * ec2 . InstanceIpv6Address { } ,
Ipv6Addresses : [ ] ec2Types . InstanceIpv6Address { } ,
SubnetId : strptr ( "azid-3" ) ,
} ,
} ,
@ -388,7 +389,6 @@ func TestEC2DiscoveryRefresh(t *testing.T) {
// EC2 client mock.
type mockEC2Client struct {
ec2iface . EC2API
ec2Data ec2DataStore
}
@ -399,16 +399,16 @@ func newMockEC2Client(ec2Data *ec2DataStore) *mockEC2Client {
return & client
}
func ( m * mockEC2Client ) DescribeAvailabilityZonesWithContext ( _ aws . Context , _ * ec2 . DescribeAvailabilityZonesInput , _ ... request . Option ) ( * ec2 . DescribeAvailabilityZonesOutput , error ) {
func ( m * mockEC2Client ) DescribeAvailabilityZones ( _ context . Context , _ * ec2 . DescribeAvailabilityZonesInput , _ ... func ( * ec2 . Options ) ) ( * ec2 . DescribeAvailabilityZonesOutput , error ) {
if len ( m . ec2Data . azToAZID ) == 0 {
return nil , errors . New ( "No AZs found" )
}
azs := make ( [ ] * ec2 . AvailabilityZone , len ( m . ec2Data . azToAZID ) )
azs := make ( [ ] ec2Types . AvailabilityZone , len ( m . ec2Data . azToAZID ) )
i := 0
for k , v := range m . ec2Data . azToAZID {
azs [ i ] = & ec2 . AvailabilityZone {
azs [ i ] = ec2Types . AvailabilityZone {
ZoneName : strptr ( k ) ,
ZoneId : strptr ( v ) ,
}
@ -420,15 +420,13 @@ func (m *mockEC2Client) DescribeAvailabilityZonesWithContext(_ aws.Context, _ *e
} , nil
}
func ( m * mockEC2Client ) DescribeInstancesPagesWithContext ( _ aws . Context , _ * ec2 . DescribeInstancesInput , fn func ( * ec2 . DescribeInstancesOutput , bool ) bool , _ ... request . Option ) error {
r := ec2 . Reservation { }
r . SetInstances ( m . ec2Data . instances )
r . SetOwnerId ( m . ec2Data . ownerID )
func ( m * mockEC2Client ) DescribeInstances ( _ context . Context , _ * ec2 . DescribeInstancesInput , _ ... func ( * ec2 . Options ) ) ( * ec2 . DescribeInstancesOutput , error ) {
r := ec2Types . Reservation { }
r . Instances = append ( r . Instances , m . ec2Data . instances ... )
r . OwnerId = aws . String ( m . ec2Data . ownerID )
o := ec2 . DescribeInstancesOutput { }
o . SetReservations ( [ ] * ec2 . Reservation { & r } )
_ = fn ( & o , true )
o . Reservations = [ ] ec2Types . Reservation { r }
return nil
return & o , nil
}