github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/migrationminion/client_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package migrationminion_test 5 6 import ( 7 "time" 8 9 "github.com/juju/errors" 10 jujutesting "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 apitesting "github.com/juju/juju/api/base/testing" 15 "github.com/juju/juju/api/migrationminion" 16 "github.com/juju/juju/apiserver/params" 17 "github.com/juju/juju/core/migration" 18 coretesting "github.com/juju/juju/testing" 19 "github.com/juju/juju/worker" 20 ) 21 22 type ClientSuite struct { 23 jujutesting.IsolationSuite 24 } 25 26 var _ = gc.Suite(&ClientSuite{}) 27 28 func (s *ClientSuite) TestWatch(c *gc.C) { 29 var stub jujutesting.Stub 30 apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { 31 stub.AddCall(objType+"."+request, id, arg) 32 switch request { 33 case "Watch": 34 *(result.(*params.NotifyWatchResult)) = params.NotifyWatchResult{ 35 NotifyWatcherId: "abc", 36 } 37 case "Next": 38 // The full success case is tested in api/watcher. 39 return errors.New("boom") 40 case "Stop": 41 } 42 return nil 43 }) 44 45 client := migrationminion.NewClient(apiCaller) 46 w, err := client.Watch() 47 c.Assert(err, jc.ErrorIsNil) 48 defer worker.Stop(w) 49 50 errC := make(chan error) 51 go func() { 52 errC <- w.Wait() 53 }() 54 55 select { 56 case err := <-errC: 57 c.Assert(err, gc.ErrorMatches, "boom") 58 expectedCalls := []jujutesting.StubCall{ 59 {"Migrationminion.Watch", []interface{}{"", nil}}, 60 {"MigrationStatusWatcher.Next", []interface{}{"abc", nil}}, 61 {"MigrationStatusWatcher.Stop", []interface{}{"abc", nil}}, 62 } 63 // The Stop API call happens in a separate goroutine which 64 // might execute after the worker has exited so wait for the 65 // expected calls to arrive. 66 for a := coretesting.LongAttempt.Start(); a.Next(); { 67 if len(stub.Calls()) >= len(expectedCalls) { 68 return 69 } 70 } 71 stub.CheckCalls(c, expectedCalls) 72 case <-time.After(coretesting.LongWait): 73 c.Fatal("timed out waiting for watcher to die") 74 } 75 } 76 77 func (s *ClientSuite) TestWatchErr(c *gc.C) { 78 apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { 79 return errors.New("boom") 80 }) 81 client := migrationminion.NewClient(apiCaller) 82 _, err := client.Watch() 83 c.Assert(err, gc.ErrorMatches, "boom") 84 } 85 86 func (s *ClientSuite) TestReport(c *gc.C) { 87 var stub jujutesting.Stub 88 apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error { 89 stub.AddCall(objType+"."+request, arg) 90 return nil 91 }) 92 93 client := migrationminion.NewClient(apiCaller) 94 err := client.Report("id", migration.IMPORT, true) 95 c.Assert(err, jc.ErrorIsNil) 96 97 stub.CheckCalls(c, []jujutesting.StubCall{ 98 {"MigrationMinion.Report", []interface{}{params.MinionReport{ 99 MigrationId: "id", 100 Phase: "IMPORT", 101 Success: true, 102 }}}, 103 }) 104 } 105 106 func (s *ClientSuite) TestReportError(c *gc.C) { 107 apiCaller := apitesting.APICallerFunc(func(string, int, string, string, interface{}, interface{}) error { 108 return errors.New("boom") 109 }) 110 111 client := migrationminion.NewClient(apiCaller) 112 err := client.Report("id", migration.IMPORT, true) 113 c.Assert(err, gc.ErrorMatches, "boom") 114 }