func checkChangePassword(c *gc.C, stub *testing.Stub) error { // We prepend the unauth/success pair that triggers password // change, and consume them in apiOpen below... //errUnauth := ¶ms.Error{Code: params.CodeUnauthorized} //allErrs := append([]error{errUnauth, nil}, errs...) // //stub := &testing.Stub{} //stub.SetErrors(allErrs...) expectConn := &mockConn{stub: stub} apiOpen := func(info *api.Info, opts api.DialOpts) (api.Connection, error) { // ...but we *don't* record the calls themselves; they // are tested plenty elsewhere, and hiding them makes // client code simpler. if err := stub.NextErr(); err != nil { return nil, err } return expectConn, nil } entity := names.NewApplicationTag("omg") connect := func() (api.Connection, error) { return apicaller.ScaryConnect(&mockAgent{ stub: stub, model: coretesting.ModelTag, entity: entity, }, apiOpen) } conn, err := lifeTest(c, stub, apiagent.Alive, connect) c.Check(conn, gc.IsNil) return err }
// newMockWatcher consumes an error from the supplied testing.Stub, and // returns a state.NotifyWatcher that either works or doesn't depending // on whether the error was nil. func newMockWatcher(stub *testing.Stub) *mockWatcher { changes := make(chan struct{}, 1) err := stub.NextErr() if err == nil { changes <- struct{}{} } else { close(changes) } return &mockWatcher{ err: err, changes: changes, } }
func strategyTest(stub *testing.Stub, strategy utils.AttemptStrategy, test func(api.OpenFunc) (api.Connection, error)) (api.Connection, error) { unpatch := testing.PatchValue(apicaller.Strategy, strategy) defer unpatch() return test(func(info *api.Info, opts api.DialOpts) (api.Connection, error) { // copy because I don't trust what might happen to info stub.AddCall("apiOpen", *info, opts) err := stub.NextErr() if err != nil { return nil, err } return &mockConn{stub: stub}, nil }) }
func (s *InterfaceSuite) TestRequestRebootNow(c *gc.C) { ctx := s.GetContext(c, -1, "").(*context.HookContext) var stub testing.Stub var p *mockProcess p = &mockProcess{func() error { // Reboot priority should be set before the process // is killed, or else the client waiting for the // process to exit will race with the setting of // the priority. priority := ctx.GetRebootPriority() c.Assert(priority, gc.Equals, jujuc.RebootNow) return stub.NextErr() }} stub.SetErrors(errors.New("process is already dead")) ctx.SetProcess(p) err := ctx.RequestReboot(jujuc.RebootNow) c.Assert(err, jc.ErrorIsNil) // Everything went well, so priority should still be RebootNow. priority := ctx.GetRebootPriority() c.Assert(priority, gc.Equals, jujuc.RebootNow) }
func mockHandleAction(stub *testing.Stub) func(string, map[string]interface{}) (map[string]interface{}, error) { return func(name string, params map[string]interface{}) (map[string]interface{}, error) { stub.AddCall("HandleAction", name) return nil, stub.NextErr() } }