Skip to content

Commit

Permalink
fix: protect against an invalid resource (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Jan 31, 2024
1 parent 3f60dd8 commit 0ad7c3c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/nuke/nuke_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,19 @@ func Test_Nuke_Run_SimpleWithNoDryRun(t *testing.T) {
n.SetLogger(logrus.WithField("test", true))
n.SetRunSleep(time.Millisecond * 5)

scannerErr := n.RegisterScanner(testScope, scan.NewScanner("Owner", []string{"TestResource4"}, nil))
resource.ClearRegistry()
resource.Register(&resource.Registration{
Name: "TestResourceSuccess",
Lister: &TestResourceSuccessLister{},
})

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

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

assert.Equal(t, 0, n.Queue.Count(queue.ItemStateFinished))
assert.Equal(t, 1, n.Queue.Count(queue.ItemStateFinished))
}

// Test_Nuke_Run_Failure tests a run with a resource that fails to remove, so it should be in the failed state.
Expand Down
18 changes: 18 additions & 0 deletions pkg/nuke/testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

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

"github.com/ekristen/libnuke/pkg/errors"
Expand All @@ -13,6 +14,23 @@ import (
"github.com/ekristen/libnuke/pkg/types"
)

type TestGlobalHook struct {
t *testing.T
tf func(t *testing.T, e *logrus.Entry)
}

func (h *TestGlobalHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (h *TestGlobalHook) Fire(e *logrus.Entry) error {
if h.tf != nil {
h.tf(h.t, e)
}

return nil
}

var (
TestResourceType = "testResourceType"
TestResourceRegistration = &resource.Registration{
Expand Down
5 changes: 5 additions & 0 deletions pkg/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ func (s *Scanner) list(ctx context.Context, owner, resourceType string, opts int
lister := resource.GetLister(resourceType)
var rs []resource.Resource

if lister == nil {
logrus.Errorf("lister for resource type not found: %s", resourceType)
return
}

rs, err := lister.List(ctx, opts)
if err != nil {
var errSkipRequest liberrors.ErrSkipRequest
Expand Down

0 comments on commit 0ad7c3c

Please sign in to comment.