Skip to content

Commit

Permalink
add basic assertion functionaliy
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren committed May 2, 2022
1 parent b75b691 commit 269a27d
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
87 changes: 87 additions & 0 deletions server/assertions/assertions.go
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 "="
}
74 changes: 74 additions & 0 deletions server/assertions/assetions_test.go
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)
}
}

})
}
}
29 changes: 29 additions & 0 deletions server/traces/traces.go
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
}
7 changes: 7 additions & 0 deletions server/traces/traces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package traces_test

import "testing"

func TestTracesAttributesGet(t *testing.T) {

}

0 comments on commit 269a27d

Please sign in to comment.