From 7a6af863d954aeb805fb49131e70087ea5791bcc Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Thu, 30 Nov 2023 11:32:43 +0100 Subject: [PATCH] specs for test module API --- lib/datadog/ci/recorder.rb | 2 +- spec/datadog/ci/recorder_spec.rb | 133 +++++++++++++++++++++++++++- spec/datadog/ci/test_module_spec.rb | 17 ++++ spec/datadog/ci_spec.rb | 36 ++++++++ 4 files changed, 184 insertions(+), 4 deletions(-) create mode 100644 spec/datadog/ci/test_module_spec.rb diff --git a/lib/datadog/ci/recorder.rb b/lib/datadog/ci/recorder.rb index 55eb2772..38562dbf 100644 --- a/lib/datadog/ci/recorder.rb +++ b/lib/datadog/ci/recorder.rb @@ -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 diff --git a/spec/datadog/ci/recorder_spec.rb b/spec/datadog/ci/recorder_spec.rb index 41e5f2dc..c8fa88a3 100644 --- a/spec/datadog/ci/recorder_spec.rb +++ b/spec/datadog/ci/recorder_spec.rb @@ -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) } @@ -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 @@ -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 @@ -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 } @@ -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 diff --git a/spec/datadog/ci/test_module_spec.rb b/spec/datadog/ci/test_module_spec.rb new file mode 100644 index 00000000..98d6d08d --- /dev/null +++ b/spec/datadog/ci/test_module_spec.rb @@ -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 diff --git a/spec/datadog/ci_spec.rb b/spec/datadog/ci_spec.rb index d8a71aa3..5388535f 100644 --- a/spec/datadog/ci_spec.rb +++ b/spec/datadog/ci_spec.rb @@ -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