Skip to content

Commit

Permalink
first stab on test module API
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 29, 2023
1 parent cd3e0d4 commit e54cd82
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/datadog/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ def active_test_session
recorder.active_test_session
end

def start_test_module(test_module_name, service_name: nil, tags: {})
recorder.start_test_module(test_module_name, service_name: service_name, tags: tags)
end

def active_test_module
recorder.active_test_module
end

# Return a {Datadog::CI::Test ci_test} that will trace a test called `test_name`.
# Raises an error if a test is already active.
# If there is an active test session, the new test will be connected to the session.
Expand Down Expand Up @@ -253,6 +261,11 @@ def deactivate_test_session
recorder.deactivate_test_session
end

# Internal only, to finish a test module use Datadog::CI::TestModule#finish
def deactivate_test_module
recorder.deactivate_test_module
end

private

def components
Expand Down
6 changes: 4 additions & 2 deletions lib/datadog/ci/ext/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ module Test
TAG_SKIP_REASON = "test.skip_reason" # DEV: Not populated yet
TAG_STATUS = "test.status"
TAG_SUITE = "test.suite"
TAG_MODULE = "test.module"
TAG_TRAITS = "test.traits"
TAG_TYPE = "test.type"
TAG_COMMAND = "test.command"

# those tags are special and they are used to conrrelate tests with the test sessions, suites, and modules
# those tags are special and they are used to correlate tests with the test sessions, suites, and modules
TAG_TEST_SESSION_ID = "_test.session_id"
SPECIAL_TAGS = [TAG_TEST_SESSION_ID].freeze
TAG_TEST_MODULE_ID = "_test.module_id"
SPECIAL_TAGS = [TAG_TEST_SESSION_ID, TAG_TEST_MODULE_ID].freeze

# tags that can be inherited from the test session
INHERITABLE_TAGS = [TAG_FRAMEWORK, TAG_FRAMEWORK_VERSION, TAG_TYPE].freeze
Expand Down
44 changes: 44 additions & 0 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ def start_test_session(service_name: nil, tags: {})
test_session
end

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

test_session = active_test_session
if test_session
service_name ||= test_session.service

tags = test_session.inheritable_tags.merge(tags)
end

span_options = {
service: service_name,
span_type: Ext::AppTypes::TYPE_TEST_MODULE
}

tracer_span = Datadog::Tracing.trace(test_module_name, **span_options)
trace = Datadog::Tracing.active_trace

set_trace_origin(trace)

tags[Ext::Test::TAG_TEST_SESSION_ID] = test_session.id if test_session
tags[Ext::Test::TAG_TEST_MODULE_ID] = tracer_span.id
tags[Ext::Test::TAG_MODULE] = test_module_name

test_module = build_test_module(tracer_span, tags)
@global_context.activate_test_module!(test_module)

test_module
end

# Creates a new span for a CI test
def trace_test(test_name, service_name: nil, operation_name: "test", tags: {}, &block)
return skip_tracing(block) unless enabled
Expand Down Expand Up @@ -130,6 +160,10 @@ def active_test_session
@global_context.active_test_session
end

def active_test_module
@global_context.active_test_module
end

# TODO: does it make sense to have a parameter here?
def deactivate_test(test)
@local_context.deactivate_test!(test)
Expand All @@ -139,6 +173,10 @@ def deactivate_test_session
@global_context.deactivate_test_session!
end

def deactivate_test_module
@global_context.deactivate_test_module!
end

private

def skip_tracing(block = nil)
Expand All @@ -160,6 +198,12 @@ def build_test_session(tracer_span, tags)
test_session
end

def build_test_module(tracer_span, tags)
test_module = TestModule.new(tracer_span)
set_initial_tags(test_module, tags)
test_module
end

def build_test(tracer_span, tags)
test = Test.new(tracer_span)
set_initial_tags(test, tags)
Expand Down
2 changes: 1 addition & 1 deletion lib/datadog/ci/test_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestModule < ConcurrentSpan
def finish
super

# CI.deactivate_test_module
CI.deactivate_test_module
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions sig/datadog/ci.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ module Datadog

def self.start_test_session: (?service_name: String?, ?tags: Hash[untyped, untyped]) -> Datadog::CI::Span

def self.start_test_module: (String test_module_name, ?service_name: String?, ?tags: Hash[untyped, untyped]) -> Datadog::CI::Span

def self.trace: (String span_type, String span_name, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Span span) -> untyped } -> untyped

def self.active_test_session: () -> Datadog::CI::TestSession?

def self.active_test_module: () -> Datadog::CI::TestModule?

def self.active_test: () -> Datadog::CI::Test?

def self.active_span: (String span_type) -> Datadog::CI::Span?
Expand All @@ -18,6 +22,8 @@ module Datadog

def self.deactivate_test_session: () -> void

def self.deactivate_test_module: () -> void

def self.components: () -> Datadog::CI::Configuration::Components

def self.recorder: () -> Datadog::CI::Recorder
Expand Down
4 changes: 4 additions & 0 deletions sig/datadog/ci/ext/test.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Datadog

TAG_SUITE: String

TAG_MODULE: String

TAG_TRAITS: String

TAG_TYPE: String
Expand All @@ -26,6 +28,8 @@ module Datadog

TAG_TEST_SESSION_ID: String

TAG_TEST_MODULE_ID: String

SPECIAL_TAGS: Array[String]

INHERITABLE_TAGS: Array[String]
Expand Down
8 changes: 8 additions & 0 deletions sig/datadog/ci/recorder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ module Datadog

def start_test_session: (?service_name: String?, ?tags: Hash[untyped, untyped]) -> Datadog::CI::Span

def start_test_module: (String test_module_name, ?service_name: String?, ?tags: Hash[untyped, untyped]) -> Datadog::CI::Span

def active_test_session: () -> Datadog::CI::TestSession?

def active_test_module: () -> Datadog::CI::TestModule?

def active_test: () -> Datadog::CI::Test?

def active_span: () -> Datadog::CI::Span?
Expand All @@ -30,6 +34,8 @@ module Datadog

def deactivate_test_session: () -> void

def deactivate_test_module: () -> void

def create_datadog_span: (String span_name, ?span_options: Hash[untyped, untyped], ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Span span) -> untyped } -> untyped

def set_trace_origin: (Datadog::Tracing::TraceOperation trace) -> untyped
Expand All @@ -40,6 +46,8 @@ module Datadog

def build_test_session: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::TestSession

def build_test_module: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::TestModule

def build_span: (Datadog::Tracing::SpanOperation tracer_span, Hash[untyped, untyped] tags) -> Datadog::CI::Span

def set_initial_tags: (Datadog::CI::Span ci_span, Hash[untyped, untyped] tags) -> void
Expand Down

0 comments on commit e54cd82

Please sign in to comment.