Skip to content

Commit

Permalink
feat: add cluster in logger
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Jul 30, 2024
1 parent 6ea7051 commit e9b5f3d
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 16 deletions.
31 changes: 27 additions & 4 deletions pkg/engine/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/engine/logging/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 23 additions & 3 deletions pkg/engine/logging/l.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type logger struct {
clock clock.PassiveClock
test string
step string
cluster *string
resource client.Object
}

Expand All @@ -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")
Expand All @@ -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,
}
}
10 changes: 5 additions & 5 deletions pkg/engine/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "📄"
)
7 changes: 6 additions & 1 deletion pkg/engine/logging/testing/fake_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/engine/logging/testing/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ type (

type Logger interface {
Log(Operation, Status, *color.Color, ...fmt.Stringer)
WithCluster(*string) Logger
WithResource(client.Object) Logger
}
1 change: 1 addition & 0 deletions pkg/runner/processors/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/runner/processors/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions pkg/runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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...)
},
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit e9b5f3d

Please sign in to comment.