-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package assertions | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kubeshop/tracetest/traces" | ||
) | ||
|
||
type Selector string // TODO: use actual selectors | ||
|
||
type Comparator interface { | ||
Compare(string, string) error | ||
fmt.Stringer | ||
} | ||
|
||
type Assertion struct { | ||
Attribute string | ||
Comparator Comparator | ||
Value string | ||
} | ||
|
||
func (a Assertion) Assert(spans []traces.Span) AssertionResult { | ||
results := make([]AssertionSpanResults, len(spans)) | ||
for i, span := range spans { | ||
results[i] = a.apply(span) | ||
} | ||
return AssertionResult{ | ||
Assertion: a, | ||
AssertionSpanResults: results, | ||
} | ||
} | ||
|
||
func (a Assertion) apply(span traces.Span) AssertionSpanResults { | ||
attr := span.Attributes.Get(a.Attribute) | ||
return AssertionSpanResults{ | ||
Span: &span, | ||
ActualValue: attr, | ||
CompareErr: a.Comparator.Compare(a.Value, attr), | ||
} | ||
} | ||
|
||
type AssertionResult struct { | ||
Assertion | ||
AssertionSpanResults []AssertionSpanResults | ||
} | ||
|
||
type AssertionSpanResults struct { | ||
Span *traces.Span | ||
ActualValue string | ||
CompareErr error | ||
} | ||
|
||
type TestDefinition map[Selector][]Assertion | ||
|
||
type TestResult map[Selector]AssertionResult | ||
|
||
func Assert(trace traces.Trace, defs TestDefinition) TestResult { | ||
testResult := TestResult{} | ||
for selector, asserts := range defs { | ||
spans := findSpans(trace, selector) | ||
for _, assertion := range asserts { | ||
testResult[selector] = assertion.Assert(spans) | ||
} | ||
} | ||
return testResult | ||
} | ||
|
||
func findSpans(t traces.Trace, s Selector) []traces.Span { | ||
// todo: actually implement search | ||
return []traces.Span{t.RootSpan} | ||
} | ||
|
||
var ErrNoMatch = fmt.Errorf("no match") | ||
|
||
type ComparatorEq struct{} | ||
|
||
func (c ComparatorEq) Compare(expected, actual string) error { | ||
if expected == actual { | ||
return nil | ||
} | ||
|
||
return ErrNoMatch | ||
} | ||
|
||
func (c ComparatorEq) String() string { | ||
return "=" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package assertions_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kubeshop/tracetest/assertions" | ||
"github.com/kubeshop/tracetest/traces" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAssertion(t *testing.T) { | ||
|
||
cases := []struct { | ||
name string | ||
testDef assertions.TestDefinition | ||
trace traces.Trace | ||
expectedResult assertions.TestResult | ||
}{ | ||
{ | ||
name: "simple assertion works", | ||
testDef: assertions.TestDefinition{ | ||
"selector": []assertions.Assertion{ | ||
{ | ||
Attribute: "tracetest.span.duration", | ||
Comparator: assertions.ComparatorEq{}, | ||
Value: "2000", | ||
}, | ||
}, | ||
}, | ||
trace: traces.Trace{ | ||
RootSpan: traces.Span{ | ||
Attributes: traces.Attributes{ | ||
"tracetest.span.duration": "2000", | ||
}, | ||
}, | ||
}, | ||
expectedResult: assertions.TestResult{ | ||
"selector": assertions.AssertionResult{ | ||
Assertion: assertions.Assertion{ | ||
Attribute: "tracetest.span.duration", | ||
Comparator: assertions.ComparatorEq{}, | ||
Value: "2000", | ||
}, | ||
AssertionSpanResults: []assertions.AssertionSpanResults{ | ||
{ActualValue: "2000", CompareErr: nil}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
cl := c | ||
t.Parallel() | ||
|
||
actual := assertions.Assert(cl.trace, cl.testDef) | ||
|
||
for expectedSel, expectedAR := range cl.expectedResult { | ||
actualAR, ok := actual[expectedSel] | ||
assert.True(t, ok, "expected selector %s not found", expectedSel) | ||
assert.Equal(t, expectedAR.Assertion, actualAR.Assertion) | ||
assert.Len(t, actualAR.AssertionSpanResults, len(expectedAR.AssertionSpanResults)) | ||
|
||
for i, expectedSpanRes := range expectedAR.AssertionSpanResults { | ||
actualSpanRes := actualAR.AssertionSpanResults[i] | ||
assert.Equal(t, expectedSpanRes.ActualValue, actualSpanRes.ActualValue) | ||
assert.Equal(t, expectedSpanRes.CompareErr, actualSpanRes.CompareErr) | ||
} | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package traces | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
type Trace struct { | ||
ID trace.TraceID | ||
RootSpan Span | ||
} | ||
|
||
type Attributes map[string]string | ||
|
||
func (a Attributes) Get(key string) string { | ||
if v, ok := a[key]; ok { | ||
return v | ||
} | ||
|
||
return "" | ||
} | ||
|
||
type Span struct { | ||
ID trace.SpanID | ||
Name string | ||
Attributes Attributes | ||
|
||
Parent *Span | ||
Children []*Span | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package traces_test | ||
|
||
import "testing" | ||
|
||
func TestTracesAttributesGet(t *testing.T) { | ||
|
||
} |