Skip to content

Commit

Permalink
Merge pull request #32 from menglingwei/feature/actuators-unit-tests
Browse files Browse the repository at this point in the history
Feature/actuators unit tests
  • Loading branch information
openshift-ci[bot] committed Jul 1, 2022
2 parents 4145108 + 7d483e7 commit b9287c0
Show file tree
Hide file tree
Showing 18 changed files with 5,220 additions and 200 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
k8s.io/client-go v0.24.1
k8s.io/klog v1.0.0
k8s.io/klog/v2 v2.60.1
k8s.io/kubectl v0.24.1
sigs.k8s.io/controller-runtime v0.12.1
sigs.k8s.io/controller-tools v0.9.0
sigs.k8s.io/yaml v1.3.0
Expand Down Expand Up @@ -101,7 +102,6 @@ require (
k8s.io/cli-runtime v0.24.1 // indirect
k8s.io/component-base v0.24.1 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/kubectl v0.24.1 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/kustomize/api v0.11.4 // indirect
Expand Down
383 changes: 205 additions & 178 deletions pkg/actuators/machine/actuator_test.go

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions pkg/actuators/machine/filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package machine

import (
"reflect"
"testing"

"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func TestClusterTagFilter(t *testing.T) {
var cases = []struct {
clusterId string
machineName string
expected []ecs.DescribeInstancesTag
}{
{
clusterId: "testCluster",
machineName: "test-machine",
expected: []ecs.DescribeInstancesTag{
{
Key: "kubernetes.io/cluster/testCluster",
Value: "owned",
},
{
Key: "Name",
Value: "test-machine",
},
},
},
}

for i, c := range cases {
t.Run(c.machineName, func(t *testing.T) {
actual := clusterTagFilter(c.clusterId, c.machineName)
if !reflect.DeepEqual(c.expected, actual) {
t.Errorf("test #%d: expected %+v, got %+v", i, c.expected, actual)
}
})
}
}
23 changes: 18 additions & 5 deletions pkg/actuators/machine/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ func getSecurityGroupIDByTags(machine runtimeclient.ObjectKey, machineProviderCo
}

func buildDescribeSecurityGroupsTag(tags []machinev1.Tag) *[]ecs.DescribeSecurityGroupsTag {
describeSecurityGroupsTag := make([]ecs.DescribeSecurityGroupsTag, len(tags))

for index, tag := range tags {
rawTagList := removeDuplicatedMachineTags(tags)
describeSecurityGroupsTag := make([]ecs.DescribeSecurityGroupsTag, len(rawTagList))
for index, tag := range rawTagList {
describeSecurityGroupsTag[index] = ecs.DescribeSecurityGroupsTag{
Key: tag.Key,
Value: tag.Value,
Expand Down Expand Up @@ -478,9 +478,10 @@ func getVSwitchIDFromTags(machine runtimeclient.ObjectKey, mpc *machinev1.Alibab
}

func buildDescribeVSwitchesTag(tags []machinev1.Tag) *[]vpc.DescribeVSwitchesTag {
describeVSwitchesTag := make([]vpc.DescribeVSwitchesTag, len(tags))
rawTagList := removeDuplicatedMachineTags(tags)
describeVSwitchesTag := make([]vpc.DescribeVSwitchesTag, len(rawTagList))

for index, tag := range tags {
for index, tag := range rawTagList {
describeVSwitchesTag[index] = vpc.DescribeVSwitchesTag{
Key: tag.Key,
Value: tag.Value,
Expand All @@ -490,6 +491,18 @@ func buildDescribeVSwitchesTag(tags []machinev1.Tag) *[]vpc.DescribeVSwitchesTag
return &describeVSwitchesTag
}

func removeDuplicatedMachineTags(machineTags []machinev1.Tag) []*machinev1.Tag {
rawTagList := make([]*machinev1.Tag, 0)
for _, tag := range machineTags {
rawTagList = append(rawTagList, &machinev1.Tag{
Key: tag.Key,
Value: tag.Value,
})
}

return removeDuplicatedTags(rawTagList)
}

// buildTagList compile a list of ecs tags from machine provider spec and infrastructure object platform spec
func buildTagList(machineName string, clusterID string, machineTags []machinev1.Tag) []*machinev1.Tag {
rawTagList := make([]*machinev1.Tag, 0)
Expand Down
Loading

0 comments on commit b9287c0

Please sign in to comment.