From e9b5f3da8081d97d861475d5d32500f131381736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Tue, 30 Jul 2024 02:55:32 +0200 Subject: [PATCH] feat: add cluster in logger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/engine/context/context.go | 31 ++++++++++++++++++++--- pkg/engine/logging/context.go | 8 ++++++ pkg/engine/logging/l.go | 26 ++++++++++++++++--- pkg/engine/logging/logger.go | 10 ++++---- pkg/engine/logging/testing/fake_logger.go | 7 ++++- pkg/engine/logging/testing/logger.go | 1 + pkg/runner/processors/test.go | 1 + pkg/runner/processors/tests.go | 2 ++ pkg/runner/run.go | 11 +++++--- 9 files changed, 81 insertions(+), 16 deletions(-) diff --git a/pkg/engine/context/context.go b/pkg/engine/context/context.go index 8af5e05d1..a873cd217 100644 --- a/pkg/engine/context/context.go +++ b/pkg/engine/context/context.go @@ -12,10 +12,22 @@ import ( "k8s.io/client-go/rest" ) +type CurrentCluster struct { + name string + cluster clusters.Cluster +} + +func (cc *CurrentCluster) Name() *string { + if cc == nil { + return nil + } + return &cc.name +} + type TestContext struct { *model.Summary bindings binding.Bindings - cluster clusters.Cluster + cluster *CurrentCluster clusters clusters.Registry dryRun bool } @@ -45,12 +57,16 @@ func (tc *TestContext) Clusters() clusters.Registry { return tc.clusters } -func (tc *TestContext) CurrentCluster() clusters.Cluster { +func (tc *TestContext) CurrentCluster() *CurrentCluster { return tc.cluster } func (tc *TestContext) CurrentClusterClient() (*rest.Config, client.Client, error) { - config, client, err := tc.clusters.Build(tc.cluster) + var cluster clusters.Cluster + if tc.cluster != nil { + cluster = tc.cluster.cluster + } + config, client, err := tc.clusters.Build(cluster) if err == nil && client != nil && tc.DryRun() { client = dryrun.New(client) } @@ -72,7 +88,14 @@ func (tc TestContext) WithCluster(ctx context.Context, name string, cluster clus } func (tc TestContext) WithCurrentCluster(ctx context.Context, name string) TestContext { - tc.cluster = tc.Cluster(name) + if cluster := tc.Cluster(name); cluster == nil { + tc.cluster = nil + } else { + tc.cluster = &CurrentCluster{ + name: name, + cluster: cluster, + } + } return tc } diff --git a/pkg/engine/logging/context.go b/pkg/engine/logging/context.go index 88ea7b5fd..3bfddbe91 100644 --- a/pkg/engine/logging/context.go +++ b/pkg/engine/logging/context.go @@ -18,3 +18,11 @@ func FromContext(ctx context.Context) Logger { func IntoContext(ctx context.Context, logger Logger) context.Context { return context.WithValue(ctx, contextKey{}, logger) } + +func WithCluster(ctx context.Context, cluster *string) context.Context { + logger := FromContext(ctx) + if logger != nil { + ctx = IntoContext(ctx, logger.WithCluster(cluster)) + } + return ctx +} diff --git a/pkg/engine/logging/l.go b/pkg/engine/logging/l.go index 5f60f3b95..03369460d 100644 --- a/pkg/engine/logging/l.go +++ b/pkg/engine/logging/l.go @@ -15,6 +15,7 @@ type logger struct { clock clock.PassiveClock test string step string + cluster *string resource client.Object } @@ -31,19 +32,26 @@ func NewLogger(t TLogger, clock clock.PassiveClock, test string, step string) Lo func (l *logger) Log(operation Operation, status Status, color *color.Color, args ...fmt.Stringer) { sprint := fmt.Sprint opLen := 9 - stLen := 5 + stLen := 0 if color != nil { sprint = color.Sprint opLen += 14 - stLen += 14 + // stLen += 14 } a := make([]any, 0, len(args)+2) - prefix := fmt.Sprintf("%s| %s | %s | %s | %-*s | %-*s |", eraser, l.clock.Now().Format("15:04:05"), sprint(l.test), sprint(l.step), opLen, sprint(operation), stLen, sprint(status)) + prefix := fmt.Sprintf("%s| %s | %s | %s | %-*s | %-*s |", eraser, l.clock.Now().Format("15:04:05"), sprint(l.test), sprint(l.step), opLen, sprint(operation), stLen, status) if l.resource != nil { gvk := l.resource.GetObjectKind().GroupVersionKind() key := client.Key(l.resource) prefix = fmt.Sprintf("%s %s/%s @ %s", prefix, gvk.GroupVersion(), gvk.Kind, client.Name(key)) } + if l.cluster != nil { + cluster := *l.cluster + if cluster == "" { + cluster = "@default" + } + prefix = fmt.Sprintf("%s (%s)", prefix, cluster) + } a = append(a, prefix) for _, arg := range args { a = append(a, "\n") @@ -52,12 +60,24 @@ func (l *logger) Log(operation Operation, status Status, color *color.Color, arg l.t.Log(fmt.Sprint(a...)) } +func (l *logger) WithCluster(cluster *string) Logger { + return &logger{ + t: l.t, + clock: l.clock, + test: l.test, + step: l.step, + cluster: cluster, + resource: l.resource, + } +} + func (l *logger) WithResource(resource client.Object) Logger { return &logger{ t: l.t, clock: l.clock, test: l.test, step: l.step, + cluster: l.cluster, resource: resource, } } diff --git a/pkg/engine/logging/logger.go b/pkg/engine/logging/logger.go index 707530234..a362a841a 100644 --- a/pkg/engine/logging/logger.go +++ b/pkg/engine/logging/logger.go @@ -32,9 +32,9 @@ const ( ) const ( - DoneStatus Status = "DONE" - ErrorStatus Status = "ERROR" - OkStatus Status = "OK" - RunStatus Status = "RUN" - LogStatus Status = "LOG" + DoneStatus Status = "🏁" + ErrorStatus Status = "❌" + OkStatus Status = "✅" + RunStatus Status = "🚧" + LogStatus Status = "📄" ) diff --git a/pkg/engine/logging/testing/fake_logger.go b/pkg/engine/logging/testing/fake_logger.go index 983f5eea7..657dce9e1 100644 --- a/pkg/engine/logging/testing/fake_logger.go +++ b/pkg/engine/logging/testing/fake_logger.go @@ -13,7 +13,12 @@ type FakeLogger struct { numCalls int } -func (f *FakeLogger) WithResource(resource client.Object) Logger { +func (f *FakeLogger) WithCluster(*string) Logger { + defer func() { f.numCalls++ }() + return f +} + +func (f *FakeLogger) WithResource(client.Object) Logger { defer func() { f.numCalls++ }() return f } diff --git a/pkg/engine/logging/testing/logger.go b/pkg/engine/logging/testing/logger.go index acf2c8680..8e3aa5906 100644 --- a/pkg/engine/logging/testing/logger.go +++ b/pkg/engine/logging/testing/logger.go @@ -14,5 +14,6 @@ type ( type Logger interface { Log(Operation, Status, *color.Color, ...fmt.Stringer) + WithCluster(*string) Logger WithResource(client.Object) Logger } diff --git a/pkg/runner/processors/test.go b/pkg/runner/processors/test.go index 4fd9372f8..72275dc72 100644 --- a/pkg/runner/processors/test.go +++ b/pkg/runner/processors/test.go @@ -156,6 +156,7 @@ func (p *testProcessor) Run(ctx context.Context, nspacer namespacer.Namespacer, name = fmt.Sprintf("step-%d", i+1) } ctx := logging.IntoContext(ctx, logging.NewLogger(t, p.clock, p.test.Test.Name, fmt.Sprintf("%-*s", p.size, name))) + ctx = logging.WithCluster(ctx, tc.CurrentCluster().Name()) info := StepInfo{ Id: i + 1, } diff --git a/pkg/runner/processors/tests.go b/pkg/runner/processors/tests.go index e2455ee07..38f97719d 100644 --- a/pkg/runner/processors/tests.go +++ b/pkg/runner/processors/tests.go @@ -79,6 +79,7 @@ func (p *testsProcessor) Run(ctx context.Context, tc engine.Context, tests ...di logging.Log(ctx, logging.Internal, logging.ErrorStatus, color.BoldRed, logging.ErrSection(err)) failer.FailNow(ctx) } + ctx = logging.WithCluster(ctx, tc.CurrentCluster().Name()) var nspacer namespacer.Namespacer if namespace != nil { nspacer = namespacer.New(namespace.GetName()) @@ -111,6 +112,7 @@ func (p *testsProcessor) Run(ctx context.Context, tc engine.Context, tests ...di } } ctx = logging.IntoContext(ctx, logging.NewLogger(t, p.clock, test.Test.Name, fmt.Sprintf("%-*s", size, "@chainsaw"))) + ctx = logging.WithCluster(ctx, tc.CurrentCluster().Name()) info := TestInfo{ Id: i + 1, ScenarioId: s + 1, diff --git a/pkg/runner/run.go b/pkg/runner/run.go index a344596a8..95a57197c 100644 --- a/pkg/runner/run.go +++ b/pkg/runner/run.go @@ -46,7 +46,7 @@ func run( if config.Report != nil && config.Report.Format != "" { testsReport = report.New(config.Report.Name) } - tc, err := setupTestContext(ctx, values, cfg, config) + tc, err := setupTestContext(ctx, values, cfg) if err != nil { return nil, err } @@ -63,6 +63,7 @@ func run( t.Parallel() ctx := testing.IntoContext(ctx, t) ctx = logging.IntoContext(ctx, logging.NewLogger(t, clock, t.Name(), "@chainsaw")) + ctx = logging.WithCluster(ctx, tc.CurrentCluster().Name()) processor := processors.NewTestsProcessor(config, clock, testsReport) processor.Run(ctx, tc, tests...) }, @@ -88,7 +89,7 @@ func run( return tc.Summary, nil } -func setupTestContext(ctx context.Context, values any, cluster *rest.Config, config model.Configuration) (engine.Context, error) { +func setupTestContext(ctx context.Context, values any, cluster *rest.Config) (engine.Context, error) { tc := enginecontext.EmptyContext() tc = engine.WithValues(ctx, tc, values) if cluster != nil { @@ -97,7 +98,11 @@ func setupTestContext(ctx context.Context, values any, cluster *rest.Config, con return tc, err } tc = tc.WithCluster(ctx, clusters.DefaultClient, cluster) - return engine.WithCurrentCluster(ctx, tc, clusters.DefaultClient) + tc, err := engine.WithCurrentCluster(ctx, tc, clusters.DefaultClient) + if err != nil { + return tc, err + } + return tc, err } return tc, nil }