Skip to content

Commit

Permalink
squashing bugs (#22)
Browse files Browse the repository at this point in the history
* add codeclimate test coverage badge

* add: release badge

* fix: export scanner properties so they can be hashed

* fix: lowercase semaphore, mutateOptsFunc

* attempt to ignore

* hash separate struct

* just use owner
  • Loading branch information
ekristen authored Jan 23, 2024
1 parent 51ef4ee commit d8899f3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 49 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![GoDoc](https://godoc.org/github.com/ekristen/libnuke?status.svg)](https://godoc.org/github.com/ekristen/libnuke)
[![release](https://img.shields.io/github/release/ekristen/libnuke.svg)](/~https://github.com/ekristen/libnuke/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/ekristen/libnuke)](https://goreportcard.com/report/github.com/ekristen/libnuke)
[![Test Coverage](https://api.codeclimate.com/v1/badges/dc4078a236e89486b4ca/test_coverage)](https://codeclimate.com/github/ekristen/libnuke/test_coverage)
[![Maintainability](https://api.codeclimate.com/v1/badges/dc4078a236e89486b4ca/maintainability)](https://codeclimate.com/github/ekristen/libnuke/maintainability)
[![codecov](https://codecov.io/gh/ekristen/libnuke/graph/badge.svg?token=UJJOUJ98G4)](https://codecov.io/gh/ekristen/libnuke)
[![tests](/~https://github.com/ekristen/libnuke/actions/workflows/tests.yml/badge.svg)](/~https://github.com/ekristen/libnuke/actions/workflows/tests.yml)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/fatih/color v1.16.0
github.com/gotidy/ptr v1.4.0
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/sirupsen/logrus v1.9.3
github.com/stevenle/topsort v0.2.0
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4 h1:NK3O7S5FRD/wj7ORQ5C3Mx1STpyEMuFe+/F0Lakd1Nk=
github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4/go.mod h1:FqD3ES5hx6zpzDainDaHgkTIqrPaI9uX4CVWqYZoQjY=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
9 changes: 2 additions & 7 deletions pkg/nuke/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"slices"
"time"

"github.com/mitchellh/hashstructure/v2"
"github.com/sirupsen/logrus"

liberrors "github.com/ekristen/libnuke/pkg/errors"
Expand Down Expand Up @@ -145,12 +144,8 @@ func (n *Nuke) RegisterScanner(scope resource.Scope, scanner *Scanner) error {
n.Scanners = make(map[resource.Scope][]*Scanner)
}

hash, err := hashstructure.Hash(scanner, hashstructure.FormatV2, nil)
if err != nil {
return err
}

hashString := fmt.Sprintf("%s-%d", scope, hash)
hashString := fmt.Sprintf("%s-%s", scope, scanner.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 Down
8 changes: 4 additions & 4 deletions pkg/nuke/nuke_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Test_NukeFiltersMatch(t *testing.T) {
SessionOne: "testing",
SecondResource: true,
}
scanner := NewScanner("owner", []string{testResourceType2}, opts)
scanner := NewScanner("Owner", []string{testResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
assert.NoError(t, sErr)
Expand Down Expand Up @@ -89,7 +89,7 @@ func Test_NukeFiltersMatchInverted(t *testing.T) {
SessionOne: "testing",
SecondResource: true,
}
scanner := NewScanner("owner", []string{testResourceType2}, opts)
scanner := NewScanner("Owner", []string{testResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
assert.NoError(t, sErr)
Expand Down Expand Up @@ -122,7 +122,7 @@ func Test_Nuke_Filters_NoMatch(t *testing.T) {
SessionOne: "testing",
SecondResource: true,
}
scanner := NewScanner("owner", []string{testResourceType2}, opts)
scanner := NewScanner("Owner", []string{testResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
assert.NoError(t, sErr)
Expand Down Expand Up @@ -154,7 +154,7 @@ func Test_Nuke_Filters_ErrorCustomProps(t *testing.T) {
opts := TestOpts{
SessionOne: "testing",
}
scanner := NewScanner("owner", []string{testResourceType}, opts)
scanner := NewScanner("Owner", []string{testResourceType}, opts)

sErr := n.RegisterScanner(testScope, scanner)
assert.NoError(t, sErr)
Expand Down
12 changes: 6 additions & 6 deletions pkg/nuke/nuke_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Test_Nuke_Run_Simple(t *testing.T) {
Lister: &TestResourceSuccessLister{},
})

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

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

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

runErr := n.Run(context.TODO())
Expand All @@ -122,7 +122,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, NewScanner("Owner", []string{"TestResource4"}, nil))
assert.NoError(t, scannerErr)

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

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

Expand Down Expand Up @@ -179,7 +179,7 @@ func Test_NukeRunWithMaxWaitRetries(t *testing.T) {
Lister: &TestResourceWaitLister{},
})

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

Expand Down Expand Up @@ -233,7 +233,7 @@ func TestNuke_RunWithWaitOnDependencies(t *testing.T) {
},
})

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

Expand Down
36 changes: 30 additions & 6 deletions pkg/nuke/nuke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,32 @@ func Test_Nuke_Scanners_Duplicate(t *testing.T) {
assert.Len(t, n.Scanners[testScope], 1)
}

func TestNuke_RegisterMultipleScanners(t *testing.T) {
n := New(testParameters, nil, nil)
n.SetLogger(logrus.WithField("test", true))
n.SetRunSleep(time.Millisecond * 5)

opts := struct {
name string
}{
name: "test",
}

var mutateOpts = func(o interface{}, resourceType string) interface{} {
return o
}

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

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

assert.NoError(t, n.RegisterScanner(testScope, s))
assert.NoError(t, n.RegisterScanner(testScope, s2))
assert.Len(t, n.Scanners[testScope], 2)
}

func Test_Nuke_RegisterPrompt(t *testing.T) {
n := New(testParameters, nil, nil)
n.SetLogger(logrus.WithField("test", true))
Expand Down Expand Up @@ -217,7 +243,7 @@ func Test_Nuke_Scan(t *testing.T) {
opts := TestOpts{
SessionOne: "testing",
}
scanner := NewScanner("owner", []string{testResourceType, testResourceType2}, opts)
scanner := NewScanner("Owner", []string{testResourceType, testResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
assert.NoError(t, sErr)
Expand Down Expand Up @@ -293,7 +319,7 @@ func Test_Nuke_Run(t *testing.T) {
opts := TestOpts{
SessionOne: "testing",
}
scanner := NewScanner("owner", []string{testResourceType}, opts)
scanner := NewScanner("Owner", []string{testResourceType}, opts)

sErr := n.RegisterScanner(testScope, scanner)
assert.NoError(t, sErr)
Expand Down Expand Up @@ -325,7 +351,7 @@ func Test_Nuke_Run_Error(t *testing.T) {
opts := TestOpts{
SessionOne: "testing",
}
scanner := NewScanner("owner", []string{testResourceType2}, opts)
scanner := NewScanner("Owner", []string{testResourceType2}, opts)

sErr := n.RegisterScanner(testScope, scanner)
assert.NoError(t, sErr)
Expand Down Expand Up @@ -406,13 +432,11 @@ func Test_Nuke_Run_ItemStateHold(t *testing.T) {
Lister: &TestResource4Lister{},
})

scannerErr := n.RegisterScanner(testScope, NewScanner("owner", []string{"TestResource4"}, nil))
scannerErr := n.RegisterScanner(testScope, 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))
}

// -----------------------------------------------
33 changes: 16 additions & 17 deletions pkg/nuke/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ const ScannerParallelQueries = 16
// Scanner is collection of resource types that will be scanned for existing resources and added to the
// item queue for processing. These items will be filtered and then processed.
type Scanner struct {
Items chan *queue.Item `hash:"ignore"`
semaphore *semaphore.Weighted `hash:"ignore"`

resourceTypes []string
options interface{}
owner string
mutateOptsFunc MutateOptsFunc
Items chan *queue.Item `hash:"ignore"`
semaphore *semaphore.Weighted `hash:"ignore"`
ResourceTypes []string
Options interface{}
Owner string
mutateOptsFunc MutateOptsFunc `hash:"ignore"`
}

// MutateOptsFunc is a function that can mutate the options for a given resource type. This is useful for when you
// need to pass in a different set of options for a given resource type. For example, AWS nuke needs to be able to
// MutateOptsFunc is a function that can mutate the Options for a given resource type. This is useful for when you
// need to pass in a different set of Options for a given resource type. For example, AWS nuke needs to be able to
// populate the region and session for a given resource type give that it might only exist in us-east-1.
type MutateOptsFunc func(opts interface{}, resourceType string) interface{}

Expand All @@ -40,9 +39,9 @@ func NewScanner(owner string, resourceTypes []string, opts interface{}) *Scanner
return &Scanner{
Items: make(chan *queue.Item, 10000),
semaphore: semaphore.NewWeighted(ScannerParallelQueries),
resourceTypes: resourceTypes,
options: opts,
owner: owner,
ResourceTypes: resourceTypes,
Options: opts,
Owner: owner,
}
}

Expand All @@ -51,8 +50,8 @@ type IScanner interface {
list(resourceType string)
}

// RegisterMutateOptsFunc registers a mutate options function for the scanner. The mutate options function is called
// for each resource type that is being scanned. This allows you to mutate the options for a given resource type.
// RegisterMutateOptsFunc registers a mutate Options function for the scanner. The mutate Options function is called
// for each resource type that is being scanned. This allows you to mutate the Options for a given resource type.
func (s *Scanner) RegisterMutateOptsFunc(morph MutateOptsFunc) error {
if s.mutateOptsFunc != nil {
return fmt.Errorf("mutateOptsFunc already registered")
Expand All @@ -63,17 +62,17 @@ func (s *Scanner) RegisterMutateOptsFunc(morph MutateOptsFunc) error {

// Run starts the scanner and runs the lister for each resource type.
func (s *Scanner) Run(ctx context.Context) error {
for _, resourceType := range s.resourceTypes {
for _, resourceType := range s.ResourceTypes {
if err := s.semaphore.Acquire(ctx, 1); err != nil {
return err
}

opts := s.options
opts := s.Options
if s.mutateOptsFunc != nil {
opts = s.mutateOptsFunc(opts, resourceType)
}

go s.list(ctx, s.owner, resourceType, opts)
go s.list(ctx, s.Owner, resourceType, opts)
}

// Wait for all routines to finish.
Expand Down
12 changes: 6 additions & 6 deletions pkg/nuke/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func Test_NewScannerWithMorphOpts(t *testing.T) {
return o1
}

scanner := NewScanner("owner", []string{testResourceType}, opts)
scanner := NewScanner("Owner", []string{testResourceType}, opts)
mutateErr := scanner.RegisterMutateOptsFunc(morphOpts)
assert.NoError(t, mutateErr)

Expand Down Expand Up @@ -206,7 +206,7 @@ func Test_NewScannerWithDuplicateMorphOpts(t *testing.T) {
return o1
}

scanner := NewScanner("owner", []string{testResourceType}, opts)
scanner := NewScanner("Owner", []string{testResourceType}, opts)
optErr := scanner.RegisterMutateOptsFunc(morphOpts)
assert.NoError(t, optErr)

Expand Down Expand Up @@ -235,7 +235,7 @@ func Test_NewScannerWithResourceListerError(t *testing.T) {
ThrowError: true,
}

scanner := NewScanner("owner", []string{testResourceType}, opts)
scanner := NewScanner("Owner", []string{testResourceType}, opts)
err := scanner.Run(context.TODO())
assert.NoError(t, err)

Expand Down Expand Up @@ -268,7 +268,7 @@ func Test_NewScannerWithResourceListerErrorSkip(t *testing.T) {
ThrowSkipError: true,
}

scanner := NewScanner("owner", []string{testResourceType}, opts)
scanner := NewScanner("Owner", []string{testResourceType}, opts)
err := scanner.Run(context.TODO())
assert.NoError(t, err)

Expand Down Expand Up @@ -301,7 +301,7 @@ func Test_NewScannerWithResourceListerErrorUnknownEndpoint(t *testing.T) {
ThrowEndpointError: true,
}

scanner := NewScanner("owner", []string{testResourceType}, opts)
scanner := NewScanner("Owner", []string{testResourceType}, opts)
err := scanner.Run(context.TODO())
assert.NoError(t, err)

Expand Down Expand Up @@ -338,7 +338,7 @@ func Test_NewScannerWithResourceListerPanic(t *testing.T) {
Panic: true,
}
scanner := NewScanner("owner", []string{testResourceType}, opts, nil)
scanner := NewScanner("Owner", []string{testResourceType}, opts, nil)
scanner.Run()
assert.Len(t, scanner.Items, 0)
Expand Down

0 comments on commit d8899f3

Please sign in to comment.