Top Level Namespace

Defined Under Namespace

Modules: Cucumber, Maze, Selenium Classes: Hash, Logger

Constant Summary collapse

TIMESTAMP_REGEX =

A regex providing the pattern expected from timestamps

/^\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:[\d\.]+(Z|[+-]\d{2}:\d{2})?$/

Span steps collapse

Runner steps collapse

Network steps collapse

Feature flag steps collapse

Error reporting steps collapse

Multipart request assertion steps collapse

Request assertion steps collapse

Platform dependent steps collapse

Instance Method Summary collapse

Instance Method Details

#assert_attribute(field, key, expected) ⇒ Object



336
337
338
339
340
# File 'lib/features/steps/trace_steps.rb', line 336

def assert_attribute(field, key, expected)
  list = Maze::Server.traces
  attributes = Maze::Helper.read_key_path(list.current[:body], "#{field}.attributes")
  Maze.check.equal({ 'key' => key, 'value' => expected }, attributes.find { |a| a['key'] == key })
end

#assert_equal_with_nullability(expected_value, payload_value) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/features/steps/platform_dependent_steps.rb', line 143

def assert_equal_with_nullability(expected_value, payload_value)
  case expected_value
  when '@null'
    Maze.check.nil(payload_value)
  when '@not_null'
    Maze.check.not_nil(payload_value)
  else
    Maze.check.equal(expected_value, payload_value)
  end
end

#assert_received_minimum_span_count(list, minimum) ⇒ Object



291
292
293
# File 'lib/features/steps/trace_steps.rb', line 291

def assert_received_minimum_span_count(list, minimum)
  assert_received_spans(list, minimum)
end

#assert_received_ranged_span_count(list, minimum, maximum) ⇒ Object



295
296
297
# File 'lib/features/steps/trace_steps.rb', line 295

def assert_received_ranged_span_count(list, minimum, maximum)
  assert_received_spans(list, minimum, maximum)
end

#assert_received_requests(request_count, list, list_name, precise = true, maximum = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/features/steps/request_assertion_steps.rb', line 11

def assert_received_requests(request_count, list, list_name, precise = true, maximum = nil)
  timeout = Maze.config.receive_requests_wait
  # Interval set to 0.5s to make it more likely to detect erroneous extra requests,
  # without impacting overall speed too much
  wait = Maze::Wait.new(interval: 0.5, timeout: timeout)

  last_count = 0
  start_time = Time.now
  received = wait.until do

    count_now = list.size_remaining
    elapsed = Time.now - start_time
    if elapsed > Maze.config.receive_requests_slow_threshold
      if count_now > last_count
        $logger.warn "Received #{count_now - last_count} request(s) after #{elapsed.round(1)}s"
      end
    end
    last_count = count_now
    count_now >= request_count
  end

  unless received
    raise Test::Unit::AssertionFailedError.new <<-MESSAGE
    Expected #{request_count} #{list_name} but received #{list.size_remaining} within the #{timeout}s timeout.
    This could indicate that:
    - Bugsnag crashed with a fatal error.
    - Bugsnag did not make the requests that it should have done.
    - The requests were made, but not deemed to be valid (e.g. missing integrity header).
    - The requests made were prevented from being received due to a network or other infrastructure issue.
    Please check the Maze Runner and device logs to confirm.)
    MESSAGE
  end

  if precise
    Maze.check.equal(request_count, list.size_remaining, "#{list.size_remaining} #{list_name} received")
  else
    Maze.check.operator(request_count, :<=, list.size_remaining, "#{list.size_remaining} #{list_name} received")
    Maze.check.operator(maximum, :>=, list.size_remaining, "#{list.size_remaining} #{list_name} received") unless maximum.nil?
  end
end

#assert_received_span_count(list, count) ⇒ Object



287
288
289
# File 'lib/features/steps/trace_steps.rb', line 287

def assert_received_span_count(list, count)
  assert_received_spans(list, count, count)
end

#assert_received_spans(list, min_received, max_received = nil) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/features/steps/trace_steps.rb', line 299

