github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/logger/logger_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package logger_test 5 6 import ( 7 "time" 8 9 "github.com/juju/loggo" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 "gopkg.in/juju/names.v2" 13 14 "github.com/juju/juju/agent" 15 apilogger "github.com/juju/juju/api/logger" 16 "github.com/juju/juju/juju/testing" 17 "github.com/juju/juju/state" 18 "github.com/juju/juju/worker" 19 "github.com/juju/juju/worker/logger" 20 ) 21 22 // worstCase is used for timeouts when timing out 23 // will fail the test. Raising this value should 24 // not affect the overall running time of the tests 25 // unless they fail. 26 const worstCase = 5 * time.Second 27 28 type LoggerSuite struct { 29 testing.JujuConnSuite 30 31 loggerAPI *apilogger.State 32 machine *state.Machine 33 } 34 35 var _ = gc.Suite(&LoggerSuite{}) 36 37 func (s *LoggerSuite) SetUpTest(c *gc.C) { 38 s.JujuConnSuite.SetUpTest(c) 39 apiConn, machine := s.OpenAPIAsNewMachine(c) 40 // Create the machiner API facade. 41 s.loggerAPI = apilogger.NewState(apiConn) 42 c.Assert(s.loggerAPI, gc.NotNil) 43 s.machine = machine 44 } 45 46 func (s *LoggerSuite) waitLoggingInfo(c *gc.C, expected string) { 47 timeout := time.After(worstCase) 48 for { 49 select { 50 case <-timeout: 51 c.Fatalf("timeout while waiting for logging info to change") 52 case <-time.After(10 * time.Millisecond): 53 loggerInfo := loggo.LoggerInfo() 54 if loggerInfo != expected { 55 c.Logf("logging is %q, still waiting", loggerInfo) 56 continue 57 } 58 return 59 } 60 } 61 } 62 63 type mockConfig struct { 64 agent.Config 65 c *gc.C 66 tag names.Tag 67 } 68 69 func (mock *mockConfig) Tag() names.Tag { 70 return mock.tag 71 } 72 73 func agentConfig(c *gc.C, tag names.Tag) *mockConfig { 74 return &mockConfig{c: c, tag: tag} 75 } 76 77 func (s *LoggerSuite) makeLogger(c *gc.C) (worker.Worker, *mockConfig) { 78 config := agentConfig(c, s.machine.Tag()) 79 w, err := logger.NewLogger(s.loggerAPI, config) 80 c.Assert(err, jc.ErrorIsNil) 81 return w, config 82 } 83 84 func (s *LoggerSuite) TestRunStop(c *gc.C) { 85 loggingWorker, _ := s.makeLogger(c) 86 c.Assert(worker.Stop(loggingWorker), gc.IsNil) 87 } 88 89 func (s *LoggerSuite) TestInitialState(c *gc.C) { 90 config, err := s.State.ModelConfig() 91 c.Assert(err, jc.ErrorIsNil) 92 expected := config.LoggingConfig() 93 94 initial := "<root>=DEBUG;wibble=ERROR" 95 c.Assert(expected, gc.Not(gc.Equals), initial) 96 97 loggo.DefaultContext().ResetLoggerLevels() 98 err = loggo.ConfigureLoggers(initial) 99 c.Assert(err, jc.ErrorIsNil) 100 101 loggingWorker, _ := s.makeLogger(c) 102 defer worker.Stop(loggingWorker) 103 104 s.waitLoggingInfo(c, expected) 105 }