-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log the phase output in its unformatted form. (#1432)
This ensures that later output parsing code doesn't have to handle additional log metadata prefixes. It aligns with the previous implementation in kube.Exec() at https://github.com/kanisterio/kanister/blob/c3ee5854d48f63073012b924d3c793ddcb58ea49/pkg/function/kube_exec.go#L94-L101 Signed-off-by: Ivan Sim <ivan.sim@kasten.io>
- Loading branch information
Showing
3 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package format | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/kanisterio/kanister/pkg/output" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type FormatTest struct{} | ||
|
||
var _ = Suite(&FormatTest{}) | ||
|
||
func (s *FormatTest) TestLogToForPhaseOutputs(c *C) { | ||
const ( | ||
pod = "test-pod-logto" | ||
container = "test-container-logto" | ||
) | ||
|
||
// invariant: format.LogTo() will not format phase outputs. | ||
|
||
var testCases = []struct { | ||
keys []string | ||
values []string | ||
}{ | ||
{ | ||
keys: []string{"key1"}, | ||
values: []string{"value1"}, | ||
}, | ||
{ | ||
keys: []string{"key1", "key2"}, | ||
values: []string{"value1", "value2"}, | ||
}, | ||
{ | ||
keys: []string{"key1", "key2", "key3"}, | ||
values: []string{"value1", "value2", "value3"}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
expected := "" | ||
input := &bytes.Buffer{} | ||
actual := &bytes.Buffer{} | ||
|
||
for i, key := range tc.keys { | ||
// create the phase output for each pair of the given k/v | ||
kv := &bytes.Buffer{} | ||
err := output.PrintOutputTo(kv, key, tc.values[i]) | ||
c.Assert(err, IsNil) | ||
|
||
kvRaw := fmt.Sprintf("%s\n", kv.String()) | ||
if _, err := input.WriteString(kvRaw); err != nil { | ||
c.Assert(err, IsNil) | ||
} | ||
|
||
expected += fmt.Sprintf("%s {\"key\":\"%s\",\"value\":\"%s\"}\n", output.PhaseOpString, key, tc.values[i]) | ||
} | ||
LogTo(actual, pod, container, input.String()) | ||
c.Check(expected, DeepEquals, actual.String()) | ||
} | ||
} | ||
|
||
func (s *FormatTest) TestLogToForNormalLogs(c *C) { | ||
const ( | ||
pod = "test-pod-logto" | ||
container = "test-container-logto" | ||
) | ||
|
||
var testCases = []struct { | ||
input string | ||
expected string | ||
count int // count represents how many "Out"s in the results | ||
}{ | ||
{ | ||
input: "", | ||
expected: "", | ||
count: 1, | ||
}, | ||
{ | ||
input: "test logs", | ||
expected: `"Out":"test logs"`, | ||
count: 1, | ||
}, | ||
{ | ||
input: "test logs\ntest logs", | ||
expected: `"Out":"test logs"`, | ||
count: 2, // the line break causes 2 log lines to be printed | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
actual := &bytes.Buffer{} | ||
LogTo(actual, pod, container, tc.input) | ||
|
||
c.Assert(strings.Contains(actual.String(), tc.expected), Equals, true) | ||
c.Assert(strings.Count(actual.String(), tc.expected), Equals, tc.count) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters