shared.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // +build integration
  2. // Package smoke contains shared step definitions that are used across integration tests
  3. package smoke
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "reflect"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "github.com/gucumber/gucumber"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/aws/aws-sdk-go/aws"
  15. "github.com/aws/aws-sdk-go/aws/awserr"
  16. "github.com/aws/aws-sdk-go/aws/awsutil"
  17. "github.com/aws/aws-sdk-go/aws/session"
  18. )
  19. // Session is a shared session for all integration smoke tests to use.
  20. var Session = session.Must(session.NewSession())
  21. func init() {
  22. logLevel := Session.Config.LogLevel
  23. if os.Getenv("DEBUG") != "" {
  24. logLevel = aws.LogLevel(aws.LogDebug)
  25. }
  26. if os.Getenv("DEBUG_SIGNING") != "" {
  27. logLevel = aws.LogLevel(aws.LogDebugWithSigning)
  28. }
  29. if os.Getenv("DEBUG_BODY") != "" {
  30. logLevel = aws.LogLevel(aws.LogDebugWithHTTPBody)
  31. }
  32. Session.Config.LogLevel = logLevel
  33. gucumber.When(`^I call the "(.+?)" API$`, func(op string) {
  34. call(op, nil, false)
  35. })
  36. gucumber.When(`^I call the "(.+?)" API with:$`, func(op string, args [][]string) {
  37. call(op, args, false)
  38. })
  39. gucumber.Then(`^the value at "(.+?)" should be a list$`, func(member string) {
  40. vals, _ := awsutil.ValuesAtPath(gucumber.World["response"], member)
  41. assert.NotNil(gucumber.T, vals)
  42. })
  43. gucumber.Then(`^the response should contain a "(.+?)"$`, func(member string) {
  44. vals, _ := awsutil.ValuesAtPath(gucumber.World["response"], member)
  45. assert.NotEmpty(gucumber.T, vals)
  46. })
  47. gucumber.When(`^I attempt to call the "(.+?)" API with:$`, func(op string, args [][]string) {
  48. call(op, args, true)
  49. })
  50. gucumber.Then(`^I expect the response error code to be "(.+?)"$`, func(code string) {
  51. err, ok := gucumber.World["error"].(awserr.Error)
  52. assert.True(gucumber.T, ok, "no error returned")
  53. if ok {
  54. assert.Equal(gucumber.T, code, err.Code(), "Error: %v", err)
  55. }
  56. })
  57. gucumber.And(`^I expect the response error message to include:$`, func(data string) {
  58. err, ok := gucumber.World["error"].(awserr.Error)
  59. assert.True(gucumber.T, ok, "no error returned")
  60. if ok {
  61. assert.Contains(gucumber.T, err.Error(), data)
  62. }
  63. })
  64. gucumber.And(`^I expect the response error message to include one of:$`, func(table [][]string) {
  65. err, ok := gucumber.World["error"].(awserr.Error)
  66. assert.True(gucumber.T, ok, "no error returned")
  67. if ok {
  68. found := false
  69. for _, row := range table {
  70. if strings.Contains(err.Error(), row[0]) {
  71. found = true
  72. break
  73. }
  74. }
  75. assert.True(gucumber.T, found, fmt.Sprintf("no error messages matched: \"%s\"", err.Error()))
  76. }
  77. })
  78. gucumber.And(`^I expect the response error message not be empty$`, func() {
  79. err, ok := gucumber.World["error"].(awserr.Error)
  80. assert.True(gucumber.T, ok, "no error returned")
  81. assert.NotEmpty(gucumber.T, err.Message())
  82. })
  83. gucumber.When(`^I call the "(.+?)" API with JSON:$`, func(s1 string, data string) {
  84. callWithJSON(s1, data, false)
  85. })
  86. gucumber.When(`^I attempt to call the "(.+?)" API with JSON:$`, func(s1 string, data string) {
  87. callWithJSON(s1, data, true)
  88. })
  89. gucumber.Then(`^the error code should be "(.+?)"$`, func(s1 string) {
  90. err, ok := gucumber.World["error"].(awserr.Error)
  91. assert.True(gucumber.T, ok, "no error returned")
  92. assert.Equal(gucumber.T, s1, err.Code())
  93. })
  94. gucumber.And(`^the error message should contain:$`, func(data string) {
  95. err, ok := gucumber.World["error"].(awserr.Error)
  96. assert.True(gucumber.T, ok, "no error returned")
  97. assert.Contains(gucumber.T, err.Error(), data)
  98. })
  99. gucumber.Then(`^the request should fail$`, func() {
  100. err, ok := gucumber.World["error"].(awserr.Error)
  101. assert.True(gucumber.T, ok, "no error returned")
  102. assert.Error(gucumber.T, err)
  103. })
  104. gucumber.Then(`^the request should be successful$`, func() {
  105. err, ok := gucumber.World["error"].(awserr.Error)
  106. assert.False(gucumber.T, ok, "error returned")
  107. assert.NoError(gucumber.T, err)
  108. })
  109. }
  110. // findMethod finds the op operation on the v structure using a case-insensitive
  111. // lookup. Returns nil if no method is found.
  112. func findMethod(v reflect.Value, op string) *reflect.Value {
  113. t := v.Type()
  114. op = strings.ToLower(op)
  115. for i := 0; i < t.NumMethod(); i++ {
  116. name := t.Method(i).Name
  117. if strings.ToLower(name) == op {
  118. m := v.MethodByName(name)
  119. return &m
  120. }
  121. }
  122. return nil
  123. }
  124. // call calls an operation on gucumber.World["client"] by the name op using the args
  125. // table of arguments to set.
  126. func call(op string, args [][]string, allowError bool) {
  127. v := reflect.ValueOf(gucumber.World["client"])
  128. if m := findMethod(v, op); m != nil {
  129. t := m.Type()
  130. in := reflect.New(t.In(0).Elem())
  131. fillArgs(in, args)
  132. resps := m.Call([]reflect.Value{in})
  133. gucumber.World["response"] = resps[0].Interface()
  134. gucumber.World["error"] = resps[1].Interface()
  135. if !allowError {
  136. err, _ := gucumber.World["error"].(error)
  137. assert.NoError(gucumber.T, err)
  138. }
  139. } else {
  140. assert.Fail(gucumber.T, "failed to find operation "+op)
  141. }
  142. }
  143. // reIsNum is a regular expression matching a numeric input (integer)
  144. var reIsNum = regexp.MustCompile(`^\d+$`)
  145. // reIsArray is a regular expression matching a list
  146. var reIsArray = regexp.MustCompile(`^\['.*?'\]$`)
  147. var reArrayElem = regexp.MustCompile(`'(.+?)'`)
  148. // fillArgs fills arguments on the input structure using the args table of
  149. // arguments.
  150. func fillArgs(in reflect.Value, args [][]string) {
  151. if args == nil {
  152. return
  153. }
  154. for _, row := range args {
  155. path := row[0]
  156. var val interface{} = row[1]
  157. if reIsArray.MatchString(row[1]) {
  158. quotedStrs := reArrayElem.FindAllString(row[1], -1)
  159. strs := make([]*string, len(quotedStrs))
  160. for i, e := range quotedStrs {
  161. str := e[1 : len(e)-1]
  162. strs[i] = &str
  163. }
  164. val = strs
  165. } else if reIsNum.MatchString(row[1]) { // handle integer values
  166. num, err := strconv.ParseInt(row[1], 10, 64)
  167. if err == nil {
  168. val = num
  169. }
  170. }
  171. awsutil.SetValueAtPath(in.Interface(), path, val)
  172. }
  173. }
  174. func callWithJSON(op, j string, allowError bool) {
  175. v := reflect.ValueOf(gucumber.World["client"])
  176. if m := findMethod(v, op); m != nil {
  177. t := m.Type()
  178. in := reflect.New(t.In(0).Elem())
  179. fillJSON(in, j)
  180. resps := m.Call([]reflect.Value{in})
  181. gucumber.World["response"] = resps[0].Interface()
  182. gucumber.World["error"] = resps[1].Interface()
  183. if !allowError {
  184. err, _ := gucumber.World["error"].(error)
  185. assert.NoError(gucumber.T, err)
  186. }
  187. } else {
  188. assert.Fail(gucumber.T, "failed to find operation "+op)
  189. }
  190. }
  191. func fillJSON(in reflect.Value, j string) {
  192. d := json.NewDecoder(strings.NewReader(j))
  193. if err := d.Decode(in.Interface()); err != nil {
  194. panic(err)
  195. }
  196. }