-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ut: add unit test for pkg/ingress/mcp
Signed-off-by: charlie <qianglin98@qq.com>
- Loading branch information
1 parent
4776d62
commit 16b5b0e
Showing
1 changed file
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
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" | ||
) | ||
|
||
func Test_generate(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Spec config.Spec | ||
IsErr bool | ||
}{ | ||
{ | ||
Name: "VirtualService", | ||
Spec: &networking.VirtualService{}, | ||
IsErr: false, | ||
}, | ||
{ | ||
Name: "Gateway", | ||
Spec: &networking.Gateway{}, | ||
IsErr: false, | ||
}, | ||
{ | ||
Name: "EnvoyFilter", | ||
Spec: &networking.EnvoyFilter{}, | ||
IsErr: false, | ||
}, | ||
{ | ||
Name: "DestinationRule", | ||
Spec: &networking.DestinationRule{}, | ||
IsErr: false, | ||
}, | ||
{ | ||
Name: "WasmPlugin", | ||
Spec: &extensions.WasmPlugin{}, | ||
IsErr: false, | ||
}, | ||
{ | ||
Name: "string", | ||
Spec: "test", | ||
IsErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
cfg := config.Config{ | ||
Spec: test.Spec, | ||
} | ||
_, _, err := generate(nil, []config.Config{cfg}, nil, nil) | ||
if (err != nil && !test.IsErr) || (err == nil && test.IsErr) { | ||
t.Errorf("Failed to generate config: %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
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 | ||
} |