Class: Datadog::CI::TestVisibility::Component
- Inherits:
-
Object
- Object
- Datadog::CI::TestVisibility::Component
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
-
#active_span ⇒ Object
-
#active_test ⇒ Object
-
#active_test_module ⇒ Object
-
#active_test_session ⇒ Object
-
#active_test_suite(test_suite_name) ⇒ Object
-
#client_process? ⇒ Boolean
-
#configure(library_configuration, test_session) ⇒ Object
-
#deactivate_test ⇒ Object
-
#deactivate_test_module ⇒ Object
-
#deactivate_test_session ⇒ Object
-
#deactivate_test_suite(test_suite_name) ⇒ Object
-
#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
constructor
A new instance of Component.
-
#itr_enabled? ⇒ Boolean
-
#restore_state_from_datadog_test_runner ⇒ Object
-
#shutdown! ⇒ Object
-
#start_test_module(test_module_name, service: nil, tags: {}) ⇒ Object
-
#start_test_session(service: nil, tags: {}, estimated_total_tests_count: 0, distributed: false, local_test_suites_mode: true) ⇒ Object
-
#start_test_suite(test_suite_name, service: nil, tags: {}) ⇒ Object
-
#tests_skipped_by_tia_count ⇒ Object
-
#total_tests_count ⇒ Object
-
#trace(span_name, type: "span", tags: {}, &block) ⇒ Object
-
#trace_test(test_name, test_suite_name, service: nil, tags: {}, &block) ⇒ Object
#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
Returns a new instance of 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_enabled = false
@known_tests_client = known_tests_client
@known_tests = Set.new
if context_service_uri
@context_service_uri = context_service_uri
@is_client_process = true
end
@local_test_suites_mode = true
end
|
Instance Attribute Details
#context_service_uri ⇒ Object
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_tests ⇒ Object
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_enabled ⇒ Object
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_mode ⇒ Object
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_name ⇒ Object
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_enabled ⇒ Object
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_span ⇒ Object
149
150
151
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 149
def active_span
@context.active_span
end
|
#active_test ⇒ Object
153
154
155
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 153
def active_test
@context.active_test
end
|
#active_test_module ⇒ Object
161
162
163
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 161
def active_test_module
maybe_remote_context.active_test_module
end
|
#active_test_session ⇒ Object
157
158
159
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 157
def active_test_session
maybe_remote_context.active_test_session
end
|
#active_test_suite(test_suite_name) ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 165
def active_test_suite(test_suite_name)
return single_active_test_suite if test_suite_name.nil?
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
222
223
224
225
226
227
228
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 222
def client_process?
(forked? && !@context_service_uri.nil? && !@context_service_uri.empty?) || @is_client_process
end
|
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_test ⇒ Object
177
178
179
180
181
182
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 177
def deactivate_test
test = active_test
on_test_finished(test) if test
@context.deactivate_test
end
|
#deactivate_test_module ⇒ Object
191
192
193
194
195
196
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 191
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_session ⇒ Object
184
185
186
187
188
189
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 184
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
198
199
200
201
202
203
204
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 198
def deactivate_test_suite(test_suite_name)
test_suite = active_test_suite(test_suite_name)
on_test_suite_finished(test_suite) if test_suite
@context.deactivate_test_suite(test_suite_name)
end
|
#itr_enabled? ⇒ Boolean
214
215
216
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 214
def itr_enabled?
test_optimisation.enabled?
end
|
#restore_state_from_datadog_test_runner ⇒ Object
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
256
257
258
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 230
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}" }
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
218
219
220
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 218
def shutdown!
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_count ⇒ Object
210
211
212
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 210
def tests_skipped_by_tia_count
maybe_remote_context.tests_skipped_by_tia_count
end
|
#total_tests_count ⇒ Object
206
207
208
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 206
def total_tests_count
maybe_remote_context.total_tests_count
end
|
#trace(span_name, type: "span", tags: {}, &block) ⇒ Object
139
140
141
142
143
144
145
146
147
|
# File 'lib/datadog/ci/test_visibility/component.rb', line 139
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
135
136
137
|
# 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|
subscribe_to_after_stop_event(test.tracer_span)
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)
subscribe_to_after_stop_event(test.tracer_span)
on_test_started(test)
test
end
end
|