Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Jan 16, 2024
1 parent 8935f90 commit f5f2240
Show file tree
Hide file tree
Showing 32 changed files with 105 additions and 88 deletions.
2 changes: 1 addition & 1 deletion docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type ExampleResource struct {
ID *string
}

func (r *ExampleResource) Remove() error {
func (r *ExampleResource) Remove(_ context.Context) error {
// remove the resource, an error will put the resource in failed state
// resources in failed state are retried a number of times
return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestConfigBlocklist(t *testing.T) {
}

func TestLoadExampleConfig(t *testing.T) {
config, err := Load("test-fixtures/example.yaml")
config, err := Load("testdata/example.yaml")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestResolveDeprecations(t *testing.T) {
}

func TestConfigValidation(t *testing.T) {
config, err := Load("test-fixtures/example.yaml")
config, err := Load("testdata/example.yaml")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestConfigValidation(t *testing.T) {
}

func TestDeprecatedConfigKeys(t *testing.T) {
config, err := Load("test-fixtures/deprecated-keys-config.yaml")
config, err := Load("testdata/deprecated-keys-config.yaml")
if err != nil {
t.Fatal(err)
}
Expand All @@ -243,7 +243,7 @@ func TestDeprecatedConfigKeys(t *testing.T) {
}

func TestFilterMerge(t *testing.T) {
config, err := Load("test-fixtures/example.yaml")
config, err := Load("testdata/example.yaml")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func TestFilterMerge(t *testing.T) {
}

func TestGetCustomRegion(t *testing.T) {
config, err := Load("test-fixtures/example.yaml")
config, err := Load("testdata/example.yaml")
if err != nil {
t.Fatal(err)
}
Expand Down
20 changes: 17 additions & 3 deletions pkg/config/testdata/example.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
---
tenant-blocklist:
regions:
- "eu-west-1"
- stratoscale

account-blocklist:
- 1234567890

endpoints:
- region: stratoscale
tls_insecure_skip_verify: true
services:
- service: ec2
url: https://stratoscale.cloud.internal/api/v2/aws/ec2
- service: s3
url: https://stratoscale.cloud.internal:1060
tls_insecure_skip_verify: true

resource-types:
targets:
- DynamoDBTable
Expand All @@ -10,7 +24,7 @@ resource-types:
excludes:
- IAMRole

tenants:
accounts:
555133742:
presets:
- "terraform"
Expand All @@ -28,4 +42,4 @@ presets:
filters:
S3Bucket:
- type: glob
value: "my-statebucket-*"
value: "my-statebucket-*"
8 changes: 2 additions & 6 deletions pkg/nuke/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Parameters struct {
}

type Nuke struct {
sdknuke.Nuke
*sdknuke.Nuke
Parameters Parameters
Config *config.Nuke
Account awsutil.Account
Expand All @@ -47,11 +47,7 @@ func (n *Nuke) Prompt() error {

func New(params Parameters, config *config.Nuke, filters filter.Filters, account awsutil.Account) *Nuke {
n := Nuke{
Nuke: sdknuke.Nuke{
Parameters: params.Parameters,
FeatureFlags: &featureflag.FeatureFlags{},
Filters: filters,
},
Nuke: sdknuke.New(params.Parameters, filters),
Parameters: params,
Config: config,
Account: account,
Expand Down
21 changes: 14 additions & 7 deletions resources/cloudcontrol_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package resources

import (
"context"

"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)

func TestCloudControlParseProperties(t *testing.T) {
Expand All @@ -15,20 +13,29 @@ func TestCloudControlParseProperties(t *testing.T) {
cases := []struct {
name string
payload string
want string
want []string
}{
{
name: "ActualEC2VPC",
payload: `{"VpcId":"vpc-456","InstanceTenancy":"default","CidrBlockAssociations":["vpc-cidr-assoc-1234", "vpc-cidr-assoc-5678"],"CidrBlock":"10.10.0.0/16","Tags":[{"Value":"Kubernetes VPC","Key":"Name"}]}`,
want: `[CidrBlock: "10.10.0.0/16", CidrBlockAssociations.["vpc-cidr-assoc-1234"]: "true", CidrBlockAssociations.["vpc-cidr-assoc-5678"]: "true", InstanceTenancy: "default", Tags.["Name"]: "Kubernetes VPC", VpcId: "vpc-456"]`,
want: []string{
`CidrBlock: "10.10.0.0/16"`,
`Tags.["Name"]: "Kubernetes VPC"`,
`VpcId: "vpc-456"`,
`InstanceTenancy: "default"`,
`CidrBlockAssociations.["vpc-cidr-assoc-1234"]: "true"`,
`CidrBlockAssociations.["vpc-cidr-assoc-5678"]: "true"`,
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
result, err := cloudControlParseProperties(tc.payload)
require.NoError(t, err)
require.Equal(t, tc.want, result.String())
assert.NoError(t, err)
for _, w := range tc.want {
assert.Contains(t, result.String(), w)
}
})
}
}
40 changes: 31 additions & 9 deletions resources/cloudformation-stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package resources

import (
"context"

"testing"

"github.com/golang/mock/gomock"
"github.com/gotidy/ptr"
"github.com/stretchr/testify/assert"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudformation"

"github.com/ekristen/libnuke/pkg/featureflag"

"github.com/ekristen/aws-nuke/mocks/mock_cloudformationiface"
)

Expand All @@ -27,8 +29,11 @@ func TestCloudformationStack_Remove_StackAlreadyDeleted(t *testing.T) {
stack: &cloudformation.Stack{
StackName: aws.String("foobar"),
},
featureFlags: &featureflag.FeatureFlags{},
}

stack.featureFlags.New("DisableDeletionProtection_CloudformationStack", ptr.Bool(true), ptr.Bool(true))

mockCloudformation.EXPECT().DescribeStacks(gomock.Eq(&cloudformation.DescribeStacksInput{
StackName: aws.String("foobar"),
})).Return(&cloudformation.DescribeStacksOutput{
Expand All @@ -39,7 +44,7 @@ func TestCloudformationStack_Remove_StackAlreadyDeleted(t *testing.T) {
},
}, nil)

err := stack.Remove()
err := stack.Remove(context.TODO())
a.Nil(err)
}

Expand All @@ -54,14 +59,16 @@ func TestCloudformationStack_Remove_StackDoesNotExist(t *testing.T) {
svc: mockCloudformation,
stack: &cloudformation.Stack{
StackName: aws.String("foobar"),
},
}, featureFlags: &featureflag.FeatureFlags{},
}

stack.featureFlags.New("DisableDeletionProtection_CloudformationStack", ptr.Bool(true), ptr.Bool(true))

mockCloudformation.EXPECT().DescribeStacks(gomock.Eq(&cloudformation.DescribeStacksInput{
StackName: aws.String("foobar"),
})).Return(nil, awserr.New("ValidationFailed", "Stack with id foobar does not exist", nil))

err := stack.Remove()
err := stack.Remove(context.TODO())
a.Nil(err)
}

Expand All @@ -77,8 +84,11 @@ func TestCloudformationStack_Remove_DeleteFailed(t *testing.T) {
stack: &cloudformation.Stack{
StackName: aws.String("foobar"),
},
featureFlags: &featureflag.FeatureFlags{},
}

stack.featureFlags.New("DisableDeletionProtection_CloudformationStack", ptr.Bool(true), ptr.Bool(true))

gomock.InOrder(
mockCloudformation.EXPECT().DescribeStacks(gomock.Eq(&cloudformation.DescribeStacksInput{
StackName: aws.String("foobar"),
Expand Down Expand Up @@ -114,7 +124,7 @@ func TestCloudformationStack_Remove_DeleteFailed(t *testing.T) {
})).Return(nil),
)

err := stack.Remove()
err := stack.Remove(context.TODO())
a.Nil(err)
}

Expand All @@ -131,8 +141,11 @@ func TestCloudformationStack_Remove_DeleteInProgress(t *testing.T) {
stack: &cloudformation.Stack{
StackName: aws.String("foobar"),
},
featureFlags: &featureflag.FeatureFlags{},
}

stack.featureFlags.New("DisableDeletionProtection_CloudformationStack", ptr.Bool(true), ptr.Bool(true))

gomock.InOrder(
mockCloudformation.EXPECT().DescribeStacks(gomock.Eq(&cloudformation.DescribeStacksInput{
StackName: aws.String("foobar"),
Expand All @@ -149,7 +162,7 @@ func TestCloudformationStack_Remove_DeleteInProgress(t *testing.T) {
})).Return(nil),
)

err := stack.Remove()
err := stack.Remove(context.TODO())
a.Nil(err)
}

Expand Down Expand Up @@ -178,8 +191,11 @@ func TestCloudformationStack_Remove_Stack_InCompletedStatus(t *testing.T) {
stack: &cloudformation.Stack{
StackName: aws.String("foobar"),
},
featureFlags: &featureflag.FeatureFlags{},
}

stack.featureFlags.New("DisableDeletionProtection_CloudformationStack", ptr.Bool(true), ptr.Bool(true))

gomock.InOrder(
mockCloudformation.EXPECT().DescribeStacks(gomock.Eq(&cloudformation.DescribeStacksInput{
StackName: aws.String("foobar"),
Expand All @@ -200,7 +216,7 @@ func TestCloudformationStack_Remove_Stack_InCompletedStatus(t *testing.T) {
})).Return(nil),
)

err := stack.Remove()
err := stack.Remove(context.TODO())
a.Nil(err)
})
}
Expand All @@ -225,8 +241,11 @@ func TestCloudformationStack_Remove_Stack_CreateInProgress(t *testing.T) {
stack: &cloudformation.Stack{
StackName: aws.String("foobar"),
},
featureFlags: &featureflag.FeatureFlags{},
}

stack.featureFlags.New("DisableDeletionProtection_CloudformationStack", ptr.Bool(true), ptr.Bool(true))

gomock.InOrder(
mockCloudformation.EXPECT().DescribeStacks(gomock.Eq(&cloudformation.DescribeStacksInput{
StackName: aws.String("foobar"),
Expand All @@ -251,7 +270,7 @@ func TestCloudformationStack_Remove_Stack_CreateInProgress(t *testing.T) {
})).Return(nil),
)

err := stack.Remove()
err := stack.Remove(context.TODO())
a.Nil(err)
})
}
Expand All @@ -277,8 +296,11 @@ func TestCloudformationStack_Remove_Stack_UpdateInProgress(t *testing.T) {
stack: &cloudformation.Stack{
StackName: aws.String("foobar"),
},
featureFlags: &featureflag.FeatureFlags{},
}

stack.featureFlags.New("DisableDeletionProtection_CloudformationStack", ptr.Bool(true), ptr.Bool(true))

gomock.InOrder(
mockCloudformation.EXPECT().DescribeStacks(gomock.Eq(&cloudformation.DescribeStacksInput{
StackName: aws.String("foobar"),
Expand All @@ -303,7 +325,7 @@ func TestCloudformationStack_Remove_Stack_UpdateInProgress(t *testing.T) {
})).Return(nil),
)

err := stack.Remove()
err := stack.Remove(context.TODO())
a.Nil(err)
})
}
Expand Down
7 changes: 3 additions & 4 deletions resources/cloudformation-stackset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resources

import (
"context"

"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -74,7 +73,7 @@ func TestCloudformationStackSet_Remove(t *testing.T) {
StackSetName: aws.String("foobar"),
})).Return(&cloudformation.DeleteStackSetOutput{}, nil)

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

Expand Down Expand Up @@ -149,7 +148,7 @@ func TestCloudformationStackSet_Remove_MultipleAccounts(t *testing.T) {
StackSetName: aws.String("foobar"),
})).Return(&cloudformation.DeleteStackSetOutput{}, nil)

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

Expand Down Expand Up @@ -196,6 +195,6 @@ func TestCloudformationStackSet_Remove_DeleteStackInstanceFailed(t *testing.T) {
},
}, nil)

err := stackSet.Remove()
err := stackSet.Remove(context.TODO())
a.EqualError(err, "unable to delete stackSet=foobar operationId=o1 status=FAILED")
}
4 changes: 1 addition & 3 deletions resources/cloudformation-type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resources

import (
"context"

"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -63,7 +62,6 @@ func TestCloudformationType_Remove(t *testing.T) {
Arn: aws.String("foobar"),
})

err := cfnType.Remove()
err := cfnType.Remove(context.TODO())
a.Nil(err)

}
3 changes: 1 addition & 2 deletions resources/iam-group-policies_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resources

import (
"context"

"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -32,6 +31,6 @@ func Test_Mock_IAMGroupPolicy_Remove(t *testing.T) {
GroupName: aws.String(iamGroupPolicy.groupName),
})).Return(&iam.DeleteGroupPolicyOutput{}, nil)

err := iamGroupPolicy.Remove()
err := iamGroupPolicy.Remove(context.TODO())
a.Nil(err)
}
Loading

0 comments on commit f5f2240

Please sign in to comment.