Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SNS topics fetch panics on errors #2784

Merged
merged 4 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/resources/providers/awslib/sns/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (p *Provider) ListTopics(ctx context.Context) ([]types.Topic, error) {
output, err := c.ListTopics(ctx, input)
if err != nil {
p.log.Errorf("Could not list SNS Topics. Error: %s", err)
return nil, err
}
all = append(all, output.Topics...)
if output.NextToken == nil {
Expand Down Expand Up @@ -93,6 +94,7 @@ func (p *Provider) ListTopicsWithSubscriptions(ctx context.Context) ([]awslib.Aw
output, err := c.ListTopics(ctx, input)
if err != nil {
p.log.Errorf("Could not list SNS Topics. Error: %s", err)
return nil, err
}

for _, topic := range output.Topics {
Expand Down
58 changes: 58 additions & 0 deletions internal/resources/providers/awslib/sns/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package sns

import (
"context"
"errors"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/service/sns/types"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand All @@ -37,7 +39,9 @@ type (
clientMocks map[string][2]mocks
)

var errMock = errors.New("mock error")
var regions = []string{"us-east-1"}
var logger = logp.NewLogger("TestSNSProvider")

func TestProvider_ListTopics(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -68,6 +72,16 @@ func TestProvider_ListTopics(t *testing.T) {
},
topic: "topic-arn",
},
{
name: "with error",
mocks: clientMocks{
"ListTopics": [2]mocks{
{mock.Anything, mock.Anything},
{nil, errMock},
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -76,6 +90,7 @@ func TestProvider_ListTopics(t *testing.T) {
c.On(name, call[0]...).Return(call[1]...)
}
p := &Provider{
log: logger,
clients: createMockClients(c, regions),
}
got, err := p.ListTopics(context.Background())
Expand Down Expand Up @@ -126,6 +141,7 @@ func TestProvider_ListSubscriptionsByTopic(t *testing.T) {
c.On(name, call[0]...).Return(call[1]...)
}
p := &Provider{
log: logger,
clients: createMockClients(c, regions),
}
got, err := p.ListSubscriptionsByTopic(context.Background(), regions[0], tt.topic)
Expand Down Expand Up @@ -185,6 +201,47 @@ func TestProvider_ListTopicsWithSubscriptions(t *testing.T) {
},
},
},
{
name: "with error in ListTopics",
mocks: clientMocks{
"ListTopics": [2]mocks{
{mock.Anything, mock.Anything},
{nil, errMock},
},
"ListSubscriptionsByTopic": [2]mocks{
{mock.Anything, mock.Anything},
{nil, errMock},
},
},
wantErr: true,
},
{
name: "with error in ListSubscriptionsByTopic",
mocks: clientMocks{
"ListTopics": [2]mocks{
{mock.Anything, mock.Anything},
{&sns.ListTopicsOutput{
Topics: []types.Topic{
{
TopicArn: aws.String("topic-arn"),
},
},
}, nil},
},
"ListSubscriptionsByTopic": [2]mocks{
{mock.Anything, mock.Anything},
{nil, errMock},
},
},
want: []awslib.AwsResource{
&TopicInfo{
Topic: types.Topic{
TopicArn: pointers.Ref("topic-arn"),
},
Subscriptions: nil,
region: "us-east-1",
},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -193,6 +250,7 @@ func TestProvider_ListTopicsWithSubscriptions(t *testing.T) {
c.On(name, call[0]...).Return(call[1]...)
}
p := &Provider{
log: logger,
clients: createMockClients(c, regions),
}
got, err := p.ListTopicsWithSubscriptions(context.Background())
Expand Down