1
// Copyright 2015-2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
"github.com/juju/testing"
8
jc "github.com/juju/testing/checkers"
9
"github.com/juju/utils"
10
gc "gopkg.in/check.v1"
11
"gopkg.in/juju/names.v2"
13
"github.com/juju/juju/agent"
14
"github.com/juju/juju/api"
15
apiagent "github.com/juju/juju/api/agent"
16
"github.com/juju/juju/api/base"
17
"github.com/juju/juju/apiserver/params"
18
coretesting "github.com/juju/juju/testing"
19
"github.com/juju/juju/worker"
20
"github.com/juju/juju/worker/apicaller"
23
var errNotProvisioned = ¶ms.Error{Code: params.CodeNotProvisioned}
24
var errNotAuthorized = ¶ms.Error{Code: params.CodeUnauthorized}
26
type mockAgent struct {
33
func (mock *mockAgent) CurrentConfig() agent.Config {
40
func (mock *mockAgent) ChangeConfig(mutator agent.ConfigMutator) error {
41
mock.stub.AddCall("ChangeConfig")
42
if err := mock.stub.NextErr(); err != nil {
45
return mutator(&mockSetter{stub: mock.stub})
48
type dummyConfig struct {
54
func (dummy dummyConfig) Tag() names.Tag {
58
func (dummy dummyConfig) Model() names.ModelTag {
62
func (dummy dummyConfig) APIInfo() (*api.Info, bool) {
64
ModelTag: dummy.model,
70
func (dummy dummyConfig) OldPassword() string {
74
type mockSetter struct {
79
func (mock *mockSetter) Migrate(params agent.MigrateParams) error {
80
mock.stub.AddCall("Migrate", params)
81
return mock.stub.NextErr()
84
func (mock *mockSetter) SetOldPassword(pw string) {
85
mock.stub.AddCall("SetOldPassword", pw)
89
func (mock *mockSetter) SetPassword(pw string) {
90
mock.stub.AddCall("SetPassword", pw)
94
type mockConn struct {
100
func (mock *mockConn) ModelTag() (names.ModelTag, error) {
101
mock.stub.AddCall("ModelTag")
102
if err := mock.stub.NextErr(); err != nil {
103
return names.ModelTag{}, err
105
return coretesting.ModelTag, nil
108
func (mock *mockConn) Broken() <-chan struct{} {
112
func (mock *mockConn) Close() error {
113
mock.stub.AddCall("Close")
114
return mock.stub.NextErr()
117
func newMockConnFacade(stub *testing.Stub, life apiagent.Life) apiagent.ConnFacade {
118
return &mockConnFacade{
124
type mockConnFacade struct {
129
func (mock *mockConnFacade) Life(entity names.Tag) (apiagent.Life, error) {
130
mock.stub.AddCall("Life", entity)
131
if err := mock.stub.NextErr(); err != nil {
134
return mock.life, nil
137
func (mock *mockConnFacade) SetPassword(entity names.Tag, password string) error {
138
mock.stub.AddCall("SetPassword", entity, password)
139
return mock.stub.NextErr()
142
type dummyWorker struct {
146
func assertStop(c *gc.C, w worker.Worker) {
147
c.Assert(worker.Stop(w), jc.ErrorIsNil)
150
func assertStopError(c *gc.C, w worker.Worker, match string) {
151
c.Assert(worker.Stop(w), gc.ErrorMatches, match)
154
func lifeTest(c *gc.C, stub *testing.Stub, life apiagent.Life, test func() (api.Connection, error)) (api.Connection, error) {
155
expectConn := &mockConn{stub: stub}
156
newFacade := func(apiCaller base.APICaller) (apiagent.ConnFacade, error) {
157
c.Check(apiCaller, jc.DeepEquals, expectConn)
158
return newMockConnFacade(stub, life), nil
160
unpatch := testing.PatchValue(apicaller.NewConnFacade, newFacade)
165
// TODO(katco): 2016-08-09: lp:1611427
166
func strategyTest(stub *testing.Stub, strategy utils.AttemptStrategy, test func(api.OpenFunc) (api.Connection, error)) (api.Connection, error) {
167
unpatch := testing.PatchValue(apicaller.Strategy, strategy)
169
return test(func(info *api.Info, opts api.DialOpts) (api.Connection, error) {
170
// copy because I don't trust what might happen to info
171
stub.AddCall("apiOpen", *info, opts)
172
err := stub.NextErr()
176
return &mockConn{stub: stub}, nil
180
func checkOpenCalls(c *gc.C, stub *testing.Stub, passwords ...string) {
181
calls := openCalls(names.ModelTag{}, nil, passwords...)
182
stub.CheckCalls(c, calls)
185
func openCalls(model names.ModelTag, entity names.Tag, passwords ...string) []testing.StubCall {
186
calls := make([]testing.StubCall, len(passwords))
187
for i, pw := range passwords {
193
calls[i] = testing.StubCall{
195
Args: []interface{}{info, api.DialOpts{}},