2021-04-29 18:32:41 +00:00
|
|
|
package diagnose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"github.com/go-test/deep"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiagnoseOtelResults(t *testing.T) {
|
|
|
|
expected := &Result{
|
|
|
|
Name: "make-coffee",
|
2021-05-22 02:21:11 +00:00
|
|
|
Status: ErrorStatus,
|
2021-04-29 18:32:41 +00:00
|
|
|
Warnings: []string{
|
|
|
|
"coffee getting low",
|
|
|
|
},
|
|
|
|
Children: []*Result{
|
|
|
|
{
|
|
|
|
Name: "warm-milk",
|
|
|
|
Status: OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "brew-coffee",
|
|
|
|
Status: OkStatus,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "pick-scone",
|
|
|
|
Status: ErrorStatus,
|
|
|
|
Message: "no scones",
|
|
|
|
},
|
2021-05-12 17:54:40 +00:00
|
|
|
{
|
2021-06-03 16:01:14 +00:00
|
|
|
Name: "dispose-grounds",
|
|
|
|
Status: SkippedStatus,
|
|
|
|
Message: "skipped as requested",
|
2021-05-12 17:54:40 +00:00
|
|
|
},
|
2021-04-29 18:32:41 +00:00
|
|
|
},
|
|
|
|
}
|
2021-05-22 02:21:11 +00:00
|
|
|
sess := New(os.Stdout)
|
2021-05-12 17:54:40 +00:00
|
|
|
sess.SetSkipList([]string{"dispose-grounds"})
|
2021-04-29 18:32:41 +00:00
|
|
|
ctx := Context(context.Background(), sess)
|
|
|
|
|
|
|
|
func() {
|
|
|
|
ctx, span := StartSpan(ctx, "make-coffee")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
makeCoffee(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
results := sess.Finalize(ctx)
|
|
|
|
results.ZeroTimes()
|
|
|
|
if !reflect.DeepEqual(results, expected) {
|
|
|
|
t.Fatalf("results mismatch: %s", strings.Join(deep.Equal(results, expected), "\n"))
|
|
|
|
}
|
|
|
|
results.Write(os.Stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
const coffeeLeft = 3
|
|
|
|
|
|
|
|
func makeCoffee(ctx context.Context) error {
|
|
|
|
if coffeeLeft < 5 {
|
|
|
|
Warn(ctx, "coffee getting low")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Test(ctx, "warm-milk", warmMilk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = brewCoffee(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
SpotCheck(ctx, "pick-scone", pickScone)
|
|
|
|
|
2021-05-12 17:54:40 +00:00
|
|
|
Test(ctx, "dispose-grounds", Skippable("dispose-grounds", disposeGrounds))
|
2021-04-29 18:32:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func warmMilk(ctx context.Context) error {
|
|
|
|
// Always succeeds
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func brewCoffee(ctx context.Context) error {
|
|
|
|
ctx, span := StartSpan(ctx, "brew-coffee")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
//Brewing happens here, successfully
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func pickScone() error {
|
|
|
|
return errors.New("no scones")
|
|
|
|
}
|
2021-05-12 17:54:40 +00:00
|
|
|
|
|
|
|
func disposeGrounds(_ context.Context) error {
|
|
|
|
//Done!
|
|
|
|
return nil
|
|
|
|
}
|