github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/utils/strings_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"strings"
     7  	"testing"
     8  
     9  	envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestFromYamlToPb(t *testing.T) {
    14  	cases := []struct {
    15  		name    string
    16  		yamlStr string
    17  		pbMsg   *envoy_core_v3.Address
    18  		errMsg  string
    19  	}{
    20  		{
    21  			"Deserialize correctly",
    22  			`socket_address:
    23    address: "1.2.3.4"
    24    port_value: 1234`,
    25  			&envoy_core_v3.Address{
    26  				Address: &envoy_core_v3.Address_SocketAddress{
    27  					SocketAddress: &envoy_core_v3.SocketAddress{
    28  						Address:       "1.2.3.4",
    29  						PortSpecifier: &envoy_core_v3.SocketAddress_PortValue{PortValue: 1234},
    30  					},
    31  				},
    32  			},
    33  			"",
    34  		},
    35  		{
    36  			"Deserialize bad YAML",
    37  			`AAAAAAA`,
    38  			&envoy_core_v3.Address{},
    39  			"json: cannot unmarshal string into Go value of type map[string]json.RawMessage",
    40  		},
    41  		{
    42  			"Deserialize good YAML but incompatible with Proto Msg",
    43  			`A: 1`,
    44  			&envoy_core_v3.Address{},
    45  			"unknown field \"A\" in envoy.config.core.v3.Address",
    46  		},
    47  	}
    48  
    49  	for _, test := range cases {
    50  		t.Run(test.name, func(t *testing.T) {
    51  			var obj envoy_core_v3.Address
    52  			err := FromYamlToPb(strings.NewReader(test.yamlStr), &obj)
    53  
    54  			if test.errMsg != "" {
    55  				assert.EqualError(t, err, test.errMsg)
    56  			} else {
    57  				assert.Nil(t, err)
    58  				assert.Equal(t, test.pbMsg.GetSocketAddress().GetAddress(), obj.GetSocketAddress().GetAddress())
    59  				assert.Equal(t, test.pbMsg.GetSocketAddress().GetPortValue(), obj.GetSocketAddress().GetPortValue())
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestFromPbToYAML(t *testing.T) {
    66  	cases := []struct {
    67  		name         string
    68  		pbMsg        *envoy_core_v3.Address
    69  		yamlContains string
    70  		errMsg       string
    71  	}{
    72  		{
    73  			"YAML contains address",
    74  			&envoy_core_v3.Address{
    75  				Address: &envoy_core_v3.Address_SocketAddress{
    76  					SocketAddress: &envoy_core_v3.SocketAddress{
    77  						Address:       "1.2.3.4",
    78  						PortSpecifier: &envoy_core_v3.SocketAddress_PortValue{PortValue: 1234},
    79  					},
    80  				},
    81  			},
    82  			"address: 1.2.3.4",
    83  			"",
    84  		},
    85  		{
    86  			"YAML does not contains address",
    87  			&envoy_core_v3.Address{
    88  				Address: &envoy_core_v3.Address_SocketAddress{},
    89  			},
    90  			"socket_address: {}",
    91  			"",
    92  		},
    93  	}
    94  
    95  	for _, test := range cases {
    96  		t.Run(test.name, func(t *testing.T) {
    97  			buffer := bytes.Buffer{}
    98  			writer := bufio.NewWriter(&buffer)
    99  
   100  			err := FromPbToYaml(writer, test.pbMsg)
   101  			if test.errMsg != "" {
   102  				assert.NotNil(t, err)
   103  				assert.ErrorContains(t, err, test.errMsg)
   104  			} else {
   105  				writer.Flush()
   106  
   107  				assert.Nil(t, err)
   108  				assert.Contains(t, string(buffer.Bytes()), test.yamlContains)
   109  			}
   110  		})
   111  	}
   112  }