MCPcopy Create free account
hub / github.com/containerd/nerdctl / TestReadRotatedJSONLog

Function TestReadRotatedJSONLog

pkg/logging/json_logger_test.go:30–110  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28)
29
30func TestReadRotatedJSONLog(t *testing.T) {
31 tmpDir := t.TempDir()
32 if runtime.GOOS == "windows" {
33 t.Skip("windows implementation does not seem to work right now and should be fixed: https://github.com/containerd/nerdctl/issues/3554")
34 }
35 file, err := os.CreateTemp(tmpDir, "logfile")
36 if err != nil {
37 t.Errorf("unable to create temp file, error: %s", err.Error())
38 }
39 stdoutBuf := &bytes.Buffer{}
40 stderrBuf := &bytes.Buffer{}
41 containerStopped := make(chan os.Signal)
42 // Start to follow the container's log.
43 fileName := file.Name()
44 go func() {
45 lvOpts := LogViewOptions{
46 Follow: true,
47 LogPath: fileName,
48 }
49 viewLogsJSONFileDirect(lvOpts, file.Name(), stdoutBuf, stderrBuf, containerStopped)
50 }()
51
52 // log in stdout
53 expectedStdout := "line0\nline1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\n"
54 dir := filepath.Dir(file.Name())
55 baseName := filepath.Base(file.Name())
56
57 // Write 10 lines to log file.
58 // Let ReadLogs start.
59 time.Sleep(50 * time.Millisecond)
60
61 type logContent struct {
62 Log string `json:"log"`
63 Stream string `json:"stream"`
64 Time string `json:"time"`
65 }
66
67 for line := 0; line < 10; line++ {
68 // Write the first three lines to log file
69 log := logContent{}
70 log.Log = fmt.Sprintf("line%d\n", line)
71 log.Stream = "stdout"
72 log.Time = time.Now().Format(time.RFC3339Nano)
73 time.Sleep(1 * time.Millisecond)
74 logData, _ := json.Marshal(log)
75 file.Write(logData)
76 file.Write([]byte("\n"))
77
78 if line == 5 {
79 file.Close()
80 // Pretend to rotate the log.
81 rotatedName := fmt.Sprintf("%s.%s", baseName, time.Now().Format("20060102-150405"))
82 rotatedName = filepath.Join(dir, rotatedName)
83 if err := os.Rename(filepath.Join(dir, baseName), rotatedName); err != nil {
84 t.Errorf("failed to rotate log %q to %q, error: %s", file.Name(), rotatedName, err.Error())
85 return
86 }
87

Callers

nothing calls this directly

Calls 12

viewLogsJSONFileDirectFunction · 0.85
MarshalMethod · 0.80
TempDirMethod · 0.65
SkipMethod · 0.65
NameMethod · 0.65
DirMethod · 0.65
WriteMethod · 0.65
RenameMethod · 0.65
CreateMethod · 0.65
ErrorMethod · 0.45
CloseMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…