github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/agent/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/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 "gopkg.in/juju/names.v2" 12 13 "github.com/juju/juju/apiserver/common" 14 "github.com/juju/juju/apiserver/facades/agent/migrationminion" 15 "github.com/juju/juju/apiserver/params" 16 apiservertesting "github.com/juju/juju/apiserver/testing" 17 "github.com/juju/juju/core/migration" 18 "github.com/juju/juju/state" 19 coretesting "github.com/juju/juju/testing" 20 ) 21 22 // Ensure that Backend remains compatible with *state.State 23 var _ migrationminion.Backend = (*state.State)(nil) 24 25 type Suite struct { 26 coretesting.BaseSuite 27 28 stub *testing.Stub 29 backend *stubBackend 30 resources *common.Resources 31 authorizer apiservertesting.FakeAuthorizer 32 } 33 34 var _ = gc.Suite(&Suite{}) 35 36 func (s *Suite) SetUpTest(c *gc.C) { 37 s.BaseSuite.SetUpTest(c) 38 39 s.stub = &testing.Stub{} 40 s.backend = &stubBackend{stub: s.stub} 41 42 s.resources = common.NewResources() 43 s.AddCleanup(func(*gc.C) { s.resources.StopAll() }) 44 45 s.authorizer = apiservertesting.FakeAuthorizer{ 46 Tag: names.NewMachineTag("99"), 47 } 48 } 49 50 func (s *Suite) TestAuthMachineAgent(c *gc.C) { 51 s.authorizer.Tag = names.NewMachineTag("42") 52 s.mustMakeAPI(c) 53 } 54 55 func (s *Suite) TestAuthUnitAgent(c *gc.C) { 56 s.authorizer.Tag = names.NewUnitTag("foo/0") 57 s.mustMakeAPI(c) 58 } 59 60 func (s *Suite) TestAuthApplicationAgent(c *gc.C) { 61 s.authorizer.Tag = names.NewApplicationTag("foo") 62 s.mustMakeAPI(c) 63 } 64 65 func (s *Suite) TestAuthNotAgent(c *gc.C) { 66 s.authorizer.Tag = names.NewUserTag("dorothy") 67 _, err := s.makeAPI() 68 c.Assert(err, gc.Equals, common.ErrPerm) 69 } 70 71 func (s *Suite) TestWatch(c *gc.C) { 72 api := s.mustMakeAPI(c) 73 result, err := api.Watch() 74 c.Assert(err, jc.ErrorIsNil) 75 c.Assert(s.resources.Get(result.NotifyWatcherId), gc.NotNil) 76 } 77 78 func (s *Suite) TestReport(c *gc.C) { 79 api := s.mustMakeAPI(c) 80 err := api.Report(params.MinionReport{ 81 MigrationId: "id", 82 Phase: "IMPORT", 83 Success: true, 84 }) 85 c.Assert(err, jc.ErrorIsNil) 86 s.stub.CheckCalls(c, []testing.StubCall{ 87 {"Migration", []interface{}{"id"}}, 88 {"Report", []interface{}{s.authorizer.Tag, migration.IMPORT, true}}, 89 }) 90 } 91 92 func (s *Suite) TestReportInvalidPhase(c *gc.C) { 93 api := s.mustMakeAPI(c) 94 err := api.Report(params.MinionReport{ 95 MigrationId: "id", 96 Phase: "WTF", 97 Success: true, 98 }) 99 c.Assert(err, gc.ErrorMatches, "unable to parse phase") 100 } 101 102 func (s *Suite) TestReportNoSuchMigration(c *gc.C) { 103 failure := errors.NotFoundf("model") 104 s.backend.modelLookupErr = failure 105 api := s.mustMakeAPI(c) 106 err := api.Report(params.MinionReport{ 107 MigrationId: "id", 108 Phase: "QUIESCE", 109 Success: false, 110 }) 111 c.Assert(errors.Cause(err), gc.Equals, failure) 112 } 113 114 func (s *Suite) makeAPI() (*migrationminion.API, error) { 115 return migrationminion.NewAPI(s.backend, s.resources, s.authorizer) 116 } 117 118 func (s *Suite) mustMakeAPI(c *gc.C) *migrationminion.API { 119 api, err := s.makeAPI() 120 c.Assert(err, jc.ErrorIsNil) 121 return api 122 } 123 124 type stubBackend struct { 125 migrationminion.Backend 126 stub *testing.Stub 127 modelLookupErr error 128 } 129 130 func (b *stubBackend) WatchMigrationStatus() state.NotifyWatcher { 131 b.stub.AddCall("WatchMigrationStatus") 132 return apiservertesting.NewFakeNotifyWatcher() 133 } 134 135 func (b *stubBackend) Migration(id string) (state.ModelMigration, error) { 136 b.stub.AddCall("Migration", id) 137 if b.modelLookupErr != nil { 138 return nil, b.modelLookupErr 139 } 140 return &stubModelMigration{stub: b.stub}, nil 141 } 142 143 type stubModelMigration struct { 144 state.ModelMigration 145 stub *testing.Stub 146 } 147 148 func (m *stubModelMigration) SubmitMinionReport(tag names.Tag, phase migration.Phase, success bool) error { 149 m.stub.AddCall("Report", tag, phase, success) 150 return nil 151 }