Skip to content

Commit f39ee9b

Browse files
committed
Add tests
1 parent 3253c78 commit f39ee9b

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

cli/azd/internal/cmd/errors_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
1414
"github.com/azure/azure-dev/cli/azd/pkg/auth"
1515
"github.com/azure/azure-dev/cli/azd/pkg/azapi"
16+
"github.com/azure/azure-dev/cli/azd/pkg/azdext"
1617
"github.com/azure/azure-dev/cli/azd/pkg/exec"
1718
"github.com/azure/azure-dev/cli/azd/test/mocks/mocktracing"
1819
"github.com/stretchr/testify/require"
@@ -148,6 +149,23 @@ func Test_MapError(t *testing.T) {
148149
fields.ErrorKey(fields.ServiceCorrelationId.Key).String("12345"),
149150
},
150151
},
152+
{
153+
name: "WithExtensionResponseError",
154+
err: &azdext.ExtensionResponseError{
155+
Message: "Rate limit exceeded",
156+
Details: "Too many requests",
157+
ErrorCode: "RateLimitExceeded",
158+
StatusCode: 429,
159+
ServiceName: "openai.azure.com",
160+
},
161+
wantErrReason: "ext.service.openai.429",
162+
wantErrDetails: []attribute.KeyValue{
163+
fields.ErrorKey(fields.ServiceName.Key).String("openai"),
164+
fields.ErrorKey(fields.ServiceHost.Key).String("openai.azure.com"),
165+
fields.ErrorKey(fields.ServiceStatusCode.Key).Int(429),
166+
fields.ErrorKey(fields.ServiceErrorCode.Key).String("RateLimitExceeded"),
167+
},
168+
},
151169
}
152170
for _, tt := range tests {
153171
t.Run(tt.name, func(t *testing.T) {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package azdext
5+
6+
import (
7+
"errors"
8+
"testing"
9+
10+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestExtensionError_RoundTrip(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
inputErr error
18+
wantInfo errorInfo
19+
useErrorStringAsMessage bool
20+
}{
21+
{
22+
name: "NilError",
23+
inputErr: nil,
24+
wantInfo: errorInfo{},
25+
},
26+
{
27+
name: "SimpleError",
28+
inputErr: errors.New("simple error"),
29+
wantInfo: errorInfo{
30+
message: "simple error",
31+
},
32+
},
33+
{
34+
name: "ExtensionResponseError",
35+
inputErr: &ExtensionResponseError{
36+
Message: "Rate limit exceeded",
37+
Details: "Too many requests",
38+
ErrorCode: "RateLimitExceeded",
39+
StatusCode: 429,
40+
ServiceName: "openai.azure.com",
41+
},
42+
wantInfo: errorInfo{
43+
message: "Rate limit exceeded",
44+
details: "Too many requests",
45+
errorCode: "RateLimitExceeded",
46+
statusCode: 429,
47+
service: "openai.azure.com",
48+
},
49+
},
50+
{
51+
name: "AzCoreResponseError",
52+
inputErr: &azcore.ResponseError{
53+
ErrorCode: "ResourceNotFound",
54+
StatusCode: 404,
55+
},
56+
wantInfo: errorInfo{
57+
errorCode: "ResourceNotFound",
58+
statusCode: 404,
59+
},
60+
useErrorStringAsMessage: true,
61+
},
62+
}
63+
64+
for _, tt := range tests {
65+
t.Run(tt.name, func(t *testing.T) {
66+
if tt.useErrorStringAsMessage {
67+
tt.wantInfo.message = tt.inputErr.Error()
68+
}
69+
70+
// Helper to verify the proto message content
71+
type protoMessage interface {
72+
GetMessage() string
73+
GetDetails() string
74+
GetErrorCode() string
75+
GetStatusCode() int32
76+
GetServiceName() string
77+
}
78+
verifyProto := func(t *testing.T, msg protoMessage) {
79+
assert.NotNil(t, msg)
80+
assert.Equal(t, tt.wantInfo.message, msg.GetMessage())
81+
assert.Equal(t, tt.wantInfo.details, msg.GetDetails())
82+
assert.Equal(t, tt.wantInfo.errorCode, msg.GetErrorCode())
83+
assert.Equal(t, tt.wantInfo.statusCode, msg.GetStatusCode())
84+
assert.Equal(t, tt.wantInfo.service, msg.GetServiceName())
85+
}
86+
87+
// Helper to verify the unwrapped error
88+
verifyUnwrapped := func(t *testing.T, err error) {
89+
var extErr *ExtensionResponseError
90+
if assert.ErrorAs(t, err, &extErr) {
91+
assert.Equal(t, tt.wantInfo.message, extErr.Message)
92+
assert.Equal(t, tt.wantInfo.details, extErr.Details)
93+
assert.Equal(t, tt.wantInfo.errorCode, extErr.ErrorCode)
94+
assert.Equal(t, int(tt.wantInfo.statusCode), extErr.StatusCode)
95+
assert.Equal(t, tt.wantInfo.service, extErr.ServiceName)
96+
}
97+
}
98+
99+
// Test ServiceTarget wrapping
100+
stMsg := WrapErrorForServiceTarget(tt.inputErr)
101+
if tt.inputErr == nil {
102+
assert.Nil(t, stMsg)
103+
} else {
104+
verifyProto(t, stMsg)
105+
verifyUnwrapped(t, UnwrapErrorFromServiceTarget(stMsg))
106+
}
107+
108+
// Test FrameworkService wrapping
109+
fsMsg := WrapErrorForFrameworkService(tt.inputErr)
110+
if tt.inputErr == nil {
111+
assert.Nil(t, fsMsg)
112+
} else {
113+
verifyProto(t, fsMsg)
114+
verifyUnwrapped(t, UnwrapErrorFromFrameworkService(fsMsg))
115+
}
116+
})
117+
}
118+
}

cli/azd/pkg/grpcbroker/message_broker_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type TestMessage struct {
2424
InnerMsg any
2525
IsProgress bool
2626
ProgressText string
27+
TraceParent string
2728
}
2829

2930
// Test request/response types for handler testing
@@ -164,6 +165,14 @@ func (e *SimpleMessageEnvelope) CreateProgressMessage(requestId string, message
164165
}
165166
}
166167

168+
func (e *SimpleMessageEnvelope) GetTraceParent(msg *TestMessage) string {
169+
return msg.TraceParent
170+
}
171+
172+
func (e *SimpleMessageEnvelope) SetTraceParent(msg *TestMessage, traceParent string) {
173+
msg.TraceParent = traceParent
174+
}
175+
167176
// TestOn_RegistersHandler tests that handlers are registered correctly
168177
func TestOn_RegistersHandler(t *testing.T) {
169178
sim := NewSimulatedBidiStream()

0 commit comments

Comments
 (0)