github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/migrationminion/migrationminion_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package migrationminion_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/apiserver/common" 13 "github.com/juju/juju/apiserver/migrationminion" 14 apiservertesting "github.com/juju/juju/apiserver/testing" 15 "github.com/juju/juju/state" 16 "github.com/juju/juju/testing" 17 ) 18 19 // Ensure that Backend remains compatible with *state.State 20 var _ migrationminion.Backend = (*state.State)(nil) 21 22 type Suite struct { 23 testing.BaseSuite 24 25 backend *stubBackend 26 resources *common.Resources 27 authorizer apiservertesting.FakeAuthorizer 28 } 29 30 var _ = gc.Suite(&Suite{}) 31 32 func (s *Suite) SetUpTest(c *gc.C) { 33 s.BaseSuite.SetUpTest(c) 34 35 s.backend = &stubBackend{} 36 migrationminion.PatchState(s, s.backend) 37 38 s.resources = common.NewResources() 39 s.AddCleanup(func(*gc.C) { s.resources.StopAll() }) 40 41 s.authorizer = apiservertesting.FakeAuthorizer{ 42 Tag: names.NewMachineTag("99"), 43 } 44 } 45 46 func (s *Suite) TestAuthMachineAgent(c *gc.C) { 47 s.authorizer.Tag = names.NewMachineTag("42") 48 s.mustMakeAPI(c) 49 } 50 51 func (s *Suite) TestAuthUnitAgent(c *gc.C) { 52 s.authorizer.Tag = names.NewUnitTag("foo/0") 53 s.mustMakeAPI(c) 54 } 55 56 func (s *Suite) TestAuthNotAgent(c *gc.C) { 57 s.authorizer.Tag = names.NewUserTag("dorothy") 58 _, err := s.makeAPI() 59 c.Assert(err, gc.Equals, common.ErrPerm) 60 } 61 62 func (s *Suite) TestWatchError(c *gc.C) { 63 s.backend.watchError = errors.New("boom") 64 api := s.mustMakeAPI(c) 65 _, err := api.Watch() 66 c.Assert(err, gc.ErrorMatches, "boom") 67 c.Assert(s.resources.Count(), gc.Equals, 0) 68 } 69 70 func (s *Suite) TestWatch(c *gc.C) { 71 api := s.mustMakeAPI(c) 72 result, err := api.Watch() 73 c.Assert(err, jc.ErrorIsNil) 74 c.Assert(s.resources.Get(result.NotifyWatcherId), gc.NotNil) 75 } 76 77 func (s *Suite) makeAPI() (*migrationminion.API, error) { 78 return migrationminion.NewAPI(nil, s.resources, s.authorizer) 79 } 80 81 func (s *Suite) mustMakeAPI(c *gc.C) *migrationminion.API { 82 api, err := migrationminion.NewAPI(nil, s.resources, s.authorizer) 83 c.Assert(err, jc.ErrorIsNil) 84 return api 85 } 86 87 type stubBackend struct { 88 migrationminion.Backend 89 watchError error 90 } 91 92 func (b *stubBackend) WatchMigrationStatus() (state.NotifyWatcher, error) { 93 if b.watchError != nil { 94 return nil, b.watchError 95 } 96 return apiservertesting.NewFakeNotifyWatcher(), nil 97 }