Skip to content

Commit

Permalink
fix audit union loop variables in closures
Browse files Browse the repository at this point in the history
Kubernetes-commit: b24dfdee1e081afbda716b2ab66377cfcc260eb0
  • Loading branch information
sxllwx authored and k8s-publishing-bot committed May 20, 2022
1 parent 681b529 commit c39f492
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/audit/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (u union) ProcessEvents(events ...*auditinternal.Event) bool {
func (u union) Run(stopCh <-chan struct{}) error {
var funcs []func() error
for _, backend := range u.backends {
backend := backend
funcs = append(funcs, func() error {
return backend.Run(stopCh)
})
Expand Down
33 changes: 33 additions & 0 deletions pkg/audit/union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,36 @@ func TestUnion(t *testing.T) {
}
}
}

type cannotMultipleRunBackend struct {
started chan struct{}
}

func (b *cannotMultipleRunBackend) ProcessEvents(events ...*auditinternal.Event) bool {
return true
}

func (b *cannotMultipleRunBackend) Run(stopCh <-chan struct{}) error {
close(b.started)
return nil
}

func (b *cannotMultipleRunBackend) Shutdown() {}

func (b *cannotMultipleRunBackend) String() string {
return "cannotMultipleRunBackend"
}

func TestUnionRun(t *testing.T) {
backends := []Backend{
&cannotMultipleRunBackend{started: make(chan struct{})},
&cannotMultipleRunBackend{started: make(chan struct{})},
&cannotMultipleRunBackend{started: make(chan struct{})},
}

b := Union(backends...)

if err := b.Run(make(chan struct{})); err != nil {
t.Errorf("union backend run: %v", err)
}
}

0 comments on commit c39f492

Please sign in to comment.