Class: Datadog::CI::TestVisibility::Component

Inherits:
Object
  • Object
show all
Includes:
Core::Utils::Forking, Utils::Stateful
Defined in:
lib/datadog/ci/test_visibility/component.rb

Overview

Core functionality of the library: tracing tests’ execution

Constant Summary collapse

FILE_STORAGE_KEY =
"test_visibility_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(known_tests_client:, test_suite_level_visibility_enabled: false, codeowners: Codeowners::Parser.new(Git::LocalRepository.root).parse, logical_test_session_name: nil, context_service_uri: nil) ⇒ Component



35
36
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
# File 'lib/datadog/ci/test_visibility/component.rb', line 35

def initialize(
  known_tests_client:,
  test_suite_level_visibility_enabled: false,
  codeowners: Codeowners::Parser.new(Git::LocalRepository.root).parse,
  logical_test_session_name: nil,
  context_service_uri: nil
)
  @test_suite_level_visibility_enabled = test_suite_level_visibility_enabled

  @context = Context.new(test_visibility_component: self)

  @codeowners = codeowners
  @logical_test_session_name = logical_test_session_name

  # "Known tests" feature fetches a list of all tests known to Datadog for this repository
  # and uses this list to determine if a test is new or not. New tests are marked with "test.is_new" tag.
  @known_tests_enabled = false
  @known_tests_client = known_tests_client
  @known_tests = Set.new

  # this is used for parallel test runners such as parallel_tests
  if context_service_uri
    @context_service_uri = context_service_uri
    @is_client_process = true
  end

  # This is used for parallel test runners such as parallel_tests.
  # If true, then test suites are created in the worker process, not the parent.
  #
  # The only test runner that requires creating test suites in the remote process is rails test runner because
  # it splits workload by test, not by test suite.
  #
  # Another test runner that splits workload by test is knapsack_pro, but we lack distributed test sessions/test suties
  # support for that one (as of 2025-03).
  @local_test_suites_mode = true
end

Instance Attribute Details

#context_service_uriObject (readonly)

Returns the value of attribute context_service_uri.



32
33
34
# File 'lib/datadog/ci/test_visibility/component.rb', line 32

def context_service_uri
  @context_service_uri
end

#known_testsObject (readonly)

Returns the value of attribute known_tests.



32
33
34
# File 'lib/datadog/ci/test_visibility/component.rb', line 32

def known_tests
  @known_tests
end

#known_tests_enabledObject (readonly)

Returns the value of attribute known_tests_enabled.



32
33
34
# File 'lib/datadog/ci/test_visibility/component.rb', line 32

def known_tests_enabled
  @known_tests_enabled
end

#local_test_suites_modeObject (readonly)

Returns the value of attribute local_test_suites_mode.



32
33
34
# File 'lib/datadog/ci/test_visibility/component.rb', line 32

def local_test_suites_mode
  @local_test_suites_mode
end

#logical_test_session_nameObject (readonly)

Returns the value of attribute logical_test_session_name.



32
33
34
# File 'lib/datadog/ci/test_visibility/component.rb', line 32

def logical_test_session_name
  @logical_test_session_name
end

#test_suite_level_visibility_enabledObject (readonly)

Returns the value of attribute test_suite_level_visibility_enabled.



32
33
34
# File 'lib/datadog/ci/test_visibility/component.rb', line 32

def test_suite_level_visibility_enabled
  @test_suite_level_visibility_enabled
end

Instance Method Details

#active_spanObject



146
147
148
# File 'lib/datadog/ci/test_visibility/component.rb', line 146

def active_span
  @context.active_span
end

#active_testObject



150
151
152
# File 'lib/datadog/ci/test_visibility/component.rb', line 150

def active_test
  @context.active_test
end

#active_test_moduleObject



158
159
160
# File 'lib/datadog/ci/test_visibility/component.rb', line 158

def active_test_module
  maybe_remote_context.active_test_module
end

#active_test_sessionObject



154
155
156
# File 'lib/datadog/ci/test_visibility/component.rb', line 154

def active_test_session
  maybe_remote_context.active_test_session
end

#active_test_suite(test_suite_name) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/datadog/ci/test_visibility/component.rb', line 162

def active_test_suite(test_suite_name)
  # when test_suite_name is not provided (from public API most likely)
  # we return the single active test suite because most of the time there is only one test suite running
  return single_active_test_suite if test_suite_name.nil?

  # when fetching test_suite to use as test's context, try local context instance first
  local_test_suite = @context.active_test_suite(test_suite_name)
  return local_test_suite if local_test_suite

  maybe_remote_context.active_test_suite(test_suite_name)
end

#client_process?Boolean



219
220
221
222
223
224
225
# File 'lib/datadog/ci/test_visibility/component.rb', line 219

def client_process?
  # We cannot assume here that every forked process is a client process
  # there are examples of custom test runners that run tests in forks but don't have a test session
  # started in the parent process.
  # So we need to check if the process is forked and if the context service URI is not empty.
  (forked? && !@context_service_uri.nil? && !@context_service_uri.empty?) || @is_client_process
end

#configure(library_configuration, test_session) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/datadog/ci/test_visibility/component.rb', line 72

