Skip to content

Commit

Permalink
Unit test additional cases in pkg/cloud/aws/actuators/machine
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvagabund committed Dec 19, 2018
1 parent adcefbe commit 8c535d5
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
142 changes: 142 additions & 0 deletions pkg/cloud/aws/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func TestMachineEvents(t *testing.T) {
machineInvalidProviderConfig.Spec.ProviderConfig.Value = nil
machineInvalidProviderConfig.Spec.ProviderConfig.ValueFrom = nil

workerMachine := machine.DeepCopy()
workerMachine.Labels[providerconfigv1.MachineTypeLabel] = "worker"

cases := []struct {
name string
machine *clusterv1.Machine
Expand Down Expand Up @@ -111,6 +114,14 @@ func TestMachineEvents(t *testing.T) {
},
event: "Normal Created Created Machine aws-actuator-testing-machine",
},
{
name: "Create worker machine event succeed",
machine: workerMachine,
operation: func(actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.CreateMachine(cluster, machine)
},
event: "Normal Created Created Machine aws-actuator-testing-machine",
},
{
name: "Delete machine event failed",
machine: machineInvalidProviderConfig,
Expand Down Expand Up @@ -164,6 +175,9 @@ func TestMachineEvents(t *testing.T) {
mockRegisterInstancesWithLoadBalancer(mockAWSClient, tc.error == lbError)
mockAWSClient.EXPECT().TerminateInstances(gomock.Any()).Return(&ec2.TerminateInstancesOutput{}, tc.terminateInstancesErr).AnyTimes()
mockAWSClient.EXPECT().RegisterInstancesWithLoadBalancer(gomock.Any()).Return(nil, tc.lbErr).AnyTimes()
mockAWSClient.EXPECT().ELBv2DescribeLoadBalancers(gomock.Any()).Return(stubDescribeLoadBalancersOutput(), tc.lbErr)
mockAWSClient.EXPECT().ELBv2DescribeTargetGroups(gomock.Any()).Return(stubDescribeTargetGroupsOutput(), nil).AnyTimes()
mockAWSClient.EXPECT().ELBv2RegisterTargets(gomock.Any()).Return(nil, nil).AnyTimes()

actuator, err := NewActuator(params)
if err != nil {
Expand Down Expand Up @@ -217,6 +231,14 @@ func TestActuator(t *testing.T) {
machineInvalidProviderConfig.Spec.ProviderConfig.Value = nil
machineInvalidProviderConfig.Spec.ProviderConfig.ValueFrom = nil

machineNoClusterID := machine.DeepCopy()
delete(machineNoClusterID.Labels, providerconfigv1.ClusterIDLabel)

pendingInstance := stubInstance("ami-a9acbbd6", "i-02fcb933c5da7085c")
pendingInstance.State = &ec2.InstanceState{
Name: aws.String(ec2.InstanceStateNamePending),
}

cases := []struct {
name string
machine *clusterv1.Machine
Expand Down Expand Up @@ -340,6 +362,45 @@ func TestActuator(t *testing.T) {
actuator.Update(context.TODO(), cluster, machine)
},
},
{
name: "Update machine status fails (instance pending)",
describeInstancesOutput: &ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
pendingInstance,
},
},
},
},
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Update(context.TODO(), cluster, machine)
},
},
{
name: "Update machine failed (two running instances, error terminating one)",
describeInstancesOutput: &ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{
stubInstance("ami-a9acbbd6", "i-02fcb933c5da7085c"),
stubInstance("ami-a9acbbd7", "i-02fcb933c5da7085d"),
},
},
},
},
terminateInstancesErr: fmt.Errorf("error"),
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Update(context.TODO(), cluster, machine)
},
},
{
name: "Update machine with failure (cluster ID missing)",
machine: machineNoClusterID,
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Update(context.TODO(), cluster, machine)
},
},
{
name: "Update machine failed (error updating load balancers)",
lbErr: fmt.Errorf("error"),
Expand All @@ -354,6 +415,20 @@ func TestActuator(t *testing.T) {
actuator.Describe(cluster, machine)
},
},
{
name: "Describe machine failed (invalid configuration)",
machine: machineInvalidProviderConfig,
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Describe(cluster, machine)
},
},
{
name: "Describe machine failed (error creating aws service)",
error: awsServiceError,
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Describe(cluster, machine)
},
},
{
name: "Describe machine fails (no running instance)",
describeInstancesOutput: &ec2.DescribeInstancesOutput{
Expand All @@ -373,6 +448,67 @@ func TestActuator(t *testing.T) {
actuator.Describe(cluster, machine)
},
},
{
name: "Exists machine failed (invalid configuration)",
machine: machineInvalidProviderConfig,
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Exists(context.TODO(), cluster, machine)
},
},
{
name: "Exists machine fails (no running instance)",
describeInstancesOutput: &ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{},
},
},
},
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Exists(context.TODO(), cluster, machine)
},
},
{
name: "Delete machine failed (invalid configuration)",
machine: machineInvalidProviderConfig,
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Delete(context.TODO(), cluster, machine)
},
},
{
name: "Delete machine failed (error creating aws service)",
error: awsServiceError,
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Delete(context.TODO(), cluster, machine)
},
},
{
name: "Delete machine failed (error getting running instances)",
describeInstancesErr: fmt.Errorf("error"),
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Delete(context.TODO(), cluster, machine)
},
},
{
name: "Delete machine failed (no running instances)",
describeInstancesOutput: &ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{
{
Instances: []*ec2.Instance{},
},
},
},
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Delete(context.TODO(), cluster, machine)
},
},
{
name: "Delete machine failed (error terminating instances)",
terminateInstancesErr: fmt.Errorf("error"),
operation: func(objectClient client.Client, actuator *Actuator, cluster *clusterv1.Cluster, machine *clusterv1.Machine) {
actuator.Delete(context.TODO(), cluster, machine)
},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -410,6 +546,9 @@ func TestActuator(t *testing.T) {

mockAWSClient.EXPECT().TerminateInstances(gomock.Any()).Return(&ec2.TerminateInstancesOutput{}, tc.terminateInstancesErr).AnyTimes()
mockAWSClient.EXPECT().RegisterInstancesWithLoadBalancer(gomock.Any()).Return(nil, tc.lbErr).AnyTimes()
mockAWSClient.EXPECT().ELBv2DescribeLoadBalancers(gomock.Any()).Return(stubDescribeLoadBalancersOutput(), tc.lbErr).AnyTimes()
mockAWSClient.EXPECT().ELBv2DescribeTargetGroups(gomock.Any()).Return(stubDescribeTargetGroupsOutput(), nil).AnyTimes()
mockAWSClient.EXPECT().ELBv2RegisterTargets(gomock.Any()).Return(nil, nil).AnyTimes()

if tc.machine == nil {
tc.operation(fakeClient, actuator, cluster, machine)
Expand Down Expand Up @@ -501,6 +640,9 @@ func TestAvailabiltyZone(t *testing.T) {
t.Fatal(err)
}

// no load balancers tested
machinePc.LoadBalancers = nil

machinePc.Placement.AvailabilityZone = tc.availabilityZone
if tc.subnet == "" {
machinePc.Subnet.ID = nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloud/aws/actuators/machine/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func stubProviderConfig() *providerconfigv1.AWSMachineProviderConfig {
Name: "cluster-int",
Type: providerconfigv1.ClassicLoadBalancerType,
},
{
Name: "cluster-net-lb",
Type: providerconfigv1.NetworkLoadBalancerType,
},
},
}
}
Expand Down

0 comments on commit 8c535d5

Please sign in to comment.