Make any section skippable via string contains skip checking. (#12055)
* Make any section skippable via string contains skip checking * Fix unit test
This commit is contained in:
parent
d1cc297cd9
commit
1aa98e81f7
|
@ -167,7 +167,7 @@ func (c *OperatorDiagnoseCommand) RunWithParsedFlags() int {
|
|||
}
|
||||
}
|
||||
ctx := diagnose.Context(context.Background(), c.diagnose)
|
||||
c.diagnose.SetSkipList(c.flagSkips)
|
||||
c.diagnose.SkipFilters = c.flagSkips
|
||||
err := c.offlineDiagnostics(ctx)
|
||||
|
||||
results := c.diagnose.Finalize(ctx)
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/vault/sdk/helper/strutil"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
|
@ -38,10 +39,10 @@ var noopTracer = trace.NewNoopTracerProvider().Tracer("vault-diagnose")
|
|||
type testFunction func(context.Context) error
|
||||
|
||||
type Session struct {
|
||||
tc *TelemetryCollector
|
||||
tracer trace.Tracer
|
||||
tp *sdktrace.TracerProvider
|
||||
skip map[string]bool
|
||||
tc *TelemetryCollector
|
||||
tracer trace.Tracer
|
||||
tp *sdktrace.TracerProvider
|
||||
SkipFilters []string
|
||||
}
|
||||
|
||||
// New initializes a Diagnose tracing session. In particular this wires a TelemetryCollector, which
|
||||
|
@ -60,21 +61,14 @@ func New(w io.Writer) *Session {
|
|||
tp: tp,
|
||||
tc: tc,
|
||||
tracer: tracer,
|
||||
skip: make(map[string]bool),
|
||||
}
|
||||
return sess
|
||||
}
|
||||
|
||||
func (s *Session) SetSkipList(ls []string) {
|
||||
for _, e := range ls {
|
||||
s.skip[e] = true
|
||||
}
|
||||
}
|
||||
|
||||
// IsSkipped returns true if skipName is present in the skip list. Can be used in combination with Skip to mark a
|
||||
// span skipped and conditionally skip some logic.
|
||||
func (s *Session) IsSkipped(skipName string) bool {
|
||||
return s.skip[skipName]
|
||||
// IsSkipped returns true if skipName is present in the SkipFilters list. Can be used in combination with Skip to mark a
|
||||
// span skipped and conditionally skips some logic.
|
||||
func (s *Session) IsSkipped(spanName string) bool {
|
||||
return strutil.StrListContainsCaseInsensitive(s.SkipFilters, spanName)
|
||||
}
|
||||
|
||||
// Context returns a new context with a defined diagnose session
|
||||
|
@ -191,6 +185,12 @@ func addSpotCheckResult(ctx context.Context, eventName, checkName, message strin
|
|||
}
|
||||
|
||||
func SpotCheck(ctx context.Context, checkName string, f func() error) error {
|
||||
sess := CurrentSession(ctx)
|
||||
if sess.IsSkipped(checkName) {
|
||||
SpotSkipped(ctx, checkName, "skipped as requested")
|
||||
return nil
|
||||
}
|
||||
|
||||
err := f()
|
||||
if err != nil {
|
||||
SpotError(ctx, checkName, err)
|
||||
|
@ -206,6 +206,11 @@ func SpotCheck(ctx context.Context, checkName string, f func() error) error {
|
|||
func Test(ctx context.Context, spanName string, function testFunction, options ...trace.SpanOption) error {
|
||||
ctx, span := StartSpan(ctx, spanName, options...)
|
||||
defer span.End()
|
||||
sess := CurrentSession(ctx)
|
||||
if sess.IsSkipped(spanName) {
|
||||
Skipped(ctx, "skipped as requested")
|
||||
return nil
|
||||
}
|
||||
|
||||
err := function(ctx)
|
||||
if err != nil {
|
||||
|
@ -233,22 +238,6 @@ func WithTimeout(d time.Duration, f testFunction) testFunction {
|
|||
}
|
||||
}
|
||||
|
||||
// Skippable wraps a Test function with logic that will not run the test if the skipName
|
||||
// was in the session's skip list
|
||||
func Skippable(skipName string, f testFunction) testFunction {
|
||||
return func(ctx context.Context) error {
|
||||
session := CurrentSession(ctx)
|
||||
if session != nil {
|
||||
if !session.IsSkipped(skipName) {
|
||||
return f(ctx)
|
||||
} else {
|
||||
Skipped(ctx, "skipped as requested")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// CapitalizeFirstLetter returns a string with the first letter capitalized
|
||||
func CapitalizeFirstLetter(msg string) string {
|
||||
words := strings.Split(msg, " ")
|
||||
|
|
|
@ -63,7 +63,7 @@ func TestDiagnoseOtelResults(t *testing.T) {
|
|||
},
|
||||
}
|
||||
sess := New(os.Stdout)
|
||||
sess.SetSkipList([]string{"dispose-grounds"})
|
||||
sess.SkipFilters = []string{"dispose-grounds"}
|
||||
ctx := Context(context.Background(), sess)
|
||||
|
||||
func() {
|
||||
|
@ -107,7 +107,7 @@ func makeCoffee(ctx context.Context) error {
|
|||
|
||||
SpotCheck(ctx, "pick-scone", pickScone)
|
||||
|
||||
Test(ctx, "dispose-grounds", Skippable("dispose-grounds", disposeGrounds))
|
||||
Test(ctx, "dispose-grounds", disposeGrounds)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ const (
|
|||
status_ok = "\u001b[32m[ success ]\u001b[0m "
|
||||
status_failed = "\u001b[31m[ failure ]\u001b[0m "
|
||||
status_warn = "\u001b[33m[ warning ]\u001b[0m "
|
||||
status_skipped = "\u001b[90m[ skip ]\u001b[0m "
|
||||
status_skipped = "\u001b[90m[ skipped ]\u001b[0m "
|
||||
same_line = "\x0d"
|
||||
ErrorStatus = 2
|
||||
WarningStatus = 1
|
||||
|
|
Loading…
Reference in New Issue