Skip to content

Commit

Permalink
Replace stubMachineAPIResources call with call to individual resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvagabund committed Jan 7, 2019
1 parent 04b541e commit 1f5dcd9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
19 changes: 16 additions & 3 deletions pkg/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/actuators/machine/awsclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ 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"
mockaws "sigs.k8s.io/cluster-api-provider-aws/pkg/client/mock"
)

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
Expand Down
6 changes: 3 additions & 3 deletions pkg/actuators/machine/instaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
37 changes: 17 additions & 20 deletions pkg/actuators/machine/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down

0 comments on commit 1f5dcd9

Please sign in to comment.