Skip to content

Commit e087555

Browse files
committed
Add unit test for helper pkg
1 parent d9d46fd commit e087555

16 files changed

+8136
-0
lines changed

pkg/helpers/atom_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package helpers_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/convox/rack/pkg/helpers"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestAtomStatus(t *testing.T) {
11+
testData := []struct {
12+
given, expect string
13+
}{
14+
{
15+
given: "Failed",
16+
expect: "failed",
17+
},
18+
{
19+
given: "Rollback",
20+
expect: "rollback",
21+
},
22+
{
23+
given: "Deadline",
24+
expect: "updating",
25+
},
26+
{
27+
given: "dfdf",
28+
expect: "running",
29+
},
30+
}
31+
32+
for _, td := range testData {
33+
require.Equal(t, td.expect, helpers.AtomStatus(td.given))
34+
}
35+
}

pkg/helpers/aws_test.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package helpers_test
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"context"
7+
"testing"
8+
"time"
9+
10+
"github.com/aws/aws-sdk-go/aws"
11+
"github.com/aws/aws-sdk-go/service/cloudformation"
12+
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
13+
"github.com/convox/rack/pkg/helpers"
14+
mockaws "github.com/convox/rack/pkg/mock/aws"
15+
"github.com/convox/rack/pkg/structs"
16+
"github.com/stretchr/testify/mock"
17+
"github.com/stretchr/testify/require"
18+
)
19+
20+
func TestCloudformationDescribe(t *testing.T) {
21+
cf := &mockaws.CloudFormationAPI{}
22+
expectStack := &cloudformation.Stack{
23+
StackId: aws.String("id1"),
24+
StackName: aws.String("stack1"),
25+
}
26+
27+
cf.On("DescribeStacks", mock.Anything).Return(func(st *cloudformation.DescribeStacksInput) *cloudformation.DescribeStacksOutput {
28+
require.Equal(t, expectStack.StackName, st.StackName)
29+
return &cloudformation.DescribeStacksOutput{
30+
Stacks: []*cloudformation.Stack{
31+
expectStack,
32+
},
33+
}
34+
}, nil)
35+
36+
stack, err := helpers.CloudformationDescribe(cf, *expectStack.StackName)
37+
require.NoError(t, err)
38+
require.Equal(t, expectStack, stack)
39+
}
40+
41+
func TestCloudformationInstall(t *testing.T) {
42+
cf := &mockaws.CloudFormationAPI{}
43+
expectStack := &cloudformation.Stack{
44+
StackId: aws.String("id1"),
45+
StackName: aws.String("stack1"),
46+
StackStatus: aws.String("CREATE_COMPLETE"),
47+
}
48+
49+
cf.On("CreateChangeSet", mock.Anything).Return(func(st *cloudformation.CreateChangeSetInput) *cloudformation.CreateChangeSetOutput {
50+
return &cloudformation.CreateChangeSetOutput{
51+
StackId: expectStack.StackId,
52+
}
53+
}, nil)
54+
55+
cf.On("WaitUntilChangeSetCreateComplete", mock.Anything).Return(func(in *cloudformation.DescribeChangeSetInput) error {
56+
require.Equal(t, expectStack.StackId, in.StackName)
57+
return nil
58+
})
59+
60+
cf.On("DescribeChangeSet", mock.Anything).Return(func(in *cloudformation.DescribeChangeSetInput) *cloudformation.DescribeChangeSetOutput {
61+
require.Equal(t, expectStack.StackId, in.StackName)
62+
return &cloudformation.DescribeChangeSetOutput{}
63+
}, nil)
64+
65+
cf.On("ExecuteChangeSet", mock.Anything).Return(nil, func(in *cloudformation.ExecuteChangeSetInput) error {
66+
require.Equal(t, expectStack.StackId, in.StackName)
67+
return nil
68+
})
69+
70+
cf.On("DescribeStacks", mock.Anything).Return(&cloudformation.DescribeStacksOutput{
71+
Stacks: []*cloudformation.Stack{
72+
expectStack,
73+
},
74+
}, nil)
75+
76+
err := helpers.CloudformationInstall(cf, *expectStack.StackName, "", map[string]string{}, map[string]string{}, func(i1, i2 int) {})
77+
require.NoError(t, err)
78+
}
79+
80+
func TestCloudformationParameters(t *testing.T) {
81+
data := `
82+
Parameters:
83+
key1: 1
84+
key2: 2
85+
`
86+
expect := map[string]bool{
87+
"key1": true,
88+
"key2": true,
89+
}
90+
got, err := helpers.CloudformationParameters([]byte(data))
91+
require.NoError(t, err)
92+
require.Equal(t, expect, got)
93+
}
94+
95+
func TestCloudformationUninstall(t *testing.T) {
96+
cf := &mockaws.CloudFormationAPI{}
97+
expectStack := &cloudformation.Stack{
98+
StackId: aws.String("id1"),
99+
StackName: aws.String("stack1"),
100+
StackStatus: aws.String("CREATE_COMPLETE"),
101+
}
102+
103+
cf.On("DeleteStack", mock.Anything).Return(nil, nil)
104+
105+
cf.On("WaitUntilStackDeleteComplete", mock.Anything).Return(func(in *cloudformation.DescribeStacksInput) error {
106+
require.Equal(t, expectStack.StackName, in.StackName)
107+
return nil
108+
})
109+
110+
err := helpers.CloudformationUninstall(cf, *expectStack.StackName)
111+
require.NoError(t, err)
112+
}
113+
114+
func TestCloudformationUpdate(t *testing.T) {
115+
cf := &mockaws.CloudFormationAPI{}
116+
expectStack := &cloudformation.Stack{
117+
StackId: aws.String("id1"),
118+
StackName: aws.String("stack1"),
119+
Parameters: []*cloudformation.Parameter{
120+
{
121+
ParameterKey: aws.String("param1"),
122+
ParameterValue: aws.String("old"),
123+
},
124+
},
125+
}
126+
127+
cf.On("DescribeStacks", mock.Anything).Return(&cloudformation.DescribeStacksOutput{
128+
Stacks: []*cloudformation.Stack{
129+
expectStack,
130+
},
131+
}, nil)
132+
133+
cf.On("UpdateStack", mock.Anything).Return(func(in *cloudformation.UpdateStackInput) *cloudformation.UpdateStackOutput {
134+
require.Equal(t, expectStack.Parameters[0].ParameterKey, in.Parameters[0].ParameterKey)
135+
require.Equal(t, aws.String("new"), in.Parameters[0].ParameterValue)
136+
return nil
137+
}, nil)
138+
139+
err := helpers.CloudformationUpdate(cf, *expectStack.StackName, "", map[string]string{
140+
"param1": "new",
141+
}, map[string]string{}, "")
142+
require.NoError(t, err)
143+
}
144+
145+
func TestCloudWatchLogsStream(t *testing.T) {
146+
cf := &mockaws.CloudWatchLogsAPI{}
147+
cf.On("FilterLogEvents", mock.Anything).Return(func(in *cloudwatchlogs.FilterLogEventsInput) *cloudwatchlogs.FilterLogEventsOutput {
148+
return &cloudwatchlogs.FilterLogEventsOutput{
149+
Events: []*cloudwatchlogs.FilteredLogEvent{
150+
{
151+
EventId: aws.String("id1"),
152+
IngestionTime: aws.Int64(time.Now().Unix()),
153+
LogStreamName: aws.String("strm"),
154+
Message: aws.String("hello"),
155+
Timestamp: aws.Int64(time.Now().Unix()),
156+
},
157+
},
158+
}
159+
}, nil)
160+
161+
ctx, cancel := context.WithCancel(context.Background())
162+
163+
go func() {
164+
time.Sleep(1 * time.Second)
165+
cancel()
166+
}()
167+
168+
wc := &myWriteCloser{
169+
bufio.NewWriter(&bytes.Buffer{}),
170+
}
171+
err := helpers.CloudWatchLogsStream(ctx, cf, wc, "grp", "strm", structs.LogsOptions{})
172+
require.NoError(t, err)
173+
}
174+
175+
type myWriteCloser struct {
176+
*bufio.Writer
177+
}
178+
179+
func (mwc *myWriteCloser) Close() error {
180+
// Noop
181+
return nil
182+
}

