Class: Datadog::CI::TestOptimisation::Component

Inherits:
Object
  • Object
show all
Includes:
Utils::Stateful
Defined in:
lib/datadog/ci/test_optimisation/component.rb

Overview

Test Impact Analysis implementation Integrates with backend to provide test impact analysis data and skip tests that are not impacted by the changes

Constant Summary collapse

FILE_STORAGE_KEY =
"test_optimisation_component_state"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Stateful

#load_component_state, #load_json, #store_component_state

Constructor Details

#initialize(dd_env:, config_tags: {}, api: nil, coverage_writer: nil, enabled: false, bundle_location: nil, use_single_threaded_coverage: false, use_allocation_tracing: true, static_dependencies_tracking_enabled: false) ⇒ Component

Returns a new instance of Component.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/datadog/ci/test_optimisation/component.rb', line 37

def initialize(
  dd_env:,
  config_tags: {},
  api: nil,
  coverage_writer: nil,
  enabled: false,
  bundle_location: nil,
  use_single_threaded_coverage: false,
  use_allocation_tracing: true,
  static_dependencies_tracking_enabled: false
)
  @enabled = enabled
  @api = api
  @dd_env = dd_env
  @config_tags = config_tags || {}

  @bundle_location = if bundle_location && !File.absolute_path?(bundle_location)
    File.join(Git::LocalRepository.root, bundle_location)
  else
    bundle_location
  end
  @use_single_threaded_coverage = use_single_threaded_coverage
  @use_allocation_tracing = use_allocation_tracing
  @static_dependencies_tracking_enabled = static_dependencies_tracking_enabled

  @test_skipping_enabled = false
  @code_coverage_enabled = false

  @coverage_writer = coverage_writer

  @correlation_id = nil
  @skippable_tests = Set.new

  @mutex = Mutex.new

  # Context coverage: stores coverage collected during before(:context)/before(:all) hooks
  # keyed by context_id (e.g., RSpec scoped_id for example groups)
  # Only used when use_single_threaded_coverage is false (multi-threaded mode)
  @context_coverages = {}
  @context_coverages_mutex = Mutex.new

  # Currently active context ID for context coverage collection
  @current_context_id = nil
  @current_context_id_mutex = Mutex.new

  Datadog.logger.debug("TestOptimisation initialized with enabled: #{@enabled}")
end

Instance Attribute Details

#code_coverage_enabledObject (readonly)

Returns the value of attribute code_coverage_enabled.



34
35
36
# File 'lib/datadog/ci/test_optimisation/component.rb', line 34

def code_coverage_enabled
  @code_coverage_enabled
end

#correlation_idObject (readonly)

Returns the value of attribute correlation_id.



34
35
36
# File 'lib/datadog/ci/test_optimisation/component.rb', line 34

def correlation_id
  @correlation_id
end

#enabledObject (readonly)

Returns the value of attribute enabled.



34
35
36
# File 'lib/datadog/ci/test_optimisation/component.rb', line 34

def enabled
  @enabled
end

#skippable_testsObject (readonly)

Returns the value of attribute skippable_tests.



34
35
36
# File 'lib/datadog/ci/test_optimisation/component.rb', line 34

def skippable_tests
  @skippable_tests
end

#skippable_tests_fetch_errorObject (readonly)

Returns the value of attribute skippable_tests_fetch_error.



34
35
36
# File 'lib/datadog/ci/test_optimisation/component.rb', line 34

def skippable_tests_fetch_error
  @skippable_tests_fetch_error
end

#test_skipping_enabledObject (readonly)

Returns the value of attribute test_skipping_enabled.



34
35
36
# File 'lib/datadog/ci/test_optimisation/component.rb', line 34

def test_skipping_enabled
  @test_skipping_enabled
end

Instance Method Details

#clear_context_coverage(context_id) ⇒ void

This method returns an undefined value.

Clears stored context coverage for a specific context. Should be called when a context finishes (e.g., after(:context) completes).

Parameters:

  • context_id (String)

    The context ID to clear



262
263
264
265
266
267
268
269
270
# File 'lib/datadog/ci/test_optimisation/component.rb', line 262

def clear_context_coverage(context_id)
  return unless context_coverage_enabled?

  @context_coverages_mutex.synchronize do
    @context_coverages.delete(context_id)

    Datadog.logger.debug { "Cleared context coverage for [#{context_id}]" }
  end
end

#code_coverage?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/datadog/ci/test_optimisation/component.rb', line 126

def code_coverage?
  @code_coverage_enabled
end

#configure(remote_configuration, test_session) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/datadog/ci/test_optimisation/component.rb', line 85

