Skip to content

Commit

Permalink
[8.17](backport #2784) Fix SNS topics fetch panics on errors (#2903)
Browse files Browse the repository at this point in the history
Fix SNS topics fetch panics on errors (#2784)

* SNS panic fix

* Fixing lint and tests

* Linter

(cherry picked from commit 3dc0d07)

Co-authored-by: Evgeniy Belyi <[email protected]>
Co-authored-by: Rômulo Farias <[email protected]>
  • Loading branch information
3 people authored Jan 7, 2025
1 parent 6f3c002 commit 288f80b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
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

0 comments on commit 288f80b

Please sign in to comment.