Skip to content

Commit

Permalink
specs for test module API
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Nov 30, 2023
1 parent e54cd82 commit 7a6af86
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/datadog/ci/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Recorder

def initialize(enabled: true, test_suite_level_visibility_enabled: false)
@enabled = enabled
@test_suite_level_visibility_enabled = test_suite_level_visibility_enabled
@test_suite_level_visibility_enabled = enabled && test_suite_level_visibility_enabled

@environment_tags = Ext::Environment.tags(ENV).freeze
@local_context = Context::Local.new
Expand Down
133 changes: 130 additions & 3 deletions spec/datadog/ci/recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@
end

context "when test suite level visibility is disabled" do
let(:service_name) { "my-service" }
let(:tags) { {"test.framework" => "my-framework", "my.tag" => "my_value"} }

include_context "CI mode activated" do
let(:experimental_test_suite_level_visibility_enabled) { false }
end

describe "#trace_test_session" do
let(:service_name) { "my-service" }
let(:tags) { {"test.framework" => "my-framework", "my.tag" => "my_value"} }

subject { recorder.start_test_session(service_name: service_name, tags: tags) }

it { is_expected.to be_kind_of(Datadog::CI::NullSpan) }
Expand All @@ -124,6 +124,18 @@
expect(recorder.active_test_session).to be_nil
end
end

describe "#trace_test_module" do
let(:module_name) { "my-module" }

subject { recorder.start_test_module(module_name, service_name: service_name, tags: tags) }

it { is_expected.to be_kind_of(Datadog::CI::NullSpan) }

it "does not activate module" do
expect(recorder.active_test_module).to be_nil
end
end
end

context "when test suite level visibility is enabled" do
Expand Down Expand Up @@ -343,6 +355,83 @@
end
end

describe "#start_test_module" do
let(:module_name) { "my-module" }
let(:service_name) { "my-service" }
let(:tags) { {"test.framework" => "my-framework", "my.tag" => "my_value"} }

subject { recorder.start_test_module(module_name, service_name: service_name, tags: tags) }

context "when there is no active test session" do
it "returns a new CI test_module span" do
expect(subject).to be_kind_of(Datadog::CI::TestModule)
expect(subject.name).to eq(module_name)
expect(subject.service).to eq(service_name)
expect(subject.span_type).to eq(Datadog::CI::Ext::AppTypes::TYPE_TEST_MODULE)
end

it "sets the test module id" do
expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_MODULE_ID)).to eq(subject.id.to_s)
end

it "sets the test module tag" do
expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_MODULE)).to eq(module_name)
end

it "doesn't connect the test module span to the test session" do
expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to be_nil
end

it "sets the provided tags correctly" do
expect(subject.get_tag("test.framework")).to eq("my-framework")
expect(subject.get_tag("my.tag")).to eq("my_value")
end

it_behaves_like "span with environment tags"
it_behaves_like "span with default tags"
it_behaves_like "span with runtime tags"
it_behaves_like "trace with ciapp-test origin" do
let(:trace_under_test) do
subject.finish

trace
end
end
end

context "when there is an active test session" do
let(:service_name) { nil }
let(:session_service_name) { "session_service_name" }
let(:session_tags) { {"test.framework_version" => "1.0", "my.session.tag" => "my_session_value"} }
let(:test_session) { recorder.start_test_session(service_name: session_service_name, tags: session_tags) }

before do
test_session
end

it "returns a new CI module span using service from the test session" do
expect(subject).to be_kind_of(Datadog::CI::TestModule)
expect(subject.name).to eq(module_name)
expect(subject.service).to eq(session_service_name)
end

it "sets the provided tags correctly while inheriting some tags from the session" do
expect(subject.get_tag("test.framework")).to eq("my-framework")
expect(subject.get_tag("test.framework_version")).to eq("1.0")
expect(subject.get_tag("my.tag")).to eq("my_value")
expect(subject.get_tag("my.session.tag")).to be_nil
end

it "connects the test module span to the test session" do
expect(subject.get_tag(Datadog::CI::Ext::Test::TAG_TEST_SESSION_ID)).to eq(test_session.id.to_s)
end

it "does not start a new trace" do
expect(subject.tracer_span.trace_id).to eq(test_session.tracer_span.trace_id)
end
end
end

describe "#active_test_session" do
subject { recorder.active_test_session }
context "when there is no active test session" do
Expand All @@ -361,6 +450,24 @@
end
end

describe "#active_test_module" do
subject { recorder.active_test_module }
context "when there is no active test module" do
it { is_expected.to be_nil }
end

context "when test module is started" do
let(:test_module) { recorder.start_test_module("my module") }
before do
test_module
end

it "returns the active test module" do
expect(subject).to be(test_module)
end
end
end

describe "#active_test" do
subject { recorder.active_test }

Expand Down Expand Up @@ -454,5 +561,25 @@
end
end
end

describe "#deactivate_test_module" do
subject { recorder.deactivate_test_module }

context "when there is no active test module" do
it { is_expected.to be_nil }
end

context "when deactivating the currently active test module" do
before do
recorder.start_test_module("my module")
end

it "deactivates the test module" do
subject

expect(recorder.active_test_module).to be_nil
end
end
end
end
end
17 changes: 17 additions & 0 deletions spec/datadog/ci/test_module_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

RSpec.describe Datadog::CI::TestModule do
let(:tracer_span) { instance_double(Datadog::Tracing::SpanOperation, finish: true) }

describe "#finish" do
subject(:ci_test_module) { described_class.new(tracer_span) }

before { allow(Datadog::CI).to receive(:deactivate_test_module) }

it "deactivates the test module" do
ci_test_module.finish

expect(Datadog::CI).to have_received(:deactivate_test_module)
end
end
end
36 changes: 36 additions & 0 deletions spec/datadog/ci_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,40 @@

it { is_expected.to be_nil }
end

describe "::start_test_module" do
subject(:start_test_module) { described_class.start_test_module("my-module") }

let(:ci_test_module) { instance_double(Datadog::CI::TestModule) }

before do
allow(recorder).to(
receive(:start_test_module).with("my-module", service_name: nil, tags: {}).and_return(ci_test_module)
)
end

it { is_expected.to be(ci_test_module) }
end

describe "::active_test_module" do
subject(:active_test_module) { described_class.active_test_module }

let(:ci_test_module) { instance_double(Datadog::CI::TestModule) }

before do
allow(recorder).to receive(:active_test_module).and_return(ci_test_module)
end

it { is_expected.to be(ci_test_module) }
end

describe "::deactivate_test_module" do
subject(:deactivate_test_module) { described_class.deactivate_test_module }

before do
allow(recorder).to receive(:deactivate_test_module)
end

it { is_expected.to be_nil }
end
end

0 comments on commit 7a6af86

Please sign in to comment.