def configure(remote_configuration, test_session)
  return unless enabled?

  Datadog.logger.debug("Configuring TestOptimisation with remote configuration: #{remote_configuration}")

  @enabled = remote_configuration.itr_enabled?
  @test_skipping_enabled = @enabled && remote_configuration.tests_skipping_enabled?
  @code_coverage_enabled = @enabled && remote_configuration.code_coverage_enabled?

  test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_ENABLED, @test_skipping_enabled)
  test_session.set_tag(Ext::Test::TAG_CODE_COVERAGE_ENABLED, @code_coverage_enabled)
  # we skip tests, not suites
  test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_TYPE, Ext::Test::ITR_TEST_SKIPPING_MODE)

  if @code_coverage_enabled
    load_datadog_cov!

    populate_static_dependencies_map!
  end

  # Load component state first, and if successful, skip fetching skippable tests
  # Also try to restore from DDTest cache if available
  if skipping_tests?
    return if load_component_state
    return if restore_state_from_datadog_test_runner

    fetch_skippable_tests(test_session)
    store_component_state if test_session.distributed
  end

  Datadog.logger.debug("Configured TestOptimisation with enabled: #{@enabled}, skipping_tests: #{@test_skipping_enabled}, code_coverage: #{@code_coverage_enabled}")
end

#context_coverage_enabled?Boolean

Returns whether context coverage collection is enabled. Context coverage is disabled in single-threaded mode.

Returns:

  • (Boolean)


276
277
278
# File 'lib/datadog/ci/test_optimisation/component.rb', line 276

def context_coverage_enabled?
  enabled? && code_coverage? && !@use_single_threaded_coverage
end

#enabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/datadog/ci/test_optimisation/component.rb', line 118

def enabled?
  @enabled
end

#mark_if_skippable(test) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/datadog/ci/test_optimisation/component.rb', line 286

def mark_if_skippable(test)
  return if !enabled? || !skipping_tests?

  if skippable?(test.datadog_test_id) && !test.attempt_to_fix?
    test.set_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR, "true")

    Datadog.logger.debug { "Marked test as skippable: #{test.datadog_test_id}" }
  else
    Datadog.logger.debug { "Test is not skippable: #{test.datadog_test_id}" }
  end
end

#on_test_context_started(context_id) ⇒ void

This method returns an undefined value.

Called when a test context (e.g., RSpec example group with before(:context)) starts. Starts collecting coverage that will be merged into all tests within this context.

Parameters:

  • context_id (String)

    A stable identifier for the context (e.g., RSpec scoped_id)



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/datadog/ci/test_optimisation/component.rb', line 155

def on_test_context_started(context_id)
  return unless context_coverage_enabled?

  # Stop and store any existing context coverage before starting new one.
  # This ensures that outer context coverage is preserved when nested contexts start.
  stop_context_coverage_and_store

  Datadog.logger.debug { "Starting context coverage collection for context [#{context_id}]" }

  # Store the context_id we're collecting for
  @current_context_id_mutex.synchronize do
    @current_context_id = context_id
  end

  coverage_collector&.start
end

#on_test_finished(test, context) ⇒ Datadog::CI::TestOptimisation::Coverage::Event?

Called when a test finishes. This method:

  1. Stops test coverage collection

  2. Merges context coverage from all relevant contexts

  3. Writes the combined coverage event

  4. Records ITR statistics if test was skipped by TIA

Parameters:

Returns:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/datadog/ci/test_optimisation/component.rb', line 204

def on_test_finished(test, context)
  return unless enabled?

  # Handle ITR statistics
  if test.skipped_by_test_impact_analysis?
    Telemetry.itr_skipped

    context.incr_tests_skipped_by_tia_count
  end

  # Handle code coverage
  return unless code_coverage?
  Telemetry.code_coverage_finished(test)

  coverage = coverage_collector&.stop

  # if test was skipped, we discard coverage data
  return if test.skipped?
  coverage ||= {}

  # Merge context coverage from all relevant contexts
  context_ids = test.context_ids || []
  merge_context_coverages_into_test(coverage, context_ids)

  if coverage.empty?
    Telemetry.code_coverage_is_empty
    return
  end

  # cucumber's gherkin files are not covered by the code coverage collector - we add them here explicitly
  test_source_file = test.source_file
  ensure_test_source_covered(test_source_file, coverage) unless test_source_file.nil?

  # if we have static dependencies tracking enabled then we can make the coverage
  # more precise by fetching which files we depend on based on constants usage
  enrich_coverage_with_static_dependencies(coverage)

  Telemetry.code_coverage_files(coverage.size)

  coverage_event = Coverage::Event.new(
    test_id: test.id.to_s,
    test_suite_id: test.test_suite_id.to_s,
    test_session_id: test.test_session_id.to_s,
    coverage: coverage
  )

  Datadog.logger.debug { "Writing coverage event \n #{coverage_event.pretty_inspect}" }

  write(coverage_event)

  coverage_event
