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

Adds ITR cache subtest support #245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion instrumentation/gocheck/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ func isTestCached(c *chk.C) bool {
cachedMap := config.GetCachedTestsMap()
if _, ok := cachedMap[fqn]; ok {
instrumentation.Logger().Printf("Test '%v' is cached.", fqn)
fmt.Print("[SCOPE CACHED] ")
return true
}
instrumentation.Logger().Printf("Test '%v' is not cached.", fqn)
Expand Down
22 changes: 17 additions & 5 deletions instrumentation/testing/config/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ import (
"sync"
)

type (
TestDescription struct {
Suite string
Name string
}
)

var (
testsToSkip map[string]struct{}
testsToSkip map[string]TestDescription

m sync.Mutex
)

// Gets the map of cached tests
func GetCachedTestsMap() map[string]struct{} {
func GetCachedTestsMap() map[string]TestDescription {
m.Lock()
defer m.Unlock()

Expand All @@ -22,14 +29,19 @@ func GetCachedTestsMap() map[string]struct{} {
}

config := instrumentation.GetRemoteConfiguration()
testsToSkip = map[string]struct{}{}
testsToSkip = map[string]TestDescription{}
if config != nil {
if iCached, ok := config["cached"]; ok {
cachedTests := iCached.([]interface{})
for _, item := range cachedTests {
testItem := item.(map[string]interface{})
testFqn := fmt.Sprintf("%v.%v", testItem["test_suite"], testItem["test_name"])
testsToSkip[testFqn] = struct{}{}
suite := fmt.Sprint(testItem["test_suite"])
name := fmt.Sprint(testItem["test_name"])
testFqn := fmt.Sprintf("%s.%s", suite, name)
testsToSkip[testFqn] = TestDescription{
Suite: suite,
Name: name,
}
}
}
}
Expand Down
61 changes: 33 additions & 28 deletions instrumentation/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,30 @@ func StartTest(t *testing.T, opts ...Option) *Test {
func StartTestFromCaller(t *testing.T, pc uintptr, opts ...Option) *Test {

// check if the test is cached
if isTestCached(t, pc) {

test := &Test{t: t, ctx: context.Background()}
for _, opt := range opts {
opt(test)
}

// Extracting the testing func name (by removing any possible sub-test suffix `{test_func}/{sub_test}`)
// to search the func source code bounds and to calculate the package name.
fullTestName := runner.GetOriginalTestName(t.Name())
pName, _ := instrumentation.GetPackageAndName(pc)

testTags := opentracing.Tags{
"span.kind": "test",
"test.name": fullTestName,
"test.suite": pName,
"test.framework": "testing",
"test.language": "go",
if ok, testsDescription := isTestCached(t, pc); ok {
ctx := context.Background()
tracer := instrumentation.Tracer()
for _, desc := range testsDescription {
startTime := time.Now()
options := []opentracing.StartSpanOption{
opentracing.Tags{
"span.kind": "test",
"test.name": desc.Name,
"test.suite": desc.Suite,
"test.framework": "testing",
"test.language": "go",
},
opentracing.StartTime(startTime),
}
span, _ := opentracing.StartSpanFromContextWithTracer(ctx, tracer, desc.Name, options...)
span.SetBaggageItem("trace.kind", "test")
span.SetTag("test.status", tags.TestStatus_CACHE)
span.FinishWithOptions(opentracing.FinishOptions{
FinishTime: startTime,
})
}
span, _ := opentracing.StartSpanFromContextWithTracer(test.ctx, instrumentation.Tracer(), fullTestName, testTags)
span.SetBaggageItem("trace.kind", "test")
span.SetTag("test.status", tags.TestStatus_CACHE)
span.Finish()
t.SkipNow()
return test
return nil

} else {

Expand Down Expand Up @@ -320,16 +319,22 @@ func addAutoInstrumentedTest(t *testing.T) {
}

// Get if the test is cached
func isTestCached(t *testing.T, pc uintptr) bool {
pkgName, testName := instrumentation.GetPackageAndName(pc)
func isTestCached(t *testing.T, pc uintptr) (bool, []config.TestDescription) {
pkgName, _ := instrumentation.GetPackageAndName(pc)
testName := runner.GetOriginalTestName(t.Name())
fqn := fmt.Sprintf("%s.%s", pkgName, testName)
cachedMap := config.GetCachedTestsMap()
if _, ok := cachedMap[fqn]; ok {
instrumentation.Logger().Printf("Test '%v' is cached.", fqn)
fmt.Print("[SCOPE CACHED] ")
var tests []config.TestDescription
for _, v := range cachedMap {
if v.Suite == pkgName && strings.HasPrefix(v.Name, testName) {
tests = append(tests, v)
}
}
reflection.SkipAndFinishTest(t)
return true
return true, tests
}
instrumentation.Logger().Printf("Test '%v' is not cached.", fqn)
return false
return false, nil
}