def configure(library_configuration, test_session)
  return unless test_suite_level_visibility_enabled
  return unless library_configuration.known_tests_enabled?

  @known_tests_enabled = true
  return if load_component_state

  fetch_known_tests(test_session)
  store_component_state if test_session.distributed
end

#deactivate_testObject



174
175
176
177
178
179
# File 'lib/datadog/ci/test_visibility/component.rb', line 174

def deactivate_test
  test = active_test
  on_test_finished(test) if test

  @context.deactivate_test
end

#deactivate_test_moduleObject



188
189
190
191
192
193
# File 'lib/datadog/ci/test_visibility/component.rb', line 188

def deactivate_test_module
  test_module = active_test_module
  on_test_module_finished(test_module) if test_module

  @context.deactivate_test_module
end

#deactivate_test_sessionObject



181
182
183
184
185
186
# File 'lib/datadog/ci/test_visibility/component.rb', line 181

def deactivate_test_session
  test_session = active_test_session
  on_test_session_finished(test_session) if test_session

  @context.deactivate_test_session
end

#deactivate_test_suite(test_suite_name) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/datadog/ci/test_visibility/component.rb', line 195

def deactivate_test_suite(test_suite_name)
  test_suite = active_test_suite(test_suite_name)
  on_test_suite_finished(test_suite) if test_suite

  # deactivation always happens on the same process where test suite is located
  @context.deactivate_test_suite(test_suite_name)
end

#itr_enabled?Boolean



211
212
213
# File 'lib/datadog/ci/test_visibility/component.rb', line 211

def itr_enabled?
  test_optimisation.enabled?
end

#restore_state_from_datadog_test_runnerObject



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_visibility/component.rb', line 227

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

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

  Datadog.logger.debug { "Restored known tests from DDTest: #{known_tests_data}" }

  # Use the KnownTests class method to parse the JSON data
  known_tests_data = {
    "data" => {
      "attributes" => known_tests_data
    }
  }

  @known_tests = KnownTests::Response.from_json(known_tests_data).tests
  @known_tests_enabled = !@known_tests.empty?

  unless @known_tests_enabled
    Datadog.logger.debug("Empty set of known tests from the DDTest cache file")
  end

  Datadog.logger.debug { "Found [#{@known_tests.size}] known tests from context" }

  true
end

#shutdown!Object



215
216
217
# File 'lib/datadog/ci/test_visibility/component.rb', line 215

def shutdown!
  # noop, there is no thread owned by test visibility component
end

#start_test_module(test_module_name, service: nil, tags: {}) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/datadog/ci/test_visibility/component.rb', line 99

def start_test_module(test_module_name, service: nil, tags: {})
  return skip_tracing unless test_suite_level_visibility_enabled

  test_module = maybe_remote_context.start_test_module(test_module_name, service: service, tags: tags)
  on_test_module_started(test_module)

  test_module
end

#start_test_session(service: nil, tags: {}, estimated_total_tests_count: 0, distributed: false, local_test_suites_mode: true) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/datadog/ci/test_visibility/component.rb', line 83

def start_test_session(service: nil, tags: {}, estimated_total_tests_count: 0, distributed: false, local_test_suites_mode: true)
  return skip_tracing unless test_suite_level_visibility_enabled

  @local_test_suites_mode = local_test_suites_mode

  start_drb_service

  test_session = maybe_remote_context.start_test_session(service: service, tags: tags)
  test_session.estimated_total_tests_count = estimated_total_tests_count
  test_session.distributed = distributed

  on_test_session_started(test_session)

  test_session
end

#start_test_suite(test_suite_name, service: nil, tags: {}) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/datadog/ci/test_visibility/component.rb', line 108

def start_test_suite(test_suite_name, service: nil, tags: {})
  return skip_tracing unless test_suite_level_visibility_enabled

  context = @local_test_suites_mode ? @context : maybe_remote_context

  test_suite = context.start_test_suite(test_suite_name, service: service, tags: tags)
  on_test_suite_started(test_suite)
  test_suite
end

#tests_skipped_by_tia_countObject



207
208
209
# File 'lib/datadog/ci/test_visibility/component.rb', line 207

def tests_skipped_by_tia_count
  maybe_remote_context.tests_skipped_by_tia_count
end

#total_tests_countObject



203
204
205
# File 'lib/datadog/ci/test_visibility/component.rb', line 203

def total_tests_count
  maybe_remote_context.total_tests_count
end

#trace(span_name, type: "span", tags: {}, &block) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/datadog/ci/test_visibility/component.rb', line 136

def trace(span_name, type: "span", tags: {}, &block)
  if block
    @context.trace(span_name, type: type, tags: tags) do |span|
      block.call(span)
    end
  else
    @context.trace(span_name, type: type, tags: tags)
  end
end

#trace_test(test_name, test_suite_name, service: nil, tags: {}, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/datadog/ci/test_visibility/component.rb', line 118

def trace_test(test_name, test_suite_name, service: nil, tags: {}, &block)
  test_suite = active_test_suite(test_suite_name)
  tags[Ext::Test::TAG_SUITE] ||= test_suite_name

  if block
    @context.trace_test(test_name, test_suite, service: service, tags: tags) do |test|
      on_test_started(test)
      res = block.call(test)
      on_test_finished(test)
      res
    end
  else
    test = @context.trace_test(test_name, test_suite, service: service, tags: tags)
    on_test_started(test)
    test
  end
end