Skip to content

Commit

Permalink
ut: add unit test for pkg/ingress/mcp
Browse files Browse the repository at this point in the history
Signed-off-by: charlie <qianglin98@qq.com>
  • Loading branch information
Charlie17Li committed Dec 8, 2022
1 parent 44aab80 commit 80eb8df
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion pkg/ingress/mcp/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package mcp

// nolint
import (
"path"
"testing"

"github.com/gogo/protobuf/types"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
extensions "istio.io/api/extensions/v1alpha1"
mcp "istio.io/api/mcp/v1alpha1"
networking "istio.io/api/networking/v1alpha3"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/config"
)

Expand Down Expand Up @@ -36,7 +43,7 @@ func Test_generate(t *testing.T) {
},
{
Name: "WasmPlugin",
Spec: extensions.WasmPlugin{},
Spec: &extensions.WasmPlugin{},
IsErr: false,
},
{
Expand All @@ -58,3 +65,65 @@ func Test_generate(t *testing.T) {
})
}
}

func Test_Marshal(t *testing.T) {
cfg := config.Config{
Spec: &networking.VirtualService{
Hosts: []string{"127.0.0.1", "192.168.0.1"},
},
}

val1, _, err := generate(nil, []config.Config{cfg}, nil, nil)
if err != nil {
t.Fatalf("failed to call generate: %v", err)
}

val2, _, err := generate_VirtualService(nil, []config.Config{cfg}, nil, nil)
if err != nil {
t.Fatalf("failed to call generate_VirtualService: %v", err)
}

c1, c2 := &mcp.Resource{}, &mcp.Resource{}
err = ptypes.UnmarshalAny(val1[0], c1) // nolint
if err != nil {
t.Fatal(err)
}

err = ptypes.UnmarshalAny(val2[0], c2) // nolint
if err != nil {
t.Fatal(err)
}

if !c1.Body.Equal(c2.Body) {
t.Fatalf("Marshal failed")
}
}

func generate_VirtualService(proxy *model.Proxy, configs []config.Config, w *model.WatchedResource,
updates *model.PushRequest) ([]*any.Any, model.XdsLogDetails, error) {
resources := make([]*any.Any, 0)
for _, cfg := range configs {
body, err := types.MarshalAny(cfg.Spec.(*networking.VirtualService))
if err != nil {
return nil, model.DefaultXdsLogDetails, err
}
createTime, err := types.TimestampProto(cfg.CreationTimestamp)
if err != nil {
return nil, model.DefaultXdsLogDetails, err
}
resource := &mcp.Resource{
Body: body,
Metadata: &mcp.Metadata{
Name: path.Join(cfg.Namespace, cfg.Name),
CreateTime: createTime,
},
}

mcpAny, err := ptypes.MarshalAny(resource) // nolint
if err != nil {
return nil, model.DefaultXdsLogDetails, err
}
resources = append(resources, mcpAny)
}
return resources, model.DefaultXdsLogDetails, nil
}

0 comments on commit 80eb8df

Please sign in to comment.