-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlogfile_test.go
134 lines (110 loc) · 3.05 KB
/
logfile_test.go
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
package workers
import (
"fmt"
"log"
"net"
"os"
"regexp"
"testing"
"time"
"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-logger"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func Test_LogFileText(t *testing.T) {
testcases := []struct {
mode string
pattern string
}{
{
mode: pkgconfig.ModeText,
pattern: "0b dns.collector A",
},
{
mode: pkgconfig.ModeJSON,
pattern: "\"qname\":\"dns.collector\"",
},
{
mode: pkgconfig.ModeFlatJSON,
pattern: "\"dns.qname\":\"dns.collector\"",
},
}
for i, tc := range testcases {
t.Run(tc.mode, func(t *testing.T) {
// create a temp file
f, err := os.CreateTemp("", fmt.Sprintf("temp_logfile%d", i))
if err != nil {
log.Fatal(err)
}
defer os.Remove(f.Name()) // clean up
// config
config := pkgconfig.GetDefaultConfig()
config.Loggers.LogFile.FilePath = f.Name()
config.Loggers.LogFile.Mode = tc.mode
config.Loggers.LogFile.FlushInterval = 0
// init generator in testing mode
g := NewLogFile(config, logger.New(false), "test")
// start the logger
go g.StartCollect()
// send fake dns message to logger
dm := dnsutils.GetFakeDNSMessage()
dm.DNSTap.Identity = dnsutils.DNSTapIdentityTest
g.GetInputChannel() <- dm
time.Sleep(time.Second)
g.Stop()
// read temp file and check content
data := make([]byte, 1024)
count, err := f.Read(data)
if err != nil {
log.Fatal(err)
}
pattern := regexp.MustCompile(tc.pattern)
if !pattern.MatchString(string(data[:count])) {
t.Errorf("loki test error want %s, got: %s", tc.pattern, string(data[:count]))
}
})
}
}
func Test_LogFileWrite_PcapMode(t *testing.T) {
// create a temp file
f, err := os.CreateTemp("", "temp_pcapfile")
if err != nil {
log.Fatal(err)
}
defer os.Remove(f.Name()) // clean up
// config
config := pkgconfig.GetDefaultConfig()
config.Loggers.LogFile.FilePath = f.Name()
config.Loggers.LogFile.Mode = pkgconfig.ModePCAP
// init generator in testing mode
g := NewLogFile(config, logger.New(false), "test")
// init fake dm
dm := dnsutils.GetFakeDNSMessage()
// fake network packet
pkt := []gopacket.SerializableLayer{}
eth := &layers.Ethernet{SrcMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}
eth.EthernetType = layers.EthernetTypeIPv4
ip4 := &layers.IPv4{Version: 4, TTL: 64}
ip4.SrcIP = net.ParseIP("127.0.0.1")
ip4.DstIP = net.ParseIP("127.0.0.1")
ip4.Protocol = layers.IPProtocolUDP
udp := &layers.UDP{}
udp.SrcPort = layers.UDPPort(1000)
udp.DstPort = layers.UDPPort(53)
udp.SetNetworkLayerForChecksum(ip4)
pkt = append(pkt, gopacket.Payload(dm.DNS.Payload), udp, ip4, eth)
// write fake dns message and network packet
g.WriteToPcap(dm, pkt)
// read temp file and check content
data := make([]byte, 100)
count, err := f.Read(data)
if err != nil {
t.Errorf("unexpected error: %e", err)
}
if count == 0 {
t.Errorf("no data in pcap file")
}
}