github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/migrationmaster/migrationmaster_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package migrationmaster_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 jc "github.com/juju/testing/checkers" 10 "github.com/juju/utils" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/apiserver/common" 14 "github.com/juju/juju/apiserver/migrationmaster" 15 "github.com/juju/juju/apiserver/params" 16 apiservertesting "github.com/juju/juju/apiserver/testing" 17 coremigration "github.com/juju/juju/core/migration" 18 "github.com/juju/juju/migration" 19 "github.com/juju/juju/state" 20 "github.com/juju/juju/testing" 21 ) 22 23 // Ensure that Backend remains compatible with *state.State 24 var _ migrationmaster.Backend = (*state.State)(nil) 25 26 type Suite struct { 27 testing.BaseSuite 28 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.backend = &stubBackend{ 40 migration: new(stubMigration), 41 } 42 migrationmaster.PatchState(s, s.backend) 43 44 s.resources = common.NewResources() 45 s.AddCleanup(func(*gc.C) { s.resources.StopAll() }) 46 47 s.authorizer = apiservertesting.FakeAuthorizer{ 48 EnvironManager: true, 49 } 50 } 51 52 func (s *Suite) TestNotEnvironManager(c *gc.C) { 53 s.authorizer.EnvironManager = false 54 55 api, err := s.makeAPI() 56 c.Assert(api, gc.IsNil) 57 c.Assert(err, gc.Equals, common.ErrPerm) 58 } 59 60 func (s *Suite) TestWatch(c *gc.C) { 61 api := s.mustMakeAPI(c) 62 63 watchResult, err := api.Watch() 64 c.Assert(err, jc.ErrorIsNil) 65 c.Assert(watchResult.NotifyWatcherId, gc.Not(gc.Equals), "") 66 } 67 68 func (s *Suite) TestWatchError(c *gc.C) { 69 s.backend.watchError = errors.New("boom") 70 api := s.mustMakeAPI(c) 71 72 w, err := api.Watch() 73 c.Assert(w, gc.Equals, params.NotifyWatchResult{}) 74 c.Assert(err, gc.ErrorMatches, "boom") 75 } 76 77 func (s *Suite) TestGetMigrationStatus(c *gc.C) { 78 api := s.mustMakeAPI(c) 79 80 status, err := api.GetMigrationStatus() 81 c.Assert(err, jc.ErrorIsNil) 82 c.Assert(status, gc.DeepEquals, params.FullMigrationStatus{ 83 Spec: params.ModelMigrationSpec{ 84 ModelTag: names.NewModelTag(modelUUID).String(), 85 TargetInfo: params.ModelMigrationTargetInfo{ 86 ControllerTag: names.NewModelTag(controllerUUID).String(), 87 Addrs: []string{"1.1.1.1:1", "2.2.2.2:2"}, 88 CACert: "trust me", 89 AuthTag: names.NewUserTag("admin").String(), 90 Password: "secret", 91 }, 92 }, 93 Attempt: 1, 94 Phase: "READONLY", 95 }) 96 } 97 98 func (s *Suite) TestSetPhase(c *gc.C) { 99 api := s.mustMakeAPI(c) 100 101 err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"}) 102 c.Assert(err, jc.ErrorIsNil) 103 104 c.Assert(s.backend.migration.phaseSet, gc.Equals, coremigration.ABORT) 105 } 106 107 func (s *Suite) TestSetPhaseNoMigration(c *gc.C) { 108 s.backend.getErr = errors.New("boom") 109 api := s.mustMakeAPI(c) 110 111 err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"}) 112 c.Assert(err, gc.ErrorMatches, "could not get migration: boom") 113 } 114 115 func (s *Suite) TestSetPhaseBadPhase(c *gc.C) { 116 api := s.mustMakeAPI(c) 117 118 err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "wat"}) 119 c.Assert(err, gc.ErrorMatches, `invalid phase: "wat"`) 120 } 121 122 func (s *Suite) TestSetPhaseError(c *gc.C) { 123 s.backend.migration.setPhaseErr = errors.New("blam") 124 api := s.mustMakeAPI(c) 125 126 err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"}) 127 c.Assert(err, gc.ErrorMatches, "failed to set phase: blam") 128 } 129 130 func (s *Suite) TestExport(c *gc.C) { 131 exportModel := func(migration.StateExporter) ([]byte, error) { 132 return []byte("foo"), nil 133 } 134 migrationmaster.PatchExportModel(s, exportModel) 135 api := s.mustMakeAPI(c) 136 137 serialized, err := api.Export() 138 139 c.Assert(err, jc.ErrorIsNil) 140 c.Assert(serialized, gc.DeepEquals, params.SerializedModel{ 141 Bytes: []byte("foo"), 142 }) 143 } 144 145 func (s *Suite) makeAPI() (*migrationmaster.API, error) { 146 return migrationmaster.NewAPI(nil, s.resources, s.authorizer) 147 } 148 149 func (s *Suite) mustMakeAPI(c *gc.C) *migrationmaster.API { 150 api, err := migrationmaster.NewAPI(nil, s.resources, s.authorizer) 151 c.Assert(err, jc.ErrorIsNil) 152 return api 153 } 154 155 type stubBackend struct { 156 migrationmaster.Backend 157 158 watchError error 159 getErr error 160 migration *stubMigration 161 } 162 163 func (b *stubBackend) WatchForModelMigration() (state.NotifyWatcher, error) { 164 if b.watchError != nil { 165 return nil, b.watchError 166 } 167 return apiservertesting.NewFakeNotifyWatcher(), nil 168 } 169 170 func (b *stubBackend) GetModelMigration() (state.ModelMigration, error) { 171 if b.getErr != nil { 172 return nil, b.getErr 173 } 174 return b.migration, nil 175 } 176 177 type stubMigration struct { 178 state.ModelMigration 179 setPhaseErr error 180 phaseSet coremigration.Phase 181 } 182 183 func (m *stubMigration) Phase() (coremigration.Phase, error) { 184 return coremigration.READONLY, nil 185 } 186 187 func (m *stubMigration) Attempt() (int, error) { 188 return 1, nil 189 } 190 191 func (m *stubMigration) ModelUUID() string { 192 return modelUUID 193 } 194 195 func (m *stubMigration) TargetInfo() (*coremigration.TargetInfo, error) { 196 return &coremigration.TargetInfo{ 197 ControllerTag: names.NewModelTag(controllerUUID), 198 Addrs: []string{"1.1.1.1:1", "2.2.2.2:2"}, 199 CACert: "trust me", 200 AuthTag: names.NewUserTag("admin"), 201 Password: "secret", 202 }, nil 203 } 204 205 func (m *stubMigration) SetPhase(phase coremigration.Phase) error { 206 if m.setPhaseErr != nil { 207 return m.setPhaseErr 208 } 209 m.phaseSet = phase 210 return nil 211 } 212 213 var modelUUID string 214 var controllerUUID string 215 216 func init() { 217 modelUUID = utils.MustNewUUID().String() 218 controllerUUID = utils.MustNewUUID().String() 219 }