diff --git a/test/utils/localupcluster/localupcluster.go b/test/utils/localupcluster/localupcluster.go index 4cc0e142f4b..32c16e77976 100644 --- a/test/utils/localupcluster/localupcluster.go +++ b/test/utils/localupcluster/localupcluster.go @@ -44,6 +44,7 @@ import ( "time" "github.com/onsi/gomega" + "github.com/onsi/gomega/format" "github.com/onsi/gomega/gstruct" corev1 "k8s.io/api/core/v1" @@ -299,12 +300,39 @@ func (c *Cluster) LoadConfig(tCtx ktesting.TContext) *restclient.Config { } // GetSystemLogs returns the output of the given component, the empty string and false if not started. -func (c *Cluster) GetSystemLogs(tCtx ktesting.TContext, component KubeComponentName) (string, bool) { +func (c *Cluster) GetSystemLogs(tCtx ktesting.TContext, component KubeComponentName) (GString, bool) { cmd, ok := c.running[component] if !ok { return "", false } - return cmd.Output(tCtx), true + return GString(cmd.Output(tCtx)), true +} + +// GString is useful for log output because in contrast to the default behavior for +// strings it truncates in the middle. +type GString string + +var ( + _ format.GomegaStringer = GString("") + _ fmt.Stringer = GString("") +) + +func (gs GString) GomegaString() string { + s := string(gs) + if len(s) < format.MaxLength { + return s + } + elipsis := "\n...\n" + keepLen := format.MaxLength - len(elipsis) + if keepLen < 0 { + // Cannot truncate in the middle, result would be too long. Leave it to Gomega. + return s + } + return s[:keepLen/2] + elipsis + s[len(gs)-keepLen/2:] +} + +func (gs GString) String() string { + return string(gs) } type ModifyOptions struct {