From 9b9dcebea87f6a719f4d5ad6a08314612cf9c616 Mon Sep 17 00:00:00 2001 From: Matthew Fisher Date: Wed, 9 Oct 2019 14:12:09 -0700 Subject: [PATCH] fix(kube): watch events from a matching pod Signed-off-by: Matthew Fisher --- pkg/kube/client.go | 2 +- pkg/releasetesting/environment.go | 10 +++++++--- pkg/releasetesting/environment_test.go | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index 6064f42ef..0d2ca20e6 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -935,7 +935,7 @@ func (c *Client) WaitAndGetCompletedPodPhase(namespace string, reader io.Reader, } func (c *Client) watchPodUntilComplete(timeout time.Duration, info *resource.Info) error { - lw := cachetools.NewListWatchFromClient(info.Client, info.Mapping.Resource.Resource, info.Namespace, fields.Everything()) + lw := cachetools.NewListWatchFromClient(info.Client, info.Mapping.Resource.Resource, info.Namespace, fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name=%s", info.Name))) c.Log("Watching pod %s for completion with timeout of %v", info.Name, timeout) ctx, cancel := watchtools.ContextWithOptionalTimeout(context.Background(), timeout) diff --git a/pkg/releasetesting/environment.go b/pkg/releasetesting/environment.go index 9e9be7c61..4da012a1f 100644 --- a/pkg/releasetesting/environment.go +++ b/pkg/releasetesting/environment.go @@ -21,6 +21,7 @@ import ( "fmt" "io/ioutil" "log" + "strings" "sync" "time" @@ -31,6 +32,9 @@ import ( "k8s.io/helm/pkg/tiller/environment" ) +// logEscaper is necessary for escaping control characters found in the log stream. +var logEscaper = strings.NewReplacer("%", "%%") + // Environment encapsulates information about where test suite executes and returns results type Environment struct { Namespace string @@ -136,7 +140,7 @@ func (env *Environment) GetLogs(testManifests []string) { env.streamError(err.Error()) continue } - if len(infos) < 1 { + if len(infos) == 0 { env.streamError(fmt.Sprint("Pod manifest is invalid. Unable to obtain the logs")) continue } @@ -151,7 +155,7 @@ func (env *Environment) GetLogs(testManifests []string) { env.streamError(err.Error()) continue } - msg := fmt.Sprintf("\nPOD LOGS: %s\n%s", podName, logs) - env.streamMessage(msg, release.TestRun_UNKNOWN) + msg := fmt.Sprintf("\nPOD LOGS: %s\n%s", podName, logEscaper.Replace(string(logs))) + env.streamMessage(msg, release.TestRun_RUNNING) } } diff --git a/pkg/releasetesting/environment_test.go b/pkg/releasetesting/environment_test.go index 826dfbaf7..4daaa6bdb 100644 --- a/pkg/releasetesting/environment_test.go +++ b/pkg/releasetesting/environment_test.go @@ -95,6 +95,16 @@ func TestGetTestPodLogs(t *testing.T) { mockTestEnv.KubeClient = newGetLogKubeClient() mockTestEnv.GetLogs(mockTestSuite.TestManifests) + + expectedMessage := "ERROR: Pod manifest is invalid. Unable to obtain the logs" + + stream := mockTestEnv.Stream.(*mockStream) + if len(stream.messages) != 1 { + t.Errorf("Expected 1 message, got: %v", len(stream.messages)) + } + if stream.messages[0].Msg != expectedMessage { + t.Errorf("Expected message '%s', got: %v", expectedMessage, stream.messages[0].Msg) + } } func TestStreamMessage(t *testing.T) {