github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/migrationflag/facade_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package migrationflag_test 5 6 import ( 7 "errors" 8 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/api/base" 14 basetesting "github.com/juju/juju/api/base/testing" 15 "github.com/juju/juju/api/migrationflag" 16 "github.com/juju/juju/apiserver/params" 17 "github.com/juju/juju/core/migration" 18 "github.com/juju/juju/watcher" 19 ) 20 21 type FacadeSuite struct { 22 testing.IsolationSuite 23 } 24 25 var _ = gc.Suite(&FacadeSuite{}) 26 27 func (*FacadeSuite) TestPhaseCallError(c *gc.C) { 28 stub := &testing.Stub{} 29 apiCaller := apiCaller(c, stub, func(interface{}) error { 30 return errors.New("bork") 31 }) 32 facade := migrationflag.NewFacade(apiCaller, nil) 33 34 phase, err := facade.Phase(someUUID) 35 c.Check(err, gc.ErrorMatches, "bork") 36 c.Check(phase, gc.Equals, migration.UNKNOWN) 37 checkCalls(c, stub, "Phase") 38 } 39 40 func (*FacadeSuite) TestPhaseNoResults(c *gc.C) { 41 stub := &testing.Stub{} 42 apiCaller := apiCaller(c, stub, func(interface{}) error { 43 return nil 44 }) 45 facade := migrationflag.NewFacade(apiCaller, nil) 46 47 phase, err := facade.Phase(someUUID) 48 c.Check(err, gc.ErrorMatches, "expected 1 result, got 0") 49 c.Check(phase, gc.Equals, migration.UNKNOWN) 50 checkCalls(c, stub, "Phase") 51 } 52 53 func (*FacadeSuite) TestPhaseExtraResults(c *gc.C) { 54 stub := &testing.Stub{} 55 apiCaller := apiCaller(c, stub, func(response interface{}) error { 56 outPtr, ok := response.(*params.PhaseResults) 57 c.Assert(ok, jc.IsTrue) 58 outPtr.Results = []params.PhaseResult{ 59 {Phase: "ABORT"}, 60 {Phase: "DONE"}, 61 } 62 return nil 63 }) 64 facade := migrationflag.NewFacade(apiCaller, nil) 65 66 phase, err := facade.Phase(someUUID) 67 c.Check(err, gc.ErrorMatches, "expected 1 result, got 2") 68 c.Check(phase, gc.Equals, migration.UNKNOWN) 69 checkCalls(c, stub, "Phase") 70 } 71 72 func (*FacadeSuite) TestPhaseError(c *gc.C) { 73 stub := &testing.Stub{} 74 apiCaller := apiCaller(c, stub, func(response interface{}) error { 75 outPtr, ok := response.(*params.PhaseResults) 76 c.Assert(ok, jc.IsTrue) 77 outPtr.Results = []params.PhaseResult{ 78 {Error: ¶ms.Error{Message: "mneh"}}, 79 } 80 return nil 81 }) 82 facade := migrationflag.NewFacade(apiCaller, nil) 83 84 phase, err := facade.Phase(someUUID) 85 c.Check(err, gc.ErrorMatches, "mneh") 86 c.Check(phase, gc.Equals, migration.UNKNOWN) 87 checkCalls(c, stub, "Phase") 88 } 89 90 func (*FacadeSuite) TestPhaseInvalid(c *gc.C) { 91 stub := &testing.Stub{} 92 apiCaller := apiCaller(c, stub, func(response interface{}) error { 93 outPtr, ok := response.(*params.PhaseResults) 94 c.Assert(ok, jc.IsTrue) 95 outPtr.Results = []params.PhaseResult{{Phase: "COLLABORATE"}} 96 return nil 97 }) 98 facade := migrationflag.NewFacade(apiCaller, nil) 99 100 phase, err := facade.Phase(someUUID) 101 c.Check(err, gc.ErrorMatches, `unknown phase "COLLABORATE"`) 102 c.Check(phase, gc.Equals, migration.UNKNOWN) 103 checkCalls(c, stub, "Phase") 104 } 105 106 func (*FacadeSuite) TestPhaseSuccess(c *gc.C) { 107 stub := &testing.Stub{} 108 apiCaller := apiCaller(c, stub, func(response interface{}) error { 109 outPtr, ok := response.(*params.PhaseResults) 110 c.Assert(ok, jc.IsTrue) 111 outPtr.Results = []params.PhaseResult{{Phase: "ABORT"}} 112 return nil 113 }) 114 facade := migrationflag.NewFacade(apiCaller, nil) 115 116 phase, err := facade.Phase(someUUID) 117 c.Check(err, jc.ErrorIsNil) 118 c.Check(phase, gc.Equals, migration.ABORT) 119 checkCalls(c, stub, "Phase") 120 } 121 122 func (*FacadeSuite) TestWatchCallError(c *gc.C) { 123 stub := &testing.Stub{} 124 apiCaller := apiCaller(c, stub, func(interface{}) error { 125 return errors.New("bork") 126 }) 127 facade := migrationflag.NewFacade(apiCaller, nil) 128 129 watch, err := facade.Watch(someUUID) 130 c.Check(err, gc.ErrorMatches, "bork") 131 c.Check(watch, gc.IsNil) 132 checkCalls(c, stub, "Watch") 133 } 134 135 func (*FacadeSuite) TestWatchNoResults(c *gc.C) { 136 stub := &testing.Stub{} 137 apiCaller := apiCaller(c, stub, func(interface{}) error { 138 return nil 139 }) 140 facade := migrationflag.NewFacade(apiCaller, nil) 141 142 watch, err := facade.Watch(someUUID) 143 c.Check(err, gc.ErrorMatches, "expected 1 result, got 0") 144 c.Check(watch, gc.IsNil) 145 checkCalls(c, stub, "Watch") 146 } 147 148 func (*FacadeSuite) TestWatchExtraResults(c *gc.C) { 149 stub := &testing.Stub{} 150 apiCaller := apiCaller(c, stub, func(response interface{}) error { 151 outPtr, ok := response.(*params.NotifyWatchResults) 152 c.Assert(ok, jc.IsTrue) 153 outPtr.Results = []params.NotifyWatchResult{ 154 {NotifyWatcherId: "123"}, 155 {NotifyWatcherId: "456"}, 156 } 157 return nil 158 }) 159 facade := migrationflag.NewFacade(apiCaller, nil) 160 161 watch, err := facade.Watch(someUUID) 162 c.Check(err, gc.ErrorMatches, "expected 1 result, got 2") 163 c.Check(watch, gc.IsNil) 164 checkCalls(c, stub, "Watch") 165 } 166 167 func (*FacadeSuite) TestWatchError(c *gc.C) { 168 stub := &testing.Stub{} 169 apiCaller := apiCaller(c, stub, func(response interface{}) error { 170 outPtr, ok := response.(*params.NotifyWatchResults) 171 c.Assert(ok, jc.IsTrue) 172 outPtr.Results = []params.NotifyWatchResult{ 173 {Error: ¶ms.Error{Message: "snfl"}}, 174 } 175 return nil 176 }) 177 facade := migrationflag.NewFacade(apiCaller, nil) 178 179 watch, err := facade.Watch(someUUID) 180 c.Check(err, gc.ErrorMatches, "snfl") 181 c.Check(watch, gc.IsNil) 182 checkCalls(c, stub, "Watch") 183 } 184 185 func (*FacadeSuite) TestWatchSuccess(c *gc.C) { 186 stub := &testing.Stub{} 187 apiCaller := apiCaller(c, stub, func(response interface{}) error { 188 outPtr, ok := response.(*params.NotifyWatchResults) 189 c.Assert(ok, jc.IsTrue) 190 outPtr.Results = []params.NotifyWatchResult{ 191 {NotifyWatcherId: "789"}, 192 } 193 return nil 194 }) 195 expectWatch := &struct{ watcher.NotifyWatcher }{} 196 newWatcher := func(gotCaller base.APICaller, result params.NotifyWatchResult) watcher.NotifyWatcher { 197 c.Check(gotCaller, gc.NotNil) // uncomparable 198 c.Check(result, jc.DeepEquals, params.NotifyWatchResult{ 199 NotifyWatcherId: "789", 200 }) 201 return expectWatch 202 } 203 facade := migrationflag.NewFacade(apiCaller, newWatcher) 204 205 watch, err := facade.Watch(someUUID) 206 c.Check(err, jc.ErrorIsNil) 207 c.Check(watch, gc.Equals, expectWatch) 208 checkCalls(c, stub, "Watch") 209 } 210 211 func apiCaller(c *gc.C, stub *testing.Stub, set func(interface{}) error) base.APICaller { 212 return basetesting.APICallerFunc(func( 213 objType string, version int, 214 id, request string, 215 args, response interface{}, 216 ) error { 217 c.Check(objType, gc.Equals, "MigrationFlag") 218 c.Check(version, gc.Equals, 0) 219 c.Check(id, gc.Equals, "") 220 stub.AddCall(request, args) 221 return set(response) 222 }) 223 } 224 225 func checkCalls(c *gc.C, stub *testing.Stub, names ...string) { 226 stub.CheckCallNames(c, names...) 227 for _, call := range stub.Calls() { 228 c.Check(call.Args, jc.DeepEquals, []interface{}{ 229 params.Entities{ 230 []params.Entity{{"model-some-uuid"}}, 231 }, 232 }) 233 } 234 } 235 236 const someUUID = "some-uuid"