Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ut: add unit test for pkg/ingress/mcp #104

Merged
merged 7 commits into from
Feb 1, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions pkg/ingress/mcp/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package mcp

import (
"testing"

"github.com/golang/protobuf/ptypes"
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"
)

func TestGenerate(t *testing.T) {
tests := []struct {
name string
fn func() *model.PushContext
generator model.McpResourceGenerator
isErr bool
}{
{
name: "VirtualService",
fn: func() *model.PushContext {
ctx := model.NewPushContext()
cfg := config.Config{
Spec: &networking.VirtualService{},
}
ctx.AllVirtualServices = []config.Config{cfg}
return ctx
},
generator: VirtualServiceGenerator{},
isErr: false,
},
{
name: "Gateway",
fn: func() *model.PushContext {
ctx := model.NewPushContext()
cfg := config.Config{
Spec: &networking.Gateway{},
}
ctx.AllGateways = []config.Config{cfg}
return ctx
},
generator: GatewayGenerator{},
isErr: false,
},
{
name: "EnvoyFilter",
fn: func() *model.PushContext {
ctx := model.NewPushContext()
cfg := config.Config{
Spec: &networking.EnvoyFilter{},
}
ctx.AllEnvoyFilters = []config.Config{cfg}
return ctx
},
generator: EnvoyFilterGenerator{},
isErr: false,
},
{
name: "DestinationRule",
fn: func() *model.PushContext {
ctx := model.NewPushContext()
cfg := config.Config{
Spec: &networking.DestinationRule{},
}
ctx.AllDestinationRules = []config.Config{cfg}
return ctx
},
generator: DestinationRuleGenerator{},
isErr: false,
},
{
name: "WasmPlugin",
fn: func() *model.PushContext {
ctx := model.NewPushContext()
cfg := config.Config{
Spec: &extensions.WasmPlugin{},
}
ctx.AllWasmplugins = []config.Config{cfg}
return ctx
},
generator: WasmpluginGenerator{},
isErr: false,
},
{
name: "WasmPlugin with wrong config",
fn: func() *model.PushContext {
ctx := model.NewPushContext()
cfg := config.Config{
Spec: "string",
}
ctx.AllWasmplugins = []config.Config{cfg}
return ctx
},
generator: WasmpluginGenerator{},
isErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, _, err := test.generator.Generate(nil, test.fn(), nil, nil)
if (err != nil && !test.isErr) || (err == nil && test.isErr) {
t.Errorf("Failed to generate config: %v", err)
}
})
}
}

func TestMarshal(t *testing.T) {
Charlie17Li marked this conversation as resolved.
Show resolved Hide resolved
ctx := model.NewPushContext()
cfg := config.Config{
Spec: &networking.VirtualService{
Charlie17Li marked this conversation as resolved.
Show resolved Hide resolved
Hosts: []string{"127.0.0.1", "192.168.0.1"},
},
}
ctx.AllVirtualServices = []config.Config{cfg}

generator := VirtualServiceGenerator{}

val1, _, err := generator.Generate(nil, ctx, nil, nil)
if err != nil {
t.Fatalf("failed to call generate: %v", err)
}

val2, _, err := generator.Generate(nil, ctx, nil, nil)
if err != nil {
t.Fatalf("failed to call generate: %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) {
Charlie17Li marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("Marshal failed")
}
}