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

Function TestReadJSONLogs

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

Source from the content-addressed store, hash-verified

150}
151
152func TestReadJSONLogs(t *testing.T) {
153 file, err := os.CreateTemp("", "TestFollowLogs")
154 if err != nil {
155 t.Fatalf("unable to create temp file")
156 }
157 defer os.Remove(file.Name())
158 file.WriteString(`{"log":"line1\n","stream":"stdout","time":"2024-07-12T03:09:24.916296732Z"}` + "\n")
159 file.WriteString(`{"log":"line2\n","stream":"stdout","time":"2024-07-12T03:09:24.916296732Z"}` + "\n")
160 file.WriteString(`{"log":"line3\n","stream":"stdout","time":"2024-07-12T03:09:24.916296732Z"}` + "\n")
161
162 stopChan := make(chan os.Signal)
163 testCases := []struct {
164 name string
165 logViewOptions LogViewOptions
166 expected string
167 }{
168 {
169 name: "default log options should output all lines",
170 logViewOptions: LogViewOptions{
171 LogPath: file.Name(),
172 Tail: 0,
173 },
174 expected: "line1\nline2\nline3\n",
175 },
176 {
177 name: "using Tail 2 should output last 2 lines",
178 logViewOptions: LogViewOptions{
179 LogPath: file.Name(),
180 Tail: 2,
181 },
182 expected: "line2\nline3\n",
183 },
184 {
185 name: "using Tail 4 should output all lines when the log has less than 4 lines",
186 logViewOptions: LogViewOptions{
187 LogPath: file.Name(),
188 Tail: 4,
189 },
190 expected: "line1\nline2\nline3\n",
191 },
192 {
193 name: "using Tail 0 should output all",
194 logViewOptions: LogViewOptions{
195 LogPath: file.Name(),
196 Tail: 0,
197 },
198 expected: "line1\nline2\nline3\n",
199 },
200 }
201 for _, tc := range testCases {
202 t.Run(tc.name, func(t *testing.T) {
203 stdoutBuf := bytes.NewBuffer(nil)
204 stderrBuf := bytes.NewBuffer(nil)
205 err = viewLogsJSONFileDirect(tc.logViewOptions, file.Name(), stdoutBuf, stderrBuf, stopChan)
206
207 if err != nil {
208 t.Fatal(err.Error())
209 }

Callers

nothing calls this directly

Calls 6

viewLogsJSONFileDirectFunction · 0.85
RemoveMethod · 0.65
NameMethod · 0.65
RunMethod · 0.65
ErrorMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…