Skip to content

Commit

Permalink
Test input to AWS API when creating instaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Demichev committed Oct 12, 2020
1 parent b1a6ba6 commit 6588351
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 11 deletions.
205 changes: 197 additions & 8 deletions pkg/actuators/machine/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func TestRemoveStoppedMachine(t *testing.T) {
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
stubInstance("ami-a9acbbd6", "i-02fcb933c5da7085c"),
stubInstance(stubAMIID, stubInstanceID),
},
},
},
Expand All @@ -321,7 +321,7 @@ func TestRemoveStoppedMachine(t *testing.T) {
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
stubInstance("ami-a9acbbd6", "i-02fcb933c5da7085c"),
stubInstance(stubAMIID, stubInstanceID),
stubInstance("ami-a9acbbd7", "i-02fcb933c5da7085d"),
},
},
Expand Down Expand Up @@ -349,6 +349,9 @@ func TestLaunchInstance(t *testing.T) {
t.Fatalf("Unable to build test machine manifest: %v", err)
}

providerConfig := stubProviderConfig()
stubTagList := buildTagList(machine.Name, stubClusterID, providerConfig.Tags)

cases := []struct {
name string
providerConfig *awsproviderv1.AWSMachineProviderConfig
Expand All @@ -362,6 +365,7 @@ func TestLaunchInstance(t *testing.T) {
instancesOutput *ec2.Reservation
instancesErr error
succeeds bool
runInstancesInput *ec2.RunInstancesInput
}{
{
name: "Security groups with filters",
Expand All @@ -379,8 +383,34 @@ func TestLaunchInstance(t *testing.T) {
},
},
},
instancesOutput: stubReservation("ami-a9acbbd6", "i-02fcb933c5da7085c"),
instancesOutput: stubReservation(stubAMIID, stubInstanceID),
succeeds: true,
runInstancesInput: &ec2.RunInstancesInput{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(*providerConfig.IAMInstanceProfile.ID),
},
ImageId: aws.String(*providerConfig.AMI.ID),
InstanceType: &providerConfig.InstanceType,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: providerConfig.KeyName,
TagSpecifications: []*ec2.TagSpecification{{
ResourceType: aws.String("instance"),
Tags: stubTagList,
}, {
ResourceType: aws.String("volume"),
Tags: stubTagList,
}},
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
{
DeviceIndex: aws.Int64(providerConfig.DeviceIndex),
AssociatePublicIpAddress: providerConfig.PublicIP,
SubnetId: providerConfig.Subnet.ID,
Groups: []*string{aws.String("groupID")},
},
},
UserData: aws.String(""),
},
},
{
name: "Security groups with filters with error",
Expand All @@ -405,6 +435,31 @@ func TestLaunchInstance(t *testing.T) {
securityGroupOutput: &ec2.DescribeSecurityGroupsOutput{
SecurityGroups: []*ec2.SecurityGroup{},
},
runInstancesInput: &ec2.RunInstancesInput{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(*providerConfig.IAMInstanceProfile.ID),
},
ImageId: aws.String(*providerConfig.AMI.ID),
InstanceType: &providerConfig.InstanceType,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: providerConfig.KeyName,
TagSpecifications: []*ec2.TagSpecification{{
ResourceType: aws.String("instance"),
Tags: stubTagList,
}, {
ResourceType: aws.String("volume"),
Tags: stubTagList,
}},
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
{
DeviceIndex: aws.Int64(providerConfig.DeviceIndex),
AssociatePublicIpAddress: providerConfig.PublicIP,
SubnetId: providerConfig.Subnet.ID,
},
},
UserData: aws.String(""),
},
},
{
name: "Subnet with filters",
Expand All @@ -418,8 +473,43 @@ func TestLaunchInstance(t *testing.T) {
},
},
},
instancesOutput: stubReservation("ami-a9acbbd6", "i-02fcb933c5da7085c"),
instancesOutput: stubReservation(stubAMIID, stubInstanceID),
succeeds: true,
runInstancesInput: &ec2.RunInstancesInput{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(*providerConfig.IAMInstanceProfile.ID),
},
ImageId: aws.String(*providerConfig.AMI.ID),
InstanceType: &providerConfig.InstanceType,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: providerConfig.KeyName,
TagSpecifications: []*ec2.TagSpecification{{
ResourceType: aws.String("instance"),
Tags: stubTagList,
}, {
ResourceType: aws.String("volume"),
Tags: stubTagList,
}},
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
{
DeviceIndex: aws.Int64(providerConfig.DeviceIndex),
AssociatePublicIpAddress: providerConfig.PublicIP,
SubnetId: aws.String("subnetID"),
Groups: []*string{
aws.String("sg-00868b02fbe29de17"),
aws.String("sg-0a4658991dc5eb40a"),
aws.String("sg-009a70e28fa4ba84e"),
aws.String("sg-07323d56fb932c84c"),
aws.String("sg-08b1ffd32874d59a2"),
},
},
},
UserData: aws.String(""),
Placement: &ec2.Placement{
AvailabilityZone: aws.String("us-east-1a"),
},
},
},
{
name: "Subnet with filters with error",
Expand Down Expand Up @@ -453,8 +543,40 @@ func TestLaunchInstance(t *testing.T) {
},
},
},
instancesOutput: stubReservation("ami-a9acbbd6", "i-02fcb933c5da7085c"),
instancesOutput: stubReservation(stubAMIID, stubInstanceID),
succeeds: true,
runInstancesInput: &ec2.RunInstancesInput{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(*providerConfig.IAMInstanceProfile.ID),
},
ImageId: aws.String("ami-1111"),
InstanceType: &providerConfig.InstanceType,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: providerConfig.KeyName,
TagSpecifications: []*ec2.TagSpecification{{
ResourceType: aws.String("instance"),
Tags: stubTagList,
}, {
ResourceType: aws.String("volume"),
Tags: stubTagList,
}},
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
{
DeviceIndex: aws.Int64(providerConfig.DeviceIndex),
AssociatePublicIpAddress: providerConfig.PublicIP,
SubnetId: providerConfig.Subnet.ID,
Groups: []*string{
aws.String("sg-00868b02fbe29de17"),
aws.String("sg-0a4658991dc5eb40a"),
aws.String("sg-009a70e28fa4ba84e"),
aws.String("sg-07323d56fb932c84c"),
aws.String("sg-08b1ffd32874d59a2"),
},
},
},
UserData: aws.String(""),
},
},
{
name: "AMI with filters with error",
Expand All @@ -476,6 +598,41 @@ func TestLaunchInstance(t *testing.T) {
imageOutput: &ec2.DescribeImagesOutput{
Images: []*ec2.Image{},
},
runInstancesInput: &ec2.RunInstancesInput{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(*providerConfig.IAMInstanceProfile.ID),
},
ImageId: aws.String(*providerConfig.AMI.ID),
InstanceType: &providerConfig.InstanceType,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: providerConfig.KeyName,
TagSpecifications: []*ec2.TagSpecification{{
ResourceType: aws.String("instance"),
Tags: stubTagList,
}, {
ResourceType: aws.String("volume"),
Tags: stubTagList,
}},
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
{
DeviceIndex: aws.Int64(providerConfig.DeviceIndex),
AssociatePublicIpAddress: providerConfig.PublicIP,
SubnetId: aws.String("subnetID"),
Groups: []*string{
aws.String("sg-00868b02fbe29de17"),
aws.String("sg-0a4658991dc5eb40a"),
aws.String("sg-009a70e28fa4ba84e"),
aws.String("sg-07323d56fb932c84c"),
aws.String("sg-08b1ffd32874d59a2"),
},
},
},
UserData: aws.String(""),
Placement: &ec2.Placement{
AvailabilityZone: aws.String("us-east-1a"),
},
},
},
{
name: "AMI with filters with two images",
Expand All @@ -499,8 +656,40 @@ func TestLaunchInstance(t *testing.T) {
},
},
},
instancesOutput: stubReservation("ami-a9acbbd6", "i-02fcb933c5da7085c"),
instancesOutput: stubReservation(stubAMIID, stubInstanceID),
succeeds: true,
runInstancesInput: &ec2.RunInstancesInput{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(*providerConfig.IAMInstanceProfile.ID),
},
ImageId: aws.String("ami-1111"),
InstanceType: &providerConfig.InstanceType,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: providerConfig.KeyName,
TagSpecifications: []*ec2.TagSpecification{{
ResourceType: aws.String("instance"),
Tags: stubTagList,
}, {
ResourceType: aws.String("volume"),
Tags: stubTagList,
}},
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
{
DeviceIndex: aws.Int64(providerConfig.DeviceIndex),
AssociatePublicIpAddress: providerConfig.PublicIP,
SubnetId: providerConfig.Subnet.ID,
Groups: []*string{
aws.String("sg-00868b02fbe29de17"),
aws.String("sg-0a4658991dc5eb40a"),
aws.String("sg-009a70e28fa4ba84e"),
aws.String("sg-07323d56fb932c84c"),
aws.String("sg-08b1ffd32874d59a2"),
},
},
},
UserData: aws.String(""),
},
},
{
name: "AMI not specified",
Expand All @@ -516,7 +705,7 @@ func TestLaunchInstance(t *testing.T) {
mockAWSClient.EXPECT().DescribeAvailabilityZones(gomock.Any()).Return(nil, tc.azErr).AnyTimes()
mockAWSClient.EXPECT().DescribeSubnets(gomock.Any()).Return(tc.subnetOutput, tc.subnetErr).AnyTimes()
mockAWSClient.EXPECT().DescribeImages(gomock.Any()).Return(tc.imageOutput, tc.imageErr).AnyTimes()
mockAWSClient.EXPECT().RunInstances(gomock.Any()).Return(tc.instancesOutput, tc.instancesErr).AnyTimes()
mockAWSClient.EXPECT().RunInstances(tc.runInstancesInput).Return(tc.instancesOutput, tc.instancesErr).AnyTimes()

_, launchErr := launchInstance(machine, tc.providerConfig, nil, mockAWSClient)
t.Log(launchErr)
Expand Down Expand Up @@ -623,7 +812,7 @@ func TestCorrectExistingTags(t *testing.T) {
}
clusterID, _ := getClusterID(machine)
instance := ec2.Instance{
InstanceId: aws.String("i-02fcb933c5da7085c"),
InstanceId: aws.String(stubInstanceID),
}
testCases := []struct {
name string
Expand Down
8 changes: 5 additions & 3 deletions pkg/actuators/machine/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ const (
awsCredentialsSecretName = "aws-credentials-secret"
userDataSecretName = "aws-actuator-user-data-secret"

keyName = "aws-actuator-key-name"
stubClusterID = "aws-actuator-cluster"
keyName = "aws-actuator-key-name"
stubClusterID = "aws-actuator-cluster"
stubAMIID = "ami-a9acbbd6"
stubInstanceID = "i-02fcb933c5da7085c"
)

const userDataBlob = `#cloud-config
Expand All @@ -41,7 +43,7 @@ runcmd:
func stubProviderConfig() *awsproviderv1.AWSMachineProviderConfig {
return &awsproviderv1.AWSMachineProviderConfig{
AMI: awsproviderv1.AWSResourceReference{
ID: aws.String("ami-a9acbbd6"),
ID: aws.String(stubAMIID),
},
CredentialsSecret: &corev1.LocalObjectReference{
Name: awsCredentialsSecretName,
Expand Down

0 comments on commit 6588351

Please sign in to comment.