def assert_received_spans(list, min_received, max_received = nil)
  timeout = Maze.config.receive_requests_wait
  wait = Maze::Wait.new(timeout: timeout)

  received = wait.until { spans_from_request_list(list).size >= min_received }
  received_count = spans_from_request_list(list).size

  unless received
    raise Test::Unit::AssertionFailedError.new <<-MESSAGE
    Expected #{min_received} spans but received #{received_count} within the #{timeout}s timeout.
    This could indicate that:
    - Bugsnag crashed with a fatal error.
    - Bugsnag did not make the requests that it should have done.
    - The requests were made, but not deemed to be valid (e.g. missing integrity header).
    - The requests made were prevented from being received due to a network or other infrastructure issue.
    Please check the Maze Runner and device logs to confirm.)
    MESSAGE
  end

  Maze.check.operator(max_received, :>=, received_count, "#{received_count} spans received") if max_received

  Maze::Schemas::Validator.validate_payload_elements(list, 'trace')
end

#attribute_value_matches?(attribute_value, expected_type, expected_value) ⇒ Boolean

Returns:

  • (Boolean)


261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/features/steps/trace_steps.rb', line 261

def attribute_value_matches?(attribute_value, expected_type, expected_value)
  # Check that the required value type key is present
  unless attribute_value.keys.include?(expected_type)
    return false
  end

  case expected_type
  when 'bytesValue', 'stringValue'
    expected_value.eql?(attribute_value[expected_type])
  when 'intValue'
    expected_value.to_i.eql?(attribute_value[expected_type].to_i)
  when 'doubleValue'
    expected_value.to_f.eql?(attribute_value[expected_type])
  when 'boolValue'
    expected_value.eql?('true').eql?(attribute_value[expected_type])
  when 'arrayValue'
    expected_value == attribute_value[expected_type]['values']
  when 'kvlistValue'
    $logger.error('Span attribute validation does not currently support the "kvlistValue" type')
    false
  else
    $logger.error("An invalid attribute type was expected: '#{expected_type}'")
    false
  end
end

#check_array_attribute_equal(field, attribute, expected_values) ⇒ Object



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/features/steps/trace_steps.rb', line 342

def check_array_attribute_equal(field, attribute, expected_values)
  actual_values = get_attribute_value(field, attribute, 'arrayValue')['values']

  # Convert string representations of integers to integers for comparison
  actual_values.map! do |value|
    if value.key?('intValue')
      value['intValue'].to_i
    else
      value
    end
  end

  expected_values.map! do |value|
    if value.key?('intValue')
      value['intValue'].to_i
    else
      value
    end
  end

  Maze.check.equal(expected_values, actual_values)
end

#check_attribute_equal(field, attribute, attr_type, expected) ⇒ Object



331
332
333
334
# File 'lib/features/steps/trace_steps.rb', line 331

def check_attribute_equal(field, attribute, attr_type, expected)
  actual = get_attribute_value field, attribute, attr_type
  Maze.check.equal(expected, actual)
end

#create_defaulting_generator(codes, default) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/features/steps/network_steps.rb', line 79

def create_defaulting_generator(codes, default)
  enumerator = Enumerator.new do |yielder|
    codes.each do |code|
      yielder.yield code
    end

    loop do
      yielder.yield default
    end
  end
  Maze::Generator.new enumerator
end

#get_attribute_value(field, attribute, attr_type) ⇒ Object



323
324
325
326
327
328
329
# File 'lib/features/steps/trace_steps.rb', line 323

def get_attribute_value(field, attribute, attr_type)
  list = Maze::Server.list_for 'trace'
  attributes = Maze::Helper.read_key_path list.current[:body], "#{field}.attributes"
  attribute = attributes.find { |a| a['key'] == attribute }
  value = attribute&.dig 'value', attr_type
  attr_type == 'intValue' && value.is_a?(String) ? value.to_i : value
end

#get_expected_platform_value(platform_values) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/features/steps/platform_dependent_steps.rb', line 154

def get_expected_platform_value(platform_values)
  os = Maze::Helper.get_current_platform
  expected_value = Hash[platform_values.raw][os.downcase]
  raise("There is no expected value for the current platform \"#{os}\"") if expected_value.nil?

  expected_value
end

#has_feature_flags?(event) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/features/steps/feature_flag_steps.rb', line 176

def has_feature_flags?(event)
  if event.has_key?('featureFlags')
    Maze.check.false(
      event['featureFlags'].nil?,
      'The feature flags key was present, but null'
    )
    Maze.check.true(
      event['featureFlags'].is_a?(Array),
      "The feature flags key was present, but the value: #{event['featureFlags']} must be an array"
    )
    !event['featureFlags'].empty?
  else
    false
  end
end

#match_string_platform_regexes(request_type, field_path, platform_values) ⇒ Object

