You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.7 KiB
107 lines
2.7 KiB
package tracing |
|
|
|
import ( |
|
"context" |
|
"reflect" |
|
"testing" |
|
|
|
"github.com/go-kratos/kratos/v2" |
|
"github.com/go-kratos/kratos/v2/metadata" |
|
|
|
"go.opentelemetry.io/otel/propagation" |
|
) |
|
|
|
func TestMetadata_Inject(t *testing.T) { |
|
type args struct { |
|
appName string |
|
carrier propagation.TextMapCarrier |
|
} |
|
tests := []struct { |
|
name string |
|
args args |
|
want string |
|
}{ |
|
{ |
|
name: "https://go-kratos.dev", |
|
args: args{"https://go-kratos.dev", propagation.HeaderCarrier{}}, |
|
want: "https://go-kratos.dev", |
|
}, |
|
{ |
|
name: "https://github.com/go-kratos/kratos", |
|
args: args{"https://github.com/go-kratos/kratos", propagation.HeaderCarrier{"mode": []string{"test"}}}, |
|
want: "https://github.com/go-kratos/kratos", |
|
}, |
|
} |
|
for _, tt := range tests { |
|
t.Run(tt.name, func(t *testing.T) { |
|
a := kratos.New(kratos.Name(tt.args.appName)) |
|
ctx := kratos.NewContext(context.Background(), a) |
|
m := new(Metadata) |
|
m.Inject(ctx, tt.args.carrier) |
|
if res := tt.args.carrier.Get(serviceHeader); tt.want != res { |
|
t.Errorf("Get(serviceHeader) :%s want: %s", res, tt.want) |
|
} |
|
}) |
|
} |
|
} |
|
|
|
func TestMetadata_Extract(t *testing.T) { |
|
type args struct { |
|
parent context.Context |
|
carrier propagation.TextMapCarrier |
|
} |
|
tests := []struct { |
|
name string |
|
args args |
|
want string |
|
crash bool |
|
}{ |
|
{ |
|
name: "https://go-kratos.dev", |
|
args: args{ |
|
parent: context.Background(), |
|
carrier: propagation.HeaderCarrier{"X-Md-Service-Name": []string{"https://go-kratos.dev"}}, |
|
}, |
|
want: "https://go-kratos.dev", |
|
}, |
|
{ |
|
name: "https://github.com/go-kratos/kratos", |
|
args: args{ |
|
parent: metadata.NewServerContext(context.Background(), metadata.Metadata{}), |
|
carrier: propagation.HeaderCarrier{"X-Md-Service-Name": []string{"https://github.com/go-kratos/kratos"}}, |
|
}, |
|
want: "https://github.com/go-kratos/kratos", |
|
}, |
|
{ |
|
name: "https://github.com/go-kratos/kratos", |
|
args: args{ |
|
parent: metadata.NewServerContext(context.Background(), metadata.Metadata{}), |
|
carrier: propagation.HeaderCarrier{"X-Md-Service-Name": nil}, |
|
}, |
|
crash: true, |
|
}, |
|
} |
|
for _, tt := range tests { |
|
t.Run(tt.name, func(t *testing.T) { |
|
b := Metadata{} |
|
ctx := b.Extract(tt.args.parent, tt.args.carrier) |
|
md, ok := metadata.FromServerContext(ctx) |
|
if !ok { |
|
if tt.crash { |
|
return |
|
} |
|
t.Errorf("expect %v, got %v", true, ok) |
|
} |
|
if !reflect.DeepEqual(md.Get(serviceHeader), tt.want) { |
|
t.Errorf("expect %v, got %v", tt.want, md.Get(serviceHeader)) |
|
} |
|
}) |
|
} |
|
} |
|
|
|
func TestFields(t *testing.T) { |
|
b := Metadata{} |
|
if !reflect.DeepEqual(b.Fields(), []string{"x-md-service-name"}) { |
|
t.Errorf("expect %v, got %v", []string{"x-md-service-name"}, b.Fields()) |
|
} |
|
}
|
|
|