DRA upgrade/downgrade: better Gomega failure for system log output

Truncating the kube-controller-manager log output at the end when waiting for
"Caches are synced" fails isn't useful because it doesn't show how far the
controller got (might be stuck). This can be solved by implementing
GomegaStringer with truncation in the middle.
This commit is contained in:
Patrick Ohly
2025-12-04 13:34:22 +01:00
parent 3025b0a7b4
commit 36dbf37cf1

View File

@@ -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 {