pkg/helpers/certificate_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package helpers_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/convox/rack/pkg/helpers"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestCertificate(t *testing.T) {
11+
cert, err := helpers.CertificateSelfSigned("convox.com")
12+
require.NoError(t, err)
13+
14+
_, err = helpers.CertificateCA("convox.com", cert)
15+
require.NoError(t, err)
16+
}

pkg/helpers/coalesec_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package helpers_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/convox/rack/pkg/helpers"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestCoalesceInt(t *testing.T) {
11+
testData := []struct {
12+
given []int
13+
expect int
14+
}{
15+
{
16+
given: []int{1, 2, 3, 4, 5},
17+
expect: 1,
18+
},
19+
{
20+
given: []int{1},
21+
expect: 1,
22+
},
23+
{
24+
given: []int{0, 0, 0, 2, 4},
25+
expect: 2,
26+
},
27+
}
28+
29+
for _, td := range testData {
30+
require.Equal(t, td.expect, helpers.CoalesceInt(td.given...))
31+
}
32+
}
33+
34+
func TestCoalesceString(t *testing.T) {
35+
testData := []struct {
36+
given []string
37+
expect string
38+
}{
39+
{
40+
given: []string{"1", "2"},
41+
expect: "1",
42+
},
43+
{
44+
given: []string{"1"},
45+
expect: "1",
46+
},
47+
{
48+
given: []string{"", "", "2", "3"},
49+
expect: "2",
50+
},
51+
}
52+
53+
for _, td := range testData {
54+
require.Equal(t, td.expect, helpers.CoalesceString(td.given...))
55+
}
56+
}

