github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/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" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/apiserver/common" 12 "github.com/juju/juju/apiserver/logger" 13 "github.com/juju/juju/apiserver/params" 14 apiservertesting "github.com/juju/juju/apiserver/testing" 15 jujutesting "github.com/juju/juju/juju/testing" 16 "github.com/juju/juju/state" 17 statetesting "github.com/juju/juju/state/testing" 18 ) 19 20 type loggerSuite struct { 21 jujutesting.JujuConnSuite 22 23 // These are raw State objects. Use them for setup and assertions, but 24 // should never be touched by the API calls themselves 25 rawMachine *state.Machine 26 logger *logger.LoggerAPI 27 resources *common.Resources 28 authorizer apiservertesting.FakeAuthorizer 29 } 30 31 var _ = gc.Suite(&loggerSuite{}) 32 33 func (s *loggerSuite) SetUpTest(c *gc.C) { 34 s.JujuConnSuite.SetUpTest(c) 35 s.resources = common.NewResources() 36 s.AddCleanup(func(_ *gc.C) { s.resources.StopAll() }) 37 38 // Create a machine to work with 39 var err error 40 s.rawMachine, err = s.State.AddMachine("quantal", state.JobHostUnits) 41 c.Assert(err, jc.ErrorIsNil) 42 43 // The default auth is as the machine agent 44 s.authorizer = apiservertesting.FakeAuthorizer{ 45 Tag: s.rawMachine.Tag(), 46 } 47 s.logger, err = logger.NewLoggerAPI(s.State, s.resources, s.authorizer) 48 c.Assert(err, jc.ErrorIsNil) 49 } 50 51 func (s *loggerSuite) TestNewLoggerAPIRefusesNonAgent(c *gc.C) { 52 // We aren't even a machine agent 53 anAuthorizer := s.authorizer 54 anAuthorizer.Tag = s.AdminUserTag(c) 55 endPoint, err := logger.NewLoggerAPI(s.State, s.resources, anAuthorizer) 56 c.Assert(endPoint, gc.IsNil) 57 c.Assert(err, gc.ErrorMatches, "permission denied") 58 } 59 60 func (s *loggerSuite) TestNewLoggerAPIAcceptsUnitAgent(c *gc.C) { 61 // We aren't even a machine agent 62 anAuthorizer := s.authorizer 63 anAuthorizer.Tag = names.NewUnitTag("germany/7") 64 endPoint, err := logger.NewLoggerAPI(s.State, s.resources, anAuthorizer) 65 c.Assert(err, jc.ErrorIsNil) 66 c.Assert(endPoint, gc.NotNil) 67 } 68 69 func (s *loggerSuite) TestWatchLoggingConfigNothing(c *gc.C) { 70 // Not an error to watch nothing 71 results := s.logger.WatchLoggingConfig(params.Entities{}) 72 c.Assert(results.Results, gc.HasLen, 0) 73 } 74 75 func (s *loggerSuite) setLoggingConfig(c *gc.C, loggingConfig string) { 76 err := s.State.UpdateEnvironConfig(map[string]interface{}{"logging-config": loggingConfig}, nil, nil) 77 c.Assert(err, jc.ErrorIsNil) 78 envConfig, err := s.State.EnvironConfig() 79 c.Assert(err, jc.ErrorIsNil) 80 c.Assert(envConfig.LoggingConfig(), gc.Equals, loggingConfig) 81 } 82 83 func (s *loggerSuite) TestWatchLoggingConfig(c *gc.C) { 84 args := params.Entities{ 85 Entities: []params.Entity{{Tag: s.rawMachine.Tag().String()}}, 86 } 87 results := s.logger.WatchLoggingConfig(args) 88 c.Assert(results.Results, gc.HasLen, 1) 89 c.Assert(results.Results[0].NotifyWatcherId, gc.Not(gc.Equals), "") 90 c.Assert(results.Results[0].Error, gc.IsNil) 91 resource := s.resources.Get(results.Results[0].NotifyWatcherId) 92 c.Assert(resource, gc.NotNil) 93 94 w := resource.(state.NotifyWatcher) 95 wc := statetesting.NewNotifyWatcherC(c, s.State, w) 96 wc.AssertNoChange() 97 98 newLoggingConfig := "<root>=WARN;juju.log.test=DEBUG;unit=INFO" 99 s.setLoggingConfig(c, newLoggingConfig) 100 101 wc.AssertOneChange() 102 statetesting.AssertStop(c, w) 103 wc.AssertClosed() 104 } 105 106 func (s *loggerSuite) TestWatchLoggingConfigRefusesWrongAgent(c *gc.C) { 107 // We are a machine agent, but not the one we are trying to track 108 args := params.Entities{ 109 Entities: []params.Entity{{Tag: "machine-12354"}}, 110 } 111 results := s.logger.WatchLoggingConfig(args) 112 // It is not an error to make the request, but the specific item is rejected 113 c.Assert(results.Results, gc.HasLen, 1) 114 c.Assert(results.Results[0].NotifyWatcherId, gc.Equals, "") 115 c.Assert(results.Results[0].Error, gc.DeepEquals, apiservertesting.ErrUnauthorized) 116 } 117 118 func (s *loggerSuite) TestLoggingConfigForNoone(c *gc.C) { 119 // Not an error to request nothing, dumb, but not an error. 120 results := s.logger.LoggingConfig(params.Entities{}) 121 c.Assert(results.Results, gc.HasLen, 0) 122 } 123 124 func (s *loggerSuite) TestLoggingConfigRefusesWrongAgent(c *gc.C) { 125 args := params.Entities{ 126 Entities: []params.Entity{{Tag: "machine-12354"}}, 127 } 128 results := s.logger.LoggingConfig(args) 129 c.Assert(results.Results, gc.HasLen, 1) 130 result := results.Results[0] 131 c.Assert(result.Error, gc.DeepEquals, apiservertesting.ErrUnauthorized) 132 } 133 134 func (s *loggerSuite) TestLoggingConfigForAgent(c *gc.C) { 135 newLoggingConfig := "<root>=WARN;juju.log.test=DEBUG;unit=INFO" 136 s.setLoggingConfig(c, newLoggingConfig) 137 138 args := params.Entities{ 139 Entities: []params.Entity{{Tag: s.rawMachine.Tag().String()}}, 140 } 141 results := s.logger.LoggingConfig(args) 142 c.Assert(results.Results, gc.HasLen, 1) 143 result := results.Results[0] 144 c.Assert(result.Error, gc.IsNil) 145 c.Assert(result.Result, gc.Equals, newLoggingConfig) 146 }