| 1 |
// Copyright 2024, Google Inc. |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
| 12 |
// in the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived from |
| 16 |
// this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
// Package logtest is a helper for validating logging tests. |
| 31 |
// |
| 32 |
// To update conformance tests in this package run `go test -update_golden` |
| 33 |
package logtest |
| 34 |
|
| 35 |
import ( |
| 36 |
"bytes" |
| 37 |
"encoding/json" |
| 38 |
"flag" |
| 39 |
"os" |
| 40 |
"path/filepath" |
| 41 |
"testing" |
| 42 |
|
| 43 |
"github.com/google/go-cmp/cmp" |
| 44 |
) |
| 45 |
|
| 46 |
var updateGolden = flag.Bool("update-golden", false, "update golden files") |
| 47 |
|
| 48 |
// DiffTest is a test helper, testing got against contents of a goldenFile. |
| 49 |
func DiffTest(t *testing.T, tempFile, goldenFile string) { |
| 50 |
rawGot, err := os.ReadFile(tempFile) |
| 51 |
if err != nil { |
| 52 |
t.Fatal(err) |
| 53 |
} |
| 54 |
t.Helper() |
| 55 |
if *updateGolden { |
| 56 |
got := removeLogVariance(t, rawGot) |
| 57 |
if err := os.WriteFile(filepath.Join("testdata", goldenFile), got, os.ModePerm); err != nil { |
| 58 |
t.Fatal(err) |
| 59 |
} |
| 60 |
return |
| 61 |
} |
| 62 |
|
| 63 |
want, err := os.ReadFile(filepath.Join("testdata", goldenFile)) |
| 64 |
if err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
got := removeLogVariance(t, rawGot) |
| 68 |
|
| 69 |
if diff := cmp.Diff(want, got); diff != "" { |
| 70 |
t.Errorf("mismatch(-want, +got): %s", diff) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// removeLogVariance removes parts of log lines that may differ between runs |
| 75 |
// and/or machines. |
| 76 |
func removeLogVariance(t *testing.T, in []byte) []byte { |
| 77 |
if len(in) == 0 { |
| 78 |
return in |
| 79 |
} |
| 80 |
bs := bytes.Split(in, []byte("\n")) |
| 81 |
for i, b := range bs { |
| 82 |
if len(b) == 0 { |
| 83 |
continue |
| 84 |
} |
| 85 |
m := map[string]any{} |
| 86 |
if err := json.Unmarshal(b, &m); err != nil { |
| 87 |
t.Fatal(err) |
| 88 |
} |
| 89 |
delete(m, "timestamp") |
| 90 |
if sl, ok := m["sourceLocation"].(map[string]any); ok { |
| 91 |
delete(sl, "file") |
| 92 |
// So that if test cases move around in this file they don't cause |
| 93 |
// failures |
| 94 |
delete(sl, "line") |
| 95 |
} |
| 96 |
b2, err := json.Marshal(m) |
| 97 |
if err != nil { |
| 98 |
t.Fatal(err) |
| 99 |
} |
| 100 |
t.Logf("%s", b2) |
| 101 |
bs[i] = b2 |
| 102 |
} |
| 103 |
return bytes.Join(bs, []byte("\n")) |
| 104 |
} |