pkg/helpers/endpoint_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package helpers_test
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"testing"
7+
8+
"github.com/convox/rack/pkg/helpers"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestEndpointCheck(t *testing.T) {
13+
ht := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14+
return
15+
}))
16+
defer ht.Close()
17+
18+
err := helpers.EndpointCheck(ht.URL)
19+
require.NoError(t, err)
20+
}
21+
22+
func TestEndpointWait(t *testing.T) {
23+
ht := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
24+
return
25+
}))
26+
defer ht.Close()
27+
28+
err := helpers.EndpointWait(ht.URL)
29+
require.NoError(t, err)
30+
}

pkg/helpers/env_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package helpers_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/convox/rack/pkg/helpers"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestEnvDiff(t *testing.T) {
12+
testData := []struct {
13+
env1 map[string]string
14+
env2 map[string]string
15+
expect string
16+
}{
17+
{
18+
env1: map[string]string{
19+
"a": "1",
20+
"b": "1",
21+
},
22+
env2: map[string]string{
23+
"a": "2",
24+
"c": "1",
25+
},
26+
expect: "add:c change:a remove:b",
27+
},
28+
{
29+
env1: map[string]string{
30+
"a": "1",
31+
"b": "1",
32+
},
33+
env2: map[string]string{
34+
"a": "1",
35+
"b": "1",
36+
},
37+
expect: "",
38+
},
39+
}
40+
41+
envGen := func(m map[string]string) string {
42+
out := ""
43+
for k, v := range m {
44+
out = out + fmt.Sprintf("%s=%s\n", k, v)
45+
}
46+
return out
47+
}
48+
49+
for _, td := range testData {
50+
got, err := helpers.EnvDiff(envGen(td.env1), envGen(td.env2))
51+
require.NoError(t, err)
52+
require.Equal(t, td.expect, got)
53+
}
54+
}

0 commit comments

Comments
 (0)