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