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