Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option for complex matching against s3 buckets #113

Merged
merged 35 commits into from
Jun 15, 2020
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b6cf24f
Update README.md
May 5, 2020
ca1f734
Mock a config package that reads a file
May 5, 2020
fc192c7
Add some todo comments
May 5, 2020
d5ebb98
Support filtering by s3 bucket names
May 6, 2020
5af5a1d
Update README.md
May 6, 2020
8ac6bc1
Remove some incomplete files for now
May 6, 2020
465e0cb
Restructure config format
rhoboat Jun 9, 2020
95c315e
Add config mocks to exclude names by regex
rhoboat Jun 9, 2020
c15edf9
Add support for excluding s3 bucketNames by regex
rhoboat Jun 9, 2020
73ca2d7
Improve logging of nukable resources
rhoboat Jun 9, 2020
ac3bbb2
Config tests
rhoboat Jun 10, 2020
5b68287
Remove unnecessary tags
rhoboat Jun 10, 2020
9e2f913
Rename some things
rhoboat Jun 10, 2020
dad4af4
Apply suggestions from code review
rhoboat Jun 10, 2020
7be18cf
Code review changes
rhoboat Jun 10, 2020
10aeb5c
Pointer changes
rhoboat Jun 10, 2020
1bb7bb7
Add tables to show supported config file options
rhoboat Jun 11, 2020
d49a650
Add description of include and exclude used together
rhoboat Jun 11, 2020
50e84b8
Tweaks to readme
rhoboat Jun 11, 2020
b20e17f
Tweaks
rhoboat Jun 11, 2020
9c7be1e
More tweaks
rhoboat Jun 11, 2020
616e544
Refactor to reduce cyclomatic complexity
rhoboat Jun 11, 2020
cb195f9
Move filtering logic into s3 module
rhoboat Jun 11, 2020
6a057e2
Test
rhoboat Jun 12, 2020
cb77cfd
Clean up test
rhoboat Jun 12, 2020
d82b131
Update README.md
rhoboat Jun 12, 2020
66949d0
Improve test with comments
rhoboat Jun 12, 2020
2a54398
Merge branch 'matching-s3-108' of github.com:rhoboat/cloud-nuke into …
rhoboat Jun 12, 2020
13f3ea9
WIP Deferring bucket deletion started happening too early
rhoboat Jun 12, 2020
cc9190d
Group assignments for clarity
rhoboat Jun 12, 2020
f9d0f4a
Moving deferral to cleanup buckets into t.Run works
rhoboat Jun 12, 2020
4e625a0
Code review nits and cleanup
rhoboat Jun 12, 2020
42ce01d
Test debug logging
rhoboat Jun 15, 2020
10863dc
Modify test to work in ca-central-1 reliably
rhoboat Jun 15, 2020
7cdb5e5
Remove unused lib
rhoboat Jun 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 54 additions & 11 deletions aws/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ func bucketNamesForConfigTests() []string {

// TestFilterS3Bucket_Config tests listing only S3 buckets that match config file
func TestFilterS3Bucket_Config(t *testing.T) {
rhoboat marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

// Create test buckets
awsParams, err := newS3TestAWSParams()
require.NoError(t, err, "Failed to setup AWS params")
Expand All @@ -495,26 +497,67 @@ func TestFilterS3Bucket_Config(t *testing.T) {
require.NoErrorf(t, err, "Failed to create test bucket - %s", bucketName)
}

awsSession, err := S3TestCreateNewAWSSession("")
require.NoError(t, err, "Failed to create random session")

// Clean up test buckets
defer nukeAllS3Buckets(awsParams.awsSession, aws.StringSlice(bucketNames), 1000)
// defer nukeAllS3Buckets(awsParams.awsSession, aws.StringSlice(bucketNames), 1000)
rhoboat marked this conversation as resolved.
Show resolved Hide resolved

// Define test case
type testCaseStruct struct {
name string
args TestFilterS3BucketArgs
}

// We compare with the slice of bucketNames that should match
matches := []string{}
matches = append(matches, bucketNames[0:3]...)
includeBuckets := []string{}
excludeBuckets := []string{}
filterBuckets := []string{}
filterBuckets = append(filterBuckets, bucketNames[:3]...)
includeBuckets = append(includeBuckets, bucketNames[:4]...)
excludeBuckets = append(excludeBuckets, bucketNames[:3]...)
excludeBuckets = append(excludeBuckets, bucketNames[4])
excludeBuckets = append(excludeBuckets, bucketNames[6:]...)

var configObj *config.Config
configObj, err = config.GetConfig("../config/mocks/s3_filter_names.yaml")
testCases := []testCaseStruct{
{
"IncludeAndExclude",
TestFilterS3BucketArgs{
configFilePath: "../config/mocks/s3_filter_names.yaml",
matches: filterBuckets,
},
},
{
"Exclude",
TestFilterS3BucketArgs{
configFilePath: "../config/mocks/s3_exclude_names.yaml",
matches: excludeBuckets,
},
},
{
"Include",
TestFilterS3BucketArgs{
configFilePath: "../config/mocks/s3_include_names.yaml",
matches: includeBuckets,
},
},
}

// Verify that only filtered buckets are listed
bucketNamesPerRegion, err := getAllS3Buckets(awsParams.awsSession, time.Now().Add(1*time.Hour), []string{awsParams.region}, "", 100, *configObj)
for _, tc := range testCases {
// Capture the range variable as per https://blog.golang.org/subtests
// Not doing this will lead to tc being set to the last entry in the testCases
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

require.NoError(t, err, "Failed to list S3 Buckets")
require.Equal(t, len(matches), len(bucketNamesPerRegion[awsParams.region]))
require.Subset(t, aws.StringValueSlice(bucketNamesPerRegion[awsParams.region]), matches)
var configObj *config.Config
configObj, err = config.GetConfig(tc.args.configFilePath)

// Verify that only filtered buckets are listed
bucketNamesPerRegion, err := getAllS3Buckets(awsSession, time.Now().Add(1*time.Hour), []string{awsParams.region}, "", 100, *configObj)

require.NoError(t, err, "Failed to list S3 Buckets")
require.Equal(t, len(tc.args.matches), len(bucketNamesPerRegion[awsParams.region]))
require.Subset(t, aws.StringValueSlice(bucketNamesPerRegion[awsParams.region]), tc.args.matches)
})
}
}