Skip to content

Commit 560f0b7

Browse files
committed
global print logs for tests
1 parent 012d566 commit 560f0b7

File tree

6 files changed

+39
-78
lines changed

6 files changed

+39
-78
lines changed

maintnotifications/e2e/scenario_endpoint_types_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"net"
77
"os"
8-
"runtime"
98
"strings"
109
"testing"
1110
"time"
@@ -94,28 +93,17 @@ func TestEndpointTypesPushNotifications(t *testing.T) {
9493
// redefine p and e for each test to get
9594
// proper test name in logs and proper test failures
9695
var p = func(format string, args ...interface{}) {
97-
_, filename, line, _ := runtime.Caller(1)
98-
format = "%s:%d [%s][ENDPOINT-TYPES] " + format + "\n"
99-
ts := time.Now().Format("15:04:05.000")
100-
args = append([]interface{}{filename, line, ts}, args...)
101-
fmt.Printf(format, args...)
96+
printLog("ENDPOINT-TYPES", false, format, args...)
10297
}
10398

10499
var e = func(format string, args ...interface{}) {
105100
errorsDetected = true
106-
_, filename, line, _ := runtime.Caller(1)
107-
format = "%s:%d [%s][ENDPOINT-TYPES][ERROR] " + format + "\n"
108-
ts := time.Now().Format("15:04:05.000")
109-
args = append([]interface{}{filename, line, ts}, args...)
110-
fmt.Printf(format, args...)
101+
printLog("ENDPOINT-TYPES", true, format, args...)
111102
}
112103

113104
var ef = func(format string, args ...interface{}) {
114-
errorsDetected = true
115-
format = "[%s][ENDPOINT-TYPES][ERROR] " + format
116-
ts := time.Now().Format("15:04:05.000")
117-
args = append([]interface{}{ts}, args...)
118-
t.Fatalf(format, args...)
105+
printLog("ENDPOINT-TYPES", true, format, args...)
106+
t.FailNow()
119107
}
120108

121109
p("Testing endpoint type: %s - %s", endpointTest.name, endpointTest.description)

maintnotifications/e2e/scenario_push_notifications_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"runtime"
87
"strings"
98
"testing"
109
"time"
@@ -34,28 +33,17 @@ func TestPushNotifications(t *testing.T) {
3433
var errorsDetected = false
3534

3635
var p = func(format string, args ...interface{}) {
37-
_, filename, line, _ := runtime.Caller(1)
38-
format = "%s:%d [%s][PUSH-NOTIFICATIONS] " + format + "\n"
39-
ts := time.Now().Format("15:04:05.000")
40-
args = append([]interface{}{filename, line, ts}, args...)
41-
fmt.Printf(format, args...)
36+
printLog("PUSH-NOTIFICATIONS", false, format, args...)
4237
}
4338

4439
var e = func(format string, args ...interface{}) {
4540
errorsDetected = true
46-
_, filename, line, _ := runtime.Caller(1)
47-
format = "%s:%d [%s][PUSH-NOTIFICATIONS][ERROR] " + format + "\n"
48-
ts := time.Now().Format("15:04:05.000")
49-
args = append([]interface{}{filename, line, ts}, args...)
50-
fmt.Printf(format, args...)
41+
printLog("PUSH-NOTIFICATIONS", true, format, args...)
5142
}
5243

5344
var ef = func(format string, args ...interface{}) {
54-
errorsDetected = true
55-
format = "[%s][PUSH-NOTIFICATIONS][ERROR] " + format
56-
ts := time.Now().Format("15:04:05.000")
57-
args = append([]interface{}{ts}, args...)
58-
t.Fatalf(format, args...)
45+
printLog("PUSH-NOTIFICATIONS", true, format, args...)
46+
t.FailNow()
5947
}
6048

6149
logCollector.ClearLogs()

maintnotifications/e2e/scenario_stress_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"runtime"
87
"sync"
98
"testing"
109
"time"
@@ -27,28 +26,17 @@ func TestStressPushNotifications(t *testing.T) {
2726
var errorsDetected = false
2827

2928
var p = func(format string, args ...interface{}) {
30-
_, filename, line, _ := runtime.Caller(1)
31-
format = "%s:%d [%s][STRESS] " + format + "\n"
32-
ts := time.Now().Format("15:04:05.000")
33-
args = append([]interface{}{filename, line, ts}, args...)
34-
fmt.Printf(format, args...)
29+
printLog("STRESS", false, format, args...)
3530
}
3631

3732
var e = func(format string, args ...interface{}) {
3833
errorsDetected = true
39-
_, filename, line, _ := runtime.Caller(1)
40-
format = "%s:%d [%s][STRESS][ERROR] " + format + "\n"
41-
ts := time.Now().Format("15:04:05.000")
42-
args = append([]interface{}{filename, line, ts}, args...)
43-
fmt.Printf(format, args...)
34+
printLog("STRESS", true, format, args...)
4435
}
4536

4637
var ef = func(format string, args ...interface{}) {
47-
errorsDetected = true
48-
format = "[%s][STRESS][ERROR] " + format
49-
ts := time.Now().Format("15:04:05.000")
50-
args = append([]interface{}{ts}, args...)
51-
t.Fatalf(format, args...)
38+
printLog("STRESS", true, format, args...)
39+
t.FailNow()
5240
}
5341

5442
logCollector.ClearLogs()

maintnotifications/e2e/scenario_timeout_configs_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"runtime"
87
"strings"
98
"testing"
109
"time"
@@ -27,20 +26,12 @@ func TestTimeoutConfigurationsPushNotifications(t *testing.T) {
2726

2827
var errorsDetected = false
2928
var p = func(format string, args ...interface{}) {
30-
_, filename, line, _ := runtime.Caller(1)
31-
format = "%s:%d [%s][TIMEOUT-CONFIGS] " + format + "\n"
32-
ts := time.Now().Format("15:04:05.000")
33-
args = append([]interface{}{filename, line, ts}, args...)
34-
fmt.Printf(format, args...)
29+
printLog("TIMEOUT-CONFIGS", false, format, args...)
3530
}
3631

3732
var e = func(format string, args ...interface{}) {
3833
errorsDetected = true
39-
_, filename, line, _ := runtime.Caller(1)
40-
format = "%s:%d [%s][TIMEOUT-CONFIGS][ERROR] " + format + "\n"
41-
ts := time.Now().Format("15:04:05.000")
42-
args = append([]interface{}{filename, line, ts}, args...)
43-
fmt.Printf(format, args...)
34+
printLog("TIMEOUT-CONFIGS", true, format, args...)
4435
}
4536

4637
// Test different timeout configurations
@@ -109,10 +100,8 @@ func TestTimeoutConfigurationsPushNotifications(t *testing.T) {
109100
t.Run(timeoutTest.name, func(t *testing.T) {
110101
errorsDetected = false
111102
var ef = func(format string, args ...interface{}) {
112-
format = "[%s][TIMEOUT-CONFIGS][ERROR] " + format
113-
ts := time.Now().Format("15:04:05.000")
114-
args = append([]interface{}{ts}, args...)
115-
t.Fatalf(format, args...)
103+
printLog("TIMEOUT-CONFIGS", true, format, args...)
104+
t.FailNow()
116105
}
117106

118107
p("Testing timeout configuration: %s - %s", timeoutTest.name, timeoutTest.description)

maintnotifications/e2e/scenario_tls_configs_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"os"
7-
"runtime"
87
"strings"
98
"testing"
109
"time"
@@ -27,20 +26,12 @@ func ТestTLSConfigurationsPushNotifications(t *testing.T) {
2726
var dump = true
2827
var errorsDetected = false
2928
var p = func(format string, args ...interface{}) {
30-
_, filename, line, _ := runtime.Caller(1)
31-
format = "%s:%d [%s][TLS-CONFIGS] " + format + "\n"
32-
ts := time.Now().Format("15:04:05.000")
33-
args = append([]interface{}{filename, line, ts}, args...)
34-
fmt.Printf(format, args...)
29+
printLog("TLS-CONFIGS", false, format, args...)
3530
}
3631

3732
var e = func(format string, args ...interface{}) {
3833
errorsDetected = true
39-
_, filename, line, _ := runtime.Caller(1)
40-
format = "%s:%d [%s][TLS-CONFIGS][ERROR] " + format + "\n"
41-
ts := time.Now().Format("15:04:05.000")
42-
args = append([]interface{}{filename, line, ts}, args...)
43-
fmt.Printf(format, args...)
34+
printLog("TLS-CONFIGS", true, format, args...)
4435
}
4536

4637
// Test different TLS configurations
@@ -105,10 +96,8 @@ func ТestTLSConfigurationsPushNotifications(t *testing.T) {
10596
t.Run(tlsTest.name, func(t *testing.T) {
10697
errorsDetected = false
10798
var ef = func(format string, args ...interface{}) {
108-
format = "[%s][TLS-CONFIGS][ERROR] " + format
109-
ts := time.Now().Format("15:04:05.000")
110-
args = append([]interface{}{ts}, args...)
111-
t.Fatalf(format, args...)
99+
printLog("TLS-CONFIGS", true, format, args...)
100+
t.FailNow()
112101
}
113102

114103
if tlsTest.skipReason != "" {

maintnotifications/e2e/utils_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package e2e
22

3+
import (
4+
"fmt"
5+
"path/filepath"
6+
"runtime"
7+
"time"
8+
)
9+
310
func isTimeout(errMsg string) bool {
411
return contains(errMsg, "i/o timeout") ||
512
contains(errMsg, "deadline exceeded") ||
@@ -42,3 +49,15 @@ func min(a, b int) int {
4249
}
4350
return b
4451
}
52+
53+
func printLog(group string, isError bool, format string, args ...interface{}) {
54+
_, filename, line, _ := runtime.Caller(2)
55+
filename = filepath.Base(filename)
56+
if isError {
57+
format = "%s:%d [%s][%s][ERROR] " + format + "\n"
58+
}
59+
format = "%s:%d [%s][%s] " + format + "\n"
60+
ts := time.Now().Format("15:04:05.000")
61+
args = append([]interface{}{filename, line, ts, group}, args...)
62+
fmt.Printf(format, args...)
63+
}

0 commit comments

Comments
 (0)