2021-01-25 17:43:05 +00:00
|
|
|
package acceptance
|
|
|
|
|
|
|
|
import (
|
2021-02-04 21:06:09 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2021-01-25 17:43:05 +00:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2021-02-04 21:06:09 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-01-25 17:43:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccTestCase_resolveTerraformEnv(t *testing.T) {
|
|
|
|
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("ACC_TEST_VAR", "foobar")
|
|
|
|
os.Setenv("TEST_VAR", "barfoo")
|
|
|
|
os.Setenv("TEST_VAR_2", "barfoo")
|
|
|
|
os.Setenv("ACC_TEST_VAR_3", "")
|
|
|
|
os.Setenv("TEST_VAR_3", "barfoo")
|
|
|
|
os.Setenv("TEST_VAR_4", "barfoo")
|
|
|
|
os.Setenv("ACC_TEST_VAR_4", "")
|
|
|
|
|
|
|
|
testCase := AccTestCase{}
|
|
|
|
env := testCase.resolveTerraformEnv()
|
2021-02-05 12:19:10 +00:00
|
|
|
expected := map[string]string{
|
2021-02-05 12:40:33 +00:00
|
|
|
"TEST_VAR": "foobar",
|
2021-02-05 12:19:10 +00:00
|
|
|
"TEST_VAR_2": "barfoo",
|
|
|
|
"TEST_VAR_3": "",
|
|
|
|
"TEST_VAR_4": "",
|
|
|
|
}
|
2021-01-25 17:43:05 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, env) {
|
|
|
|
t.Fatalf("Variable env override not working, got: %+v, expected %+v", env, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-02-04 21:06:09 +00:00
|
|
|
|
|
|
|
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: 1 * 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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|