Skip to content

Commit 08f2612

Browse files
committed
manifestclient/mutation_tracke: improve some methods of AllActionsTracker to not panic when no data
1 parent 4139e55 commit 08f2612

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

pkg/manifestclient/mutation_tracker.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ func (a *AllActionsTracker[T]) ListActions() []Action {
9292

9393
func (a *AllActionsTracker[T]) RequestsForAction(action Action) []SerializedRequestish {
9494
ret := []SerializedRequestish{}
95-
mutations := a.actionToTracker[action].Mutations()
95+
tracker, ok := a.actionToTracker[action]
96+
if !ok {
97+
return nil
98+
}
99+
mutations := tracker.Mutations()
96100
for _, mutation := range mutations {
97101
ret = append(ret, mutation)
98102
}
@@ -101,7 +105,11 @@ func (a *AllActionsTracker[T]) RequestsForAction(action Action) []SerializedRequ
101105

102106
func (a *AllActionsTracker[T]) RequestsForResource(metadata ActionMetadata) []SerializedRequestish {
103107
ret := []SerializedRequestish{}
104-
mutations := a.actionToTracker[metadata.Action].Mutations()
108+
tracker, ok := a.actionToTracker[metadata.Action]
109+
if !ok {
110+
return nil
111+
}
112+
mutations := tracker.Mutations()
105113
for _, mutation := range mutations {
106114
if mutation.GetSerializedRequest().GetLookupMetadata() == metadata {
107115
ret = append(ret, mutation)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package manifestclient_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/openshift/library-go/pkg/manifestclient"
7+
)
8+
9+
// TestNoPanicFromAllActionTracker
10+
// is a simple test to ensure that the read methods
11+
// of NewAllActionsTracker do not panic on an empty instance.
12+
func TestNoPanicFromAllActionTracker(t *testing.T) {
13+
target := manifestclient.NewAllActionsTracker[manifestclient.FileOriginatedSerializedRequest]()
14+
15+
if actions := target.ListActions(); len(actions) != 0 {
16+
t.Errorf("ListActions() returned non empty response: %v", actions)
17+
}
18+
19+
if reqs := target.AllRequests(); len(reqs) != 0 {
20+
t.Errorf("AllRequests() returned non empty response: %v", reqs)
21+
}
22+
23+
ret := target.RequestsForAction(manifestclient.ActionUpdate)
24+
if ret != nil {
25+
t.Errorf("RequestsForAction() returned non nil response: %v", ret)
26+
}
27+
28+
ret = target.RequestsForResource(manifestclient.ActionMetadata{Action: manifestclient.ActionApply})
29+
if ret != nil {
30+
t.Errorf("RequestsForResource() returned non nil response: %v", ret)
31+
}
32+
33+
// just make sure it doens panic
34+
_ = target.DeepCopy()
35+
}

0 commit comments

Comments
 (0)