github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/api/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  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/juju/names.v2"
    10  
    11  	"github.com/juju/juju/api"
    12  	"github.com/juju/juju/api/logger"
    13  	jujutesting "github.com/juju/juju/juju/testing"
    14  	"github.com/juju/juju/state"
    15  	"github.com/juju/juju/watcher/watchertest"
    16  )
    17  
    18  type loggerSuite struct {
    19  	jujutesting.JujuConnSuite
    20  
    21  	// These are raw State objects. Use them for setup and assertions, but
    22  	// should never be touched by the API calls themselves
    23  	rawMachine     *state.Machine
    24  	rawCharm       *state.Charm
    25  	rawApplication *state.Application
    26  	rawUnit        *state.Unit
    27  
    28  	logger *logger.State
    29  }
    30  
    31  var _ = gc.Suite(&loggerSuite{})
    32  
    33  func (s *loggerSuite) SetUpTest(c *gc.C) {
    34  	s.JujuConnSuite.SetUpTest(c)
    35  	var stateAPI api.Connection
    36  	stateAPI, s.rawMachine = s.OpenAPIAsNewMachine(c)
    37  	// Create the logger facade.
    38  	s.logger = logger.NewState(stateAPI)
    39  	c.Assert(s.logger, gc.NotNil)
    40  }
    41  
    42  func (s *loggerSuite) TestLoggingConfigWrongMachine(c *gc.C) {
    43  	config, err := s.logger.LoggingConfig(names.NewMachineTag("42"))
    44  	c.Assert(err, gc.ErrorMatches, "permission denied")
    45  	c.Assert(config, gc.Equals, "")
    46  }
    47  
    48  func (s *loggerSuite) TestLoggingConfig(c *gc.C) {
    49  	config, err := s.logger.LoggingConfig(s.rawMachine.Tag())
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	c.Assert(config, gc.Not(gc.Equals), "")
    52  }
    53  
    54  func (s *loggerSuite) setLoggingConfig(c *gc.C, loggingConfig string) {
    55  	err := s.BackingState.UpdateModelConfig(map[string]interface{}{"logging-config": loggingConfig}, nil, nil)
    56  	c.Assert(err, jc.ErrorIsNil)
    57  }
    58  
    59  func (s *loggerSuite) TestWatchLoggingConfig(c *gc.C) {
    60  	watcher, err := s.logger.WatchLoggingConfig(s.rawMachine.Tag())
    61  	c.Assert(err, jc.ErrorIsNil)
    62  	wc := watchertest.NewNotifyWatcherC(c, watcher, s.BackingState.StartSync)
    63  	defer wc.AssertStops()
    64  
    65  	// Initial event
    66  	wc.AssertOneChange()
    67  
    68  	loggingConfig := "<root>=WARN;juju.log.test=DEBUG"
    69  	s.setLoggingConfig(c, loggingConfig)
    70  	// One change noticing the new version
    71  	wc.AssertOneChange()
    72  	// Setting the version to the same value doesn't trigger a change
    73  	s.setLoggingConfig(c, loggingConfig)
    74  	wc.AssertNoChange()
    75  
    76  	loggingConfig = loggingConfig + ";wibble=DEBUG"
    77  	s.setLoggingConfig(c, loggingConfig)
    78  	wc.AssertOneChange()
    79  }