Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit 0146678

Browse files
authored
Adds Go check support (#240)
* gocheck test * test * fix golinkname * go check first instrumentation * initial scope support for go-checks * enabling coverage for go-checks * remove branch fail checks * go fmt * fix test * changes * removes results from the parent TestingT from scope * add support to ignore retries for a single testing.T, remove retries for gocheck * refactoring and func to check if a test needs a retry * start implementation of runner test execution handler * adds support of cached test and retries in go-check * fail retries workflow configuration * adds support for expected failures type of errors * comments * adds log support * logger fix * change messages * adds go-check to the autoinstrument package * change the source root info with the code * fixes logs * fix windows path
1 parent a673226 commit 0146678

File tree

16 files changed

+766
-49
lines changed

16 files changed

+766
-49
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ jobs:
3232
SCOPE_DEBUG: true
3333
SCOPE_RUNNER_ENABLED: true
3434
SCOPE_RUNNER_EXCLUDE_BRANCHES: master
35+
SCOPE_TESTING_FAIL_RETRIES: 3
36+
SCOPE_TESTING_PANIC_AS_FAIL: true
3537

3638
- name: Upload Scope logs
3739
if: always()

ast/sources.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ast
22

33
import (
4+
"fmt"
45
"go/ast"
56
"go/parser"
67
"go/token"
@@ -84,9 +85,26 @@ func getCodesForFile(file string) (map[string]*MethodCodeBoundaries, error) {
8485
if bPos.IsValid() && bEnd.IsValid() {
8586
pos := fSet.PositionFor(bPos, true)
8687
end := fSet.PositionFor(bEnd, true)
88+
var instTypes []string
89+
if fDecl.Recv != nil && fDecl.Recv.List != nil {
90+
for _, recv := range fDecl.Recv.List {
91+
if recVStarExpr, ok := recv.Type.(*ast.StarExpr); ok {
92+
if recVStarExprIdent, ok := recVStarExpr.X.(*ast.Ident); ok {
93+
instTypes = append(instTypes, fmt.Sprintf("*%s", recVStarExprIdent.Name))
94+
}
95+
} else if recVIdent, ok := recv.Type.(*ast.Ident); ok {
96+
instTypes = append(instTypes, recVIdent.Name)
97+
}
98+
}
99+
}
100+
name := ""
101+
if instTypes != nil {
102+
name = fmt.Sprintf("(%s).", strings.Join(instTypes, ", "))
103+
}
104+
name = name + fDecl.Name.String()
87105
methodCode := MethodCodeBoundaries{
88106
Package: packageName,
89-
Name: fDecl.Name.String(),
107+
Name: name,
90108
File: file,
91109
Start: CodePos{
92110
Line: pos.Line,

autoinstrument/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go.undefinedlabs.com/scopeagent"
1111
"go.undefinedlabs.com/scopeagent/agent"
1212
"go.undefinedlabs.com/scopeagent/instrumentation"
13+
scopegocheck "go.undefinedlabs.com/scopeagent/instrumentation/gocheck"
1314
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
1415
)
1516

@@ -39,6 +40,8 @@ func init() {
3940
return scopeagent.Run(m, agent.WithGlobalPanicHandler())
4041
})
4142
logOnError(err)
43+
44+
scopegocheck.Init()
4245
})
4346
}
4447

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ require (
99
github.com/gogo/protobuf v1.3.1
1010
github.com/golang/protobuf v1.4.2 // indirect
1111
github.com/google/uuid v1.1.1
12-
github.com/kr/pretty v0.2.0 // indirect
1312
github.com/mitchellh/go-homedir v1.1.0
13+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
1414
github.com/opentracing/basictracer-go v1.1.0
1515
github.com/opentracing/opentracing-go v1.1.0
1616
github.com/sirupsen/logrus v1.6.0
1717
github.com/stretchr/testify v1.5.1
1818
github.com/undefinedlabs/go-mpatch v1.0.3
1919
github.com/vmihailenco/msgpack v4.0.4+incompatible
20-
golang.org/x/net v0.0.0-20200519113804-d87ec0cfa476
20+
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7
2121
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 // indirect
2222
google.golang.org/appengine v1.6.6 // indirect
2323
google.golang.org/grpc v1.29.1
24-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
24+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f
2525
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637
2626
)

go.sum

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL
4343
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
4444
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
4545
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
46-
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
47-
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
4846
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4947
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
5048
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
5149
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
5250
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
51+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
52+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
5353
github.com/opentracing/basictracer-go v1.1.0 h1:Oa1fTSBvAl8pa3U+IJYqrKm0NALwH9OsgwOqDv4xJW0=
5454
github.com/opentracing/basictracer-go v1.1.0/go.mod h1:V2HZueSJEp879yv285Aap1BS69fQMD+MNP1mRs6mBQc=
5555
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
@@ -83,8 +83,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
8383
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
8484
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
8585
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
86-
golang.org/x/net v0.0.0-20200519113804-d87ec0cfa476 h1:E7ct1C6/33eOdrGZKMoyntcEvs2dwZnDe30crG5vpYU=
87-
golang.org/x/net v0.0.0-20200519113804-d87ec0cfa476/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
86+
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0=
87+
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
8888
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
8989
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
9090
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -125,13 +125,12 @@ google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLY
125125
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
126126
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
127127
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
128-
google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw=
129128
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
130129
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
131130
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
132131
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
133-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
134-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
132+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
133+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
135134
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 h1:yiW+nvdHb9LVqSHQBXfZCieqV4fzYhNBql77zY0ykqs=
136135
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637/go.mod h1:BHsqpu/nsuzkT5BpiH1EMZPLyqSMM8JbIavyFACoFNk=
137136
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

instrumentation/testing/coverage.go renamed to instrumentation/coverage/coverage.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package testing
1+
package coverage
22

33
import (
44
"bytes"
@@ -80,7 +80,7 @@ func initCoverage() {
8080
}
8181

8282
// Clean the counters for a new coverage session
83-
func startCoverage() {
83+
func StartCoverage() {
8484
countersMutex.Lock()
8585
defer countersMutex.Unlock()
8686
if cover.Mode == "" {
@@ -97,7 +97,7 @@ func startCoverage() {
9797
}
9898

9999
// Restore counters
100-
func restoreCoverageCounters() {
100+
func RestoreCoverageCounters() {
101101
countersMutex.Lock()
102102
defer countersMutex.Unlock()
103103
if cover.Mode == "" {
@@ -111,7 +111,7 @@ func restoreCoverageCounters() {
111111
}
112112

113113
// Get the counters values and extract the coverage info
114-
func endCoverage() *coverage {
114+
func EndCoverage() *coverage {
115115
countersMutex.Lock()
116116
defer countersMutex.Unlock()
117117
if cover.Mode == "" {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package gocheck
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
. "gopkg.in/check.v1"
8+
9+
"go.undefinedlabs.com/scopeagent"
10+
"go.undefinedlabs.com/scopeagent/agent"
11+
scopetesting "go.undefinedlabs.com/scopeagent/instrumentation/testing"
12+
)
13+
14+
var (
15+
failCount = 0
16+
fatalCount = 0
17+
panicCount = 0
18+
expectedCount = 0
19+
errorCount = 0
20+
)
21+
22+
func TestMain(m *testing.M) {
23+
scopetesting.PatchTestingLogger()
24+
defer scopetesting.UnpatchTestingLogger()
25+
Init()
26+
os.Exit(scopeagent.Run(m, agent.WithGlobalPanicHandler()))
27+
}
28+
29+
// Hook up gocheck into the "go test" runner.
30+
func TestM(t *testing.T) {
31+
TestingT(t)
32+
}
33+
34+
type MySuite struct{}
35+
36+
var _ = Suite(&MySuite{})
37+
38+
func (s *MySuite) TestPass(c *C) {
39+
c.Log("Hello", "World")
40+
c.Logf("Hello: %v", "World 2")
41+
}
42+
func (s *MySuite) TestSkip(c *C) {
43+
c.Skip("My skip reason")
44+
}
45+
func (s *MySuite) TestFail(c *C) {
46+
if failCount < 2 {
47+
failCount++
48+
c.Fail()
49+
}
50+
}
51+
func (s *MySuite) TestFatal(c *C) {
52+
if fatalCount < 2 {
53+
fatalCount++
54+
c.Fatal("fatal error")
55+
}
56+
}
57+
func (s *MySuite) TestPanic(c *C) {
58+
if panicCount < 2 {
59+
panicCount++
60+
panic("Custom panic")
61+
}
62+
}
63+
func (s *MySuite) TestExpected(c *C) {
64+
c.ExpectFailure("expected failure")
65+
expectedCount++
66+
if expectedCount > 2 {
67+
c.Fail()
68+
}
69+
}
70+
func (s *MySuite) TestError(c *C) {
71+
if errorCount < 2 {
72+
errorCount++
73+
c.Error("This is an error")
74+
}
75+
}

instrumentation/gocheck/gocheck_test.s

Whitespace-only changes.

0 commit comments

Comments
 (0)