~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/migrationmaster/migrationmaster_test.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        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/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"
 
19
)
 
20
 
 
21
// Ensure that Backend remains compatible with *state.State
 
22
var _ migrationmaster.Backend = (*state.State)(nil)
 
23
 
 
24
type Suite struct {
 
25
        testing.BaseSuite
 
26
 
 
27
        backend    *stubBackend
 
28
        resources  *common.Resources
 
29
        authorizer apiservertesting.FakeAuthorizer
 
30
}
 
31
 
 
32
var _ = gc.Suite(&Suite{})
 
33
 
 
34
func (s *Suite) SetUpTest(c *gc.C) {
 
35
        s.BaseSuite.SetUpTest(c)
 
36
 
 
37
        s.backend = &stubBackend{
 
38
                migration: new(stubMigration),
 
39
        }
 
40
        migrationmaster.PatchState(s, s.backend)
 
41
 
 
42
        s.resources = common.NewResources()
 
43
        s.AddCleanup(func(*gc.C) { s.resources.StopAll() })
 
44
 
 
45
        s.authorizer = apiservertesting.FakeAuthorizer{
 
46
                EnvironManager: true,
 
47
        }
 
48
}
 
49
 
 
50
func (s *Suite) TestNotEnvironManager(c *gc.C) {
 
51
        s.authorizer.EnvironManager = false
 
52
 
 
53
        api, err := s.makeAPI()
 
54
        c.Assert(api, gc.IsNil)
 
55
        c.Assert(err, gc.Equals, common.ErrPerm)
 
56
}
 
57
 
 
58
func (s *Suite) TestWatch(c *gc.C) {
 
59
        api := s.mustMakeAPI(c)
 
60
 
 
61
        watchResult, err := api.Watch()
 
62
        c.Assert(err, jc.ErrorIsNil)
 
63
        c.Assert(watchResult.NotifyWatcherId, gc.Not(gc.Equals), "")
 
64
}
 
65
 
 
66
func (s *Suite) TestWatchError(c *gc.C) {
 
67
        s.backend.watchError = errors.New("boom")
 
68
        api := s.mustMakeAPI(c)
 
69
 
 
70
        w, err := api.Watch()
 
71
        c.Assert(w, gc.Equals, params.NotifyWatchResult{})
 
72
        c.Assert(err, gc.ErrorMatches, "boom")
 
73
}
 
74
 
 
75
func (s *Suite) TestSetPhase(c *gc.C) {
 
76
        api := s.mustMakeAPI(c)
 
77
 
 
78
        err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"})
 
79
        c.Assert(err, jc.ErrorIsNil)
 
80
 
 
81
        c.Assert(s.backend.migration.phaseSet, gc.Equals, coremigration.ABORT)
 
82
}
 
83
 
 
84
func (s *Suite) TestSetPhaseNoMigration(c *gc.C) {
 
85
        s.backend.getErr = errors.New("boom")
 
86
        api := s.mustMakeAPI(c)
 
87
 
 
88
        err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"})
 
89
        c.Assert(err, gc.ErrorMatches, "could not get migration: boom")
 
90
}
 
91
 
 
92
func (s *Suite) TestSetPhaseBadPhase(c *gc.C) {
 
93
        api := s.mustMakeAPI(c)
 
94
 
 
95
        err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "wat"})
 
96
        c.Assert(err, gc.ErrorMatches, `invalid phase: "wat"`)
 
97
}
 
98
 
 
99
func (s *Suite) TestSetPhaseError(c *gc.C) {
 
100
        s.backend.migration.setPhaseErr = errors.New("blam")
 
101
        api := s.mustMakeAPI(c)
 
102
 
 
103
        err := api.SetPhase(params.SetMigrationPhaseArgs{Phase: "ABORT"})
 
104
        c.Assert(err, gc.ErrorMatches, "failed to set phase: blam")
 
105
}
 
106
 
 
107
func (s *Suite) TestExport(c *gc.C) {
 
108
        exportModel := func(migration.StateExporter) ([]byte, error) {
 
109
                return []byte("foo"), nil
 
110
        }
 
111
        migrationmaster.PatchExportModel(s, exportModel)
 
112
        api := s.mustMakeAPI(c)
 
113
 
 
114
        serialized, err := api.Export()
 
115
 
 
116
        c.Assert(err, jc.ErrorIsNil)
 
117
        c.Assert(serialized, gc.DeepEquals, params.SerializedModel{
 
118
                Bytes: []byte("foo"),
 
119
        })
 
120
}
 
121
 
 
122
func (s *Suite) makeAPI() (*migrationmaster.API, error) {
 
123
        return migrationmaster.NewAPI(nil, s.resources, s.authorizer)
 
124
}
 
125
 
 
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)
 
129
        return api
 
130
}
 
131
 
 
132
type stubBackend struct {
 
133
        migrationmaster.Backend
 
134
 
 
135
        watchError error
 
136
        getErr     error
 
137
        migration  *stubMigration
 
138
}
 
139
 
 
140
func (b *stubBackend) WatchForModelMigration() (state.NotifyWatcher, error) {
 
141
        if b.watchError != nil {
 
142
                return nil, b.watchError
 
143
        }
 
144
        return apiservertesting.NewFakeNotifyWatcher(), nil
 
145
}
 
146
 
 
147
func (b *stubBackend) GetModelMigration() (state.ModelMigration, error) {
 
148
        if b.getErr != nil {
 
149
                return nil, b.getErr
 
150
        }
 
151
        return b.migration, nil
 
152
}
 
153
 
 
154
type stubMigration struct {
 
155
        state.ModelMigration
 
156
        setPhaseErr error
 
157
        phaseSet    coremigration.Phase
 
158
}
 
159
 
 
160
func (m *stubMigration) SetPhase(phase coremigration.Phase) error {
 
161
        if m.setPhaseErr != nil {
 
162
                return m.setPhaseErr
 
163
        }
 
164
        m.phaseSet = phase
 
165
        return nil
 
166
}