Class: Datadog::CI::TestOptimisation::Component
- Inherits:
-
Object
- Object
- Datadog::CI::TestOptimisation::Component
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
-
#code_coverage? ⇒ Boolean
-
#configure(remote_configuration, test_session) ⇒ Object
-
#enabled? ⇒ Boolean
-
#initialize(dd_env:, config_tags: {}, api: nil, coverage_writer: nil, enabled: false, bundle_location: nil, use_single_threaded_coverage: false, use_allocation_tracing: true) ⇒ Component
constructor
A new instance of Component.
-
#mark_if_skippable(test) ⇒ Object
-
#on_test_finished(test, context) ⇒ Object
-
#restore_state(state) ⇒ Object
-
#restore_state_from_datadog_test_runner ⇒ Object
-
#serialize_state ⇒ Object
Implementation of Stateful interface.
-
#shutdown! ⇒ Object
-
#skippable?(datadog_test_id) ⇒ Boolean
-
#skippable_tests_count ⇒ Object
-
#skipping_tests? ⇒ Boolean
-
#start_coverage(test) ⇒ Object
-
#stop_coverage(test) ⇒ Object
-
#storage_key ⇒ Object
-
#write_test_session_tags(test_session, skipped_tests_count) ⇒ Object
#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) ⇒ 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
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 35
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
)
@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
@test_skipping_enabled = false
@code_coverage_enabled = false
@coverage_writer = coverage_writer
@correlation_id = nil
@skippable_tests = Set.new
@mutex = Mutex.new
Datadog.logger.debug("TestOptimisation initialized with enabled: #{@enabled}")
end
|
Instance Attribute Details
#code_coverage_enabled ⇒ Object
Returns the value of attribute code_coverage_enabled.
32
33
34
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 32
def code_coverage_enabled
@code_coverage_enabled
end
|
#correlation_id ⇒ Object
Returns the value of attribute correlation_id.
32
33
34
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 32
def correlation_id
@correlation_id
end
|
#enabled ⇒ Object
Returns the value of attribute enabled.
32
33
34
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 32
def enabled
@enabled
end
|
#skippable_tests ⇒ Object
Returns the value of attribute skippable_tests.
32
33
34
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 32
def skippable_tests
@skippable_tests
end
|
#skippable_tests_fetch_error ⇒ Object
Returns the value of attribute skippable_tests_fetch_error.
32
33
34
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 32
def skippable_tests_fetch_error
@skippable_tests_fetch_error
end
|
#test_skipping_enabled ⇒ Object
Returns the value of attribute test_skipping_enabled.
32
33
34
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 32
def test_skipping_enabled
@test_skipping_enabled
end
|
Instance Method Details
#code_coverage? ⇒ Boolean
108
109
110
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 108
def code_coverage?
@code_coverage_enabled
end
|
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 71
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)
test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_TYPE, Ext::Test::ITR_TEST_SKIPPING_MODE)
load_datadog_cov! if @code_coverage_enabled
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
|
#enabled? ⇒ Boolean
100
101
102
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 100
def enabled?
@enabled
end
|
#mark_if_skippable(test) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 161
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_finished(test, context) ⇒ Object
173
174
175
176
177
178
179
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 173
def on_test_finished(test, context)
return if !test.skipped? || !test.skipped_by_test_impact_analysis?
Telemetry.itr_skipped
context.incr_tests_skipped_by_tia_count
end
|
#restore_state(state) ⇒ Object
207
208
209
210
211
212
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 207
def restore_state(state)
@mutex.synchronize do
@correlation_id = state[:correlation_id]
@skippable_tests = state[:skippable_tests]
end
end
|
#restore_state_from_datadog_test_runner ⇒ Object
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
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 218
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}" }
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_state ⇒ Object
Implementation of Stateful interface
200
201
202
203
204
205
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 200
def serialize_state
{
correlation_id: @correlation_id,
skippable_tests: @skippable_tests
}
end
|
#shutdown! ⇒ Object
195
196
197
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 195
def shutdown!
@coverage_writer&.stop
end
|
#skippable?(datadog_test_id) ⇒ Boolean
155
156
157
158
159
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 155
def skippable?(datadog_test_id)
return false if !enabled? || !skipping_tests?
@mutex.synchronize { @skippable_tests.include?(datadog_test_id) }
end
|
#skippable_tests_count ⇒ Object
191
192
193
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 191
def skippable_tests_count
skippable_tests.count
end
|
#skipping_tests? ⇒ Boolean
104
105
106
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 104
def skipping_tests?
@test_skipping_enabled
end
|
#start_coverage(test) ⇒ Object
112
113
114
115
116
117
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 112
def start_coverage(test)
return if !enabled? || !code_coverage?
Telemetry.code_coverage_started(test)
coverage_collector&.start
end
|
#stop_coverage(test) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 119
def stop_coverage(test)
return if !enabled? || !code_coverage?
Telemetry.code_coverage_finished(test)
coverage = coverage_collector&.stop
return if test.skipped?
if coverage.nil? || coverage.empty?
Telemetry.code_coverage_is_empty
return
end
test_source_file = test.source_file
ensure_test_source_covered(test_source_file, coverage) unless test_source_file.nil?
Telemetry.code_coverage_files(coverage.size)
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 #{event.pretty_inspect}" }
write(event)
event
end
|
#storage_key ⇒ Object
214
215
216
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 214
def storage_key
FILE_STORAGE_KEY
end
|
181
182
183
184
185
186
187
188
189
|
# File 'lib/datadog/ci/test_optimisation/component.rb', line 181
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
|