1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package migrationmaster_test
7
"github.com/juju/errors"
8
jc "github.com/juju/testing/checkers"
11
"github.com/juju/juju/apiserver/common"
12
"github.com/juju/juju/apiserver/migrationmaster"
13
"github.com/juju/juju/apiserver/params"
14
apiservertesting "github.com/juju/juju/apiserver/testing"
15
coremigration "github.com/juju/juju/core/migration"
16
"github.com/juju/juju/migration"
17
"github.com/juju/juju/state"
18
"github.com/juju/juju/testing"
21
// Ensure that Backend remains compatible with *state.State
22
var _ migrationmaster.Backend = (*state.State)(nil)
28
resources *common.Resources
29
authorizer apiservertesting.FakeAuthorizer
32
var _ = gc.Suite(&Suite{})
34
func (s *Suite) SetUpTest(c *gc.C) {
35
s.BaseSuite.SetUpTest(c)
37
s.backend = &stubBackend{
38
migration: new(stubMigration),
40
migrationmaster.PatchState(s, s.backend)
42
s.resources = common.NewResources()
43
s.AddCleanup(func(*gc.C) { s.resources.StopAll() })
45
s.authorizer = apiservertesting.FakeAuthorizer{
50
func (s *Suite) TestNotEnvironManager(c *gc.C) {
51
s.authorizer.EnvironManager = false
53
api, err := s.makeAPI()
54
c.Assert(api, gc.IsNil)
55
c.Assert(err, gc.Equals, common.ErrPerm)
58
func (s *Suite) TestWatch(c *gc.C) {
59
api := s.mustMakeAPI(c)
61
watchResult, err := api.Watch()
62
c.Assert(err, jc.ErrorIsNil)
63
c.Assert(watchResult.NotifyWatcherId, gc.Not(gc.Equals), "")
66
func (s *Suite) TestWatchError(c *gc.C) {
67
s.backend.watchError = errors.New("boom")
68
api := s.mustMakeAPI(c)
71
c.Assert(w, gc.Equals, params.NotifyWatchResult{})
72
c.Assert(err, gc.ErrorMatches, "boom")
75
func (s *Suite) TestSetPhase(c *gc.C) {
76
api := s.mustMakeAPI(c)
78
err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"})
79
c.Assert(err, jc.ErrorIsNil)
81
c.Assert(s.backend.migration.phaseSet, gc.Equals, coremigration.ABORT)
84
func (s *Suite) TestSetPhaseNoMigration(c *gc.C) {
85
s.backend.getErr = errors.New("boom")
86
api := s.mustMakeAPI(c)
88
err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"})
89
c.Assert(err, gc.ErrorMatches, "could not get migration: boom")
92
func (s *Suite) TestSetPhaseBadPhase(c *gc.C) {
93
api := s.mustMakeAPI(c)
95
err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "wat"})
96
c.Assert(err, gc.ErrorMatches, `invalid phase: "wat"`)
99
func (s *Suite) TestSetPhaseError(c *gc.C) {
100
s.backend.migration.setPhaseErr = errors.New("blam")
101
api := s.mustMakeAPI(c)
103
err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"})
104
c.Assert(err, gc.ErrorMatches, "failed to set phase: blam")
107
func (s *Suite) TestExport(c *gc.C) {
108
exportModel := func(migration.StateExporter) ([]byte, error) {
109
return []byte("foo"), nil
111
migrationmaster.PatchExportModel(s, exportModel)
112
api := s.mustMakeAPI(c)
114
serialized, err := api.Export()
116
c.Assert(err, jc.ErrorIsNil)
117
c.Assert(serialized, gc.DeepEquals, params.SerializedModel{
118
Bytes: []byte("foo"),
122
func (s *Suite) makeAPI() (*migrationmaster.API, error) {
123
return migrationmaster.NewAPI(nil, s.resources, s.authorizer)
126
func (s *Suite) mustMakeAPI(c *gc.C) *migrationmaster.API {
127
api, err := migrationmaster.NewAPI(nil, s.resources, s.authorizer)
128
c.Assert(err, jc.ErrorIsNil)
132
type stubBackend struct {
133
migrationmaster.Backend
137
migration *stubMigration
140
func (b *stubBackend) WatchForModelMigration() (state.NotifyWatcher, error) {
141
if b.watchError != nil {
142
return nil, b.watchError
144
return apiservertesting.NewFakeNotifyWatcher(), nil
147
func (b *stubBackend) GetModelMigration() (state.ModelMigration, error) {
151
return b.migration, nil
154
type stubMigration struct {
157
phaseSet coremigration.Phase
160
func (m *stubMigration) SetPhase(phase coremigration.Phase) error {
161
if m.setPhaseErr != nil {