Class: Datadog::CI::TestManagement::Component

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

Overview

Test management is a feature that lets people manage their flaky tests in Datadog. It includes:

  • marking test as quarantined causes test to continue running but not failing the build

  • marking test as disabled causes test to be skipped

  • marking test as “attempted to fix” causes test to be retried many times to confirm that fix worked

Constant Summary collapse

FILE_STORAGE_KEY =
"test_management_component_state"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Stateful

#load_cached_known_tests, #load_cached_settings, #load_cached_skippable_tests, #load_cached_test_management, #load_component_state, #store_component_state, #test_optimization_cache

Constructor Details

#initialize(enabled:, tests_properties_client:) ⇒ Component

Returns a new instance of Component.



24
25
26
27
28
29
# File 'lib/datadog/ci/test_management/component.rb', line 24

def initialize(enabled:, tests_properties_client:)
  @enabled = enabled

  @tests_properties_client = tests_properties_client
  @tests_properties = {}
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



22
23
24
# File 'lib/datadog/ci/test_management/component.rb', line 22

def enabled
  @enabled
end

#tests_propertiesObject (readonly)

Returns the value of attribute tests_properties.



22
23
24
# File 'lib/datadog/ci/test_management/component.rb', line 22

def tests_properties
  @tests_properties
end

Instance Method Details

#attempt_to_fix?(datadog_fqn_test_id) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
# File 'lib/datadog/ci/test_management/component.rb', line 67

def attempt_to_fix?(datadog_fqn_test_id)
  return false unless @enabled

  test_properties = @tests_properties[datadog_fqn_test_id]
  return false if test_properties.nil?

  test_properties.fetch("attempt_to_fix", false)
end

#configure(library_settings, test_session) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/datadog/ci/test_management/component.rb', line 31

def configure(library_settings, test_session)
  @enabled &&= library_settings.test_management_enabled?

  return unless @enabled

  test_session.set_tag(Ext::Test::TAG_TEST_MANAGEMENT_ENABLED, "true")

  return if load_component_state

  @tests_properties = @tests_properties_client.fetch(test_session)
  store_component_state if test_session.distributed

  Utils::Telemetry.distribution(
    Ext::Telemetry::METRIC_TEST_MANAGEMENT_TESTS_RESPONSE_TESTS,
    @tests_properties.count.to_f
  )
end

#disabled?(datadog_fqn_test_id) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
# File 'lib/datadog/ci/test_management/component.rb', line 76

def disabled?(datadog_fqn_test_id)
  return false unless @enabled

  test_properties = @tests_properties[datadog_fqn_test_id]
  return false if test_properties.nil?

  test_properties.fetch("disabled", false)
end

#restore_state(state) ⇒ Object



111
112
113
# File 'lib/datadog/ci/test_management/component.rb', line 111

def restore_state(state)
  @tests_properties = state[:tests_properties]
end

#restore_state_from_datadog_test_runnerObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/datadog/ci/test_management/component.rb', line 85

def restore_state_from_datadog_test_runner
  Datadog.logger.debug { "Restoring test management tests from Test Optimization cache" }

  test_management_data = load_cached_test_management
  if test_management_data.nil?
    Datadog.logger.debug { "Restoring test management tests failed, will request again" }
    return false
  end

  Datadog.logger.debug { "Restored test management tests from Test Optimization: #{test_management_data}" }

  tests_properties_response = TestsProperties::Response.from_json(test_management_data)
  @tests_properties = tests_properties_response.tests

  Datadog.logger.debug { "Found [#{@tests_properties.size}] test management tests from context" }

  true
end

#serialize_stateObject

Implementation of Stateful interface



105
106
107
108
109
# File 'lib/datadog/ci/test_management/component.rb', line 105

def serialize_state
  {
    tests_properties: @tests_properties
  }
end

#storage_keyObject



115
116
117
# File 'lib/datadog/ci/test_management/component.rb', line 115

def storage_key
  FILE_STORAGE_KEY
end

#tag_test_from_properties(test_span) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/datadog/ci/test_management/component.rb', line 49

def tag_test_from_properties(test_span)
  return unless @enabled

  datadog_test_id = Utils::TestRun.datadog_test_id(test_span.name, test_span.test_suite_name)
  test_properties = @tests_properties[datadog_test_id]

  if test_properties.nil?
    Datadog.logger.debug { "Test properties not found for test: #{datadog_test_id}" }
    return
  end

  Datadog.logger.debug { "Test properties for test #{datadog_test_id} are: [#{test_properties}]" }

  test_span.set_tag(Ext::Test::TAG_IS_QUARANTINED, "true") if test_properties["quarantined"]
  test_span.set_tag(Ext::Test::TAG_IS_TEST_DISABLED, "true") if test_properties["disabled"]
  test_span.set_tag(Ext::Test::TAG_IS_ATTEMPT_TO_FIX, "true") if test_properties["attempt_to_fix"]
end