Skip to content

Commit

Permalink
internal/civisibility: removes importing contrib packages from tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Oct 24, 2024
1 parent dd409ef commit a28f46e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 103 deletions.
34 changes: 11 additions & 23 deletions internal/civisibility/integrations/gotesting/testcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,12 @@ func runFlakyTestRetriesTests(m *testing.M) {
// 1 TestMyTest01
// 1 TestMyTest02 + 2 subtests
// 1 Test_Foo + 3 subtests
// 1 TestWithExternalCalls + 2 subtests
// 1 TestSkip
// 1 TestRetryWithPanic + 3 retry tests from testing_test.go
// 1 TestRetryWithFail + 3 retry tests from testing_test.go
// 1 TestRetryAlwaysFail + 10 retry tests from testing_test.go
// 1 TestNormalPassingAfterRetryAlwaysFail
// 1 TestEarlyFlakeDetection
// 2 normal spans from testing_test.go

// check spans by resource name
checkSpansByResourceName(finishedSpans, "github.com/DataDog/dd-trace-go/v2/internal/civisibility/integrations/gotesting", 1)
Expand All @@ -117,9 +115,6 @@ func runFlakyTestRetriesTests(m *testing.M) {
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/yellow_should_return_color", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/banana_should_return_fruit", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/duck_should_return_animal", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls/default", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls/custom-name", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestSkip", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestRetryWithPanic", 4)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestRetryWithFail", 4)
Expand All @@ -132,12 +127,12 @@ func runFlakyTestRetriesTests(m *testing.M) {

// check spans by type
checkSpansByType(finishedSpans,
44,
39,
1,
1,
2,
38,
2)
35,
0)

os.Exit(0)
}
Expand Down Expand Up @@ -179,7 +174,6 @@ func runEarlyFlakyTestDetectionTests(m *testing.M) {
// 11 TestMyTest01
// 11 TestMyTest02 + 22 subtests
// 11 Test_Foo + 33 subtests
// 11 TestWithExternalCalls + 22 subtests
// 11 TestSkip
// 11 TestRetryWithPanic
// 11 TestRetryWithFail
Expand All @@ -200,9 +194,6 @@ func runEarlyFlakyTestDetectionTests(m *testing.M) {
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/yellow_should_return_color", 11)
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/banana_should_return_fruit", 11)
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/duck_should_return_animal", 11)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls", 11)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls/default", 11)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls/custom-name", 11)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestSkip", 11)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestRetryWithPanic", 11)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestRetryWithFail", 11)
Expand All @@ -211,17 +202,17 @@ func runEarlyFlakyTestDetectionTests(m *testing.M) {
checkSpansByResourceName(finishedSpans, "testing_test.go.TestEarlyFlakeDetection", 11)

// check spans by tag
checkSpansByTagName(finishedSpans, constants.TestIsNew, 187)
checkSpansByTagName(finishedSpans, constants.TestIsRetry, 170)
checkSpansByTagName(finishedSpans, constants.TestIsNew, 154)
checkSpansByTagName(finishedSpans, constants.TestIsRetry, 140)

// check spans by type
checkSpansByType(finishedSpans,
218,
163,
1,
1,
2,
192,
22)
159,
0)

os.Exit(0)
}
Expand Down Expand Up @@ -298,9 +289,6 @@ func runFlakyTestRetriesWithEarlyFlakyTestDetectionTests(m *testing.M) {
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/yellow_should_return_color", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/banana_should_return_fruit", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.Test_Foo/duck_should_return_animal", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls/default", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestWithExternalCalls/custom-name", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestSkip", 1)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestRetryWithPanic", 4)
checkSpansByResourceName(finishedSpans, "testing_test.go.TestRetryWithFail", 4)
Expand All @@ -314,12 +302,12 @@ func runFlakyTestRetriesWithEarlyFlakyTestDetectionTests(m *testing.M) {

// check spans by type
checkSpansByType(finishedSpans,
64,
59,
1,
1,
2,
58,
2)
55,
0)

os.Exit(0)
}
Expand Down
80 changes: 0 additions & 80 deletions internal/civisibility/integrations/gotesting/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ package gotesting

import (
"fmt"
"net/http"
"net/http/httptest"
"runtime"
"slices"
"testing"

ddhttp "github.com/DataDog/dd-trace-go/contrib/net/http/v2"
"github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
"github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer"
ddtracer "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/internal/civisibility/constants"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -76,82 +72,6 @@ func Test_Foo(gt *testing.T) {
}
}

// TestWithExternalCalls demonstrates testing with external HTTP calls.
func TestWithExternalCalls(gt *testing.T) {
assertTest(gt)
t := (*T)(gt)

// Create a new HTTP test server
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello World"))
}))
defer s.Close()

t.Run("default", func(t *testing.T) {

// if we want to use the test span as a parent of a child span
// we can extract the SpanContext and use it in other integrations
ctx := (*T)(t).Context()

// Wrap the default HTTP transport for tracing
rt := ddhttp.WrapRoundTripper(http.DefaultTransport)
client := &http.Client{
Transport: rt,
}

// Create a new HTTP request
req, err := http.NewRequest("GET", s.URL+"/hello/world", nil)
if err != nil {
t.FailNow()
}

// Use the span context here so the http span will appear as a child of the test
req = req.WithContext(ctx)

res, err := client.Do(req)
if err != nil {
t.FailNow()
}
_ = res.Body.Close()
})

t.Run("custom-name", func(t *testing.T) {

// we can also add custom tags to the test span by retrieving the
// context and call the `ddtracer.SpanFromContext` api
ctx := (*T)(t).Context()
span, _ := ddtracer.SpanFromContext(ctx)

// Custom namer function for the HTTP request
customNamer := func(req *http.Request) string {
value := fmt.Sprintf("%s %s", req.Method, req.URL.Path)

// Then we can set custom tags to that test span
span.SetTag("customNamer.Value", value)
return value
}

rt := ddhttp.WrapRoundTripper(http.DefaultTransport, ddhttp.RTWithResourceNamer(customNamer))
client := &http.Client{
Transport: rt,
}

req, err := http.NewRequest("GET", s.URL+"/hello/world", nil)
if err != nil {
t.FailNow()
}

// Use the span context here so the http span will appear as a child of the test
req = req.WithContext(ctx)

res, err := client.Do(req)
if err != nil {
t.FailNow()
}
_ = res.Body.Close()
})
}

// TestSkip demonstrates skipping a test with a message.
func TestSkip(gt *testing.T) {
assertTest(gt)
Expand Down

0 comments on commit a28f46e

Please sign in to comment.