| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package audit |
| 18 |
|
| 19 |
import ( |
| 20 |
"strconv" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/types" |
| 24 |
auditinternal "k8s.io/apiserver/pkg/apis/audit" |
| 25 |
) |
| 26 |
|
| 27 |
type fakeBackend struct { |
| 28 |
events []*auditinternal.Event |
| 29 |
} |
| 30 |
|
| 31 |
func (f *fakeBackend) ProcessEvents(events ...*auditinternal.Event) bool { |
| 32 |
f.events = append(f.events, events...) |
| 33 |
return true |
| 34 |
} |
| 35 |
|
| 36 |
func (f *fakeBackend) Run(stopCh <-chan struct{}) error { |
| 37 |
return nil |
| 38 |
} |
| 39 |
|
| 40 |
func (f *fakeBackend) Shutdown() { |
| 41 |
// Nothing to do here. |
| 42 |
} |
| 43 |
|
| 44 |
func (f *fakeBackend) String() string { |
| 45 |
return "" |
| 46 |
} |
| 47 |
|
| 48 |
func TestUnion(t *testing.T) { |
| 49 |
backends := []Backend{ |
| 50 |
new(fakeBackend), |
| 51 |
new(fakeBackend), |
| 52 |
new(fakeBackend), |
| 53 |
} |
| 54 |
|
| 55 |
b := Union(backends...) |
| 56 |
|
| 57 |
n := 5 |
| 58 |
|
| 59 |
var events []*auditinternal.Event |
| 60 |
for i := 0; i < n; i++ { |
| 61 |
events = append(events, &auditinternal.Event{ |
| 62 |
AuditID: types.UID(strconv.Itoa(i)), |
| 63 |
}) |
| 64 |
} |
| 65 |
b.ProcessEvents(events...) |
| 66 |
|
| 67 |
for i, b := range backends { |
| 68 |
// so we can inspect the underlying events. |
| 69 |
backend := b.(*fakeBackend) |
| 70 |
|
| 71 |
if got := len(backend.events); got != n { |
| 72 |
t.Errorf("backend %d wanted %d events, got %d", i, n, got) |
| 73 |
continue |
| 74 |
} |
| 75 |
for j, event := range backend.events { |
| 76 |
wantID := types.UID(strconv.Itoa(j)) |
| 77 |
if event.AuditID != wantID { |
| 78 |
t.Errorf("backend %d event %d wanted id %s, got %s", i, j, wantID, event.AuditID) |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
type cannotMultipleRunBackend struct { |
| 85 |
started chan struct{} |
| 86 |
} |
| 87 |
|
| 88 |
func (b *cannotMultipleRunBackend) ProcessEvents(events ...*auditinternal.Event) bool { |
| 89 |
return true |
| 90 |
} |
| 91 |
|
| 92 |
func (b *cannotMultipleRunBackend) Run(stopCh <-chan struct{}) error { |
| 93 |
close(b.started) |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
func (b *cannotMultipleRunBackend) Shutdown() {} |
| 98 |
|
| 99 |
func (b *cannotMultipleRunBackend) String() string { |
| 100 |
return "cannotMultipleRunBackend" |
| 101 |
} |
| 102 |
|
| 103 |
func TestUnionRun(t *testing.T) { |
| 104 |
backends := []Backend{ |
| 105 |
&cannotMultipleRunBackend{started: make(chan struct{})}, |
| 106 |
&cannotMultipleRunBackend{started: make(chan struct{})}, |
| 107 |
&cannotMultipleRunBackend{started: make(chan struct{})}, |
| 108 |
} |
| 109 |
|
| 110 |
b := Union(backends...) |
| 111 |
|
| 112 |
if err := b.Run(make(chan struct{})); err != nil { |
| 113 |
t.Errorf("union backend run: %v", err) |
| 114 |
} |
| 115 |
} |