66package gotesting
77
88import (
9+ "bytes"
910 "compress/gzip"
1011 "encoding/json"
1112 "fmt"
@@ -15,7 +16,6 @@ import (
1516 "os"
1617 "os/exec"
1718 "strings"
18- "sync"
1919 "testing"
2020
2121 "github.com/DataDog/dd-trace-go/v2/ddtrace/ext"
@@ -25,6 +25,7 @@ import (
2525 "github.com/DataDog/dd-trace-go/v2/internal/civisibility/integrations"
2626 "github.com/DataDog/dd-trace-go/v2/internal/civisibility/utils"
2727 "github.com/DataDog/dd-trace-go/v2/internal/civisibility/utils/net"
28+ "github.com/DataDog/dd-trace-go/v2/internal/log"
2829)
2930
3031var currentM * testing.M
@@ -37,7 +38,7 @@ func TestMain(m *testing.M) {
3738 // Enable logs collection for all test scenarios (propagates to spawned child processes).
3839 _ = os .Setenv ("DD_CIVISIBILITY_LOGS_ENABLED" , "true" )
3940
40- const scenarioStarted = "Scenario %s started. \n "
41+ const scenarioStarted = "**** [ Scenario %s started] **** \n \n "
4142 // We need to spawn separated test process for each scenario
4243 scenarios := []string {"TestFlakyTestRetries" , "TestEarlyFlakeDetection" , "TestFlakyTestRetriesAndEarlyFlakeDetection" , "TestIntelligentTestRunner" , "TestManagementTests" , "TestImpactedTests" , "TestParallelEarlyFlakeDetection" }
4344
@@ -65,34 +66,32 @@ func TestMain(m *testing.M) {
6566 } else if internal .BoolEnv ("Bypass" , false ) {
6667 os .Exit (m .Run ())
6768 } else {
68- fmt .Println ("Starting tests..." )
69- var wg sync.WaitGroup
7069 for _ , v := range scenarios {
71- wg .Add (1 )
7270 cmd := exec .Command (os .Args [0 ], os .Args [1 :]... )
73- cmd .Stdout , cmd .Stderr = os .Stdout , os .Stderr
71+ var b bytes.Buffer
72+ if log .DebugEnabled () {
73+ cmd .Stdout , cmd .Stderr = os .Stdout , os .Stderr
74+ } else {
75+ cmd .Stdout = & b
76+ cmd .Stderr = & b
77+ }
7478 cmd .Env = append (cmd .Env , os .Environ ()... )
7579 cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=true" , v ))
76- go func () {
77- defer func () {
78- wg .Done ()
79- }()
80-
81- fmt .Printf ("Running scenario: %s:\n " , v )
82- fmt .Println (cmd .Env )
83- err := cmd .Run ()
84- fmt .Printf ("Done.\n \n " )
85- if err != nil {
86- if exiterr , ok := err .(* exec.ExitError ); ok {
87- fmt .Printf ("Scenario %s failed with exit code: %d\n " , v , exiterr .ExitCode ())
88- os .Exit (exiterr .ExitCode ())
80+ fmt .Printf ("\n **** [RUNNING SCENARIO: %s]\n " , v )
81+ err := cmd .Run ()
82+ fmt .Printf ("\n **** [SCENARIO %s IS DONE]\n \n " , v )
83+ if err != nil {
84+ if exiterr , ok := err .(* exec.ExitError ); ok {
85+ fmt .Printf ("\n ===========================================\n **** [SCENARIO %s FAILED WITH EXIT CODE: %d]\n " , v , exiterr .ExitCode ())
86+ if ! log .DebugEnabled () {
87+ fmt .Printf ("**** [SCENARIO %s OUTPUT]\n ===========================================\n \n %s\n " , v , b .String ())
8988 }
90- fmt .Printf ("cmd.Run: %v\n " , err )
91- os .Exit (1 )
89+ os .Exit (exiterr .ExitCode ())
9290 }
93- }()
91+ fmt .Printf ("cmd.Run: %v\n " , err )
92+ os .Exit (1 )
93+ }
9494 }
95- wg .Wait ()
9695 }
9796
9897 os .Exit (0 )
@@ -226,7 +225,6 @@ func runFlakyTestRetriesTests(m *testing.M) {
226225 // check logs
227226 checkLogs ()
228227
229- fmt .Println ("All tests passed." )
230228 os .Exit (0 )
231229}
232230
@@ -335,7 +333,6 @@ func runEarlyFlakyTestDetectionTests(m *testing.M) {
335333 // check logs
336334 checkLogs ()
337335
338- fmt .Println ("All tests passed." )
339336 os .Exit (0 )
340337}
341338
@@ -422,7 +419,6 @@ func runParallelEarlyFlakyTestDetectionTests(m *testing.M) {
422419 // check logs
423420 checkLogs ()
424421
425- fmt .Println ("All tests passed." )
426422 os .Exit (0 )
427423}
428424
@@ -597,7 +593,6 @@ func runFlakyTestRetriesWithEarlyFlakyTestDetectionTests(m *testing.M, impactedT
597593 // check logs
598594 checkLogs ()
599595
600- fmt .Println ("All tests passed." )
601596 os .Exit (0 )
602597}
603598
@@ -864,50 +859,49 @@ func runTestManagementTests(m *testing.M) {
864859 // check logs
865860 checkLogs ()
866861
867- fmt .Println ("All tests passed." )
868862 os .Exit (0 )
869863}
870864
871865func checkSpansByType (finishedSpans []* mocktracer.Span ,
872866 totalFinishedSpansCount int , sessionSpansCount int , moduleSpansCount int ,
873867 suiteSpansCount int , testSpansCount int , normalSpansCount int ) {
874868 calculatedFinishedSpans := len (finishedSpans )
875- fmt . Printf ("Number of spans received: %d\n " , calculatedFinishedSpans )
869+ log . Debug ("Number of spans received: %d" , calculatedFinishedSpans )
876870 if calculatedFinishedSpans < totalFinishedSpansCount {
877871 panic (fmt .Sprintf ("expected at least %d finished spans, got %d" , totalFinishedSpansCount , calculatedFinishedSpans ))
878872 }
879873
880874 sessionSpans := getSpansWithType (finishedSpans , constants .SpanTypeTestSession )
881875 calculatedSessionSpans := len (sessionSpans )
882- fmt . Printf ("Number of sessions received: %d\n " , calculatedSessionSpans )
876+ log . Debug ("Number of sessions received: %d" , calculatedSessionSpans )
883877 if calculatedSessionSpans != sessionSpansCount {
884878 panic (fmt .Sprintf ("expected exactly %d session span, got %d" , sessionSpansCount , calculatedSessionSpans ))
885879 }
886880
887881 moduleSpans := getSpansWithType (finishedSpans , constants .SpanTypeTestModule )
888882 calculatedModuleSpans := len (moduleSpans )
889- fmt . Printf ("Number of modules received: %d\n " , calculatedModuleSpans )
883+ log . Debug ("Number of modules received: %d" , calculatedModuleSpans )
890884 if calculatedModuleSpans != moduleSpansCount {
891885 panic (fmt .Sprintf ("expected exactly %d module span, got %d" , moduleSpansCount , calculatedModuleSpans ))
892886 }
893887
894888 suiteSpans := getSpansWithType (finishedSpans , constants .SpanTypeTestSuite )
895889 calculatedSuiteSpans := len (suiteSpans )
896- fmt . Printf ("Number of suites received: %d\n " , calculatedSuiteSpans )
890+ log . Debug ("Number of suites received: %d" , calculatedSuiteSpans )
897891 if calculatedSuiteSpans != suiteSpansCount {
898892 panic (fmt .Sprintf ("expected exactly %d suite spans, got %d" , suiteSpansCount , calculatedSuiteSpans ))
899893 }
900894
901895 testSpans := getSpansWithType (finishedSpans , constants .SpanTypeTest )
902896 calculatedTestSpans := len (testSpans )
903- fmt . Printf ("Number of tests received: %d\n " , calculatedTestSpans )
897+ log . Debug ("Number of tests received: %d" , calculatedTestSpans )
904898 if calculatedTestSpans != testSpansCount {
905899 panic (fmt .Sprintf ("expected exactly %d test spans, got %d" , testSpansCount , calculatedTestSpans ))
906900 }
907901
908902 normalSpans := getSpansWithType (finishedSpans , ext .SpanTypeHTTP )
909903 calculatedNormalSpans := len (normalSpans )
910- fmt . Printf ("Number of http spans received: %d\n " , calculatedNormalSpans )
904+ log . Debug ("Number of http spans received: %d" , calculatedNormalSpans )
911905 if calculatedNormalSpans != normalSpansCount {
912906 panic (fmt .Sprintf ("expected exactly %d normal spans, got %d" , normalSpansCount , calculatedNormalSpans ))
913907 }
@@ -969,7 +963,7 @@ func checkCapabilitiesTags(finishedSpans []*mocktracer.Span) {
969963func checkLogs () {
970964 // Assert that at least one logs payload has been sent by the library.
971965 logsEntriesCount := len (logsEntries )
972- fmt . Printf ("Number of logs received: %d\n " , logsEntriesCount )
966+ log . Debug ("Number of logs received: %d" , logsEntriesCount )
973967 if logsEntriesCount == 0 {
974968 panic ("expected at least one logs payload to be sent, but none were received" )
975969 }
@@ -1007,12 +1001,12 @@ func setUpHTTPServer(
10071001 enableKnownTests := knownTestsEnabled || earlyFlakyDetectionEnabled
10081002 // mock the settings api to enable automatic test retries
10091003 server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1010- fmt . Printf ("MockApi received request: %s\n " , r .URL .Path )
1004+ log . Debug ("MockApi received request: %s" , r .URL .Path )
10111005
10121006 // Settings request
10131007 if r .URL .Path == "/api/v2/libraries/tests/services/setting" {
10141008 body , _ := io .ReadAll (r .Body )
1015- fmt . Printf ("MockApi received body: %s\n " , body )
1009+ log . Debug ("MockApi received body: %s" , body )
10161010 w .Header ().Set ("Content-Type" , "application/json" )
10171011 response := struct {
10181012 Data struct {
@@ -1039,11 +1033,11 @@ func setUpHTTPServer(
10391033 response .Data .Attributes .EarlyFlakeDetection .SlowTestRetries .ThirtyS = 3
10401034 response .Data .Attributes .EarlyFlakeDetection .SlowTestRetries .FiveM = 2
10411035
1042- fmt . Printf ("MockApi sending response: %v\n " , response )
1036+ log . Debug ("MockApi sending response: %v" , response )
10431037 json .NewEncoder (w ).Encode (& response )
10441038 } else if enableKnownTests && r .URL .Path == "/api/v2/ci/libraries/tests" {
10451039 body , _ := io .ReadAll (r .Body )
1046- fmt . Printf ("MockApi received body: %s\n " , body )
1040+ log . Debug ("MockApi received body: %s" , body )
10471041 w .Header ().Set ("Content-Type" , "application/json" )
10481042 response := struct {
10491043 Data struct {
@@ -1057,18 +1051,18 @@ func setUpHTTPServer(
10571051 response .Data .Attributes = * earlyFlakyDetectionData
10581052 }
10591053
1060- fmt . Printf ("MockApi sending response: %v\n " , response )
1054+ log . Debug ("MockApi sending response: %v" , response )
10611055 json .NewEncoder (w ).Encode (& response )
10621056 } else if r .URL .Path == "/api/v2/git/repository/search_commits" {
10631057 body , _ := io .ReadAll (r .Body )
1064- fmt . Printf ("MockApi received body: %s\n " , body )
1058+ log . Debug ("MockApi received body: %s" , body )
10651059 w .Header ().Set ("Content-Type" , "application/json" )
10661060 w .Write ([]byte ("{}" ))
10671061 } else if r .URL .Path == "/api/v2/git/repository/packfile" {
10681062 w .WriteHeader (http .StatusAccepted )
10691063 } else if itrEnabled && r .URL .Path == "/api/v2/ci/tests/skippable" {
10701064 body , _ := io .ReadAll (r .Body )
1071- fmt . Printf ("MockApi received body: %s\n " , body )
1065+ log . Debug ("MockApi received body: %s" , body )
10721066 w .Header ().Set ("Content-Type" , "application/json" )
10731067 response := skippableResponse {
10741068 Meta : skippableResponseMeta {
@@ -1083,27 +1077,27 @@ func setUpHTTPServer(
10831077 Attributes : data ,
10841078 })
10851079 }
1086- fmt . Printf ("MockApi sending response: %v\n " , response )
1080+ log . Debug ("MockApi sending response: %v" , response )
10871081 json .NewEncoder (w ).Encode (& response )
10881082 } else if r .URL .Path == "/api/v2/logs" {
10891083 // Mock the logs intake endpoint.
10901084 reader , _ := gzip .NewReader (r .Body )
10911085 body , _ := io .ReadAll (reader )
1092- fmt . Printf ("MockApi received logs payload: %d bytes\n " , len (body ))
1086+ log . Debug ("MockApi received logs payload: %d bytes" , len (body ))
10931087 var newEntries []* mockedLogEntry
10941088 if err := json .Unmarshal (body , & newEntries ); err != nil {
1095- fmt . Printf ("MockApi received invalid logs payload: %s\n " , err )
1096- fmt . Printf ("Payload: %s\n " , string ( body ) )
1089+ log . Debug ("MockApi received invalid logs payload: %s" , err )
1090+ log . Debug ("Payload: %s" , body )
10971091 w .WriteHeader (http .StatusBadRequest )
10981092 return
10991093 }
11001094 logsEntries = append (logsEntries , newEntries ... )
1101- fmt . Printf ("MockApi received %d log entries\n " , len (newEntries ))
1095+ log . Debug ("MockApi received %d log entries" , len (newEntries ))
11021096 // A 2xx status code is required to mark the payload as accepted.
11031097 w .WriteHeader (http .StatusAccepted )
11041098 } else if r .URL .Path == "/api/v2/test/libraries/test-management/tests" {
11051099 body , _ := io .ReadAll (r .Body )
1106- fmt . Printf ("MockApi received body: %s\n " , body )
1100+ log . Debug ("MockApi received body: %s" , body )
11071101 w .Header ().Set ("Content-Type" , "application/json" )
11081102 response := struct {
11091103 Data struct {
@@ -1114,15 +1108,15 @@ func setUpHTTPServer(
11141108 }{}
11151109 response .Data .Type = "ci_app_libraries_tests"
11161110 response .Data .Attributes = * testManagementData
1117- fmt . Printf ("MockApi sending response: %v\n " , response )
1111+ log . Debug ("MockApi sending response: %v" , response )
11181112 json .NewEncoder (w ).Encode (& response )
11191113 } else {
11201114 http .NotFound (w , r )
11211115 }
11221116 }))
11231117
11241118 // set the custom agentless url and the flaky retry count env-var
1125- fmt . Printf ("Using mockapi at: %s\n " , server .URL )
1119+ log . Debug ("Using mockapi at: %s" , server .URL )
11261120 os .Setenv (constants .CIVisibilityAgentlessEnabledEnvironmentVariable , "1" )
11271121 os .Setenv (constants .CIVisibilityAgentlessURLEnvironmentVariable , server .URL )
11281122 os .Setenv (constants .APIKeyEnvironmentVariable , "12345" )
@@ -1176,7 +1170,7 @@ func getSpansWithTagNameAndValue(spans []*mocktracer.Span, tag, value string) []
11761170
11771171func showResourcesNameFromSpans (spans []* mocktracer.Span ) {
11781172 for i , span := range spans {
1179- fmt . Printf (" [%d] = %v | %v\n " , i , span .Tag (ext .ResourceName ), span .Tag (constants .TestName ))
1173+ log . Debug (" [%d] = %v | %v" , i , span .Tag (ext .ResourceName ), span .Tag (constants .TestName ))
11801174 }
11811175}
11821176
0 commit comments