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

Use acceptance tests backoff-retry in more places #1584

Merged
merged 1 commit into from
Aug 26, 2022
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
22 changes: 1 addition & 21 deletions pkg/resource/aws/aws_sns_topic_policy_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package aws_test

import (
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/acceptance"
"github.com/snyk/driftctl/test/acceptance/awsutils"
)

func TestAcc_Aws_SNSTopicPolicy(t *testing.T) {
Expand All @@ -22,23 +18,7 @@ func TestAcc_Aws_SNSTopicPolicy(t *testing.T) {
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
PreExec: func() {
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
client := repository.NewSNSRepository(awsutils.Session(), cache.New(0))
topics, err := client.ListAllTopics()
if err != nil {
logrus.Warnf("Cannot list topics: %+v", err)
return err
}
if len(topics) == 3 {
doneCh <- struct{}{}
}
return nil
})
if err != nil {
t.Fatal("Timeout while fetching SNS TOPIC")
}
},
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
Expand Down
22 changes: 1 addition & 21 deletions pkg/resource/aws/aws_sns_topic_subscription_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package aws_test

import (
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/acceptance"
"github.com/snyk/driftctl/test/acceptance/awsutils"
)

func TestAcc_Aws_SNSTopicSubscription(t *testing.T) {
Expand All @@ -22,23 +18,7 @@ func TestAcc_Aws_SNSTopicSubscription(t *testing.T) {
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
PreExec: func() {
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
client := repository.NewSNSRepository(awsutils.Session(), cache.New(0))
topics, err := client.ListAllSubscriptions()
if err != nil {
logrus.Warnf("Cannot list Subscriptions: %+v", err)
return err
}
if len(topics) == 2 {
doneCh <- struct{}{}
}
return nil
})
if err != nil {
t.Fatal("Timeout while fetching SNS Subscriptions")
}
},
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
Expand Down
21 changes: 1 addition & 20 deletions pkg/resource/aws/aws_sns_topic_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package aws_test

import (
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
"strings"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/test"

"github.com/aws/aws-sdk-go/service/sns"
Expand All @@ -31,23 +28,7 @@ func TestAcc_Aws_SNSTopic(t *testing.T) {
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
PreExec: func() {
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
client := repository.NewSNSRepository(awsutils.Session(), cache.New(0))
topics, err := client.ListAllTopics()
if err != nil {
logrus.Warnf("Cannot list topics: %+v", err)
return err
}
if len(topics) == 3 {
doneCh <- struct{}{}
}
return nil
})
if err != nil {
t.Fatal("Timeout while fetching SNS TOPIC")
}
},
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
Expand Down
21 changes: 1 addition & 20 deletions pkg/resource/aws/aws_sqs_queue_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/service/sqs"
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/acceptance/awsutils"

"github.com/snyk/driftctl/test/acceptance"
)

Expand All @@ -22,22 +18,7 @@ func TestAcc_Aws_SQSQueuePolicy(t *testing.T) {
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
PreExec: func() {
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
return sqs.New(awsutils.Session()).ListQueuesPages(&sqs.ListQueuesInput{},
func(resp *sqs.ListQueuesOutput, lastPage bool) bool {
logrus.Debugf("Retrieved %d SQS queues", len(resp.QueueUrls))
if len(resp.QueueUrls) >= 3 {
doneCh <- struct{}{}
}
return !lastPage
},
)
})
if err != nil {
t.Fatal("Timeout while fetching SQS queues")
}
},
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
Expand Down
28 changes: 4 additions & 24 deletions pkg/resource/aws/aws_sqs_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import (
"testing"
"time"

"github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/r3labs/diff/v2"
awsresources "github.com/snyk/driftctl/enumeration/resource/aws"
"github.com/snyk/driftctl/pkg/analyser"
"github.com/snyk/driftctl/test"

"github.com/r3labs/diff/v2"

"github.com/aws/aws-sdk-go/aws"

"github.com/aws/aws-sdk-go/service/sqs"
"github.com/snyk/driftctl/test/acceptance"
"github.com/snyk/driftctl/test/acceptance/awsutils"
)
Expand All @@ -30,22 +25,7 @@ func TestAcc_Aws_SQSQueue(t *testing.T) {
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
PreExec: func() {
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
return sqs.New(awsutils.Session()).ListQueuesPages(&sqs.ListQueuesInput{},
func(resp *sqs.ListQueuesOutput, lastPage bool) bool {
logrus.Debugf("Retrieved %d SQS queues", len(resp.QueueUrls))
if len(resp.QueueUrls) >= 2 {
doneCh <- struct{}{}
}
return !lastPage
},
)
})
if err != nil {
t.Fatal("Timeout while fetching SQS queues")
}
},
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
Expand Down
31 changes: 0 additions & 31 deletions test/acceptance/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,37 +476,6 @@ func Run(t *testing.T, c AccTestCase) {
}
}

func RetryFor(timeout time.Duration, f func(c chan struct{}) error) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

doneCh := make(chan struct{}, 1)
errCh := make(chan error, 1)
go func() {
for {
select {
case <-ctx.Done():
return
default:
if err := f(doneCh); err != nil {
errCh <- err
return
}
time.Sleep(1 * time.Second)
}
}
}()

select {
case <-doneCh:
return nil
case err := <-errCh:
return err
case <-ctx.Done():
return ctx.Err()
}
}

// LinearBackoff returns a function that retries using
// a back-off strategy of retrying 'n' times and doubling the
// amount of time waited after each one.
Expand Down
50 changes: 0 additions & 50 deletions test/acceptance/testing_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package acceptance

import (
"context"
"errors"
"os"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestAccTestCase_resolveTerraformEnv(t *testing.T) {

os.Clearenv()
os.Setenv("ACC_TEST_VAR", "foobar")
os.Setenv("TEST_VAR", "barfoo")
Expand All @@ -34,48 +28,4 @@ func TestAccTestCase_resolveTerraformEnv(t *testing.T) {
if !reflect.DeepEqual(expected, env) {
t.Fatalf("Variable env override not working, got: %+v, expected %+v", env, expected)
}

}

func TestRetryFor(t *testing.T) {
randomError := errors.New("random error")

cases := []struct {
name string
timeout time.Duration
f func(c chan struct{}) error
err error
}{
{
name: "success on first try",
timeout: 100 * time.Millisecond,
f: func(c chan struct{}) error {
c <- struct{}{}
return nil
},
},
{
name: "timeout exceeded",
timeout: 1 * time.Millisecond,
f: func(c chan struct{}) error {
return nil
},
err: context.DeadlineExceeded,
},
{
name: "error before timeout exceeded",
timeout: 1 * time.Millisecond,
f: func(c chan struct{}) error {
return randomError
},
err: randomError,
},
}

for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
err := RetryFor(c.timeout, c.f)
assert.Equal(tt, c.err, err)
})
}
}