File: cloudformation-stackset_test.go

package info (click to toggle)
aws-nuke 2.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,852 kB
  • sloc: makefile: 17; sh: 1
file content (197 lines) | stat: -rw-r--r-- 6,683 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package resources

import (
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/cloudformation"
	"github.com/golang/mock/gomock"
	"github.com/rebuy-de/aws-nuke/mocks/mock_cloudformationiface"
	"github.com/stretchr/testify/assert"
)

func TestCloudformationStackSet_Remove(t *testing.T) {
	a := assert.New(t)
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	mockCloudformation := mock_cloudformationiface.NewMockCloudFormationAPI(ctrl)

	stackSet := CloudFormationStackSet{
		svc: mockCloudformation,
		stackSetSummary: &cloudformation.StackSetSummary{
			StackSetName: aws.String("foobar"),
		},
	}

	mockCloudformation.EXPECT().ListStackInstances(gomock.Eq(&cloudformation.ListStackInstancesInput{
		StackSetName: aws.String("foobar"),
	})).Return(&cloudformation.ListStackInstancesOutput{
		Summaries: []*cloudformation.StackInstanceSummary{
			{
				Account: aws.String("a1"),
				Region:  aws.String("r1"),
			},
			{
				Account: aws.String("a1"),
				Region:  aws.String("r2"),
			},
		},
	}, nil)

	mockCloudformation.EXPECT().DeleteStackInstances(gomock.Eq(&cloudformation.DeleteStackInstancesInput{
		StackSetName: aws.String("foobar"),
		Accounts:     []*string{aws.String("a1")},
		Regions:      []*string{aws.String("r1"), aws.String("r2")},
		RetainStacks: aws.Bool(true),
	})).Return(&cloudformation.DeleteStackInstancesOutput{
		OperationId: aws.String("o1"),
	}, nil)

	describeStackSetStatuses := []string{
		cloudformation.StackSetOperationResultStatusPending,
		cloudformation.StackSetOperationResultStatusRunning,
		cloudformation.StackSetOperationResultStatusSucceeded,
	}
	describeStackSetOperationCalls := make([]*gomock.Call, len(describeStackSetStatuses))
	for i, status := range describeStackSetStatuses {
		describeStackSetOperationCalls[i] = mockCloudformation.EXPECT().DescribeStackSetOperation(gomock.Eq(&cloudformation.DescribeStackSetOperationInput{
			OperationId:  aws.String("o1"),
			StackSetName: aws.String("foobar"),
		})).Return(&cloudformation.DescribeStackSetOperationOutput{
			StackSetOperation: &cloudformation.StackSetOperation{
				Status: aws.String(status),
			},
		}, nil)
	}
	gomock.InOrder(describeStackSetOperationCalls...)

	mockCloudformation.EXPECT().DeleteStackSet(gomock.Eq(&cloudformation.DeleteStackSetInput{
		StackSetName: aws.String("foobar"),
	})).Return(&cloudformation.DeleteStackSetOutput{}, nil)

	err := stackSet.Remove()
	a.Nil(err)
}

