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 }
func (s *ClientSuite) TestActionFinishSuccess(c *gc.C) { tag := names.NewActionTag(utils.MustNewUUID().String()) status := "stubstatus" actionResults := map[string]interface{}{"stub": "stub"} message := "stubmsg" expectedCalls := []jujutesting.StubCall{{ "MachineActions.FinishActions", []interface{}{"", params.ActionExecutionResults{ Results: []params.ActionExecutionResult{{ ActionTag: tag.String(), Status: status, Results: actionResults, Message: message, }}, }}, }} var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) c.Check(result, gc.FitsTypeOf, ¶ms.ErrorResults{}) *(result.(*params.ErrorResults)) = params.ErrorResults{ Results: []params.ErrorResult{{}}, } return nil }) client := machineactions.NewClient(apiCaller) err := client.ActionFinish(tag, status, actionResults, message) c.Assert(err, jc.ErrorIsNil) stub.CheckCalls(c, expectedCalls) }
func (s *ClientSuite) AssertModelCall(c *gc.C, stub *jujutesting.Stub, tag names.ModelTag, call string, err error) { expectedArg := params.ModelArgs{ModelTag: tag.String()} stub.CheckCalls(c, []jujutesting.StubCall{ {"MigrationTarget." + call, []interface{}{"", expectedArg}}, }) c.Assert(err, gc.ErrorMatches, "boom") }
func (s *ClientSuite) TestActionFinishTooManyResults(c *gc.C) { tag := names.NewActionTag(utils.MustNewUUID().String()) expectedCalls := []jujutesting.StubCall{{ "MachineActions.FinishActions", []interface{}{"", params.ActionExecutionResults{ Results: []params.ActionExecutionResult{{ ActionTag: tag.String(), Status: "", Results: nil, Message: "", }}, }}, }} var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) c.Check(result, gc.FitsTypeOf, ¶ms.ErrorResults{}) res := result.(*params.ErrorResults) res.Results = make([]params.ErrorResult, 2) return nil }) client := machineactions.NewClient(apiCaller) err := client.ActionFinish(tag, "", nil, "") c.Assert(err, gc.ErrorMatches, "expected 1 result, got 2") stub.CheckCalls(c, expectedCalls) }
func (s *ClientSuite) TestActionFinishResultError(c *gc.C) { tag := names.NewActionTag(utils.MustNewUUID().String()) expectedCalls := []jujutesting.StubCall{{ "MachineActions.FinishActions", []interface{}{"", params.ActionExecutionResults{ Results: []params.ActionExecutionResult{{ ActionTag: tag.String(), Status: "", Results: nil, Message: "", }}, }}, }} expectedErr := ¶ms.Error{ Message: "rigged", Code: params.CodeNotAssigned, } var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) c.Check(result, gc.FitsTypeOf, ¶ms.ErrorResults{}) *(result.(*params.ErrorResults)) = params.ErrorResults{ Results: []params.ErrorResult{{expectedErr}}, } return nil }) client := machineactions.NewClient(apiCaller) err := client.ActionFinish(tag, "", nil, "") c.Assert(errors.Cause(err), gc.Equals, expectedErr) stub.CheckCalls(c, expectedCalls) }
func (s *ClientSuite) TestGetActionSuccess(c *gc.C) { tag := names.NewActionTag(utils.MustNewUUID().String()) expectedCalls := []jujutesting.StubCall{{ "MachineActions.Actions", []interface{}{"", params.Entities{ Entities: []params.Entity{{Tag: tag.String()}}, }}, }} expectedName := "ack" expectedParams := map[string]interface{}{"floob": "zgloob"} var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) c.Check(result, gc.FitsTypeOf, ¶ms.ActionResults{}) *(result.(*params.ActionResults)) = params.ActionResults{ Results: []params.ActionResult{{ Action: ¶ms.Action{ Name: expectedName, Parameters: expectedParams, }, }}, } return nil }) client := machineactions.NewClient(apiCaller) action, err := client.Action(tag) c.Assert(err, jc.ErrorIsNil) c.Assert(action.Name(), gc.Equals, expectedName) c.Assert(action.Params(), gc.DeepEquals, expectedParams) stub.CheckCalls(c, expectedCalls) }
func (s *ClientSuite) TestExport(c *gc.C) { var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) out := result.(*params.SerializedModel) *out = params.SerializedModel{ Bytes: []byte("foo"), Charms: []string{"cs:foo-1"}, Tools: []params.SerializedModelTools{{ Version: "2.0.0-trusty-amd64", URI: "/tools/0", }}, } return nil }) client := migrationmaster.NewClient(apiCaller, nil) out, err := client.Export() c.Assert(err, jc.ErrorIsNil) stub.CheckCalls(c, []jujutesting.StubCall{ {"MigrationMaster.Export", []interface{}{"", nil}}, }) c.Assert(out, gc.DeepEquals, migration.SerializedModel{ Bytes: []byte("foo"), Charms: []string{"cs:foo-1"}, Tools: map[version.Binary]string{ version.MustParseBinary("2.0.0-trusty-amd64"): "/tools/0", }, }) }
func (s *ClientSuite) TestRunningActionSuccess(c *gc.C) { tag := names.NewMachineTag(utils.MustNewUUID().String()) expectedCalls := []jujutesting.StubCall{{ "MachineActions.RunningActions", []interface{}{"", params.Entities{ Entities: []params.Entity{{Tag: tag.String()}}, }}, }} actionsList := []params.ActionResult{ {Action: ¶ms.Action{Name: "foo"}}, {Action: ¶ms.Action{Name: "baz"}}, } var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) c.Check(result, gc.FitsTypeOf, ¶ms.ActionsByReceivers{}) *(result.(*params.ActionsByReceivers)) = params.ActionsByReceivers{ Actions: []params.ActionsByReceiver{{ Actions: actionsList, }}, } return nil }) client := machineactions.NewClient(apiCaller) actions, err := client.RunningActions(tag) c.Assert(err, jc.ErrorIsNil) c.Assert(actions, jc.DeepEquals, actionsList) stub.CheckCalls(c, expectedCalls) }
func (s *ClientSuite) TestModelInfo(c *gc.C) { var stub jujutesting.Stub owner := names.NewUserTag("owner") apiCaller := apitesting.APICallerFunc(func(objType string, v int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) *(result.(*params.MigrationModelInfo)) = params.MigrationModelInfo{ UUID: "uuid", Name: "name", OwnerTag: owner.String(), AgentVersion: version.MustParse("1.2.3"), } return nil }) client := migrationmaster.NewClient(apiCaller, nil) model, err := client.ModelInfo() stub.CheckCalls(c, []jujutesting.StubCall{ {"MigrationMaster.ModelInfo", []interface{}{"", nil}}, }) c.Check(err, jc.ErrorIsNil) c.Check(model, jc.DeepEquals, migration.ModelInfo{ UUID: "uuid", Name: "name", Owner: owner, AgentVersion: version.MustParse("1.2.3"), }) }
func stubCallNames(stub *jujutesting.Stub) []string { var out []string for _, call := range stub.Calls() { out = append(out, call.FuncName) } return out }
func (s *ClientSuite) TestRunningActionsResultError(c *gc.C) { tag := names.NewMachineTag(utils.MustNewUUID().String()) expectedCalls := []jujutesting.StubCall{{ "MachineActions.RunningActions", []interface{}{"", params.Entities{ Entities: []params.Entity{{Tag: tag.String()}}, }}, }} expectedErr := ¶ms.Error{ Message: "rigged", Code: params.CodeNotAssigned, } var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) c.Check(result, gc.FitsTypeOf, ¶ms.ActionsByReceivers{}) *(result.(*params.ActionsByReceivers)) = params.ActionsByReceivers{ Actions: []params.ActionsByReceiver{{ Error: expectedErr, }}, } return nil }) client := machineactions.NewClient(apiCaller) action, err := client.RunningActions(tag) c.Assert(errors.Cause(err), gc.Equals, expectedErr) c.Assert(action, gc.IsNil) stub.CheckCalls(c, expectedCalls) }
func (s *ClientSuite) TestWatchResultError(c *gc.C) { tag := names.NewMachineTag("2") expectErr := ¶ms.Error{ Message: "rigged", Code: params.CodeNotAssigned, } expectedCalls := []jujutesting.StubCall{{ "MachineActions.WatchActionNotifications", []interface{}{"", params.Entities{ Entities: []params.Entity{{Tag: tag.String()}}, }}, }} var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) c.Check(result, gc.FitsTypeOf, ¶ms.StringsWatchResults{}) res := result.(*params.StringsWatchResults) res.Results = make([]params.StringsWatchResult, 1) res.Results[0].Error = expectErr return nil }) client := machineactions.NewClient(apiCaller) w, err := client.WatchActionNotifications(tag) c.Assert(errors.Cause(err), gc.Equals, expectErr) c.Assert(w, gc.IsNil) stub.CheckCalls(c, expectedCalls) }
func (s *ClientSuite) TestActionBeginSuccess(c *gc.C) { tag := names.NewActionTag(utils.MustNewUUID().String()) expectedCalls := []jujutesting.StubCall{{ "MachineActions.BeginActions", []interface{}{"", params.Entities{ Entities: []params.Entity{{Tag: tag.String()}}, }}, }} var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) c.Check(result, gc.FitsTypeOf, ¶ms.ErrorResults{}) *(result.(*params.ErrorResults)) = params.ErrorResults{ Results: []params.ErrorResult{{}}, } return nil }) client := machineactions.NewClient(apiCaller) err := client.ActionBegin(tag) c.Assert(err, jc.ErrorIsNil) stub.CheckCalls(c, expectedCalls) }
func (s *facadeSuite) TestReportKeys(c *gc.C) { stub := new(testing.Stub) apiCaller := basetesting.APICallerFunc(func( objType string, version int, id, request string, args, response interface{}, ) error { c.Check(objType, gc.Equals, "HostKeyReporter") c.Check(version, gc.Equals, 0) c.Check(id, gc.Equals, "") stub.AddCall(request, args) *response.(*params.ErrorResults) = params.ErrorResults{ Results: []params.ErrorResult{{ (*params.Error)(nil), }}, } return nil }) facade := hostkeyreporter.NewFacade(apiCaller) err := facade.ReportKeys("42", []string{"rsa", "dsa"}) c.Assert(err, jc.ErrorIsNil) stub.CheckCalls(c, []testing.StubCall{{ "ReportKeys", []interface{}{params.SSHHostKeySet{ EntityKeys: []params.SSHHostKeys{{ Tag: names.NewMachineTag("42").String(), PublicKeys: []string{"rsa", "dsa"}, }}, }}, }}) }
func (s *ClientSuite) getClientAndStub(c *gc.C) (*migrationtarget.Client, *jujutesting.Stub) { var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) return errors.New("boom") }) client := migrationtarget.NewClient(apiCaller) return client, &stub }
func checkCalls(c *gc.C, stub *testing.Stub, names ...string) { stub.CheckCallNames(c, names...) for _, call := range stub.Calls() { c.Check(call.Args, jc.DeepEquals, []interface{}{ params.Entities{ []params.Entity{{"model-some-uuid"}}, }, }) } }
func checkRemovalsMatch(c *gc.C, stub *testing.Stub, expected ...string) { var completedRemovals []string for _, call := range stub.Calls() { if call.FuncName == "CompleteRemoval" { machineId := call.Args[0].(names.MachineTag).Id() completedRemovals = append(completedRemovals, machineId) } } c.Check(completedRemovals, gc.DeepEquals, expected) }
func (s *FacadeSuite) TestAddress(c *gc.C) { var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, arg) c.Check(id, gc.Equals, "") *result.(*params.SSHAddressResults) = params.SSHAddressResults{ Results: []params.SSHAddressResult{{Address: "1.1.1.1"}}, } return nil }) facade := sshclient.NewFacade(apiCaller) expectedArg := []interface{}{params.Entities{[]params.Entity{{ names.NewUnitTag("foo/0").String(), }}}} public, err := facade.PublicAddress("foo/0") c.Assert(err, jc.ErrorIsNil) c.Check(public, gc.Equals, "1.1.1.1") stub.CheckCalls(c, []jujutesting.StubCall{{"SSHClient.PublicAddress", expectedArg}}) stub.ResetCalls() private, err := facade.PrivateAddress("foo/0") c.Assert(err, jc.ErrorIsNil) c.Check(private, gc.Equals, "1.1.1.1") stub.CheckCalls(c, []jujutesting.StubCall{{"SSHClient.PrivateAddress", expectedArg}}) }
func (*ScaryConnectSuite) assertChangePasswordSuccess(c *gc.C, stub *testing.Stub) { err := checkChangePassword(c, stub) c.Check(err, gc.Equals, apicaller.ErrChangedPassword) stub.CheckCallNames(c, "Life", "ChangeConfig", // Be careful, these are two different SetPassword receivers. "SetPassword", "SetOldPassword", "SetPassword", "Close", ) checkSaneChange(c, stub.Calls()[2:5]) }
func makeStubUploadBinaries(stub *jujutesting.Stub) func(migration.UploadBinariesConfig) error { return func(config migration.UploadBinariesConfig) error { stub.AddCall( "UploadBinaries", config.Charms, config.CharmDownloader, config.Tools, config.ToolsDownloader, ) return nil } }
func (s *FlagSuite) TestClaimError(c *gc.C) { var stub testing.Stub stub.SetErrors(errors.New("squish")) worker, err := singular.NewFlagWorker(singular.FlagConfig{ Facade: newStubFacade(&stub), Clock: &fakeClock{}, Duration: time.Hour, }) c.Check(worker, gc.IsNil) c.Check(err, gc.ErrorMatches, "squish") }
func apiCaller(c *gc.C, stub *testing.Stub, set func(interface{}) error) base.APICaller { return basetesting.APICallerFunc(func( objType string, version int, id, request string, args, response interface{}, ) error { c.Check(objType, gc.Equals, "MigrationFlag") c.Check(version, gc.Equals, 0) c.Check(id, gc.Equals, "") stub.AddCall(request, args) return set(response) }) }
func (s *ClientSuite) TestPrechecks(c *gc.C) { var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) return errors.New("blam") }) client := migrationmaster.NewClient(apiCaller, nil) err := client.Prechecks() c.Check(err, gc.ErrorMatches, "blam") stub.CheckCalls(c, []jujutesting.StubCall{ {"MigrationMaster.Prechecks", []interface{}{"", nil}}, }) }
func (s *ClientSuite) TestReap(c *gc.C) { var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) return nil }) client := migrationmaster.NewClient(apiCaller, nil) err := client.Reap() c.Check(err, jc.ErrorIsNil) stub.CheckCalls(c, []jujutesting.StubCall{ {"MigrationMaster.Reap", []interface{}{"", nil}}, }) }
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 }) }
// 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 apiCaller(c *gc.C, stub *testing.Stub, setResult setResultFunc) base.APICaller { return basetesting.APICallerFunc( func(objType string, version int, id, request string, args, response interface{}, ) error { stub.AddCall(objType, version, id, request, args) result, ok := response.(*params.ErrorResults) c.Assert(ok, jc.IsTrue) return setResult(result) }, ) }
func (s *ClientSuite) TestSetPhase(c *gc.C) { var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, id, arg) return nil }) client := migrationmaster.NewClient(apiCaller) err := client.SetPhase(migration.QUIESCE) c.Assert(err, jc.ErrorIsNil) expectedArg := params.SetMigrationPhaseArgs{Phase: "QUIESCE"} stub.CheckCalls(c, []jujutesting.StubCall{ {"MigrationMaster.SetPhase", []interface{}{"", expectedArg}}, }) }
func checkProxy(c *gc.C, useProxy bool) { var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, arg) *result.(*params.SSHProxyResult) = params.SSHProxyResult{ UseProxy: useProxy, } return nil }) facade := sshclient.NewFacade(apiCaller) result, err := facade.Proxy() c.Check(err, jc.ErrorIsNil) c.Check(result, gc.Equals, useProxy) stub.CheckCalls(c, []jujutesting.StubCall{{"SSHClient.Proxy", []interface{}{nil}}}) }
func makeClient(results params.InitiateMigrationResults) ( *controller.Client, *jujutesting.Stub, ) { var stub jujutesting.Stub apiCaller := apitesting.APICallerFunc( func(objType string, version int, id, request string, arg, result interface{}) error { stub.AddCall(objType+"."+request, arg) out := result.(*params.InitiateMigrationResults) *out = results return nil }, ) client := controller.NewClient(apiCaller) return client, &stub }