Regex match routines



197
198
199
200
201
202
203
204
# File 'lib/features/steps/platform_dependent_steps.rb', line 197

def match_string_platform_regexes(request_type, field_path, platform_values)
  expected_regex = get_expected_platform_value(platform_values)
  return if should_skip_platform_check(expected_regex)
  list = Maze::Server.list_for(request_type)
  payload_value = Maze::Helper.read_key_path(list.current[:body], field_path)

  Maze.check.match(expected_regex, payload_value)
end

#output_received_requests(request_type) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/features/support/internal_hooks.rb', line 190

def output_received_requests(request_type)
  request_queue = Maze::Server.list_for(request_type)
  count = request_queue.size_all
  if count == 0
    $logger.info "No #{request_type} received"
  else
    $logger.info "#{count} #{request_type} were received:"
    request_queue.all.each.with_index(1) do |request, number|
      $stdout.puts "--- #{request_type} #{number} of #{count}"

      $logger.info 'Request body:'
      Maze::Loggers::LogUtil.log_hash(Logger::Severity::INFO, request[:body])

      $logger.info 'Request headers:'
      Maze::Loggers::LogUtil.log_hash(Logger::Severity::INFO, request[:request].header)

      $logger.info 'Request digests:'
      Maze::Loggers::LogUtil.log_hash(Logger::Severity::INFO, request[:digests])

      $logger.info "Response body: #{request[:response].body}"
      $logger.info 'Response headers:'
      Maze::Loggers::LogUtil.log_hash(Logger::Severity::INFO, request[:response].header)
    end
  end
end

#parse_multipart_body(body) ⇒ Hash

Takes a hashmap and parses all fields into strings or hashes depending on their format Used to convert a multipart/form-data request into a JSON comparable hash

Parameters:

  • body (Hash)

    The multipart/form-data hash to parse

Returns:

  • (Hash)

    The result of parsing hash fields to strings/JSON hashes



63
64
65
66
67
68
69
# File 'lib/features/steps/multipart_request_steps.rb', line 63

def parse_multipart_body(body)
  body.each_with_object({}) do |(k, v), out|
    out[k] = JSON.parse(v.to_s)
  rescue JSON::ParserError
    out[k] = v.to_s
  end
end

#sanitized(line) ⇒ Object



277
278
279
# File 'lib/features/steps/runner_steps.rb', line 277

def sanitized(line)
  line.sub "\e[?25h", '' # Make cursor visible
end

#should_skip_platform_check(expected_value) ⇒ Object



162
163
164
# File 'lib/features/steps/platform_dependent_steps.rb', line 162

def should_skip_platform_check(expected_value)
  expected_value.eql?('@skip')
end

#spans_from_request_list(list) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/features/steps/trace_steps.rb', line 253

def spans_from_request_list list
  return list.remaining
             .flat_map { |req| req[:body]['resourceSpans'] }
             .flat_map { |r| r['scopeSpans'] }
             .flat_map { |s| s['spans'] }
             .select { |s| !s.nil? }
end

#test_boolean_platform_values(request_type, field_path, platform_values) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/features/steps/platform_dependent_steps.rb', line 113

def test_boolean_platform_values(request_type, field_path, platform_values)
  expected_value = get_expected_platform_value(platform_values)
  return if should_skip_platform_check(expected_value)

  expected_bool = case expected_value.downcase
                  when 'true'
                    true
                  when 'false'
                    false
                  else
                    expected_value
                  end
  list = Maze::Server.list_for(request_type)
  payload_value = Maze::Helper.read_key_path(list.current[:body], field_path)
  assert_equal_with_nullability(expected_bool, payload_value)
end

#test_numeric_platform_values(request_type, field_path, platform_values) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/features/steps/platform_dependent_steps.rb', line 130

def test_numeric_platform_values(request_type, field_path, platform_values)
  expected_value = get_expected_platform_value(platform_values)
  return if should_skip_platform_check(expected_value)

  list = Maze::Server.list_for(request_type)
  payload_value = Maze::Helper.read_key_path(list.current[:body], field_path)

  # Need to do a little more processing here to allow floats
  special_value = expected_value.eql?('@null') || expected_value.eql?('@not_null')
  expectation = special_value ? expected_value : expected_value.to_f
  assert_equal_with_nullability(expectation, payload_value)
end

#test_string_platform_values(request_type, field_path, platform_values) ⇒ Object

