Skip to content

Commit 23b7f2e

Browse files
committed
Remove sorting from instance keys when creating input tag specifications
1 parent 2a4d434 commit 23b7f2e

File tree

3 files changed

+78
-19
lines changed

3 files changed

+78
-19
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/sirupsen/logrus v1.9.3
4141
github.com/spf13/cobra v1.8.0
4242
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace
43+
github.com/stretchr/testify v1.9.0
4344
github.com/zgalor/weberr v0.6.0
4445
golang.org/x/crypto v0.22.0
4546
golang.org/x/text v0.14.0
@@ -169,6 +170,7 @@ require (
169170
github.com/pelletier/go-toml v1.9.5 // indirect
170171
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
171172
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
173+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
172174
github.com/prometheus/client_model v0.6.1 // indirect
173175
github.com/prometheus/common v0.52.2 // indirect
174176
github.com/prometheus/procfs v0.13.0 // indirect
@@ -185,7 +187,6 @@ require (
185187
github.com/spf13/cast v1.6.0 // indirect
186188
github.com/spf13/viper v1.18.2 // indirect
187189
github.com/stoewer/go-strcase v1.2.0 // indirect
188-
github.com/stretchr/testify v1.9.0 // indirect
189190
github.com/subosito/gotenv v1.6.0 // indirect
190191
github.com/valyala/fastjson v1.6.4 // indirect
191192
github.com/vincent-petithory/dataurl v1.0.0 // indirect

pkg/cloud/services/ec2/instances.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -619,17 +619,10 @@ func (s *Service) runInstance(role string, i *infrav1.Instance) (*infrav1.Instan
619619
resources := []string{ec2.ResourceTypeInstance, ec2.ResourceTypeVolume, ec2.ResourceTypeNetworkInterface}
620620
for _, r := range resources {
621621
spec := &ec2.TagSpecification{ResourceType: aws.String(r)}
622-
623-
// We need to sort keys for tests to work
624-
keys := make([]string, 0, len(i.Tags))
625-
for k := range i.Tags {
626-
keys = append(keys, k)
627-
}
628-
sort.Strings(keys)
629-
for _, key := range keys {
622+
for tagKey, tagValue := range i.Tags {
630623
spec.Tags = append(spec.Tags, &ec2.Tag{
631-
Key: aws.String(key),
632-
Value: aws.String(i.Tags[key]),
624+
Key: aws.String(tagKey),
625+
Value: aws.String(tagValue),
633626
})
634627
}
635628

pkg/cloud/services/ec2/instances_test.go

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ec2
1919
import (
2020
"context"
2121
"encoding/base64"
22+
"fmt"
2223
"strings"
2324
"testing"
2425

@@ -30,6 +31,7 @@ import (
3031
"github.com/google/go-cmp/cmp"
3132
. "github.com/onsi/gomega"
3233
"github.com/pkg/errors"
34+
"github.com/stretchr/testify/assert"
3335
corev1 "k8s.io/api/core/v1"
3436
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3537
"k8s.io/apimachinery/pkg/runtime"
@@ -45,6 +47,69 @@ import (
4547
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4648
)
4749

50+
type runInstancesInputMatcher struct {
51+
runInstancesInput *ec2.RunInstancesInput
52+
}
53+
54+
func (m runInstancesInputMatcher) Matches(arg interface{}) bool {
55+
riiArg := arg.(*ec2.RunInstancesInput)
56+
57+
if *m.runInstancesInput.ImageId != *riiArg.ImageId {
58+
return false
59+
}
60+
if *m.runInstancesInput.InstanceType != *riiArg.InstanceType {
61+
return false
62+
}
63+
if *m.runInstancesInput.KeyName != *riiArg.KeyName {
64+
return false
65+
}
66+
if *m.runInstancesInput.SubnetId != *riiArg.SubnetId {
67+
return false
68+
}
69+
if *m.runInstancesInput.UserData != *riiArg.UserData {
70+
return false
71+
}
72+
if *m.runInstancesInput.MaxCount != *riiArg.MaxCount {
73+
return false
74+
}
75+
if *m.runInstancesInput.MinCount != *riiArg.MinCount {
76+
return false
77+
}
78+
79+
if !assert.ElementsMatch(nil, m.runInstancesInput.SecurityGroupIds, riiArg.SecurityGroupIds) {
80+
return false
81+
}
82+
83+
if len(m.runInstancesInput.TagSpecifications) != len(riiArg.TagSpecifications) {
84+
return false
85+
}
86+
87+
for _, instanceTagSpec := range m.runInstancesInput.TagSpecifications {
88+
found := false
89+
for _, argTagSpec := range riiArg.TagSpecifications {
90+
if *instanceTagSpec.ResourceType == *argTagSpec.ResourceType {
91+
found = true
92+
if !assert.ElementsMatch(nil, instanceTagSpec.Tags, argTagSpec.Tags) {
93+
return false
94+
}
95+
}
96+
}
97+
if !found {
98+
return false
99+
}
100+
}
101+
102+
return true
103+
}
104+
105+
func (m runInstancesInputMatcher) String() string {
106+
return fmt.Sprintf("has the same elements as %v", m.runInstancesInput)
107+
}
108+
109+
func RunInstancesInputEq(runInstancesInput *ec2.RunInstancesInput) gomock.Matcher {
110+
return runInstancesInputMatcher{runInstancesInput}
111+
}
112+
48113
func TestInstanceIfExists(t *testing.T) {
49114
mockCtrl := gomock.NewController(t)
50115
defer mockCtrl.Finish()
@@ -671,7 +736,7 @@ func TestCreateInstance(t *testing.T) {
671736
},
672737
}, nil)
673738
m.
674-
RunInstancesWithContext(context.TODO(), &ec2.RunInstancesInput{
739+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
675740
ImageId: aws.String("abc"),
676741
InstanceType: aws.String("m5.2xlarge"),
677742
KeyName: aws.String("default"),
@@ -757,7 +822,7 @@ func TestCreateInstance(t *testing.T) {
757822
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
758823
MaxCount: aws.Int64(1),
759824
MinCount: aws.Int64(1),
760-
}).Return(&ec2.Reservation{
825+
})).Return(&ec2.Reservation{
761826
Instances: []*ec2.Instance{
762827
{
763828
State: &ec2.InstanceState{
@@ -919,7 +984,7 @@ func TestCreateInstance(t *testing.T) {
919984
},
920985
}, nil)
921986
m.
922-
RunInstancesWithContext(context.TODO(), &ec2.RunInstancesInput{
987+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
923988
ImageId: aws.String("abc"),
924989
InstanceType: aws.String("m5.2xlarge"),
925990
KeyName: aws.String("default"),
@@ -1005,7 +1070,7 @@ func TestCreateInstance(t *testing.T) {
10051070
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataCompressed)),
10061071
MaxCount: aws.Int64(1),
10071072
MinCount: aws.Int64(1),
1008-
}).Return(&ec2.Reservation{
1073+
})).Return(&ec2.Reservation{
10091074
Instances: []*ec2.Instance{
10101075
{
10111076
State: &ec2.InstanceState{
@@ -3337,7 +3402,7 @@ func TestCreateInstance(t *testing.T) {
33373402
},
33383403
expect: func(m *mocks.MockEC2APIMockRecorder) {
33393404
m. // TODO: Restore these parameters, but with the tags as well
3340-
RunInstancesWithContext(context.TODO(), gomock.Eq(&ec2.RunInstancesInput{
3405+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
33413406
ImageId: aws.String("abc"),
33423407
InstanceType: aws.String("m5.large"),
33433408
KeyName: aws.String("default"),
@@ -3546,7 +3611,7 @@ func TestCreateInstance(t *testing.T) {
35463611
},
35473612
expect: func(m *mocks.MockEC2APIMockRecorder) {
35483613
m. // TODO: Restore these parameters, but with the tags as well
3549-
RunInstancesWithContext(context.TODO(), gomock.Eq(&ec2.RunInstancesInput{
3614+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
35503615
ImageId: aws.String("abc"),
35513616
InstanceType: aws.String("m5.large"),
35523617
KeyName: aws.String("default"),
@@ -3775,7 +3840,7 @@ func TestCreateInstance(t *testing.T) {
37753840
},
37763841
}, nil)
37773842
m. // TODO: Restore these parameters, but with the tags as well
3778-
RunInstancesWithContext(context.TODO(), gomock.Eq(&ec2.RunInstancesInput{
3843+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
37793844
ImageId: aws.String("abc"),
37803845
InstanceType: aws.String("m5.large"),
37813846
KeyName: aws.String("default"),
@@ -3966,7 +4031,7 @@ func TestCreateInstance(t *testing.T) {
39664031
},
39674032
expect: func(m *mocks.MockEC2APIMockRecorder) {
39684033
m. // TODO: Restore these parameters, but with the tags as well
3969-
RunInstancesWithContext(context.TODO(), gomock.Eq(&ec2.RunInstancesInput{
4034+
RunInstancesWithContext(context.TODO(), RunInstancesInputEq(&ec2.RunInstancesInput{
39704035
ImageId: aws.String("abc"),
39714036
InstanceType: aws.String("m5.large"),
39724037
KeyName: aws.String("default"),

0 commit comments

Comments
 (0)