2022-11-10 17:09:06 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package accesslog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2023-06-09 01:28:37 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/tracer"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2022-11-10 17:09:06 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-12 16:09:39 +08:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.uber.org/zap"
|
2022-11-10 17:09:06 +08:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetAccessAddr(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
addr := getAccessAddr(ctx)
|
|
|
|
assert.Equal(t, "Unknown", addr)
|
|
|
|
|
|
|
|
newctx := peer.NewContext(
|
|
|
|
ctx,
|
|
|
|
&peer.Peer{
|
|
|
|
Addr: &net.IPAddr{
|
|
|
|
IP: net.IPv4(0, 0, 0, 0),
|
|
|
|
Zone: "test",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
addr = getAccessAddr(newctx)
|
|
|
|
assert.Equal(t, "ip-0.0.0.0%test", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTraceID(t *testing.T) {
|
2023-01-12 16:09:39 +08:00
|
|
|
paramtable.Init()
|
|
|
|
tracer.Init()
|
|
|
|
|
2022-11-10 17:09:06 +08:00
|
|
|
ctx := context.Background()
|
2023-01-12 16:09:39 +08:00
|
|
|
traceID, ok := getTraceID(ctx)
|
|
|
|
log.Debug("traceID", zap.String("id", traceID))
|
2022-11-10 17:09:06 +08:00
|
|
|
assert.False(t, ok)
|
|
|
|
|
2023-01-12 16:09:39 +08:00
|
|
|
traceContext, traceSpan := otel.Tracer("proxy").Start(ctx, "demo")
|
|
|
|
trueTraceID := traceSpan.SpanContext().TraceID().String()
|
|
|
|
log.Debug("traceID", zap.String("trueTraceID", trueTraceID))
|
2022-11-10 17:09:06 +08:00
|
|
|
ID, ok := getTraceID(traceContext)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, trueTraceID, ID)
|
|
|
|
|
|
|
|
ctx = metadata.AppendToOutgoingContext(ctx, clientRequestIDKey, "test")
|
|
|
|
ID, ok = getTraceID(ctx)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "test", ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetResponseSize(t *testing.T) {
|
|
|
|
resp := &milvuspb.BoolResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
Value: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := getResponseSize(nil)
|
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
_, ok = getResponseSize(resp)
|
|
|
|
assert.True(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetErrCode(t *testing.T) {
|
|
|
|
resp := &milvuspb.BoolResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UnexpectedError,
|
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
Value: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := getErrCode(nil)
|
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
code, ok := getErrCode(resp)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, int(commonpb.ErrorCode_UnexpectedError), code)
|
|
|
|
}
|