Skip to content

Commit

Permalink
fix: timestamp length only for valid timestamps (#5819)
Browse files Browse the repository at this point in the history
  • Loading branch information
exu committed Sep 6, 2024
1 parent ba72665 commit 3c88561
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cmd/kubectl-testkube/commands/testworkflows/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,14 @@ func printResultDifference(res1 *testkube.TestWorkflowResult, res2 *testkube.Tes
return changed
}

// getTimestampLength returns length of timestamp in the line if timestamp is valid RFC timestamp.
func getTimestampLength(line string) int {
// 29th character will be either '+' for +00:00 timestamp,
// or 'Z' for UTC timestamp (without 00:00 section).
if len(line) >= 29 && line[29] == '+' {
if len(line) >= 29 && (line[29] == '+' || line[29] == 'Z') {
return len(time.RFC3339Nano)
}
return LogTimestampLength
return 0
}

func watchTestWorkflowLogs(id string, signature []testkube.TestWorkflowSignature, client apiclientv1.Client) (*testkube.TestWorkflowResult, error) {
Expand Down
26 changes: 26 additions & 0 deletions cmd/kubectl-testkube/commands/testworkflows/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package testworkflows

import (
"testing"
"time"

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

func TestGetetTimestampLength(t *testing.T) {
t.Run("returns length of nano for valid timestamps", func(t *testing.T) {
l := getTimestampLength("2006-01-02T15:04:05.999999999Z07:00")
assert.Equal(t, len(time.RFC3339Nano), l)

l = getTimestampLength("2006-01-02T15:04:05.999999999+07:00")
assert.Equal(t, len(time.RFC3339Nano), l)
})

t.Run("returns 0 for invalid timestamps", func(t *testing.T) {
l := getTimestampLength("2006-01-02T15:04:05.99")
assert.Equal(t, 0, l)

l = getTimestampLength("2006-01-02T15:04:05.99")
assert.Equal(t, 0, l)
})
}

0 comments on commit 3c88561

Please sign in to comment.