Skip to content

Commit

Permalink
add experimental_test_suite_level_visibility_enabled config
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 23, 2023
1 parent 840b678 commit a0c4ecd
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/ci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class << self
# Return a {Datadog::CI::TestSesstion ci_test_session} that represents the whole test session run.
# Raises an error if a session is already active.
#
#
# The {#start_test_session} method is used to mark the start of the test session:
# ```
# Datadog::CI.start_test_session(
Expand All @@ -30,6 +29,7 @@ class << self
# @param [String] service_name the service name for this session
# @param [Hash<String,String>] tags extra tags which should be added to the test.
# @return [Datadog::CI::TestSession] returns the active, running {Datadog::CI::TestSession}.
# @return [nil] if test suite level visibility is disabled (old Datadog agent detected)
#
# @public_api
def start_test_session(service_name: nil, tags: {})
Expand Down
4 changes: 3 additions & 1 deletion lib/datadog/ci/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def activate_ci!(settings)

settings.tracing.test_mode.writer_options = writer_options

@ci_recorder = Recorder.new
@ci_recorder = Recorder.new(
test_suite_level_visibility_enabled: settings.ci.experimental_test_suite_level_visibility_enabled
)
end

def can_use_evp_proxy?(settings, agent_settings)
Expand Down
6 changes: 6 additions & 0 deletions lib/datadog/ci/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def self.add_settings!(base)
o.env CI::Ext::Settings::ENV_AGENTLESS_URL
end

option :experimental_test_suite_level_visibility_enabled do |o|
o.type :bool
o.env CI::Ext::Settings::ENV_EXPERIMENTAL_TEST_SUITE_LEVEL_VISIBILITY_ENABLED
o.default false
end

define_method(:instrument) do |integration_name, options = {}, &block|
return unless enabled

Expand Down
1 change: 1 addition & 0 deletions lib/datadog/ci/ext/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Settings
ENV_MODE_ENABLED = "DD_TRACE_CI_ENABLED"
ENV_AGENTLESS_MODE_ENABLED = "DD_CIVISIBILITY_AGENTLESS_ENABLED"
ENV_AGENTLESS_URL = "DD_CIVISIBILITY_AGENTLESS_URL"
ENV_EXPERIMENTAL_TEST_SUITE_LEVEL_VISIBILITY_ENABLED = "DD_CIVISIBILITY_EXPERIMENTAL_TEST_SUITE_LEVEL_VISIBILITY_ENABLED"
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ module Datadog
module CI
# Common behavior for CI tests
class Recorder
attr_reader :environment_tags
attr_reader :environment_tags, :test_suite_level_visibility_enabled

def initialize(test_suite_level_visibility_enabled: false)
@test_suite_level_visibility_enabled = test_suite_level_visibility_enabled

def initialize
@environment_tags = Ext::Environment.tags(ENV).freeze
@local_context = Context::Local.new
@global_context = Context::Global.new
end

def start_test_session(service_name: nil, tags: {})
return nil unless @test_suite_level_visibility_enabled

