File: log_test.go

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (136 lines) | stat: -rw-r--r-- 4,224 bytes parent folder | download
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
package logs

import (
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

var logTime time.Time

func init() {
	logTime, _ = time.Parse(LogTimeFormat, "2023-08-07T19:56:34.223758260-06:00")
}

func makeTestLogLine(typ, msg string) *LogLine {
	return &LogLine{
		Device:       "stdout",
		ParseLogType: typ,
		Msg:          msg,
		Time:         logTime,
	}
}

func TestGetTailLog(t *testing.T) {
	tests := []struct {
		name        string
		fileContent string
		tail        int
		want        []*LogLine
	}{
		{
			name: "simple tail",
			fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1
2023-08-07T19:56:34.223758260-06:00 stdout F line2
2023-08-07T19:56:34.223758260-06:00 stdout F line3
`,
			tail: 2,
			want: []*LogLine{makeTestLogLine("F", "line2"), makeTestLogLine("F", "line3")},
		},
		{
			name: "simple tail with more tail than lines",
			fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1
2023-08-07T19:56:34.223758260-06:00 stdout F line2
2023-08-07T19:56:34.223758260-06:00 stdout F line3
`,
			tail: 10,
			want: []*LogLine{makeTestLogLine("F", "line1"), makeTestLogLine("F", "line2"), makeTestLogLine("F", "line3")},
		},
		{
			name: "tail with partial logs",
			fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1
2023-08-07T19:56:34.223758260-06:00 stdout P lin
2023-08-07T19:56:34.223758260-06:00 stdout F e2
2023-08-07T19:56:34.223758260-06:00 stdout F line3
`,
			tail: 2,
			want: []*LogLine{makeTestLogLine("P", "lin"), makeTestLogLine("F", "e2"), makeTestLogLine("F", "line3")},
		},
		{
			name: "tail with partial logs and more than lines",
			fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1
2023-08-07T19:56:34.223758260-06:00 stdout P lin
2023-08-07T19:56:34.223758260-06:00 stdout F e2
2023-08-07T19:56:34.223758260-06:00 stdout F line3
`,
			tail: 10,
			want: []*LogLine{makeTestLogLine("F", "line1"), makeTestLogLine("P", "lin"), makeTestLogLine("F", "e2"), makeTestLogLine("F", "line3")},
		},
		{
			name: "multiple partial lines in a row",
			fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1
2023-08-07T19:56:34.223758260-06:00 stdout P l
2023-08-07T19:56:34.223758260-06:00 stdout P i
2023-08-07T19:56:34.223758260-06:00 stdout P n
2023-08-07T19:56:34.223758260-06:00 stdout P e
2023-08-07T19:56:34.223758260-06:00 stdout F 2
2023-08-07T19:56:34.223758260-06:00 stdout F line3
`,
			tail: 2,
			want: []*LogLine{makeTestLogLine("P", "l"), makeTestLogLine("P", "i"), makeTestLogLine("P", "n"),
				makeTestLogLine("P", "e"), makeTestLogLine("F", "2"), makeTestLogLine("F", "line3")},
		},
		{
			name: "partial line at the end",
			fileContent: `2023-08-07T19:56:34.223758260-06:00 stdout F line1
2023-08-07T19:56:34.223758260-06:00 stdout P lin
`,
			tail: 1,
			want: []*LogLine{makeTestLogLine("P", "lin")},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			dir := t.TempDir()
			file := filepath.Join(dir, "log")
			f, err := os.Create(file)
			assert.NoError(t, err, "create log file")
			_, err = f.WriteString(tt.fileContent)
			assert.NoError(t, err, "write log file")
			f.Close()
			got, err := getTailLog(file, tt.tail)
			assert.NoError(t, err, "getTailLog()")
			assert.Equal(t, tt.want, got, "log lines")
		})
	}
}

func TestGetTailLogBigFiles(t *testing.T) {
	dir := t.TempDir()
	file := filepath.Join(dir, "log")
	f, err := os.Create(file)
	assert.NoError(t, err, "create log file")
	want := make([]*LogLine, 0, 2000)
	for range 1000 {
		_, err = f.WriteString(`2023-08-07T19:56:34.223758260-06:00 stdout P lin
2023-08-07T19:56:34.223758260-06:00 stdout F e2
`)
		assert.NoError(t, err, "write log file")
		want = append(want, makeTestLogLine("P", "lin"), makeTestLogLine("F", "e2"))
	}
	f.Close()

	// try a big tail greater than the lines
	got, err := getTailLog(file, 5000)
	assert.NoError(t, err, "getTailLog()")
	assert.Equal(t, want, got, "all log lines")

	// try a smaller than lines tail
	got, err = getTailLog(file, 100)
	assert.NoError(t, err, "getTailLog()")
	// this will return the last 200 lines because of partial + full and we only count full lines for tail.
	assert.Equal(t, want[1800:2000], got, "tail 100 log lines")
}