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_component_state, #load_json, #store_component_state

Constructor Details

#initialize(enabled:, tests_properties_client:) ⇒ Component

Returns a new instance of Component.



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

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.



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

def enabled
  @enabled
end

#tests_propertiesObject (readonly)

Returns the value of attribute tests_properties.



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

def tests_properties
  @tests_properties
end

Instance Method Details

#attempt_to_fix?(datadog_fqn_test_id) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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 restore_state_from_datadog_test_runner
  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

#restore_state(state) ⇒ Object



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

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

#restore_state_from_datadog_test_runnerObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/datadog/ci/test_management/component.rb', line 78

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

  test_management_data = load_json(Ext::DDTest::TEST_MANAGEMENT_TESTS_FILE_NAME)
  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 DDTest: #{test_management_data}" }

  # Use the TestsProperties::Response class method to parse the JSON data
  # Wrap the data in the expected backend API format
  wrapped_data = {
    "data" => {
      "attributes" => test_management_data
    }
  }

  tests_properties_response = TestsProperties::Response.from_json(wrapped_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



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

def serialize_state
  {
    tests_properties: @tests_properties
  }
end

#storage_keyObject



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

def storage_key
  FILE_STORAGE_KEY
end

#tag_test_from_properties(test_span) ⇒ Object



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

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