| 150 | } |
| 151 | |
| 152 | func 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 | } |