func TestCloudformationStackSet_Remove_MultipleAccounts(t *testing.T) {
	a := assert.New(t)
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	mockCloudformation := mock_cloudformationiface.NewMockCloudFormationAPI(ctrl)

	stackSet := CloudFormationStackSet{
		svc: mockCloudformation,
		stackSetSummary: &cloudformation.StackSetSummary{
			StackSetName: aws.String("foobar"),
		},
	}

	mockCloudformation.EXPECT().ListStackInstances(gomock.Eq(&cloudformation.ListStackInstancesInput{
		StackSetName: aws.String("foobar"),
	})).Return(&cloudformation.ListStackInstancesOutput{
		Summaries: []*cloudformation.StackInstanceSummary{
			{
				Account: aws.String("a1"),
				Region:  aws.String("r1"),
			},
			{
				Account: aws.String("a1"),
				Region:  aws.String("r2"),
			},
			{
				Account: aws.String("a2"),
				Region:  aws.String("r2"),
			},
		},
	}, nil)

	mockCloudformation.EXPECT().DeleteStackInstances(gomock.Eq(&cloudformation.DeleteStackInstancesInput{
		StackSetName: aws.String("foobar"),
		Accounts:     []*string{aws.String("a1")},
		Regions:      []*string{aws.String("r1"), aws.String("r2")},
		RetainStacks: aws.Bool(true),
	})).Return(&cloudformation.DeleteStackInstancesOutput{
		OperationId: aws.String("a1-oId"),
	}, nil)
	mockCloudformation.EXPECT().DeleteStackInstances(gomock.Eq(&cloudformation.DeleteStackInstancesInput{
		StackSetName: aws.String("foobar"),
		Accounts:     []*string{aws.String("a2")},
		Regions:      []*string{aws.String("r2")},
		RetainStacks: aws.Bool(true),
	})).Return(&cloudformation.DeleteStackInstancesOutput{
		OperationId: aws.String("a2-oId"),
	}, nil)

	mockCloudformation.EXPECT().DescribeStackSetOperation(gomock.Eq(&cloudformation.DescribeStackSetOperationInput{
		OperationId:  aws.String("a1-oId"),
		StackSetName: aws.String("foobar"),
	})).Return(&cloudformation.DescribeStackSetOperationOutput{
		StackSetOperation: &cloudformation.StackSetOperation{
			Status: aws.String(cloudformation.StackSetOperationResultStatusSucceeded),
		},
	}, nil)
	mockCloudformation.EXPECT().DescribeStackSetOperation(gomock.Eq(&cloudformation.DescribeStackSetOperationInput{
		OperationId:  aws.String("a2-oId"),
		StackSetName: aws.String("foobar"),
	})).Return(&cloudformation.DescribeStackSetOperationOutput{
		StackSetOperation: &cloudformation.StackSetOperation{
			Status: aws.String(cloudformation.StackSetOperationResultStatusSucceeded),
		},
	}, nil)

	mockCloudformation.EXPECT().DeleteStackSet(gomock.Eq(&cloudformation.DeleteStackSetInput{
		StackSetName: aws.String("foobar"),
	})).Return(&cloudformation.DeleteStackSetOutput{}, nil)

	err := stackSet.Remove()
	a.Nil(err)
}

func TestCloudformationStackSet_Remove_DeleteStackInstanceFailed(t *testing.T) {
	a := assert.New(t)
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	mockCloudformation := mock_cloudformationiface.NewMockCloudFormationAPI(ctrl)

	stackSet := CloudFormationStackSet{
		svc: mockCloudformation,
		stackSetSummary: &cloudformation.StackSetSummary{
			StackSetName: aws.String("foobar"),
		},
	}

	mockCloudformation.EXPECT().ListStackInstances(gomock.Eq(&cloudformation.ListStackInstancesInput{
		StackSetName: aws.String("foobar"),
	})).Return(&cloudformation.ListStackInstancesOutput{
		Summaries: []*cloudformation.StackInstanceSummary{
			{
				Account: aws.String("a1"),
				Region:  aws.String("r1"),
			},
		},
	}, nil)

	mockCloudformation.EXPECT().DeleteStackInstances(gomock.Eq(&cloudformation.DeleteStackInstancesInput{
		StackSetName: aws.String("foobar"),
		Accounts:     []*string{aws.String("a1")},
		Regions:      []*string{aws.String("r1")},
		RetainStacks: aws.Bool(true),
	})).Return(&cloudformation.DeleteStackInstancesOutput{
		OperationId: aws.String("o1"),
	}, nil)

	mockCloudformation.EXPECT().DescribeStackSetOperation(gomock.Eq(&cloudformation.DescribeStackSetOperationInput{
		OperationId:  aws.String("o1"),
		StackSetName: aws.String("foobar"),
	})).Return(&cloudformation.DescribeStackSetOperationOutput{
		StackSetOperation: &cloudformation.StackSetOperation{
			Status: aws.String(cloudformation.StackSetOperationResultStatusFailed),
		},
	}, nil)

	err := stackSet.Remove()
	a.EqualError(err, "unable to delete stackSet=foobar operationId=o1 status=FAILED")
}