blob: 1a2f8ec449559f10dd1994cecfe035a35bfb9c06 [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 "testing"
22)
23
24func TestJournalRetention(t *testing.T) {
25 j := newJournal()
26
27 for i := 0; i < 9000; i += 1 {
28 e := &entry{
29 origin: "main",
30 payload: testPayload(fmt.Sprintf("test %d", i)),
31 }
32 j.append(e)
33 }
34
35 entries := j.getEntries("main")
36 if want, got := 8192, len(entries); want != got {
37 t.Fatalf("wanted %d entries, got %d", want, got)
38 }
39 for i, entry := range entries {
40 want := fmt.Sprintf("test %d", (9000-8192)+i)
41 got := entry.payload.message
42 if want != got {
43 t.Fatalf("wanted entry %q, got %q", want, got)
44 }
45 }
46}
47
48func TestJournalQuota(t *testing.T) {
49 j := newJournal()
50
51 for i := 0; i < 9000; i += 1 {
52 j.append(&entry{
53 origin: "chatty",
54 payload: testPayload(fmt.Sprintf("chatty %d", i)),
55 })
56 if i%10 == 0 {
57 j.append(&entry{
58 origin: "solemn",
59 payload: testPayload(fmt.Sprintf("solemn %d", i)),
60 })
61 }
62 }
63
64 entries := j.getEntries("chatty")
65 if want, got := 8192, len(entries); want != got {
66 t.Fatalf("wanted %d chatty entries, got %d", want, got)
67 }
68 entries = j.getEntries("solemn")
69 if want, got := 900, len(entries); want != got {
70 t.Fatalf("wanted %d solemn entries, got %d", want, got)
71 }
72 entries = j.getEntries("absent")
73 if want, got := 0, len(entries); want != got {
74 t.Fatalf("wanted %d absent entries, got %d", want, got)
75 }
76
77 entries = j.scanEntries(filterAll())
78 if want, got := 8192+900, len(entries); want != got {
79 t.Fatalf("wanted %d total entries, got %d", want, got)
80 }
81 setMessages := make(map[string]bool)
82 for _, entry := range entries {
83 setMessages[entry.payload.message] = true
84 }
85
86 for i := 0; i < 900; i += 1 {
87 want := fmt.Sprintf("solemn %d", i*10)
88 if !setMessages[want] {
89 t.Fatalf("could not find entry %q in journal", want)
90 }
91 }
92 for i := 0; i < 8192; i += 1 {
93 want := fmt.Sprintf("chatty %d", i+(9000-8192))
94 if !setMessages[want] {
95 t.Fatalf("could not find entry %q in journal", want)
96 }
97 }
98}
99
100func TestJournalSubtree(t *testing.T) {
101 j := newJournal()
102 j.append(&entry{origin: "a", payload: testPayload("a")})
103 j.append(&entry{origin: "a.b", payload: testPayload("a.b")})
104 j.append(&entry{origin: "a.b.c", payload: testPayload("a.b.c")})
105 j.append(&entry{origin: "a.b.d", payload: testPayload("a.b.d")})
106 j.append(&entry{origin: "e.f", payload: testPayload("e.f")})
107 j.append(&entry{origin: "e.g", payload: testPayload("e.g")})
108
109 expect := func(f filter, msgs ...string) string {
110 res := j.scanEntries(f)
111 set := make(map[string]bool)
112 for _, entry := range res {
113 set[entry.payload.message] = true
114 }
115
116 for _, want := range msgs {
117 if !set[want] {
118 return fmt.Sprintf("missing entry %q", want)
119 }
120 }
121 return ""
122 }
123
124 if res := expect(filterAll(), "a", "a.b", "a.b.c", "a.b.d", "e.f", "e.g"); res != "" {
125 t.Fatalf("All: %s", res)
126 }
127 if res := expect(filterSubtree("a"), "a", "a.b", "a.b.c", "a.b.d"); res != "" {
128 t.Fatalf("Subtree(a): %s", res)
129 }
130 if res := expect(filterSubtree("a.b"), "a.b", "a.b.c", "a.b.d"); res != "" {
131 t.Fatalf("Subtree(a.b): %s", res)
132 }
133 if res := expect(filterSubtree("e"), "e.f", "e.g"); res != "" {
134 t.Fatalf("Subtree(a.b): %s", res)
135 }
136}