2
0

base.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import json
  2. from kscore import xform_name
  3. from kscore.exceptions import ClientError
  4. import jmespath
  5. from behave import when, then
  6. from nose.tools import assert_equal
  7. from nose.tools import assert_is_instance
  8. def _params_from_table(table):
  9. # Unfortunately the way we're using table is not quite how
  10. # behave expects tables to be used:
  11. # They expect:
  12. #
  13. # | name | department |
  14. # | Barry | foo |
  15. # | Pudey | bar |
  16. # | Two-Lumps | bar |
  17. #
  18. # Where the first row are headings that indicate the
  19. # key name you can use to retrieve row values,
  20. # e.g row['name'] -> Barry.
  21. #
  22. #
  23. # We just use:
  24. # | LaunchConfigurationName | hello, world |
  25. # | ImageId | ami-12345678 |
  26. # | InstanceType | m1.small |
  27. #
  28. # So we have to grab the headings before iterating over
  29. # the table rows.
  30. params = {table.headings[0]: table.headings[1]}
  31. for row in table:
  32. params[row[0]] = row[1]
  33. return params
  34. @when(u'I call the "{}" API')
  35. def api_call_no_args(context, operation):
  36. context.response = getattr(context.client, xform_name(operation))()
  37. @when(u'I call the "{}" API with')
  38. def api_call_with_args(context, operation):
  39. params = _params_from_table(context.table)
  40. context.response = getattr(context.client, xform_name(operation))(**params)
  41. @when(u'I call the "{}" API with JSON')
  42. def api_call_with_json(context, operation):
  43. params = json.loads(context.text)
  44. context.response = getattr(context.client, xform_name(operation))(**params)
  45. @when(u'I attempt to call the "{}" API with')
  46. def api_call_with_error(context, operation):
  47. params = _params_from_table(context.table)
  48. try:
  49. getattr(context.client, xform_name(operation))(**params)
  50. except ClientError as e:
  51. context.error_response = e
  52. @when(u'I attempt to call the "{}" API with JSON')
  53. def api_call_with_json_and_error(context, operation):
  54. params = json.loads(context.text)
  55. try:
  56. getattr(context.client, xform_name(operation))(**params)
  57. except ClientError as e:
  58. context.error_response = e
  59. @then(u'I expect the response error code to be "{}"')
  60. def then_expected_error(context, code):
  61. assert_equal(context.error_response.response['Error']['Code'], code)
  62. @then(u'the value at "{}" should be a list')
  63. def then_expected_type_is_list(context, expression):
  64. # In kscore, if there are no values with an element,
  65. # it will not appear in the response dict, so it's actually
  66. # ok if the element does not exist (and is not a list).
  67. # If an exception happened the test will have already failed,
  68. # which makes this step a noop. We'll just verify
  69. # the response is a dict to ensure it made it through
  70. # our response parser properly.
  71. if not isinstance(context.response, list):
  72. raise AssertionError("Response is not a dict: %s" % context.response)
  73. @then(u'the response should contain a "{}"')
  74. def then_should_contain_key(context, key):
  75. # See then_expected_type_is_a_list for more background info.
  76. # We really just care that the request succeeded for these
  77. # smoke tests.
  78. if not isinstance(context.response, dict):
  79. raise AssertionError("Response is not a dict: %s" % context.response)
  80. @then(u'I expect the response error to contain a message')
  81. def then_error_has_message(context):
  82. if 'Message' not in context.error_response.response['Error']:
  83. raise AssertionError("Message key missing from error response: %s" %
  84. context.error_response.response)