consul/proto/pbcommon/convert_pbstruct_test.go
Michael Wilkerson 6e8716cfe7
Ent merge move envoy extension proto (#16126)
* Mw/lambda envoy extension parse region (#4107)

* updated builtin extension to parse region directly from ARN
- added a unit test
- added some comments/light refactoring

* updated golden files with proper ARNs
- ARNs need to be right format now that they are being processed

* updated tests and integration tests
- removed 'region' from all EnvoyExtension arguments
- added properly formatted ARN which includes the same region found in the removed "Region" field: 'us-east-1'

* regenerated proto files
2023-02-01 11:42:32 -08:00

129 lines
2.6 KiB
Go

package pbcommon
import (
"testing"
"github.com/hashicorp/consul/agent/structs"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/structpb"
)
func TestEnvoyExtensionsToStructs(t *testing.T) {
input := []*EnvoyExtension{
{
Name: "ext1",
Required: true,
Arguments: &structpb.Struct{
Fields: map[string]*structpb.Value{
"field1": {Kind: &structpb.Value_StringValue{StringValue: "value1"}},
"field2": {Kind: &structpb.Value_NumberValue{NumberValue: 3.14}},
},
},
},
{
Name: "ext2",
Required: false,
Arguments: &structpb.Struct{
Fields: map[string]*structpb.Value{
"field3": {Kind: &structpb.Value_StringValue{StringValue: "value2"}},
"field4": {Kind: &structpb.Value_NumberValue{NumberValue: 2.718}},
},
},
},
}
expected := []structs.EnvoyExtension{
{
Name: "ext1",
Required: true,
Arguments: map[string]interface{}{
"field1": "value1",
"field2": 3.14,
},
},
{
Name: "ext2",
Required: false,
Arguments: map[string]interface{}{
"field3": "value2",
"field4": 2.718,
},
},
}
result := EnvoyExtensionsToStructs(input)
assert.Equal(t, expected, result)
}
func TestEnvoyExtensionsFromStructs(t *testing.T) {
input := []structs.EnvoyExtension{
{
Name: "ext1",
Required: true,
Arguments: map[string]interface{}{
"field1": "value1",
"field2": 3.14,
},
},
{
Name: "ext2",
Required: false,
Arguments: map[string]interface{}{
"field3": "value2",
"field4": 2.718,
},
},
}
expected := []*EnvoyExtension{
{
Name: "ext1",
Required: true,
Arguments: &structpb.Struct{
Fields: map[string]*structpb.Value{
"field1": {Kind: &structpb.Value_StringValue{StringValue: "value1"}},
"field2": {Kind: &structpb.Value_NumberValue{NumberValue: 3.14}},
},
},
},
{
Name: "ext2",
Required: false,
Arguments: &structpb.Struct{
Fields: map[string]*structpb.Value{
"field3": {Kind: &structpb.Value_StringValue{StringValue: "value2"}},
"field4": {Kind: &structpb.Value_NumberValue{NumberValue: 2.718}},
},
},
},
}
result := EnvoyExtensionsFromStructs(input)
assert.Equal(t, expected, result)
}
func TestSliceToPBListValue(t *testing.T) {
s := []interface{}{1, 2, 3}
expected, _ := structpb.NewList(s)
tests := []struct {
input []interface{}
expected *structpb.ListValue
}{
{
[]interface{}{1, 2, 3},
expected,
},
{
[]interface{}{},
nil,
},
{
nil,
nil,
},
}
for _, tc := range tests {
result := SliceToPBListValue(tc.input)
assert.Equal(t, tc.expected, result)
}
}