mirror of
https://github.com/alibaba/higress.git
synced 2026-06-07 03:37:28 +08:00
unit-test: increase coverage, path: pkg/ingress/kube/util (#99)
Signed-off-by: iutx <root@viper.run>
This commit is contained in:
@@ -20,10 +20,24 @@ import (
|
|||||||
corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
||||||
wasm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/wasm/v3"
|
wasm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/wasm/v3"
|
||||||
v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/wasm/v3"
|
v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/wasm/v3"
|
||||||
any "google.golang.org/protobuf/types/known/anypb"
|
"github.com/gogo/protobuf/types"
|
||||||
|
"github.com/golang/protobuf/proto"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"google.golang.org/protobuf/types/known/anypb"
|
||||||
|
wrappers "google.golang.org/protobuf/types/known/wrapperspb"
|
||||||
"istio.io/istio/pilot/pkg/model"
|
"istio.io/istio/pilot/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestString(t *testing.T) {
|
||||||
|
assert.Equal(t, "cluster/foo/bar", ClusterNamespacedName{
|
||||||
|
NamespacedName: model.NamespacedName{
|
||||||
|
Name: "bar",
|
||||||
|
Namespace: "foo",
|
||||||
|
},
|
||||||
|
ClusterId: "cluster",
|
||||||
|
}.String())
|
||||||
|
}
|
||||||
|
|
||||||
func TestSplitNamespacedName(t *testing.T) {
|
func TestSplitNamespacedName(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
input string
|
input string
|
||||||
@@ -77,32 +91,170 @@ func TestCreateDestinationRuleName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMessageToGoGoStruct(t *testing.T) {
|
func TestMessageToGoGoStruct(t *testing.T) {
|
||||||
bytes := []byte("test")
|
testStr := "hello, world"
|
||||||
wasm := &wasm.Wasm{
|
testCases := []struct {
|
||||||
Config: &v3.PluginConfig{
|
name string
|
||||||
Name: "basic-auth",
|
getMsg func() (proto.Message, error)
|
||||||
FailOpen: true,
|
expect *types.Struct
|
||||||
Vm: &v3.PluginConfig_VmConfig{
|
wantErr bool
|
||||||
VmConfig: &v3.VmConfig{
|
}{
|
||||||
Runtime: "envoy.wasm.runtime.null",
|
{
|
||||||
Code: &corev3.AsyncDataSource{
|
name: "message is nil",
|
||||||
Specifier: &corev3.AsyncDataSource_Local{
|
getMsg: func() (proto.Message, error) {
|
||||||
Local: &corev3.DataSource{
|
return nil, nil
|
||||||
Specifier: &corev3.DataSource_InlineString{
|
},
|
||||||
InlineString: "envoy.wasm.basic_auth",
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "marshal error",
|
||||||
|
getMsg: func() (proto.Message, error) {
|
||||||
|
return &wasm.Wasm{
|
||||||
|
Config: &v3.PluginConfig{
|
||||||
|
Name: "error-config",
|
||||||
|
Configuration: &anypb.Any{
|
||||||
|
TypeUrl: "type.googleapis.com/google.protobuf.StringValue",
|
||||||
|
Value: []byte(testStr),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "case 1",
|
||||||
|
getMsg: func() (proto.Message, error) {
|
||||||
|
bytesVal, err := proto.Marshal(wrappers.String(testStr))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &wasm.Wasm{
|
||||||
|
Config: &v3.PluginConfig{
|
||||||
|
Name: "basic-auth",
|
||||||
|
FailOpen: true,
|
||||||
|
Vm: &v3.PluginConfig_VmConfig{
|
||||||
|
VmConfig: &v3.VmConfig{
|
||||||
|
Runtime: "envoy.wasm.runtime.null",
|
||||||
|
Code: &corev3.AsyncDataSource{
|
||||||
|
Specifier: &corev3.AsyncDataSource_Local{
|
||||||
|
Local: &corev3.DataSource{
|
||||||
|
Specifier: &corev3.DataSource_InlineString{
|
||||||
|
InlineString: "envoy.wasm.basic_auth",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Configuration: &anypb.Any{
|
||||||
|
TypeUrl: "type.googleapis.com/google.protobuf.StringValue",
|
||||||
|
Value: bytesVal,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
expect: &types.Struct{
|
||||||
|
Fields: map[string]*types.Value{
|
||||||
|
"config": {
|
||||||
|
Kind: &types.Value_StructValue{
|
||||||
|
StructValue: &types.Struct{
|
||||||
|
Fields: map[string]*types.Value{
|
||||||
|
"name": {
|
||||||
|
Kind: &types.Value_StringValue{
|
||||||
|
StringValue: "basic-auth",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"fail_open": {
|
||||||
|
Kind: &types.Value_BoolValue{
|
||||||
|
BoolValue: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"vm_config": {
|
||||||
|
Kind: &types.Value_StructValue{
|
||||||
|
StructValue: &types.Struct{
|
||||||
|
Fields: map[string]*types.Value{
|
||||||
|
"runtime": {
|
||||||
|
Kind: &types.Value_StringValue{
|
||||||
|
StringValue: "envoy.wasm.runtime.null",
|
||||||
|
}},
|
||||||
|
"code": {
|
||||||
|
Kind: &types.Value_StructValue{
|
||||||
|
StructValue: &types.Struct{
|
||||||
|
Fields: map[string]*types.Value{
|
||||||
|
"local": {
|
||||||
|
Kind: &types.Value_StructValue{
|
||||||
|
StructValue: &types.Struct{
|
||||||
|
Fields: map[string]*types.Value{
|
||||||
|
"inline_string": {
|
||||||
|
Kind: &types.Value_StringValue{
|
||||||
|
StringValue: "envoy.wasm.basic_auth",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"configuration": {
|
||||||
|
Kind: &types.Value_StructValue{
|
||||||
|
StructValue: &types.Struct{
|
||||||
|
Fields: map[string]*types.Value{
|
||||||
|
"@type": {
|
||||||
|
Kind: &types.Value_StringValue{
|
||||||
|
StringValue: "type.googleapis.com/google.protobuf.StringValue",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"value": {
|
||||||
|
Kind: &types.Value_StringValue{
|
||||||
|
StringValue: testStr,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Configuration: &any.Any{
|
|
||||||
TypeUrl: "type.googleapis.com/google.protobuf.StringValue",
|
|
||||||
Value: bytes,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
gogoStruct, _ := MessageToGoGoStruct(wasm)
|
for _, tt := range testCases {
|
||||||
t.Log(gogoStruct)
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// get proto.Message
|
||||||
|
msg, err := tt.getMsg()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("getMsg() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := MessageToGoGoStruct(msg)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("MessageToGoGoStruct() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !proto.Equal(got, tt.expect) {
|
||||||
|
t.Errorf("MessageToGoGoStruct() got = %v, want %v", got, tt.expect)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateServiceFQDN(t *testing.T) {
|
||||||
|
namespace := "default"
|
||||||
|
serviceName := "go-httpbin-v1"
|
||||||
|
expect := "go-httpbin-v1.default.svc.cluster.local"
|
||||||
|
|
||||||
|
got := CreateServiceFQDN(namespace, serviceName)
|
||||||
|
t.Log(got)
|
||||||
|
assert.Equal(t, got, expect)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user