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

feat: split scanner to own package from nuke #24

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
go mod download
- name: run go tests
run: |
go test -timeout 60s -race -coverprofile=coverage.txt -covermode=atomic ./...
go test -timeout 60s -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
Expand Down
14 changes: 8 additions & 6 deletions pkg/nuke/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
"github.com/sirupsen/logrus"

liberrors "github.com/ekristen/libnuke/pkg/errors"
libsettings "github.com/ekristen/libnuke/pkg/settings"

"github.com/ekristen/libnuke/pkg/filter"
"github.com/ekristen/libnuke/pkg/queue"
"github.com/ekristen/libnuke/pkg/resource"
libsettings "github.com/ekristen/libnuke/pkg/settings"
"github.com/ekristen/libnuke/pkg/scan"
"github.com/ekristen/libnuke/pkg/types"
"github.com/ekristen/libnuke/pkg/utils"
)
Expand Down Expand Up @@ -69,7 +71,7 @@ type Nuke struct {

ValidateHandlers []func() error
ResourceTypes map[resource.Scope]types.Collection
Scanners map[resource.Scope][]*Scanner
Scanners map[resource.Scope][]*scan.Scanner
Queue queue.Queue // Queue is the queue of resources that will be processed

scannerHashes []string // scannerHashes is used to track if a scanner has already been registered
Expand Down Expand Up @@ -139,12 +141,12 @@ func (n *Nuke) RegisterResourceTypes(scope resource.Scope, resourceTypes ...stri
// RegisterScanner is used to register a scanner against a scope. A scope is a string that is used to group resource
// types together. A scanner is what is responsible for actually querying the API for resources and adding them to
// the queue for processing.
func (n *Nuke) RegisterScanner(scope resource.Scope, scanner *Scanner) error {
func (n *Nuke) RegisterScanner(scope resource.Scope, instance *scan.Scanner) error {
if n.Scanners == nil {
n.Scanners = make(map[resource.Scope][]*Scanner)
n.Scanners = make(map[resource.Scope][]*scan.Scanner)
}

hashString := fmt.Sprintf("%s-%s", scope, scanner.Owner)
hashString := fmt.Sprintf("%s-%s", scope, instance.Owner)
n.log.Debugf("hash: %s", hashString)
if slices.Contains(n.scannerHashes, hashString) {
return fmt.Errorf("scanner is already registered, you cannot register it twice")
Expand All @@ -155,7 +157,7 @@ func (n *Nuke) RegisterScanner(scope resource.Scope, scanner *Scanner) error {
}

n.scannerHashes = append(n.scannerHashes, hashString)
n.Scanners[scope] = append(n.Scanners[scope], scanner)
n.Scanners[scope] = append(n.Scanners[scope], instance)

return nil
}
Expand Down
41 changes: 21 additions & 20 deletions pkg/nuke/nuke_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
"github.com/ekristen/libnuke/pkg/filter"
"github.com/ekristen/libnuke/pkg/queue"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/scan"
"github.com/ekristen/libnuke/pkg/types"
)

func Test_NukeFiltersBad(t *testing.T) {
filters := filter.Filters{
testResourceType: []filter.Filter{
TestResourceType: []filter.Filter{
{
Type: filter.Exact,
},
Expand All @@ -35,10 +36,10 @@ func Test_NukeFiltersBad(t *testing.T) {

func Test_NukeFiltersMatch(t *testing.T) {
resource.ClearRegistry()
resource.Register(testResourceRegistration2)
resource.Register(TestResourceRegistration2)

filters := filter.Filters{
testResourceType2: []filter.Filter{
TestResourceType2: []filter.Filter{
{
Type: filter.Exact,
Property: "test",
Expand All @@ -55,9 +56,9 @@ func Test_NukeFiltersMatch(t *testing.T) {
SessionOne: "testing",
SecondResource: true,
}
scanner := NewScanner("Owner", []string{testResourceType2}, opts)
newScanner := scan.NewScanner("Owner", []string{TestResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
sErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, sErr)

err := n.Scan(context.TODO())
Expand All @@ -68,10 +69,10 @@ func Test_NukeFiltersMatch(t *testing.T) {

func Test_NukeFiltersMatchInverted(t *testing.T) {
resource.ClearRegistry()
resource.Register(testResourceRegistration2)
resource.Register(TestResourceRegistration2)

filters := filter.Filters{
testResourceType2: []filter.Filter{
TestResourceType2: []filter.Filter{
{
Type: filter.Exact,
Property: "test",
Expand All @@ -89,9 +90,9 @@ func Test_NukeFiltersMatchInverted(t *testing.T) {
SessionOne: "testing",
SecondResource: true,
}
scanner := NewScanner("Owner", []string{testResourceType2}, opts)
newScanner := scan.NewScanner("Owner", []string{TestResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
sErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, sErr)

err := n.Scan(context.TODO())
Expand All @@ -102,10 +103,10 @@ func Test_NukeFiltersMatchInverted(t *testing.T) {

func Test_Nuke_Filters_NoMatch(t *testing.T) {
resource.ClearRegistry()
resource.Register(testResourceRegistration2)
resource.Register(TestResourceRegistration2)

filters := filter.Filters{
testResourceType: []filter.Filter{
TestResourceType: []filter.Filter{
{
Type: filter.Exact,
Property: "test",
Expand All @@ -122,9 +123,9 @@ func Test_Nuke_Filters_NoMatch(t *testing.T) {
SessionOne: "testing",
SecondResource: true,
}
scanner := NewScanner("Owner", []string{testResourceType2}, opts)
newScanner := scan.NewScanner("Owner", []string{TestResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
sErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, sErr)

err := n.Scan(context.TODO())
Expand All @@ -135,14 +136,14 @@ func Test_Nuke_Filters_NoMatch(t *testing.T) {

func Test_Nuke_Filters_ErrorCustomProps(t *testing.T) {
resource.ClearRegistry()
resource.Register(testResourceRegistration)
resource.Register(TestResourceRegistration)

filters := filter.Filters{
testResourceType: []filter.Filter{
TestResourceType: []filter.Filter{
{
Type: filter.Exact,
Property: "Name",
Value: testResourceType,
Value: TestResourceType,
},
},
}
Expand All @@ -154,9 +155,9 @@ func Test_Nuke_Filters_ErrorCustomProps(t *testing.T) {
opts := TestOpts{
SessionOne: "testing",
}
scanner := NewScanner("Owner", []string{testResourceType}, opts)
newScanner := scan.NewScanner("Owner", []string{TestResourceType}, opts)

sErr := n.RegisterScanner(testScope, scanner)
sErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, sErr)

err := n.Scan(context.TODO())
Expand All @@ -183,7 +184,7 @@ func (r *TestResourceFilter) Remove(_ context.Context) error {

func Test_Nuke_Filters_Extra(t *testing.T) {
filters := filter.Filters{
testResourceType2: []filter.Filter{
TestResourceType2: []filter.Filter{
{
Type: filter.Glob,
Property: "tag:aws:cloudformation:stack-name",
Expand All @@ -198,7 +199,7 @@ func Test_Nuke_Filters_Extra(t *testing.T) {

i := &queue.Item{
Resource: &TestResourceFilter{},
Type: testResourceType2,
Type: TestResourceType2,
}

err := n.Filter(i)
Expand Down
19 changes: 10 additions & 9 deletions pkg/nuke/nuke_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/ekristen/libnuke/pkg/queue"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/scan"
)

type TestResourceSuccess struct {
Expand Down Expand Up @@ -62,7 +63,7 @@ func Test_Nuke_Run_Simple(t *testing.T) {
Lister: &TestResourceSuccessLister{},
})

scannerErr := n.RegisterScanner(testScope, NewScanner("Owner", []string{"TestResourceSuccess"}, nil))
scannerErr := n.RegisterScanner(testScope, scan.NewScanner("Owner", []string{"TestResourceSuccess"}, nil))
assert.NoError(t, scannerErr)

runErr := n.Run(context.TODO())
Expand Down Expand Up @@ -108,7 +109,7 @@ func Test_NukeRunSimpleWithSecondPromptError(t *testing.T) {
Lister: &TestResourceSuccessLister{},
})

scannerErr := n.RegisterScanner(testScope, NewScanner("Owner", []string{"TestResourceSuccess"}, nil))
scannerErr := n.RegisterScanner(testScope, scan.NewScanner("Owner", []string{"TestResourceSuccess"}, nil))
assert.NoError(t, scannerErr)

runErr := n.Run(context.TODO())
Expand All @@ -122,7 +123,7 @@ func Test_Nuke_Run_SimpleWithNoDryRun(t *testing.T) {
n.SetLogger(logrus.WithField("test", true))
n.SetRunSleep(time.Millisecond * 5)

scannerErr := n.RegisterScanner(testScope, NewScanner("Owner", []string{"TestResource4"}, nil))
scannerErr := n.RegisterScanner(testScope, scan.NewScanner("Owner", []string{"TestResource4"}, nil))
assert.NoError(t, scannerErr)

runErr := n.Run(context.TODO())
Expand All @@ -149,8 +150,8 @@ func Test_Nuke_Run_Failure(t *testing.T) {
Lister: &TestResourceFailureLister{},
})

scanner := NewScanner("Owner", []string{"TestResourceSuccess", "TestResourceFailure"}, nil)
scannerErr := n.RegisterScanner(testScope, scanner)
newScanner := scan.NewScanner("Owner", []string{"TestResourceSuccess", "TestResourceFailure"}, nil)
scannerErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, scannerErr)

runErr := n.Run(context.TODO())
Expand Down Expand Up @@ -179,8 +180,8 @@ func Test_NukeRunWithMaxWaitRetries(t *testing.T) {
Lister: &TestResourceWaitLister{},
})

scanner := NewScanner("Owner", []string{"TestResourceSuccess"}, nil)
scannerErr := n.RegisterScanner(testScope, scanner)
newScanner := scan.NewScanner("Owner", []string{"TestResourceSuccess"}, nil)
scannerErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, scannerErr)

runErr := n.Run(context.TODO())
Expand Down Expand Up @@ -233,8 +234,8 @@ func TestNuke_RunWithWaitOnDependencies(t *testing.T) {
},
})

scanner := NewScanner("Owner", []string{"TestResourceAlpha", "TestResourceBeta"}, nil)
scannerErr := n.RegisterScanner(testScope, scanner)
newScanner := scan.NewScanner("Owner", []string{"TestResourceAlpha", "TestResourceBeta"}, nil)
scannerErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, scannerErr)

runErr := n.Run(context.TODO())
Expand Down
33 changes: 17 additions & 16 deletions pkg/nuke/nuke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"github.com/stretchr/testify/assert"

liberrors "github.com/ekristen/libnuke/pkg/errors"

"github.com/ekristen/libnuke/pkg/queue"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/scan"
"github.com/ekristen/libnuke/pkg/settings"
)

Expand Down Expand Up @@ -153,7 +155,7 @@ func Test_Nuke_Scanners(t *testing.T) {
name: "test",
}

s := NewScanner("test", []string{"TestResource"}, opts)
s := scan.NewScanner("test", []string{"TestResource"}, opts)

err := n.RegisterScanner(testScope, s)
assert.NoError(t, err)
Expand All @@ -172,7 +174,7 @@ func Test_Nuke_Scanners_Duplicate(t *testing.T) {
name: "test",
}

s := NewScanner("test", []string{"TestResource"}, opts)
s := scan.NewScanner("test", []string{"TestResource"}, opts)

err := n.RegisterScanner(testScope, s)
assert.NoError(t, err)
Expand All @@ -198,10 +200,10 @@ func TestNuke_RegisterMultipleScanners(t *testing.T) {
return o
}

s := NewScanner("test", []string{"TestResource"}, opts)
s := scan.NewScanner("test", []string{"TestResource"}, opts)
assert.NoError(t, s.RegisterMutateOptsFunc(mutateOpts))

s2 := NewScanner("test2", []string{"TestResource"}, opts)
s2 := scan.NewScanner("test2", []string{"TestResource"}, opts)
assert.NoError(t, s2.RegisterMutateOptsFunc(mutateOpts))

assert.NoError(t, n.RegisterScanner(testScope, s))
Expand All @@ -227,9 +229,9 @@ func Test_Nuke_RegisterPrompt(t *testing.T) {

func Test_Nuke_Scan(t *testing.T) {
resource.ClearRegistry()
resource.Register(testResourceRegistration)
resource.Register(TestResourceRegistration)
resource.Register(&resource.Registration{
Name: testResourceType2,
Name: TestResourceType2,
Scope: "account",
Lister: TestResourceLister{
Filtered: true,
Expand All @@ -243,9 +245,9 @@ func Test_Nuke_Scan(t *testing.T) {
opts := TestOpts{
SessionOne: "testing",
}
scanner := NewScanner("Owner", []string{testResourceType, testResourceType2}, opts)
newScanner := scan.NewScanner("Owner", []string{TestResourceType, TestResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
sErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, sErr)

err := n.Scan(context.TODO())
Expand Down Expand Up @@ -303,7 +305,7 @@ func Test_Nuke_HandleRemoveError(t *testing.T) {

func Test_Nuke_Run(t *testing.T) {
resource.ClearRegistry()
resource.Register(testResourceRegistration)
resource.Register(TestResourceRegistration)

p := &Parameters{
Force: true,
Expand All @@ -319,9 +321,9 @@ func Test_Nuke_Run(t *testing.T) {
opts := TestOpts{
SessionOne: "testing",
}
scanner := NewScanner("Owner", []string{testResourceType}, opts)
newScanner := scan.NewScanner("Owner", []string{TestResourceType}, opts)

sErr := n.RegisterScanner(testScope, scanner)
sErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, sErr)

err := n.Run(context.TODO())
Expand All @@ -331,7 +333,7 @@ func Test_Nuke_Run(t *testing.T) {
func Test_Nuke_Run_Error(t *testing.T) {
resource.ClearRegistry()
resource.Register(&resource.Registration{
Name: testResourceType2,
Name: TestResourceType2,
Scope: "account",
Lister: TestResourceLister{
RemoveError: true,
Expand All @@ -351,9 +353,9 @@ func Test_Nuke_Run_Error(t *testing.T) {
opts := TestOpts{
SessionOne: "testing",
}
scanner := NewScanner("Owner", []string{testResourceType2}, opts)
newScanner := scan.NewScanner("Owner", []string{TestResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
sErr := n.RegisterScanner(testScope, newScanner)
assert.NoError(t, sErr)

err := n.Run(context.TODO())
Expand Down Expand Up @@ -432,11 +434,10 @@ func Test_Nuke_Run_ItemStateHold(t *testing.T) {
Lister: &TestResource4Lister{},
})

scannerErr := n.RegisterScanner(testScope, NewScanner("Owner", []string{"TestResource4"}, nil))
scannerErr := n.RegisterScanner(testScope, scan.NewScanner("Owner", []string{"TestResource4"}, nil))
assert.NoError(t, scannerErr)

runErr := n.Run(context.TODO())
assert.NoError(t, runErr)

assert.Equal(t, 5, n.Queue.Count(queue.ItemStateFinished))
}
Loading
Loading