github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/pkg/conversion/struct_test.go (about)

     1  // Copyright 2018 Envoyproxy Authors
     2  //
     3  //   Licensed under the Apache License, Version 2.0 (the "License");
     4  //   you may not use this file except in compliance with the License.
     5  //   You may obtain a copy of the License at
     6  //
     7  //       http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //   Unless required by applicable law or agreed to in writing, software
    10  //   distributed under the License is distributed on an "AS IS" BASIS,
    11  //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //   See the License for the specific language governing permissions and
    13  //   limitations under the License.
    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  	core "github.com/hxx258456/ccgo/go-control-plane/envoy/config/core/v3"
    25  	discovery "github.com/hxx258456/ccgo/go-control-plane/envoy/service/discovery/v3"
    26  	"github.com/hxx258456/ccgo/go-control-plane/pkg/conversion"
    27  )
    28  
    29  func TestConversion(t *testing.T) {
    30  	pb := &discovery.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 := &discovery.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, &discovery.DiscoveryRequest{}); err == nil {
    64  		t.Error("StructToMessage(nil) => got no error")
    65  	}
    66  }