github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/log/context_test.go (about) 1 package log 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestLoggerContext(t *testing.T) { 11 ctx := context.Background() 12 assert.Equal(t, GetLogger(ctx), L) // should be same as L variable 13 assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same. 14 15 ctx = WithLogger(ctx, G(ctx).WithField("test", "one")) 16 assert.Equal(t, GetLogger(ctx).Data["test"], "one") 17 assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same. 18 } 19 20 func TestModuleContext(t *testing.T) { 21 ctx := context.Background() 22 assert.Equal(t, GetModulePath(ctx), "") 23 24 ctx = WithModule(ctx, "a") // basic behavior 25 assert.Equal(t, GetModulePath(ctx), "a") 26 logger := GetLogger(ctx) 27 assert.Equal(t, logger.Data["module"], "a") 28 29 parent, ctx := ctx, WithModule(ctx, "a") 30 assert.Equal(t, ctx, parent) // should be a no-op 31 assert.Equal(t, GetModulePath(ctx), "a") 32 assert.Equal(t, GetLogger(ctx).Data["module"], "a") 33 34 ctx = WithModule(ctx, "b") // new module 35 assert.Equal(t, GetModulePath(ctx), "a/b") 36 assert.Equal(t, GetLogger(ctx).Data["module"], "a/b") 37 38 ctx = WithModule(ctx, "c") // new module 39 assert.Equal(t, GetModulePath(ctx), "a/b/c") 40 assert.Equal(t, GetLogger(ctx).Data["module"], "a/b/c") 41 }