|
| 1 | +package discovery |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + "github.com/nats-io/nats-server/v2/test" |
| 11 | + "github.com/overmindtech/cli/go/auth" |
| 12 | + "github.com/overmindtech/cli/go/sdp-go" |
| 13 | + "github.com/overmindtech/cli/go/sdpcache" |
| 14 | + "go.opentelemetry.io/otel" |
| 15 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 16 | + "go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 17 | + semconv "go.opentelemetry.io/otel/semconv/v1.26.0" |
| 18 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 19 | +) |
| 20 | + |
| 21 | +// startEmbeddedNATSServer runs an in-process NATS for tests that need a live Engine Start. |
| 22 | +func startEmbeddedNATSServer(t *testing.T) string { |
| 23 | + t.Helper() |
| 24 | + opts := test.DefaultTestOptions |
| 25 | + opts.Port = 4739 |
| 26 | + s := test.RunServer(&opts) |
| 27 | + if !s.ReadyForConnections(10 * time.Second) { |
| 28 | + s.Shutdown() |
| 29 | + t.Fatal("could not start embedded NATS server") |
| 30 | + } |
| 31 | + t.Cleanup(func() { |
| 32 | + s.Shutdown() |
| 33 | + }) |
| 34 | + return s.ClientURL() |
| 35 | +} |
| 36 | + |
| 37 | +func setupTestTracer(t *testing.T) *tracetest.InMemoryExporter { |
| 38 | + t.Helper() |
| 39 | + exp := tracetest.NewInMemoryExporter() |
| 40 | + tp := sdktrace.NewTracerProvider( |
| 41 | + sdktrace.WithSyncer(exp), |
| 42 | + sdktrace.WithSampler(sdktrace.AlwaysSample()), |
| 43 | + ) |
| 44 | + prev := otel.GetTracerProvider() |
| 45 | + otel.SetTracerProvider(tp) |
| 46 | + t.Cleanup(func() { |
| 47 | + _ = tp.Shutdown(context.Background()) |
| 48 | + otel.SetTracerProvider(prev) |
| 49 | + }) |
| 50 | + return exp |
| 51 | +} |
| 52 | + |
| 53 | +func countExceptionEvents(spans []tracetest.SpanStub) int { |
| 54 | + n := 0 |
| 55 | + for _, s := range spans { |
| 56 | + if s.Name != "Execute" { |
| 57 | + continue |
| 58 | + } |
| 59 | + for _, ev := range s.Events { |
| 60 | + if ev.Name == semconv.ExceptionEventName { |
| 61 | + n++ |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + return n |
| 66 | +} |
| 67 | + |
| 68 | +// streamTwoSDPQueryErrorsAdapter implements ListStreamableAdapter and emits two *sdp.QueryError |
| 69 | +// values on LIST (for multi-error Execute telemetry tests). |
| 70 | +type streamTwoSDPQueryErrorsAdapter struct { |
| 71 | + *TestAdapter |
| 72 | +} |
| 73 | + |
| 74 | +func (a *streamTwoSDPQueryErrorsAdapter) ListStream(ctx context.Context, scope string, ignoreCache bool, stream QueryResultStream) { |
| 75 | + _ = ctx |
| 76 | + _ = scope |
| 77 | + _ = ignoreCache |
| 78 | + stream.SendError(&sdp.QueryError{ |
| 79 | + ErrorType: sdp.QueryError_OTHER, |
| 80 | + ErrorString: "first sdp query error", |
| 81 | + }) |
| 82 | + stream.SendError(&sdp.QueryError{ |
| 83 | + ErrorType: sdp.QueryError_OTHER, |
| 84 | + ErrorString: "second sdp query error", |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +// plainErrOnGetAdapter returns a non-QueryError from Get for every call. |
| 89 | +type plainErrOnGetAdapter struct { |
| 90 | + *TestAdapter |
| 91 | +} |
| 92 | + |
| 93 | +func (a *plainErrOnGetAdapter) Get(ctx context.Context, scope string, query string, ignoreCache bool) (*sdp.Item, error) { |
| 94 | + _ = ctx |
| 95 | + _ = scope |
| 96 | + _ = query |
| 97 | + _ = ignoreCache |
| 98 | + return nil, fmt.Errorf("plain non-sdp error") |
| 99 | +} |
| 100 | + |
| 101 | +func TestExecute_FirstSDPQueryErrorDoesNotRecordExceptionEvent(t *testing.T) { |
| 102 | + exp := setupTestTracer(t) |
| 103 | + natsURL := startEmbeddedNATSServer(t) |
| 104 | + |
| 105 | + adapter := TestAdapter{ |
| 106 | + ReturnType: "person", |
| 107 | + ReturnScopes: []string{"test", "error"}, |
| 108 | + cache: sdpcache.NewNoOpCache(), |
| 109 | + } |
| 110 | + |
| 111 | + e := newStartedEngine(t, "TestExecuteTraceSDPQueryError", &auth.NATSOptions{ |
| 112 | + Servers: []string{natsURL}, |
| 113 | + ConnectionName: "test-connection", |
| 114 | + ConnectionTimeout: time.Second, |
| 115 | + MaxReconnects: 5, |
| 116 | + }, nil, &adapter) |
| 117 | + |
| 118 | + u := uuid.New() |
| 119 | + q := &sdp.Query{ |
| 120 | + UUID: u[:], |
| 121 | + Type: "person", |
| 122 | + Method: sdp.QueryMethod_GET, |
| 123 | + Query: "foo", |
| 124 | + Scope: "error", |
| 125 | + Deadline: timestamppb.New(time.Now().Add(time.Minute)), |
| 126 | + RecursionBehaviour: &sdp.Query_RecursionBehaviour{ |
| 127 | + LinkDepth: 3, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + ch := make(chan *sdp.QueryResponse, 10) |
| 132 | + err := e.ExecuteQuery(context.Background(), q, ch) |
| 133 | + if err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + |
| 137 | + if n := countExceptionEvents(exp.GetSpans()); n != 0 { |
| 138 | + t.Fatalf("expected 0 exception events on Execute for first *sdp.QueryError, got %d", n) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestExecute_SecondSDPQueryErrorRecordsExceptionEvent(t *testing.T) { |
| 143 | + exp := setupTestTracer(t) |
| 144 | + natsURL := startEmbeddedNATSServer(t) |
| 145 | + |
| 146 | + base := &TestAdapter{ |
| 147 | + ReturnType: "person", |
| 148 | + ReturnScopes: []string{"test"}, |
| 149 | + cache: sdpcache.NewNoOpCache(), |
| 150 | + } |
| 151 | + adapter := &streamTwoSDPQueryErrorsAdapter{TestAdapter: base} |
| 152 | + |
| 153 | + e := newStartedEngine(t, "TestExecuteTraceMultiSDPQueryError", &auth.NATSOptions{ |
| 154 | + Servers: []string{natsURL}, |
| 155 | + ConnectionName: "test-connection", |
| 156 | + ConnectionTimeout: time.Second, |
| 157 | + MaxReconnects: 5, |
| 158 | + }, nil, adapter) |
| 159 | + |
| 160 | + u := uuid.New() |
| 161 | + q := &sdp.Query{ |
| 162 | + UUID: u[:], |
| 163 | + Type: "person", |
| 164 | + Method: sdp.QueryMethod_LIST, |
| 165 | + Scope: "test", |
| 166 | + Deadline: timestamppb.New(time.Now().Add(time.Minute)), |
| 167 | + RecursionBehaviour: &sdp.Query_RecursionBehaviour{ |
| 168 | + LinkDepth: 3, |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + ch := make(chan *sdp.QueryResponse, 10) |
| 173 | + err := e.ExecuteQuery(context.Background(), q, ch) |
| 174 | + if err != nil { |
| 175 | + t.Fatal(err) |
| 176 | + } |
| 177 | + |
| 178 | + if n := countExceptionEvents(exp.GetSpans()); n != 1 { |
| 179 | + t.Fatalf("expected 1 exception event on Execute (2nd *sdp.QueryError only), got %d", n) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func TestExecute_PlainErrorStillRecordsExceptionEvent(t *testing.T) { |
| 184 | + exp := setupTestTracer(t) |
| 185 | + natsURL := startEmbeddedNATSServer(t) |
| 186 | + |
| 187 | + base := &TestAdapter{ |
| 188 | + ReturnType: "person", |
| 189 | + ReturnScopes: []string{"test"}, |
| 190 | + cache: sdpcache.NewNoOpCache(), |
| 191 | + } |
| 192 | + adapter := &plainErrOnGetAdapter{TestAdapter: base} |
| 193 | + |
| 194 | + e := newStartedEngine(t, "TestExecuteTracePlainErr", &auth.NATSOptions{ |
| 195 | + Servers: []string{natsURL}, |
| 196 | + ConnectionName: "test-connection", |
| 197 | + ConnectionTimeout: time.Second, |
| 198 | + MaxReconnects: 5, |
| 199 | + }, nil, adapter) |
| 200 | + |
| 201 | + u := uuid.New() |
| 202 | + q := &sdp.Query{ |
| 203 | + UUID: u[:], |
| 204 | + Type: "person", |
| 205 | + Method: sdp.QueryMethod_GET, |
| 206 | + Query: "foo", |
| 207 | + Scope: "test", |
| 208 | + Deadline: timestamppb.New(time.Now().Add(time.Minute)), |
| 209 | + RecursionBehaviour: &sdp.Query_RecursionBehaviour{ |
| 210 | + LinkDepth: 3, |
| 211 | + }, |
| 212 | + } |
| 213 | + |
| 214 | + ch := make(chan *sdp.QueryResponse, 10) |
| 215 | + err := e.ExecuteQuery(context.Background(), q, ch) |
| 216 | + if err != nil { |
| 217 | + t.Fatal(err) |
| 218 | + } |
| 219 | + |
| 220 | + if n := countExceptionEvents(exp.GetSpans()); n != 1 { |
| 221 | + t.Fatalf("expected 1 exception event for plain error, got %d", n) |
| 222 | + } |
| 223 | +} |
0 commit comments