99 "github.com/pkg/errors"
1010 "go.viam.com/test"
1111
12+ "go.viam.com/rdk/components/generic"
1213 "go.viam.com/rdk/components/motor"
1314 "go.viam.com/rdk/components/motor/fake"
1415 "go.viam.com/rdk/config"
@@ -19,6 +20,7 @@ import (
1920 "go.viam.com/rdk/robot/framesystem"
2021 "go.viam.com/rdk/services/motion"
2122 motionBuiltin "go.viam.com/rdk/services/motion/builtin"
23+ rtestutils "go.viam.com/rdk/testutils"
2224 "go.viam.com/rdk/utils"
2325)
2426
@@ -471,3 +473,71 @@ func (m *dummyModMan) Close(ctx context.Context) error {
471473 }
472474 return nil
473475}
476+
477+ func TestDynamicModuleLogging (t * testing.T ) {
478+ modPath , err := rtestutils .BuildTempModule (t , "module/testmodule" )
479+ test .That (t , err , test .ShouldBeNil )
480+
481+ ctx := context .Background ()
482+ logger , observer := logging .NewObservedTestLogger (t )
483+
484+ helperConf := resource.Config {
485+ Name : "helper" ,
486+ API : generic .API ,
487+ Model : resource .NewModel ("rdk" , "test" , "helper" ),
488+ LogConfiguration : resource.LogConfig {
489+ Level : logging .INFO ,
490+ },
491+ }
492+ cfg := & config.Config {
493+ Components : []resource.Config {helperConf },
494+ Modules : []config.Module {{
495+ Name : "helperModule" ,
496+ ExePath : modPath ,
497+ LogLevel : "info" ,
498+ Type : "local" ,
499+ }},
500+ }
501+
502+ myRobot , err := RobotFromConfig (ctx , cfg , logger )
503+ test .That (t , err , test .ShouldBeNil )
504+ defer myRobot .Close (ctx )
505+
506+ client , err := generic .FromRobot (myRobot , "helper" )
507+ test .That (t , err , test .ShouldBeNil )
508+ defer client .Close (ctx )
509+
510+ //nolint:lll
511+ // Have the module log a line at info. It should appear as:
512+ // 2023-12-06T15:55:32.590-0500 INFO process.helperModule_/tmp/TestDynamicModuleLogging3790223620/001/testmodule.StdOut pexec/managed_process.go:244
513+ // \_ 2023-12-06T15:55:32.590-0500 INFO TestModule.rdk:component:generic/helper testmodule/main.go:147 special rare log line
514+ logLine := "special rare log line"
515+ testCmd := map [string ]interface {}{"command" : "log" , "msg" : logLine , "level" : "info" }
516+ _ , err = client .DoCommand (ctx , testCmd )
517+ test .That (t , err , test .ShouldBeNil )
518+
519+ // Our log observer should find one occurrence of the log line.
520+ test .That (t , observer .FilterMessageSnippet (logLine ).Len (), test .ShouldEqual , 1 )
521+
522+ // The module is currently configured to log at info. If the module tries to log at debug,
523+ // nothing new should be observed.
524+ testCmd = map [string ]interface {}{"command" : "log" , "msg" : logLine , "level" : "debug" }
525+ _ , err = client .DoCommand (ctx , testCmd )
526+ test .That (t , err , test .ShouldBeNil )
527+
528+ test .That (t , observer .FilterMessageSnippet (logLine ).Len (), test .ShouldEqual , 1 )
529+ test .That (t , observer .FilterMessageSnippet (logLine ).FilterMessageSnippet ("DEBUG" ).Len (), test .ShouldEqual , 0 )
530+
531+ // Change the modular component to log at DEBUG instead of INFO.
532+ cfg .Components [0 ].LogConfiguration .Level = logging .DEBUG
533+ myRobot .Reconfigure (ctx , cfg )
534+
535+ // Trying to log again at DEBUG should see our log line pattern show up a second time. Now with
536+ // DEBUG in the output string.
537+ testCmd = map [string ]interface {}{"command" : "log" , "msg" : logLine , "level" : "debug" }
538+ _ , err = client .DoCommand (ctx , testCmd )
539+ test .That (t , err , test .ShouldBeNil )
540+
541+ test .That (t , observer .FilterMessageSnippet (logLine ).Len (), test .ShouldEqual , 2 )
542+ test .That (t , observer .FilterMessageSnippet (logLine ).FilterMessageSnippet ("DEBUG" ).Len (), test .ShouldEqual , 1 )
543+ }
0 commit comments