span_options = {
service: service_name,
span_type: Ext::AppTypes::TYPE_TEST_SESSION
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Datadog

def self.start_test: (String span_name, ?service_name: String?, ?operation_name: String, ?tags: Hash[untyped, untyped]) -> Datadog::CI::Test

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

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

Expand Down
1 change: 1 addition & 0 deletions sig/datadog/ci/ext/settings.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Datadog
ENV_MODE_ENABLED: String
ENV_AGENTLESS_MODE_ENABLED: String
ENV_AGENTLESS_URL: String
ENV_EXPERIMENTAL_TEST_SUITE_LEVEL_VISIBILITY_ENABLED: String
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion sig/datadog/ci/recorder.rbs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
module Datadog
module CI
class Recorder
@test_suite_level_visibility_enabled: bool
@environment_tags: Hash[String, String]
@local_context: Datadog::CI::Context::Local
@global_context: Datadog::CI::Context::Global

attr_reader environment_tags: Hash[String, String]

def initialize: (?test_suite_level_visibility_enabled: bool) -> void

def trace_test: (String span_name, ?service_name: String?, ?operation_name: String, ?tags: Hash[untyped, untyped]) ?{ (Datadog::CI::Test span) -> untyped } -> untyped

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

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

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

Expand Down
23 changes: 21 additions & 2 deletions spec/datadog/ci/configuration/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
.to receive(:agentless_mode_enabled)
.and_return(agentless_enabled)

allow(settings.ci)
.to receive(:experimental_test_suite_level_visibility_enabled)
.and_return(experimental_test_suite_level_visibility_enabled)

allow(settings.ci)
.to receive(:agentless_url)
.and_return(agentless_url)
Expand Down Expand Up @@ -90,13 +94,28 @@
let(:agentless_url) { nil }
let(:dd_site) { nil }
let(:agentless_enabled) { false }
let(:experimental_test_suite_level_visibility_enabled) { false }
let(:evp_proxy_supported) { false }

context "is enabled" do
let(:enabled) { true }

it "creates a CI recorder" do
expect(components.ci_recorder).to be_kind_of(Datadog::CI::Recorder)
context "when #experimental_test_suite_level_visibility_enabled" do
context "is false" do
it "creates a CI recorder with test_suite_level_visibility_enabled=false" do
expect(components.ci_recorder).to be_kind_of(Datadog::CI::Recorder)
expect(components.ci_recorder.test_suite_level_visibility_enabled).to eq(false)
end
end

context "is true" do
let(:experimental_test_suite_level_visibility_enabled) { true }

it "creates a CI recorder with test_suite_level_visibility_enabled=false" do
expect(components.ci_recorder).to be_kind_of(Datadog::CI::Recorder)
expect(components.ci_recorder.test_suite_level_visibility_enabled).to eq(true)
end
end
end

context "and when #agentless_mode" do
Expand Down
45 changes: 45 additions & 0 deletions spec/datadog/ci/configuration/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,51 @@ def patcher
end
end

describe "#experimental_test_suite_level_visibility_enabled" do
subject(:experimental_test_suite_level_visibility_enabled) do
settings.ci.experimental_test_suite_level_visibility_enabled
end

it { is_expected.to be false }

context "when #{Datadog::CI::Ext::Settings::ENV_EXPERIMENTAL_TEST_SUITE_LEVEL_VISIBILITY_ENABLED}" do
around do |example|
ClimateControl.modify(
Datadog::CI::Ext::Settings::ENV_EXPERIMENTAL_TEST_SUITE_LEVEL_VISIBILITY_ENABLED => enable
) do
example.run
end
end

context "is not defined" do
let(:enable) { nil }

it { is_expected.to be false }
end

context "is set to true" do
let(:enable) { "true" }

it { is_expected.to be true }
end

context "is set to false" do
let(:enable) { "false" }

it { is_expected.to be false }
end
end
end

describe "#experimental_test_suite_level_visibility_enabled=" do
it "updates the #enabled setting" do
expect { settings.ci.experimental_test_suite_level_visibility_enabled = true }
.to change { settings.ci.experimental_test_suite_level_visibility_enabled }
.from(false)
.to(true)
end
end

describe "#instrument" do
let(:integration_name) { :fake }

Expand Down
59 changes: 36 additions & 23 deletions spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
let(:tags) { {} }
let(:expected_tags) { {} }
let(:environment_tags) { Datadog::CI::Ext::Environment.tags(ENV) }
let(:test_suite_level_visibility_enabled) { true }

let(:ci_span) do
spy("CI object spy")
end

subject(:recorder) { described_class.new }
subject(:recorder) { described_class.new(test_suite_level_visibility_enabled: test_suite_level_visibility_enabled) }

before do
allow(Datadog::Tracing).to receive(:active_trace).and_return(trace_op)
Expand Down Expand Up @@ -348,33 +349,45 @@
end

describe "#start_test_session" do
subject(:start_test_session) { recorder.start_test_session(service_name: service) }

let(:session_operation_name) { "test.session" }
let(:span_op) { Datadog::Tracing::SpanOperation.new(session_operation_name) }
let(:expected_tags) { {"_test.session_id" => span_op.id} }

before do
allow(Datadog::Tracing)
.to receive(:trace)
.with(
session_operation_name,
{
span_type: Datadog::CI::Ext::AppTypes::TYPE_TEST_SESSION,
service: service
}
)
.and_return(span_op)
context "when test suite level visibility is enabled" do
subject(:start_test_session) { recorder.start_test_session(service_name: service) }

let(:session_operation_name) { "test.session" }
let(:span_op) { Datadog::Tracing::SpanOperation.new(session_operation_name) }
let(:expected_tags) { {"_test.session_id" => span_op.id} }

before do
allow(Datadog::Tracing)
.to receive(:trace)
.with(
session_operation_name,
{
span_type: Datadog::CI::Ext::AppTypes::TYPE_TEST_SESSION,
service: service
}
)
.and_return(span_op)

allow(Datadog::CI::TestSession).to receive(:new).with(span_op).and_return(ci_span)
allow(Datadog::CI::TestSession).to receive(:new).with(span_op).and_return(ci_span)

start_test_session
start_test_session
end

it_behaves_like "internal tracing context"
it_behaves_like "initialize ci span with tags"

it { is_expected.to be(ci_span) }
end

it_behaves_like "internal tracing context"
it_behaves_like "initialize ci span with tags"
context "when test suite level visibility is disabled" do
subject(:start_test_session) { recorder.start_test_session(service_name: service) }

let(:test_suite_level_visibility_enabled) { false }

before { start_test_session }

it { is_expected.to be(ci_span) }
it { is_expected.to be_nil }
end
end

describe "#active_test_session" do
Expand Down
1 change: 1 addition & 0 deletions spec/support/ci_mode_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

Datadog.configure do |c|
c.ci.enabled = true
c.ci.experimental_test_suite_level_visibility_enabled = true
c.ci.instrument integration_name, integration_options
end
end
Expand Down

0 comments on commit a0c4ecd

Please sign in to comment.