Equality check routines



104
105
106
107
108
109
110
111
# File 'lib/features/steps/platform_dependent_steps.rb', line 104

def test_string_platform_values(request_type, field_path, platform_values)
  expected_value = get_expected_platform_value(platform_values)
  return if should_skip_platform_check(expected_value)

  list = Maze::Server.list_for(request_type)
  payload_value = Maze::Helper.read_key_path(list.current[:body], field_path)
  assert_equal_with_nullability(expected_value, payload_value)
end

#test_unhandled_state(event, unhandled, severity = nil) ⇒ Object

Tests whether an event has the correct attributes we’d expect for un/handled events

Parameters:

  • event (Integer)

    The index of the event

  • unhandled (Boolean)

    Whether the event is unhandled or handled

  • severity (String) (defaults to: nil)

    Optional. An overwritten severity to look for



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/features/steps/error_reporting_steps.rb', line 338

def test_unhandled_state(event, unhandled, severity = nil)
  expected_unhandled_state = unhandled ? 'true' : 'false'
  expected_severity = if severity
                        severity
                      elsif unhandled
                        'error'
                      else
                        'warning'
                      end
  steps %(
    Then the error payload field "events.#{event}.unhandled" is #{expected_unhandled_state}
    And the error payload field "events.#{event}.severity" equals "#{expected_severity}"
  )

  return if Maze::Helper.read_key_path(Maze::Server.errors.current[:body], "events.#{event}.session").nil?

  session_field = unhandled ? 'unhandled' : 'handled'
  steps %(
    And the error payload field "events.#{event}.session.events.#{session_field}" is greater than 0
  )
end

#valid_multipart_form_data?(request) ⇒ Boolean

Verifies a request contains the correct Content-Type header and some contents for a multipart/form-data request

Parameters:

  • request (Hash)

    The payload to test

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
# File 'lib/features/steps/multipart_request_steps.rb', line 12

def valid_multipart_form_data?(request)
  content_regex = Regexp.new('^multipart\\/form-data; boundary=[\\h-]+$')
  content_header = request[:request]['Content-Type']
  Maze.check.match(content_regex, content_header)
  Maze.check.true(
    request[:body].size.positive?,
    "Multipart request payload contained #{request[:body].size} fields"
  )
end

#validate_error_reporting_thread(payload_key, payload_value) ⇒ Object

Tests that a thread from the first event, identified by a particular key-value pair, is the error reporting thread.

Parameters:

  • payload_key (String)

    The thread identifier key

  • payload_value (Any)

    The thread identifier value



322
323
324
325
326
327
328
329
330
331
# File 'lib/features/steps/error_reporting_steps.rb', line 322

def validate_error_reporting_thread(payload_key, payload_value)
  threads = Maze::Server.errors.current[:body]['events'].first['threads']
  Maze.check.kind_of Array, threads
  count = 0

  threads.each do |thread|
    count += 1 if thread[payload_key].to_s == payload_value && thread['errorReportingThread'] == true
  end
  Maze.check.equal(1, count)
end

#verify_feature_flags_with_table(event, table) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/features/steps/feature_flag_steps.rb', line 139

def verify_feature_flags_with_table(event, table)
  Maze.check.true(
    has_feature_flags?(event),
    "Expected feature flags were not present in event: #{event}"
  )
  feature_flags = event['featureFlags']

  expected_features = table.hashes
  Maze.check.true(
    feature_flags.size == expected_features.size,
    "Expected #{expected_features.size} features,
    found #{feature_flags}"
  )
  expected_features.each do |expected|
    flag_name = expected['featureFlag']
    variant = expected['variant']
    # Test for flag name uniqueness
    Maze.check.true(
      feature_flags.one? { |flag| flag['featureFlag'].eql?(flag_name) },
      "Expected single flag with 'featureFlag' value: #{flag_name}. Present flags: #{feature_flags}"
    )
    flag = feature_flags.find { |flag| flag['featureFlag'].eql?(flag_name) }
    # Test the variant value
    if variant.nil? || expected['variant'].empty?
      Maze.check.false(
        flag.has_key?('variant'),
        "Feature flag: #{flag} expected to have no variant. All flags: #{feature_flags}"
      )
    else
      Maze.check.true(
        flag.has_key?('variant') && flag['variant'].eql?(variant),
        "Feature flag: #{flag} did not have variant: #{variant}. All flags: #{feature_flags}"
      )
    end
  end
end