diff --git a/pkg/actuators/machine/actuator_test.go b/pkg/actuators/machine/actuator_test.go index 6e5400a35e..9f9610f951 100644 --- a/pkg/actuators/machine/actuator_test.go +++ b/pkg/actuators/machine/actuator_test.go @@ -44,11 +44,15 @@ func TestMachineEvents(t *testing.T) { t.Fatalf("unable to build codec: %v", err) } - machine, cluster, awsCredentialsSecret, userDataSecret, err := stubMachineAPIResources() + machine, err := stubMachine() if err != nil { t.Fatal(err) } + cluster := stubCluster() + awsCredentialsSecret := stubAwsCredentialsSecret() + userDataSecret := stubUserDataSecret() + machineInvalidProviderConfig := machine.DeepCopy() machineInvalidProviderConfig.Spec.ProviderSpec.Value = nil machineInvalidProviderConfig.Spec.ProviderSpec.ValueFrom = nil @@ -194,11 +198,15 @@ func TestMachineEvents(t *testing.T) { } func TestActuator(t *testing.T) { - machine, cluster, awsCredentialsSecret, userDataSecret, err := stubMachineAPIResources() + machine, err := stubMachine() if err != nil { t.Fatal(err) } + cluster := stubCluster() + awsCredentialsSecret := stubAwsCredentialsSecret() + userDataSecret := stubUserDataSecret() + codec, err := providerconfigv1.NewCodec() if err != nil { t.Fatalf("unable to build codec: %v", err) @@ -623,9 +631,14 @@ func TestAvailabiltyZone(t *testing.T) { if err != nil { t.Fatalf("unable to build codec: %v", err) } + + cluster := stubCluster() + awsCredentialsSecret := stubAwsCredentialsSecret() + userDataSecret := stubUserDataSecret() + for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - machine, cluster, awsCredentialsSecret, userDataSecret, err := stubMachineAPIResources() + machine, err := stubMachine() if err != nil { t.Fatal(err) } diff --git a/pkg/actuators/machine/awsclient_test.go b/pkg/actuators/machine/awsclient_test.go index 39a36fde5c..64e6c08f08 100644 --- a/pkg/actuators/machine/awsclient_test.go +++ b/pkg/actuators/machine/awsclient_test.go @@ -5,8 +5,6 @@ import ( "testing" "time" - providerconfigv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1alpha1" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/golang/mock/gomock" @@ -14,13 +12,11 @@ import ( ) func TestAwsClient(t *testing.T) { - machine, machinePC, err := stubMachine() + machine, err := stubMachine() if err != nil { t.Fatalf("Unable to build test machine manifest: %v", err) } - machinePC.AMI = providerconfigv1.AWSResourceReference{} - cases := []struct { name string output *ec2.DescribeInstancesOutput diff --git a/pkg/actuators/machine/instaces_test.go b/pkg/actuators/machine/instaces_test.go index cd1cbf5ee9..2673f168d3 100644 --- a/pkg/actuators/machine/instaces_test.go +++ b/pkg/actuators/machine/instaces_test.go @@ -91,7 +91,7 @@ func TestBuildEC2Filters(t *testing.T) { } func TestRemoveStoppedMachine(t *testing.T) { - machine, _, err := stubMachine() + machine, err := stubMachine() if err != nil { t.Errorf("Unable to build test machine manifest: %v", err) } @@ -158,7 +158,7 @@ func TestRemoveStoppedMachine(t *testing.T) { } func TestRunningInstance(t *testing.T) { - machine, _, err := stubMachine() + machine, err := stubMachine() if err != nil { t.Fatalf("Unable to build test machine manifest: %v", err) } @@ -187,7 +187,7 @@ func TestRunningInstance(t *testing.T) { } func TestLaunchInstance(t *testing.T) { - machine, _, err := stubMachine() + machine, err := stubMachine() if err != nil { t.Fatalf("Unable to build test machine manifest: %v", err) } diff --git a/pkg/actuators/machine/stubs.go b/pkg/actuators/machine/stubs.go index 723e5b2cf6..d006ac2492 100644 --- a/pkg/actuators/machine/stubs.go +++ b/pkg/actuators/machine/stubs.go @@ -96,16 +96,16 @@ func stubProviderConfig() *providerconfigv1.AWSMachineProviderConfig { } } -func stubMachine() (*clusterv1.Machine, *providerconfigv1.AWSMachineProviderConfig, error) { +func stubMachine() (*clusterv1.Machine, error) { machinePc := stubProviderConfig() codec, err := providerconfigv1.NewCodec() if err != nil { - return nil, nil, fmt.Errorf("failed creating codec: %v", err) + return nil, fmt.Errorf("failed creating codec: %v", err) } config, err := codec.EncodeProviderConfig(machinePc) if err != nil { - return nil, nil, fmt.Errorf("encodeToProviderConfig failed: %v", err) + return nil, fmt.Errorf("encodeToProviderConfig failed: %v", err) } machine := &clusterv1.Machine{ @@ -124,35 +124,32 @@ func stubMachine() (*clusterv1.Machine, *providerconfigv1.AWSMachineProviderConf }, } - return machine, machinePc, nil + return machine, nil } -func stubMachineAPIResources() (*clusterv1.Machine, *clusterv1.Cluster, *apiv1.Secret, *apiv1.Secret, error) { - awsCredentialsSecret := utils.GenerateAwsCredentialsSecretFromEnv(awsCredentialsSecretName, defaultNamespace) - - userDataSecret := &apiv1.Secret{ +func stubCluster() *clusterv1.Cluster { + return &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: userDataSecretName, + Name: clusterID, Namespace: defaultNamespace, }, - Data: map[string][]byte{ - userDataSecretKey: []byte(userDataBlob), - }, - } - - machine, _, err := stubMachine() - if err != nil { - return nil, nil, nil, nil, err } +} - cluster := &clusterv1.Cluster{ +func stubUserDataSecret() *corev1.Secret { + return &apiv1.Secret{ ObjectMeta: metav1.ObjectMeta{ - Name: clusterID, + Name: userDataSecretName, Namespace: defaultNamespace, }, + Data: map[string][]byte{ + userDataSecretKey: []byte(userDataBlob), + }, } +} - return machine, cluster, awsCredentialsSecret, userDataSecret, nil +func stubAwsCredentialsSecret() *corev1.Secret { + return utils.GenerateAwsCredentialsSecretFromEnv(awsCredentialsSecretName, defaultNamespace) } func stubInstance(imageID, instanceID string) *ec2.Instance {