end

#on_test_started(test) ⇒ void

This method returns an undefined value.

Called when a test starts within a context. This method:

  1. Stops any in-progress context coverage collection and stores it

  2. Starts coverage collection for the test itself

Parameters:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/datadog/ci/test_optimisation/component.rb', line 178

def on_test_started(test)
  return if !enabled? || !code_coverage?

  # Stop any in-progress context coverage and store it
  stop_context_coverage_and_store

  Telemetry.code_coverage_started(test)

  context_ids = test.context_ids || []

  Datadog.logger.debug do
    "Starting test coverage for [#{test.name}] with context chain: #{context_ids.inspect}"
  end

  coverage_collector&.start
end

#restore_state(state) ⇒ Object



324
325
326
327
328
329
# File 'lib/datadog/ci/test_optimisation/component.rb', line 324

def restore_state(state)
  @mutex.synchronize do
    @correlation_id = state[:correlation_id]
    @skippable_tests = state[:skippable_tests]
  end
end

#restore_state_from_datadog_test_runnerObject



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/datadog/ci/test_optimisation/component.rb', line 335

def restore_state_from_datadog_test_runner
  Datadog.logger.debug { "Restoring skippable tests from DDTest cache" }

  skippable_tests_data = load_json(Ext::DDTest::SKIPPABLE_TESTS_FILE_NAME)
  if skippable_tests_data.nil?
    Datadog.logger.debug { "Restoring skippable tests failed, will request again" }
    return false
  end

  Datadog.logger.debug { "Restored skippable tests from DDTest: #{skippable_tests_data}" }

  transformed_data = transform_test_runner_data(skippable_tests_data)

  Datadog.logger.debug { "Skippable tests after transformation: #{transformed_data}" }

  # Use the Skippable::Response class to parse the transformed data
  skippable_response = Skippable::Response.from_json(transformed_data)

  @mutex.synchronize do
    @correlation_id = skippable_response.correlation_id
    @skippable_tests = skippable_response.tests
  end

  Datadog.logger.debug { "Found [#{@skippable_tests.size}] skippable tests from context" }
  Datadog.logger.debug { "ITR correlation ID from context: #{@correlation_id}" }

  true
end

#serialize_stateObject

Implementation of Stateful interface



317
318
319
320
321
322
# File 'lib/datadog/ci/test_optimisation/component.rb', line 317

def serialize_state
  {
    correlation_id: @correlation_id,
    skippable_tests: @skippable_tests
  }
end

#shutdown!Object



312
313
314
# File 'lib/datadog/ci/test_optimisation/component.rb', line 312

def shutdown!
  @coverage_writer&.stop
end

#skippable?(datadog_test_id) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
283
284
# File 'lib/datadog/ci/test_optimisation/component.rb', line 280

def skippable?(datadog_test_id)
  return false if !enabled? || !skipping_tests?

  @mutex.synchronize { @skippable_tests.include?(datadog_test_id) }
end

#skippable_tests_countObject



308
309
310
# File 'lib/datadog/ci/test_optimisation/component.rb', line 308

def skippable_tests_count
  skippable_tests.count
end

#skipping_tests?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/datadog/ci/test_optimisation/component.rb', line 122

def skipping_tests?
  @test_skipping_enabled
end

#start_coveragevoid

This method returns an undefined value.

Starts coverage collection. This is a low-level method that only starts the collector.



134
135
136
137
138
# File 'lib/datadog/ci/test_optimisation/component.rb', line 134

def start_coverage
  return if !enabled? || !code_coverage?

  coverage_collector&.start
end

#stop_coverageHash?

Stops coverage collection and returns raw coverage data. This is a low-level method that only stops the collector.

Returns:

  • (Hash, nil)

    Raw coverage data or nil



144
145
146
147
148
# File 'lib/datadog/ci/test_optimisation/component.rb', line 144

def stop_coverage
  return if !enabled? || !code_coverage?

  coverage_collector&.stop
end

#storage_keyObject



331
332
333
# File 'lib/datadog/ci/test_optimisation/component.rb', line 331

def storage_key
  FILE_STORAGE_KEY
end

#write_test_session_tags(test_session, skipped_tests_count) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'lib/datadog/ci/test_optimisation/component.rb', line 298

def write_test_session_tags(test_session, skipped_tests_count)
  return if !enabled?

  Datadog.logger.debug { "Finished optimised session with test skipping enabled: #{@test_skipping_enabled}" }
  Datadog.logger.debug { "#{skipped_tests_count} tests were skipped" }

  test_session.set_tag(Ext::Test::TAG_ITR_TESTS_SKIPPED, skipped_tests_count.positive?.to_s)
  test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_COUNT, skipped_tests_count)
end