github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/api/base/testing/apicaller.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package testing 5 6 import ( 7 "github.com/juju/names" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/api/base" 13 ) 14 15 // APICallerFunc is a function type that implements APICaller. 16 type APICallerFunc func(objType string, version int, id, request string, params, response interface{}) error 17 18 func (f APICallerFunc) APICall(objType string, version int, id, request string, params, response interface{}) error { 19 return f(objType, version, id, request, params, response) 20 } 21 22 func (APICallerFunc) BestFacadeVersion(facade string) int { 23 return 0 24 } 25 26 func (APICallerFunc) EnvironTag() (names.EnvironTag, error) { 27 return names.NewEnvironTag(""), nil 28 } 29 30 func (APICallerFunc) Close() error { 31 return nil 32 } 33 34 // CheckArgs holds the possible arguments to CheckingAPICaller(). Any 35 // fields non empty fields will be checked to match the arguments 36 // recieved by the APICall() method of the returned APICallerFunc. If 37 // Id is empty, but IdIsEmpty is true, the id argument is checked to 38 // be empty. The same applies to Version being empty, but if 39 // VersionIsZero set to true the version is checked to be 0. 40 type CheckArgs struct { 41 Facade string 42 Version int 43 Id string 44 Method string 45 Args interface{} 46 Results interface{} 47 48 IdIsEmpty bool 49 VersionIsZero bool 50 } 51 52 func checkArgs(c *gc.C, args *CheckArgs, facade string, version int, id, method string, inArgs, outResults interface{}) { 53 if args == nil { 54 c.Logf("checkArgs: args is nil!") 55 return 56 } else { 57 if args.Facade != "" { 58 c.Check(facade, gc.Equals, args.Facade) 59 } 60 if args.Version != 0 { 61 c.Check(version, gc.Equals, args.Version) 62 } else if args.VersionIsZero { 63 c.Check(version, gc.Equals, 0) 64 } 65 if args.Id != "" { 66 c.Check(id, gc.Equals, args.Id) 67 } else if args.IdIsEmpty { 68 c.Check(id, gc.Equals, "") 69 } 70 if args.Method != "" { 71 c.Check(method, gc.Equals, args.Method) 72 } 73 if args.Args != nil { 74 c.Check(inArgs, jc.DeepEquals, args.Args) 75 } 76 if args.Results != nil { 77 c.Check(outResults, gc.NotNil) 78 testing.PatchValue(outResults, args.Results) 79 } 80 } 81 } 82 83 // CheckingAPICaller returns an APICallerFunc which can report the 84 // number of times its APICall() method was called (if numCalls is not 85 // nil), as well as check if any of the arguments passed to the 86 // APICall() method match the values given in args (if args itself is 87 // not nil, otherwise no arguments are checked). The final error 88 // result of the APICall() will be set to err. 89 func CheckingAPICaller(c *gc.C, args *CheckArgs, numCalls *int, err error) base.APICallCloser { 90 return APICallerFunc( 91 func(facade string, version int, id, method string, inArgs, outResults interface{}) error { 92 if numCalls != nil { 93 *numCalls++ 94 } 95 if args != nil { 96 checkArgs(c, args, facade, version, id, method, inArgs, outResults) 97 } 98 return err 99 }, 100 ) 101 } 102 103 // NotifyingCheckingAPICaller returns an APICallerFunc which sends a message on the channel "called" every 104 // time it recives a call, as well as check if any of the arguments passed to the APICall() method match 105 // the values given in args (if args itself is not nil, otherwise no arguments are checked). The final 106 // error result of the APICall() will be set to err. 107 func NotifyingCheckingAPICaller(c *gc.C, args *CheckArgs, called chan struct{}, err error) base.APICaller { 108 return APICallerFunc( 109 func(facade string, version int, id, method string, inArgs, outResults interface{}) error { 110 called <- struct{}{} 111 if args != nil { 112 checkArgs(c, args, facade, version, id, method, inArgs, outResults) 113 } 114 return err 115 }, 116 ) 117 }