blob: 3e5dfba9b6cfe138eb6ada56ccb8ce945517646b [file] [log] [blame]
Serge Bazanski5faa2fc2020-09-07 14:09:30 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package logtree
18
19import (
20 "fmt"
21 "strings"
22 "testing"
23 "time"
24)
25
26func TestBacklog(t *testing.T) {
27 tree := New()
28 tree.MustLeveledFor("main").Info("hello, main!")
29 tree.MustLeveledFor("main.foo").Info("hello, main.foo!")
30 tree.MustLeveledFor("main.bar").Info("hello, main.bar!")
31 tree.MustLeveledFor("aux").Info("hello, aux!")
32
33 expect := func(dn DN, entries ...string) string {
34 res := tree.Read(dn, WithChildren(), WithBacklog(BacklogAllAvailable))
35 if want, got := len(entries), len(res.Backlog); want != got {
36 t.Fatalf("wanted %d backlog entries, got %d", want, got)
37 }
38 got := make(map[string]bool)
39 for _, entry := range res.Backlog {
40 got[entry.Message()] = true
41 }
42 for _, entry := range entries {
43 if !got[entry] {
44 return fmt.Sprintf("missing entry %q", entry)
45 }
46 }
47 return ""
48 }
49
50 if res := expect("main", "hello, main!", "hello, main.foo!", "hello, main.bar!"); res != "" {
51 t.Errorf("retrieval at main failed: %s", res)
52 }
53 if res := expect("", "hello, main!", "hello, main.foo!", "hello, main.bar!", "hello, aux!"); res != "" {
54 t.Errorf("retrieval at root failed: %s", res)
55 }
56 if res := expect("aux", "hello, aux!"); res != "" {
57 t.Errorf("retrieval at aux failed: %s", res)
58 }
59}
60
61func TestStream(t *testing.T) {
62 tree := New()
63 tree.MustLeveledFor("main").Info("hello, backlog")
64
65 res := tree.Read("", WithBacklog(BacklogAllAvailable), WithChildren(), WithStream())
66 defer res.Close()
67 if want, got := 1, len(res.Backlog); want != got {
68 t.Errorf("wanted %d backlog item, got %d", want, got)
69 }
70
71 tree.MustLeveledFor("main").Info("hello, stream")
72
73 select {
74 case <-time.After(time.Second * 1):
75 t.Fatalf("timeout elapsed")
76 case p := <-res.Stream:
77 if want, got := "hello, stream", p.Message(); want != got {
78 t.Fatalf("stream returned %q, wanted %q", got, want)
79 }
80 }
81}
82
83func TestVerbose(t *testing.T) {
84 tree := New()
85
86 tree.MustLeveledFor("main").V(10).Info("this shouldn't get logged")
87
88 reader := tree.Read("", WithBacklog(BacklogAllAvailable), WithChildren())
89 if want, got := 0, len(reader.Backlog); want != got {
90 t.Fatalf("expected nothing to be logged, got %+v", reader.Backlog)
91 }
92
93 tree.SetVerbosity("main", 10)
94 tree.MustLeveledFor("main").V(10).Info("this should get logged")
95
96 reader = tree.Read("", WithBacklog(BacklogAllAvailable), WithChildren())
97 if want, got := 1, len(reader.Backlog); want != got {
98 t.Fatalf("expected %d entries to get logged, got %d", want, got)
99 }
100}
101
102func TestMetadata(t *testing.T) {
103 tree := New()
104 tree.MustLeveledFor("main").Error("i am an error")
105 tree.MustLeveledFor("main").Warning("i am a warning")
106 tree.MustLeveledFor("main").Info("i am informative")
107 tree.MustLeveledFor("main").V(0).Info("i am a zero-level debug")
108
109 reader := tree.Read("", WithChildren(), WithBacklog(BacklogAllAvailable))
110 if want, got := 4, len(reader.Backlog); want != got {
111 t.Fatalf("expected %d entries, got %d", want, got)
112 }
113
114 for _, te := range []struct {
115 ix int
116 severity Severity
117 message string
118 }{
119 {0, ERROR, "i am an error"},
120 {1, WARNING, "i am a warning"},
121 {2, INFO, "i am informative"},
122 {3, INFO, "i am a zero-level debug"},
123 } {
124 p := reader.Backlog[te.ix]
125 if want, got := te.severity, p.Severity(); want != got {
126 t.Errorf("wanted element %d to have severity %s, got %s", te.ix, want, got)
127 }
128 if want, got := te.message, p.Message(); want != got {
129 t.Errorf("wanted element %d to have message %q, got %q", te.ix, want, got)
130 }
131 if want, got := "logtree_test.go", strings.Split(p.Location(), ":")[0]; want != got {
132 t.Errorf("wanted element %d to have file %q, got %q", te.ix, want, got)
133 }
134 }
135}
136
137func TestSeverity(t *testing.T) {
138 tree := New()
139 tree.MustLeveledFor("main").Error("i am an error")
140 tree.MustLeveledFor("main").Warning("i am a warning")
141 tree.MustLeveledFor("main").Info("i am informative")
142 tree.MustLeveledFor("main").V(0).Info("i am a zero-level debug")
143
144 reader := tree.Read("main", WithBacklog(BacklogAllAvailable), WithMinimumSeverity(WARNING))
145 if want, got := 2, len(reader.Backlog); want != got {
146 t.Fatalf("wanted %d entries, got %d", want, got)
147 }
148 if want, got := "i am an error", reader.Backlog[0].Message(); want != got {
149 t.Fatalf("wanted entry %q, got %q", want, got)
150 }
151 if want, got := "i am a warning", reader.Backlog[1].Message(); want != got {
152 t.Fatalf("wanted entry %q, got %q", want, got)
153 }
154}