Use acceptance tests backoff-retry in more places
Remove RetryFor function, replace all uses of it with ShouldRetry.main
parent
ed58b6d645
commit
d1a0c63f4c
|
@ -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) {
|
||||
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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")
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue