Skip to content

Commit

Permalink
add test for tag conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
github-vincent-miszczak committed Oct 7, 2024
1 parent ad744bd commit af23142
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
17 changes: 10 additions & 7 deletions provider/awssd/aws_sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ func NewAWSSDProvider(domainFilter endpoint.DomainFilter, namespaceType string,
namespaceTypeFilter: newSdNamespaceFilter(namespaceType),
cleanEmptyService: cleanEmptyService,
ownerID: ownerID,
tags: func() []sdtypes.Tag {
awsTags := make([]sdtypes.Tag, 0, len(tags))
for k, v := range tags {
awsTags = append(awsTags, sdtypes.Tag{Key: aws.String(k), Value: aws.String(v)})
}
return awsTags
}(),
tags: awsTags(tags),
}

return p, nil
Expand All @@ -122,6 +116,15 @@ func newSdNamespaceFilter(namespaceTypeConfig string) sdtypes.NamespaceFilter {
}
}

// awsTags converts user supplied tags to AWS format
func awsTags(tags map[string]string) []sdtypes.Tag {
awsTags := make([]sdtypes.Tag, 0, len(tags))
for k, v := range tags {
awsTags = append(awsTags, sdtypes.Tag{Key: aws.String(k), Value: aws.String(v)})
}
return awsTags
}

// Records returns list of all endpoints.
func (p *AWSSDProvider) Records(ctx context.Context) (endpoints []*endpoint.Endpoint, err error) {
namespaces, err := p.ListNamespaces(ctx)
Expand Down
36 changes: 36 additions & 0 deletions provider/awssd/aws_sd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,3 +900,39 @@ func TestAWSSDProvider_DeregisterInstance(t *testing.T) {

assert.Len(t, instances["srv1"], 0)
}

func TestAWSSDProvider_awsTags(t *testing.T) {
tests := []struct {
Expectation []sdtypes.Tag
Input map[string]string
}{
{
Expectation: []sdtypes.Tag{
{
Key: aws.String("key1"),
Value: aws.String("value1"),
},
{
Key: aws.String("key2"),
Value: aws.String("value2"),
},
},
Input: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
{
Expectation: []sdtypes.Tag{},
Input: map[string]string{},
},
{
Expectation: []sdtypes.Tag{},
Input: nil,
},
}

for _, test := range tests {
assert.EqualValues(t, test.Expectation, awsTags(test.Input))
}
}

0 comments on commit af23142

Please sign in to comment.