Merge pull request #1584 from snyk/acc-test-retry-backoff

Use acceptance tests backoff-retry in more places
main
Elie 2022-08-26 12:28:10 +02:00 committed by GitHub
commit b664f62ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 8 additions and 187 deletions

View File

@ -1,15 +1,11 @@
package aws_test package aws_test
import ( import (
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
"testing" "testing"
"time" "time"
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/test" "github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/acceptance" "github.com/snyk/driftctl/test/acceptance"
"github.com/snyk/driftctl/test/acceptance/awsutils"
) )
func TestAcc_Aws_SNSTopicPolicy(t *testing.T) { func TestAcc_Aws_SNSTopicPolicy(t *testing.T) {
@ -22,23 +18,7 @@ func TestAcc_Aws_SNSTopicPolicy(t *testing.T) {
Env: map[string]string{ Env: map[string]string{
"AWS_REGION": "us-east-1", "AWS_REGION": "us-east-1",
}, },
PreExec: func() { ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
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")
}
},
Check: func(result *test.ScanResult, stdout string, err error) { Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -1,15 +1,11 @@
package aws_test package aws_test
import ( import (
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
"testing" "testing"
"time" "time"
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/test" "github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/acceptance" "github.com/snyk/driftctl/test/acceptance"
"github.com/snyk/driftctl/test/acceptance/awsutils"
) )
func TestAcc_Aws_SNSTopicSubscription(t *testing.T) { func TestAcc_Aws_SNSTopicSubscription(t *testing.T) {
@ -22,23 +18,7 @@ func TestAcc_Aws_SNSTopicSubscription(t *testing.T) {
Env: map[string]string{ Env: map[string]string{
"AWS_REGION": "us-east-1", "AWS_REGION": "us-east-1",
}, },
PreExec: func() { ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
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")
}
},
Check: func(result *test.ScanResult, stdout string, err error) { Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -1,13 +1,10 @@
package aws_test package aws_test
import ( import (
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/cache"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/test" "github.com/snyk/driftctl/test"
"github.com/aws/aws-sdk-go/service/sns" "github.com/aws/aws-sdk-go/service/sns"
@ -31,23 +28,7 @@ func TestAcc_Aws_SNSTopic(t *testing.T) {
Env: map[string]string{ Env: map[string]string{
"AWS_REGION": "us-east-1", "AWS_REGION": "us-east-1",
}, },
PreExec: func() { ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
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")
}
},
Check: func(result *test.ScanResult, stdout string, err error) { Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -4,11 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/test" "github.com/snyk/driftctl/test"
"github.com/snyk/driftctl/test/acceptance/awsutils"
"github.com/snyk/driftctl/test/acceptance" "github.com/snyk/driftctl/test/acceptance"
) )
@ -22,22 +18,7 @@ func TestAcc_Aws_SQSQueuePolicy(t *testing.T) {
Env: map[string]string{ Env: map[string]string{
"AWS_REGION": "us-east-1", "AWS_REGION": "us-east-1",
}, },
PreExec: func() { ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
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")
}
},
Check: func(result *test.ScanResult, stdout string, err error) { Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -4,17 +4,12 @@ import (
"testing" "testing"
"time" "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" awsresources "github.com/snyk/driftctl/enumeration/resource/aws"
"github.com/snyk/driftctl/pkg/analyser" "github.com/snyk/driftctl/pkg/analyser"
"github.com/snyk/driftctl/test" "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"
"github.com/snyk/driftctl/test/acceptance/awsutils" "github.com/snyk/driftctl/test/acceptance/awsutils"
) )
@ -30,22 +25,7 @@ func TestAcc_Aws_SQSQueue(t *testing.T) {
Env: map[string]string{ Env: map[string]string{
"AWS_REGION": "us-east-1", "AWS_REGION": "us-east-1",
}, },
PreExec: func() { ShouldRetry: acceptance.LinearBackoff(10 * time.Minute),
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")
}
},
Check: func(result *test.ScanResult, stdout string, err error) { Check: func(result *test.ScanResult, stdout string, err error) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -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 // LinearBackoff returns a function that retries using
// a back-off strategy of retrying 'n' times and doubling the // a back-off strategy of retrying 'n' times and doubling the
// amount of time waited after each one. // amount of time waited after each one.

View File

@ -1,18 +1,12 @@
package acceptance package acceptance
import ( import (
"context"
"errors"
"os" "os"
"reflect" "reflect"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert"
) )
func TestAccTestCase_resolveTerraformEnv(t *testing.T) { func TestAccTestCase_resolveTerraformEnv(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("ACC_TEST_VAR", "foobar") os.Setenv("ACC_TEST_VAR", "foobar")
os.Setenv("TEST_VAR", "barfoo") os.Setenv("TEST_VAR", "barfoo")
@ -34,48 +28,4 @@ func TestAccTestCase_resolveTerraformEnv(t *testing.T) {
if !reflect.DeepEqual(expected, env) { if !reflect.DeepEqual(expected, env) {
t.Fatalf("Variable env override not working, got: %+v, expected %+v", env, expected) 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)
})
}
} }