|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors All rights reserved. |
| 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 logcounter |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "k8s.io/kubernetes/pkg/util/clock" |
| 24 | + |
| 25 | + "k8s.io/node-problem-detector/pkg/logcounter/types" |
| 26 | + "k8s.io/node-problem-detector/pkg/systemlogmonitor" |
| 27 | + systemtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types" |
| 28 | +) |
| 29 | + |
| 30 | +func NewTestLogCounter(pattern string, startTime time.Time) (types.LogCounter, *clock.FakeClock, chan *systemtypes.Log) { |
| 31 | + logCh := make(chan *systemtypes.Log) |
| 32 | + clock := clock.NewFakeClock(startTime) |
| 33 | + return &logCounter{ |
| 34 | + logCh: logCh, |
| 35 | + buffer: systemlogmonitor.NewLogBuffer(bufferSize), |
| 36 | + pattern: pattern, |
| 37 | + clock: clock, |
| 38 | + }, clock, logCh |
| 39 | +} |
| 40 | + |
| 41 | +func TestCount(t *testing.T) { |
| 42 | + startTime := time.Now() |
| 43 | + for _, tc := range []struct { |
| 44 | + description string |
| 45 | + logs []*systemtypes.Log |
| 46 | + pattern string |
| 47 | + expectedCount int |
| 48 | + }{ |
| 49 | + { |
| 50 | + description: "no logs", |
| 51 | + logs: []*systemtypes.Log{}, |
| 52 | + pattern: "", |
| 53 | + expectedCount: 0, |
| 54 | + }, |
| 55 | + { |
| 56 | + description: "one matching log", |
| 57 | + logs: []*systemtypes.Log{ |
| 58 | + { |
| 59 | + Timestamp: startTime.Add(-time.Second), |
| 60 | + Message: "0", |
| 61 | + }, |
| 62 | + }, |
| 63 | + pattern: "0", |
| 64 | + expectedCount: 1, |
| 65 | + }, |
| 66 | + { |
| 67 | + description: "one non-matching log", |
| 68 | + logs: []*systemtypes.Log{ |
| 69 | + { |
| 70 | + Timestamp: startTime.Add(-time.Second), |
| 71 | + Message: "1", |
| 72 | + }, |
| 73 | + }, |
| 74 | + pattern: "0", |
| 75 | + expectedCount: 0, |
| 76 | + }, |
| 77 | + { |
| 78 | + description: "log too new", |
| 79 | + logs: []*systemtypes.Log{ |
| 80 | + { |
| 81 | + Timestamp: startTime.Add(time.Second), |
| 82 | + Message: "0", |
| 83 | + }, |
| 84 | + }, |
| 85 | + pattern: "0", |
| 86 | + expectedCount: 0, |
| 87 | + }, |
| 88 | + { |
| 89 | + description: "many logs", |
| 90 | + logs: []*systemtypes.Log{ |
| 91 | + { |
| 92 | + Timestamp: startTime.Add(-time.Second), |
| 93 | + Message: "0", |
| 94 | + }, |
| 95 | + { |
| 96 | + Timestamp: startTime.Add(-time.Second), |
| 97 | + Message: "0", |
| 98 | + }, |
| 99 | + { |
| 100 | + Timestamp: startTime.Add(-time.Second), |
| 101 | + Message: "1", |
| 102 | + }, |
| 103 | + { |
| 104 | + Timestamp: startTime.Add(time.Second), |
| 105 | + Message: "0", |
| 106 | + }, |
| 107 | + }, |
| 108 | + pattern: "0", |
| 109 | + expectedCount: 2, |
| 110 | + }, |
| 111 | + } { |
| 112 | + t.Run(tc.description, func(t *testing.T) { |
| 113 | + counter, fakeClock, logCh := NewTestLogCounter(tc.pattern, startTime) |
| 114 | + go func(logs []*systemtypes.Log, ch chan<- *systemtypes.Log) { |
| 115 | + for _, log := range logs { |
| 116 | + ch <- log |
| 117 | + } |
| 118 | + // trigger the timeout to ensure the test doesn't block permenantly |
| 119 | + for { |
| 120 | + fakeClock.Step(2 * timeout) |
| 121 | + } |
| 122 | + }(tc.logs, logCh) |
| 123 | + actualCount := counter.Count() |
| 124 | + if actualCount != tc.expectedCount { |
| 125 | + t.Errorf("got %d; expected %d", actualCount, tc.expectedCount) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
0 commit comments