forked from elliotchance/mocksqs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
101 lines (82 loc) · 2.05 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package mocksqs_test
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
"github.com/elliotchance/mocksqs"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
)
const uuidRegexp = `[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}`
type clientDetails struct {
client sqsiface.SQSAPI
queueURL string
queueName string
cleanup func()
}
func assertRegexpError(t *testing.T, err error, regexp string) {
require.Error(t, err)
assert.Regexp(t, regexp, err.Error())
}
func dereferenceAWSStrings(ss1 []*string) (ss2 []string) {
for _, ss := range ss1 {
ss2 = append(ss2, *ss)
}
return
}
func assertContainsAWSString(t *testing.T, expected string, actual []*string) {
assert.Contains(t, dereferenceAWSStrings(actual), expected)
}
func assertAWSString(t *testing.T, expected, actual *string) {
if expected == nil {
assert.Nil(t, actual)
return
}
require.NotNil(t, actual)
assert.Equal(t, *expected, *actual)
}
func getRealSQSClient() *clientDetails {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
return &clientDetails{
client: sqs.New(sess),
cleanup: func() {},
}
}
func getMockSQSClient() *clientDetails {
client := mocksqs.New()
return &clientDetails{
client: client,
cleanup: func() {},
}
}
func getSQSClient() *clientDetails {
if os.Getenv("INTEGRATION") != "" {
return getRealSQSClient()
}
return getMockSQSClient()
}
func getSQSClientWithQueue() *clientDetails {
client := getSQSClient()
client.queueName = uuid.New().String()
result, err := client.client.CreateQueue(&sqs.CreateQueueInput{
QueueName: &client.queueName,
})
if err != nil {
panic(err)
}
client.queueURL = *result.QueueUrl
client.cleanup = func() {
_, err := client.client.DeleteQueue(&sqs.DeleteQueueInput{
QueueUrl: &client.queueURL,
})
if err != nil {
panic(err)
}
}
return client
}