|
| 1 | +// Copyright 2025 The gVisor Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package log |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestWarnOn(t *testing.T) { |
| 24 | + tw := &testWriter{} |
| 25 | + e := GoogleEmitter{Writer: &Writer{Next: tw}} |
| 26 | + bl := &BasicLogger{ |
| 27 | + Emitter: e, |
| 28 | + Level: Debug, |
| 29 | + } |
| 30 | + old := Log() |
| 31 | + log.Store(bl) |
| 32 | + defer log.Store(old) |
| 33 | + |
| 34 | + testCases := map[string]func(t *testing.T){ |
| 35 | + "testConditionControlsPrint": func(t *testing.T) { |
| 36 | + WARN_ON(false) |
| 37 | + if len(tw.lines) > 0 { |
| 38 | + t.Errorf("WARN_ON printed when it shouldn't have") |
| 39 | + } |
| 40 | + |
| 41 | + WARN_ON(true) |
| 42 | + if len(tw.lines) == 0 { |
| 43 | + t.Errorf("WARN_ON didn't print anything when it should have") |
| 44 | + } |
| 45 | + }, |
| 46 | + "testStringFormat": func(t *testing.T) { |
| 47 | + expectFile := "pkg/log/warn_on_test.go" |
| 48 | + // Don't try to match the line to make this test less |
| 49 | + // brittle to somebody accidentally sneezing on this file. |
| 50 | + expectStr := strings.SplitN(warnFmtStr, "%", 2)[0] |
| 51 | + |
| 52 | + WARN_ON(true) |
| 53 | + |
| 54 | + if len(tw.lines) == 0 { |
| 55 | + t.Errorf("WARN_ON didn't print anything when it should have") |
| 56 | + } |
| 57 | + if !strings.Contains(tw.lines[0], expectFile) { |
| 58 | + t.Errorf("WARN_ON didn't contain expected output, expected: '%s', got: '%s'", expectFile, tw.lines[0]) |
| 59 | + } |
| 60 | + if !strings.Contains(tw.lines[0], expectStr) { |
| 61 | + t.Errorf("WARN_ON didn't contain expected output, expected: '%s', got: '%s'", expectStr, tw.lines[0]) |
| 62 | + } |
| 63 | + }, |
| 64 | + "testCustomFormat": func(t *testing.T) { |
| 65 | + expectFile := "pkg/log/warn_on_test.go" |
| 66 | + expectStr1 := strings.SplitN(warnFmtStr, "%", 2)[0] |
| 67 | + expectStr2 := "This is just a test warning" |
| 68 | + WARN(true, "This is just a test warning") |
| 69 | + |
| 70 | + if len(tw.lines) == 0 { |
| 71 | + t.Errorf("WARN_ON didn't print anything when it should have") |
| 72 | + } |
| 73 | + if !strings.Contains(tw.lines[0], expectFile) { |
| 74 | + t.Errorf("WARN_ON didn't contain expected output, expected: '%s', got: '%s'", expectFile, tw.lines[0]) |
| 75 | + } |
| 76 | + if !strings.Contains(tw.lines[0], expectStr1) { |
| 77 | + t.Errorf("WARN_ON didn't contain expected output, expected: '%s', got: '%s'", expectStr1, tw.lines[0]) |
| 78 | + } |
| 79 | + if !strings.Contains(tw.lines[0], expectStr2) { |
| 80 | + t.Errorf("WARN_ON didn't contain expected output, expected: '%s', got: '%s'", expectStr2, tw.lines[0]) |
| 81 | + } |
| 82 | + }, |
| 83 | + "testWarnErr": func(t *testing.T) { |
| 84 | + expectFile := "pkg/log/warn_on_test.go" |
| 85 | + expectStr1 := strings.SplitN(warnFmtStr, "%", 2)[0] |
| 86 | + expectStr2 := "My little error string" |
| 87 | + var err error |
| 88 | + WARN_ERR(err) |
| 89 | + if len(tw.lines) > 0 { |
| 90 | + t.Errorf("WARN_ON printed when it shouldn't have") |
| 91 | + } |
| 92 | + |
| 93 | + err = fmt.Errorf("My little error string") |
| 94 | + WARN_ERR(err) |
| 95 | + if len(tw.lines) == 0 { |
| 96 | + t.Errorf("WARN_ON didn't print anything when it should have") |
| 97 | + } |
| 98 | + if !strings.Contains(tw.lines[0], expectFile) { |
| 99 | + t.Errorf("WARN_ON didn't contain expected output, expected: '%s', got: '%s'", expectFile, tw.lines[0]) |
| 100 | + } |
| 101 | + if !strings.Contains(tw.lines[0], expectStr1) { |
| 102 | + t.Errorf("WARN_ON didn't contain expected output, expected: '%s', got: '%s'", expectStr1, tw.lines[0]) |
| 103 | + } |
| 104 | + if !strings.Contains(tw.lines[0], expectStr2) { |
| 105 | + t.Errorf("WARN_ON didn't contain expected output, expected: '%s', got: '%s'", expectStr2, tw.lines[0]) |
| 106 | + } |
| 107 | + }, |
| 108 | + "testWarnOnceOnlyPrintsOnce": func(t *testing.T) { |
| 109 | + testHelperFunc := func() { |
| 110 | + WARN_ON_ONCE(true) |
| 111 | + } |
| 112 | + |
| 113 | + testHelperFunc() |
| 114 | + if len(tw.lines) == 0 { |
| 115 | + t.Errorf("WarnOnOnce didn't print anything when it should have") |
| 116 | + } |
| 117 | + tw.clear() |
| 118 | + |
| 119 | + testHelperFunc() |
| 120 | + if len(tw.lines) > 0 { |
| 121 | + t.Errorf("WarnOnOnce printed out a warning a second time when it shouldn't have") |
| 122 | + } |
| 123 | + }, |
| 124 | + "testWarnOnceDoesntClobberOthers": func(t *testing.T) { |
| 125 | + WARN_ON_ONCE(true) |
| 126 | + if len(tw.lines) == 0 { |
| 127 | + t.Errorf("First WarnOnOnce didn't print anything when it should have") |
| 128 | + } |
| 129 | + tw.clear() |
| 130 | + |
| 131 | + WARN_ON_ONCE(true) |
| 132 | + if len(tw.lines) == 0 { |
| 133 | + t.Errorf("Second WarnOnOnce didn't print anything when it should have") |
| 134 | + } |
| 135 | + }, |
| 136 | + } |
| 137 | + for name, tc := range testCases { |
| 138 | + tw.clear() |
| 139 | + t.Run(name, tc) |
| 140 | + } |
| 141 | +} |
0 commit comments