Skip to content

Commit

Permalink
Fix issue with util#DisplayLogs potentially not picking the last elem…
Browse files Browse the repository at this point in the history
…ent of lines
  • Loading branch information
rm3l committed Jun 21, 2022
1 parent b4e83ac commit 8595906
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ func DisplayLog(followLog bool, rd io.ReadCloser, writer io.Writer, compName str
index = 0
}

for i := index; i < len(lines)-1; i++ {
for i := index; i < len(lines); i++ {
_, err := fmt.Fprintf(writer, lines[i])
if err != nil {
return err
Expand Down
71 changes: 71 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package util

import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -2618,3 +2621,71 @@ func TestSafeGetBool(t *testing.T) {
})
}
}

func TestDisplayLog(t *testing.T) {
const compName = "my-comp"
type args struct {
input []string
numLines int
}
for _, tt := range []struct {
name string
wantErr bool
want []string

args
}{
{
name: "numberOfLastLines==-1",
args: args{
input: []string{"a", "b", "c"},
numLines: -1,
},
want: []string{"a\n", "b\n", "c\n"},
},
{
name: "numberOfLastLines greater than total number of lines read",
args: args{
input: []string{"one-line"},
numLines: 10,
},
want: []string{"one-line\n"},
},
} {
t.Run(tt.name, func(t *testing.T) {
var b bytes.Buffer
for _, s := range tt.input {
if _, err := b.WriteString(s + "\n"); err != nil {
t.Errorf(" failed to write input data %q", s)
return
}
}
var w bytes.Buffer
err := DisplayLog(false, io.NopCloser(&b), &w, compName, tt.numLines)

if tt.wantErr != (err != nil) {
t.Errorf("expected %v, got %v", tt.wantErr, err)
return
}

//Read w
reader := bufio.NewReader(&w)
var lines []string
var line string
for {
line, err = reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
t.Errorf("unexpected err while reading data: %v", err)
return
}
lines = append(lines, line)
}
if !reflect.DeepEqual(lines, tt.want) {
t.Errorf("expected %v, got %v", tt.want, lines)
}
})
}
}

0 comments on commit 8595906

Please sign in to comment.