forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.go
More file actions
151 lines (129 loc) · 3.72 KB
/
Copy pathhelper.go
File metadata and controls
151 lines (129 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// This file was contributed to by generative AI
package testhelpers
import (
"bufio"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// AssertLastOffset takes path of the registry file and the expected offset
// and returns true if the expected offset exists on registry. Otherwise
// false is returned. It will fail the test on any error reading/parsing
// the registry file.
func AssertLastOffset(t *testing.T, path string, offset int) bool {
entries, _ := ReadFilestreamRegistryLog(t, path)
lastEntry := entries[len(entries)-1]
if lastEntry.Offset != offset {
t.Errorf("expecting offset %d got %d instead", offset, lastEntry.Offset)
t.Log("last registry entries:")
l := len(entries)
max := len(entries)
if max > 10 {
max = 10
}
for _, e := range entries[l-max:] {
t.Logf("%+v\n", e)
}
return false
}
return true
}
type RegistryEntry struct {
Key string
Offset int
EOF bool
Filename string
TTL time.Duration
Op string
Removed bool
}
func ReadFilestreamRegistryLog(t *testing.T, path string) ([]RegistryEntry, map[string]string) {
file, err := os.Open(path)
if err != nil {
t.Fatalf("could not open file '%s': %s", path, err)
}
defer file.Close()
var entries []RegistryEntry
fileNameToNative := map[string]string{}
s := bufio.NewScanner(file)
var lastOperation string
for s.Scan() {
line := s.Bytes()
e := entry{}
if err := json.Unmarshal(line, &e); err != nil {
t.Fatalf("could not read line '%s': %s", string(line), err)
}
// Skips registry log entries containing the operation ID like:
// '{"op":"set","id":46}'
if e.Key == "" {
lastOperation = e.Op
continue
}
// Filestream entry
et := RegistryEntry{
Key: e.Key,
Offset: e.Value.Cursor.Offset,
EOF: e.Value.Cursor.EOF,
TTL: e.Value.TTL,
Filename: e.Value.Meta.Source,
Removed: lastOperation == "remove",
Op: lastOperation,
}
// Handle the log input entries, they have a different format.
if strings.HasPrefix(e.Key, "filebeat::logs") {
et.Offset = e.Value.Offset
et.Filename = e.Value.Source
if lastOperation != "set" {
continue
}
// Extract the native file identity so we can update the
// expected registry accordingly
name := filepath.Base(et.Filename)
id := strings.Join(strings.Split(et.Key, "::")[2:], "::")
fileNameToNative[name] = id
}
entries = append(entries, et)
}
return entries, fileNameToNative
}
type entry struct {
Key string `json:"k"`
Value struct {
// Filestream fields
Cursor struct {
Offset int `json:"offset"`
EOF bool `json:"eof"`
} `json:"cursor"`
Meta struct {
Source string `json:"source"`
} `json:"meta"`
// Log input fields
Source string `json:"source"`
Offset int `json:"offset"`
// Common to both inputs
TTL time.Duration `json:"ttl"`
} `json:"v"`
// Keys to read the "operation"
// e.g: {"op":"set","id":46}
Op string `json:"op"`
ID int `json:"id"`
}