Class: Datadog::CI::ITR::Runner

Inherits:
Object
  • Object
show all
Includes:
Core::Utils::Forking
Defined in:
lib/datadog/ci/itr/runner.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dd_env:, api: nil, coverage_writer: nil, enabled: false) ⇒ Runner

Returns a new instance of Runner.



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/datadog/ci/itr/runner.rb', line 28

def initialize(
  dd_env:,
  api: nil,
  coverage_writer: nil,
  enabled: false
)
  @enabled = enabled
  @api = api
  @dd_env = dd_env

  @test_skipping_enabled = false
  @code_coverage_enabled = false

  @coverage_writer = coverage_writer

  @correlation_id = nil
  @skippable_tests = []

  @skipped_tests_count = 0
  @mutex = Mutex.new

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

Instance Attribute Details

#correlation_idObject (readonly)

Returns the value of attribute correlation_id.



26
27
28
# File 'lib/datadog/ci/itr/runner.rb', line 26

def correlation_id
  @correlation_id
end

#skippable_testsObject (readonly)

Returns the value of attribute skippable_tests.



26
27
28
# File 'lib/datadog/ci/itr/runner.rb', line 26

def skippable_tests
  @skippable_tests
end

#skipped_tests_countObject (readonly)

Returns the value of attribute skipped_tests_count.



26
27
28
# File 'lib/datadog/ci/itr/runner.rb', line 26

def skipped_tests_count
  @skipped_tests_count
end

Instance Method Details

#code_coverage?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/datadog/ci/itr/runner.rb', line 88

def code_coverage?
  @code_coverage_enabled
end

#configure(remote_configuration, test_session:, git_tree_upload_worker:) ⇒ Object



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
# File 'lib/datadog/ci/itr/runner.rb', line 52

def configure(remote_configuration, test_session:, git_tree_upload_worker:)
  Datadog.logger.debug("Configuring ITR Runner with remote configuration: #{remote_configuration}")

  @enabled = Utils::Parsing.convert_to_bool(
    remote_configuration.fetch(Ext::Transport::DD_API_SETTINGS_RESPONSE_ITR_ENABLED_KEY, false)
  )
  @test_skipping_enabled = @enabled && Utils::Parsing.convert_to_bool(
    remote_configuration.fetch(Ext::Transport::DD_API_SETTINGS_RESPONSE_TESTS_SKIPPING_KEY, false)
  )
  @code_coverage_enabled = @enabled && Utils::Parsing.convert_to_bool(
    remote_configuration.fetch(Ext::Transport::DD_API_SETTINGS_RESPONSE_CODE_COVERAGE_KEY, false)
  )

  test_session.set_tag(Ext::Test::TAG_ITR_TEST_SKIPPING_ENABLED, @test_skipping_enabled)
  # currently we set this tag when ITR requires collecting code coverage
  # this will change as soon as we implement total code coverage support in this library
  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)

  load_datadog_cov! if @code_coverage_enabled

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

  fetch_skippable_tests(test_session: test_session, git_tree_upload_worker: git_tree_upload_worker)
end

#count_skipped_test(test) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/datadog/ci/itr/runner.rb', line 138

def count_skipped_test(test)
  if forked?
    Datadog.logger.warn { "ITR is not supported for forking test runners yet" }
    return
  end

  return if !test.skipped? || !test.skipped_by_itr?

  @mutex.synchronize do
    @skipped_tests_count += 1
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/datadog/ci/itr/runner.rb', line 80

def enabled?
  @enabled
end

#mark_if_skippable(test) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/datadog/ci/itr/runner.rb', line 125

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

  skippable_test_id = Utils::TestRun.skippable_test_id(test.name, test.test_suite_name, test.parameters)
  if @skippable_tests.include?(skippable_test_id)
    test.set_tag(Ext::Test::TAG_ITR_SKIPPED_BY_ITR, "true")

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

#shutdown!Object



158
159
160
# File 'lib/datadog/ci/itr/runner.rb', line 158

def shutdown!
  @coverage_writer&.stop
end

#skipping_tests?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/datadog/ci/itr/runner.rb', line 84

def skipping_tests?
  @test_skipping_enabled
end

#start_coverage(test) ⇒ Object



92
93
94
95
96
# File 'lib/datadog/ci/itr/runner.rb', line 92

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

  coverage_collector&.start
end

#stop_coverage(test) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/datadog/ci/itr/runner.rb', line 98

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

  coverage = coverage_collector&.stop
  return if coverage.nil? || coverage.empty?

  return if test.skipped?

  test_source_file = test.source_file

  # cucumber's gherkin files are not covered by the code coverage collector
  ensure_test_source_covered(test_source_file, coverage) unless test_source_file.nil?

  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

#write_test_session_tags(test_session) ⇒ Object



151
152
153
154
155
156
# File 'lib/datadog/ci/itr/runner.rb', line 151

def write_test_session_tags(test_session)
  return if !enabled?

  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