github.com/juju/juju@v0.0.0-20240327075706-a90865de2538/api/agent/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  	"github.com/juju/names/v5"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/api"
    12  	"github.com/juju/juju/api/agent/logger"
    13  	"github.com/juju/juju/core/watcher/watchertest"
    14  	jujutesting "github.com/juju/juju/juju/testing"
    15  	"github.com/juju/juju/state"
    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  
    25  	logger *logger.State
    26  }
    27  
    28  var _ = gc.Suite(&loggerSuite{})
    29  
    30  func (s *loggerSuite) SetUpTest(c *gc.C) {
    31  	s.JujuConnSuite.SetUpTest(c)
    32  	var stateAPI api.Connection
    33  	stateAPI, s.rawMachine = s.OpenAPIAsNewMachine(c)
    34  	// Create the logger facade.
    35  	s.logger = logger.NewState(stateAPI)
    36  	c.Assert(s.logger, gc.NotNil)
    37  }
    38  
    39  func (s *loggerSuite) TestLoggingConfigWrongMachine(c *gc.C) {
    40  	config, err := s.logger.LoggingConfig(names.NewMachineTag("42"))
    41  	c.Assert(err, gc.ErrorMatches, "permission denied")
    42  	c.Assert(config, gc.Equals, "")
    43  }
    44  
    45  func (s *loggerSuite) TestLoggingConfig(c *gc.C) {
    46  	config, err := s.logger.LoggingConfig(s.rawMachine.Tag())
    47  	c.Assert(err, jc.ErrorIsNil)
    48  	c.Assert(config, gc.Not(gc.Equals), "")
    49  }
    50  
    51  func (s *loggerSuite) setLoggingConfig(c *gc.C, loggingConfig string) {
    52  	err := s.Model.UpdateModelConfig(map[string]interface{}{"logging-config": loggingConfig}, nil)
    53  	c.Assert(err, jc.ErrorIsNil)
    54  }
    55  
    56  func (s *loggerSuite) TestWatchLoggingConfig(c *gc.C) {
    57  	watcher, err := s.logger.WatchLoggingConfig(s.rawMachine.Tag())
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	wc := watchertest.NewNotifyWatcherC(c, watcher)
    60  	defer wc.AssertStops()
    61  
    62  	// Initial event
    63  	wc.AssertOneChange()
    64  
    65  	loggingConfig := "<root>=WARN;juju.log.test=DEBUG"
    66  	s.setLoggingConfig(c, loggingConfig)
    67  	// One change noticing the new version
    68  	wc.AssertOneChange()
    69  	// Setting the version to the same value doesn't trigger a change
    70  	s.setLoggingConfig(c, loggingConfig)
    71  	wc.AssertNoChange()
    72  
    73  	loggingConfig = loggingConfig + ";wibble=DEBUG"
    74  	s.setLoggingConfig(c, loggingConfig)
    75  	wc.AssertOneChange()
    76  }