|
| 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 | +} |
0 commit comments