941d01eee3
* Create helpers which integrate with OpenTelemetry for diagnose collection * Go mod vendor * Comments * Update vault/diagnose/helpers.go Co-authored-by: swayne275 <swayne275@gmail.com> * Add unit test/example * tweak output * More comments * add spot check concept * Get unit tests working on Result structs * wip * Fix unit test * Get unit tests working, and make diagnose sessions local rather than global * Comments * Last comments * No need for init * :| * Fix helpers_test * wip * wip * wip * Revendor otel * Fix merge related problems * imports * Fix unit tests Co-authored-by: swayne275 <swayne275@gmail.com>
103 lines
1.7 KiB
Go
103 lines
1.7 KiB
Go
package diagnose
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/go-test/deep"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDiagnoseOtelResults(t *testing.T) {
|
|
expected := &Result{
|
|
Name: "make-coffee",
|
|
Status: ErrorStatus,
|
|
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",
|
|
},
|
|
{
|
|
Name: "dispose-grounds",
|
|
Status: SkippedStatus,
|
|
},
|
|
},
|
|
}
|
|
sess := New(os.Stdout)
|
|
sess.SetSkipList([]string{"dispose-grounds"})
|
|
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)
|
|
|
|
Test(ctx, "dispose-grounds", Skippable("dispose-grounds", disposeGrounds))
|
|
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")
|
|
}
|
|
|
|
func disposeGrounds(_ context.Context) error {
|
|
//Done!
|
|
return nil
|
|
}
|