Skip to content

Commit b664f62

Browse files
authored
Merge pull request #1584 from snyk/acc-test-retry-backoff
Use acceptance tests backoff-retry in more places
2 parents ed58b6d + d1a0c63 commit b664f62

File tree

7 files changed

+8
-187
lines changed

7 files changed

+8
-187
lines changed

pkg/resource/aws/aws_sns_topic_policy_test.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package aws_test
22

33
import (
4-
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
5-
"github.com/snyk/driftctl/enumeration/remote/cache"
64
"testing"
75
"time"
86

9-
"github.com/sirupsen/logrus"
107
"github.com/snyk/driftctl/test"
118
"github.com/snyk/driftctl/test/acceptance"
12-
"github.com/snyk/driftctl/test/acceptance/awsutils"
139
)
1410

1511
func TestAcc_Aws_SNSTopicPolicy(t *testing.T) {
@@ -22,23 +18,7 @@ func TestAcc_Aws_SNSTopicPolicy(t *testing.T) {
2218
Env: map[string]string{
2319
"AWS_REGION": "us-east-1",
2420
},
25-
PreExec: func() {
26-
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
27-
client := repository.NewSNSRepository(awsutils.Session(), cache.New(0))
28-
topics, err := client.ListAllTopics()
29-
if err != nil {
30-
logrus.Warnf("Cannot list topics: %+v", err)
31-
return err
32-
}
33-
if len(topics) == 3 {
34-
doneCh <- struct{}{}
35-
}
36-
return nil
37-
})
38-
if err != nil {
39-
t.Fatal("Timeout while fetching SNS TOPIC")
40-
}
41-
},
21+
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
4222
Check: func(result *test.ScanResult, stdout string, err error) {
4323
if err != nil {
4424
t.Fatal(err)

pkg/resource/aws/aws_sns_topic_subscription_test.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package aws_test
22

33
import (
4-
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
5-
"github.com/snyk/driftctl/enumeration/remote/cache"
64
"testing"
75
"time"
86

9-
"github.com/sirupsen/logrus"
107
"github.com/snyk/driftctl/test"
118
"github.com/snyk/driftctl/test/acceptance"
12-
"github.com/snyk/driftctl/test/acceptance/awsutils"
139
)
1410

1511
func TestAcc_Aws_SNSTopicSubscription(t *testing.T) {
@@ -22,23 +18,7 @@ func TestAcc_Aws_SNSTopicSubscription(t *testing.T) {
2218
Env: map[string]string{
2319
"AWS_REGION": "us-east-1",
2420
},
25-
PreExec: func() {
26-
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
27-
client := repository.NewSNSRepository(awsutils.Session(), cache.New(0))
28-
topics, err := client.ListAllSubscriptions()
29-
if err != nil {
30-
logrus.Warnf("Cannot list Subscriptions: %+v", err)
31-
return err
32-
}
33-
if len(topics) == 2 {
34-
doneCh <- struct{}{}
35-
}
36-
return nil
37-
})
38-
if err != nil {
39-
t.Fatal("Timeout while fetching SNS Subscriptions")
40-
}
41-
},
21+
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
4222
Check: func(result *test.ScanResult, stdout string, err error) {
4323
if err != nil {
4424
t.Fatal(err)

pkg/resource/aws/aws_sns_topic_test.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package aws_test
22

33
import (
4-
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
5-
"github.com/snyk/driftctl/enumeration/remote/cache"
64
"strings"
75
"testing"
86
"time"
97

10-
"github.com/sirupsen/logrus"
118
"github.com/snyk/driftctl/test"
129

1310
"github.com/aws/aws-sdk-go/service/sns"
@@ -31,23 +28,7 @@ func TestAcc_Aws_SNSTopic(t *testing.T) {
3128
Env: map[string]string{
3229
"AWS_REGION": "us-east-1",
3330
},
34-
PreExec: func() {
35-
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
36-
client := repository.NewSNSRepository(awsutils.Session(), cache.New(0))
37-
topics, err := client.ListAllTopics()
38-
if err != nil {
39-
logrus.Warnf("Cannot list topics: %+v", err)
40-
return err
41-
}
42-
if len(topics) == 3 {
43-
doneCh <- struct{}{}
44-
}
45-
return nil
46-
})
47-
if err != nil {
48-
t.Fatal("Timeout while fetching SNS TOPIC")
49-
}
50-
},
31+
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
5132
Check: func(result *test.ScanResult, stdout string, err error) {
5233
if err != nil {
5334
t.Fatal(err)

pkg/resource/aws/aws_sqs_queue_policy_test.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import (
44
"testing"
55
"time"
66

7-
"github.com/aws/aws-sdk-go/service/sqs"
8-
"github.com/sirupsen/logrus"
97
"github.com/snyk/driftctl/test"
10-
"github.com/snyk/driftctl/test/acceptance/awsutils"
11-
128
"github.com/snyk/driftctl/test/acceptance"
139
)
1410

@@ -22,22 +18,7 @@ func TestAcc_Aws_SQSQueuePolicy(t *testing.T) {
2218
Env: map[string]string{
2319
"AWS_REGION": "us-east-1",
2420
},
25-
PreExec: func() {
26-
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
27-
return sqs.New(awsutils.Session()).ListQueuesPages(&sqs.ListQueuesInput{},
28-
func(resp *sqs.ListQueuesOutput, lastPage bool) bool {
29-
logrus.Debugf("Retrieved %d SQS queues", len(resp.QueueUrls))
30-
if len(resp.QueueUrls) >= 3 {
31-
doneCh <- struct{}{}
32-
}
33-
return !lastPage
34-
},
35-
)
36-
})
37-
if err != nil {
38-
t.Fatal("Timeout while fetching SQS queues")
39-
}
40-
},
21+
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
4122
Check: func(result *test.ScanResult, stdout string, err error) {
4223
if err != nil {
4324
t.Fatal(err)

pkg/resource/aws/aws_sqs_queue_test.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ import (
44
"testing"
55
"time"
66

7-
"github.com/sirupsen/logrus"
8-
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/service/sqs"
9+
"github.com/r3labs/diff/v2"
910
awsresources "github.com/snyk/driftctl/enumeration/resource/aws"
1011
"github.com/snyk/driftctl/pkg/analyser"
1112
"github.com/snyk/driftctl/test"
12-
13-
"github.com/r3labs/diff/v2"
14-
15-
"github.com/aws/aws-sdk-go/aws"
16-
17-
"github.com/aws/aws-sdk-go/service/sqs"
1813
"github.com/snyk/driftctl/test/acceptance"
1914
"github.com/snyk/driftctl/test/acceptance/awsutils"
2015
)
@@ -30,22 +25,7 @@ func TestAcc_Aws_SQSQueue(t *testing.T) {
3025
Env: map[string]string{
3126
"AWS_REGION": "us-east-1",
3227
},
33-
PreExec: func() {
34-
err := acceptance.RetryFor(60*time.Second, func(doneCh chan struct{}) error {
35-
return sqs.New(awsutils.Session()).ListQueuesPages(&sqs.ListQueuesInput{},
36-
func(resp *sqs.ListQueuesOutput, lastPage bool) bool {
37-
logrus.Debugf("Retrieved %d SQS queues", len(resp.QueueUrls))
38-
if len(resp.QueueUrls) >= 2 {
39-
doneCh <- struct{}{}
40-
}
41-
return !lastPage
42-
},
43-
)
44-
})
45-
if err != nil {
46-
t.Fatal("Timeout while fetching SQS queues")
47-
}
48-
},
28+
ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
4929
Check: func(result *test.ScanResult, stdout string, err error) {
5030
if err != nil {
5131
t.Fatal(err)

test/acceptance/testing.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -476,37 +476,6 @@ func Run(t *testing.T, c AccTestCase) {
476476
}
477477
}
478478

479-
func RetryFor(timeout time.Duration, f func(c chan struct{}) error) error {
480-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
481-
defer cancel()
482-
483-
doneCh := make(chan struct{}, 1)
484-
errCh := make(chan error, 1)
485-
go func() {
486-
for {
487-
select {
488-
case <-ctx.Done():
489-
return
490-
default:
491-
if err := f(doneCh); err != nil {
492-
errCh <- err
493-
return
494-
}
495-
time.Sleep(1 * time.Second)
496-
}
497-
}
498-
}()
499-
500-
select {
501-
case <-doneCh:
502-
return nil
503-
case err := <-errCh:
504-
return err
505-
case <-ctx.Done():
506-
return ctx.Err()
507-
}
508-
}
509-
510479
// LinearBackoff returns a function that retries using
511480
// a back-off strategy of retrying 'n' times and doubling the
512481
// amount of time waited after each one.

test/acceptance/testing_test.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package acceptance
22

33
import (
4-
"context"
5-
"errors"
64
"os"
75
"reflect"
86
"testing"
9-
"time"
10-
11-
"github.com/stretchr/testify/assert"
127
)
138

149
func TestAccTestCase_resolveTerraformEnv(t *testing.T) {
15-
1610
os.Clearenv()
1711
os.Setenv("ACC_TEST_VAR", "foobar")
1812
os.Setenv("TEST_VAR", "barfoo")
@@ -34,48 +28,4 @@ func TestAccTestCase_resolveTerraformEnv(t *testing.T) {
3428
if !reflect.DeepEqual(expected, env) {
3529
t.Fatalf("Variable env override not working, got: %+v, expected %+v", env, expected)
3630
}
37-
38-
}
39-
40-
func TestRetryFor(t *testing.T) {
41-
randomError := errors.New("random error")
42-
43-
cases := []struct {
44-
name string
45-
timeout time.Duration
46-
f func(c chan struct{}) error
47-
err error
48-
}{
49-
{
50-
name: "success on first try",
51-
timeout: 100 * time.Millisecond,
52-
f: func(c chan struct{}) error {
53-
c <- struct{}{}
54-
return nil
55-
},
56-
},
57-
{
58-
name: "timeout exceeded",
59-
timeout: 1 * time.Millisecond,
60-
f: func(c chan struct{}) error {
61-
return nil
62-
},
63-
err: context.DeadlineExceeded,
64-
},
65-
{
66-
name: "error before timeout exceeded",
67-
timeout: 1 * time.Millisecond,
68-
f: func(c chan struct{}) error {
69-
return randomError
70-
},
71-
err: randomError,
72-
},
73-
}
74-
75-
for _, c := range cases {
76-
t.Run(c.name, func(tt *testing.T) {
77-
err := RetryFor(c.timeout, c.f)
78-
assert.Equal(tt, c.err, err)
79-
})
80-
}
8131
}

0 commit comments

Comments
 (0)