...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package conversion_test
16
17 import (
18 "testing"
19
20 "github.com/golang/protobuf/proto"
21 pstruct "github.com/golang/protobuf/ptypes/struct"
22 "github.com/google/go-cmp/cmp"
23
24 v2 "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2"
25 core "github.com/datawire/ambassador/v2/pkg/api/envoy/api/v2/core"
26 "github.com/datawire/ambassador/v2/pkg/envoy-control-plane/conversion"
27 )
28
29 func TestConversion(t *testing.T) {
30 pb := &v2.DiscoveryRequest{
31 VersionInfo: "test",
32 Node: &core.Node{Id: "proxy"},
33 }
34 st, err := conversion.MessageToStruct(pb)
35 if err != nil {
36 t.Fatalf("unexpected error %v", err)
37 }
38 pbst := map[string]*pstruct.Value{
39 "version_info": {Kind: &pstruct.Value_StringValue{StringValue: "test"}},
40 "node": {Kind: &pstruct.Value_StructValue{StructValue: &pstruct.Struct{
41 Fields: map[string]*pstruct.Value{
42 "id": {Kind: &pstruct.Value_StringValue{StringValue: "proxy"}},
43 },
44 }}},
45 }
46 if !cmp.Equal(st.Fields, pbst, cmp.Comparer(proto.Equal)) {
47 t.Errorf("MessageToStruct(%v) => got %v, want %v", pb, st.Fields, pbst)
48 }
49
50 out := &v2.DiscoveryRequest{}
51 err = conversion.StructToMessage(st, out)
52 if err != nil {
53 t.Fatalf("unexpected error %v", err)
54 }
55 if !cmp.Equal(pb, out, cmp.Comparer(proto.Equal)) {
56 t.Errorf("StructToMessage(%v) => got %v, want %v", st, out, pb)
57 }
58
59 if _, err = conversion.MessageToStruct(nil); err == nil {
60 t.Error("MessageToStruct(nil) => got no error")
61 }
62
63 if err = conversion.StructToMessage(nil, &v2.DiscoveryRequest{}); err == nil {
64 t.Error("StructToMessage(nil) => got no error")
65 }
66 }
67
View as plain text