Class: Datadog::CI::TestVisibility::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/ci/test_visibility/component.rb

Overview

Common behavior for CI tests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test_suite_level_visibility_enabled: false, codeowners: Codeowners::Parser.new(Git::LocalRepository.root).parse, logical_test_session_name: nil) ⇒ Component

Returns a new instance of Component.



23
24
25
26
27
28
29
30
31
32
# File 'lib/datadog/ci/test_visibility/component.rb', line 23

def initialize(
  test_suite_level_visibility_enabled: false,
  codeowners: Codeowners::Parser.new(Git::LocalRepository.root).parse,
  logical_test_session_name: nil
)
  @test_suite_level_visibility_enabled = test_suite_level_visibility_enabled
  @context = Context.new
  @codeowners = codeowners
  @logical_test_session_name = logical_test_session_name
end

Instance Attribute Details

#logical_test_session_nameObject (readonly)

Returns the value of attribute logical_test_session_name.



21
22
23
# File 'lib/datadog/ci/test_visibility/component.rb', line 21

def logical_test_session_name
  @logical_test_session_name
end

#test_suite_level_visibility_enabledObject (readonly)

Returns the value of attribute test_suite_level_visibility_enabled.



21
22
23
# File 'lib/datadog/ci/test_visibility/component.rb', line 21

def test_suite_level_visibility_enabled
  @test_suite_level_visibility_enabled
end

Instance Method Details

#active_spanObject



88
89
90
# File 'lib/datadog/ci/test_visibility/component.rb', line 88

def active_span
  @context.active_span
end

#active_testObject



92
93
94
# File 'lib/datadog/ci/test_visibility/component.rb', line 92

def active_test
  @context.active_test
end

#active_test_moduleObject



100
101
102
# File 'lib/datadog/ci/test_visibility/component.rb', line 100

def active_test_module
  @context.active_test_module
end

#active_test_sessionObject



96
97
98
# File 'lib/datadog/ci/test_visibility/component.rb', line 96

def active_test_session
  @context.active_test_session
end

#active_test_suite(test_suite_name) ⇒ Object



104
105
106
# File 'lib/datadog/ci/test_visibility/component.rb', line 104

def active_test_suite(test_suite_name)
  @context.active_test_suite(test_suite_name)
end

#deactivate_testObject



108
109
110
111
112
113
# File 'lib/datadog/ci/test_visibility/component.rb', line 108

def deactivate_test
  test = active_test
  on_test_finished(test) if test

  @context.deactivate_test
end

#deactivate_test_moduleObject



122
123
124
125
126
127
# File 'lib/datadog/ci/test_visibility/component.rb', line 122

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_sessionObject



115
116
117
118
119
120
# File 'lib/datadog/ci/test_visibility/component.rb', line 115

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



129
130
131
132
133
134
# File 'lib/datadog/ci/test_visibility/component.rb', line 129

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

Returns:

  • (Boolean)


136
137
138
# File 'lib/datadog/ci/test_visibility/component.rb', line 136

def itr_enabled?
  test_optimisation.enabled?
end

#shutdown!Object



140
141
142
# File 'lib/datadog/ci/test_visibility/component.rb', line 140

def shutdown!
  # noop, there is no thread owned by test visibility component
end

#start_test_module(test_module_name, service: nil, tags: {}) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/datadog/ci/test_visibility/component.rb', line 44

def start_test_module(test_module_name, service: nil, tags: {})
  return skip_tracing unless test_suite_level_visibility_enabled

  test_module = @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: {}, total_tests_count: 0) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/datadog/ci/test_visibility/component.rb', line 34

def start_test_session(service: nil, tags: {}, total_tests_count: 0)
  return skip_tracing unless test_suite_level_visibility_enabled

  test_session = @context.start_test_session(service: service, tags: tags)
  test_session.total_tests_count = total_tests_count

  on_test_session_started(test_session)
  test_session
end

#start_test_suite(test_suite_name, service: nil, tags: {}) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/datadog/ci/test_visibility/component.rb', line 52

def start_test_suite(test_suite_name, service: nil, tags: {})
  return skip_tracing unless test_suite_level_visibility_enabled

  test_suite = @context.start_test_suite(test_suite_name, service: service, tags: tags)
  on_test_suite_started(test_suite)
  test_suite
end

#trace(span_name, type: "span", tags: {}, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/datadog/ci/test_visibility/component.rb', line 78

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/datadog/ci/test_visibility/component.rb', line 60

def trace_test(test_name, test_suite_name, service: nil, tags: {}, &block)
  if block
    @context.trace_test(test_name, test_suite_name, 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_name, service: service, tags: tags)
    subscribe_to_after_stop_event(test.tracer_span)
    on_test_started(test)
    test
  end
end