...

Source file src/github.com/emissary-ingress/emissary/v3/pkg/api/getambassador.io/v2/crd_mapping_test.go

Documentation: github.com/emissary-ingress/emissary/v3/pkg/api/getambassador.io/v2

     1  package v2_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"sigs.k8s.io/yaml"
     9  
    10  	crds "github.com/emissary-ingress/emissary/v3/pkg/api/getambassador.io/v2"
    11  )
    12  
    13  func TestMappingLabelSpecifier(t *testing.T) {
    14  	t.Parallel()
    15  	type TestResource struct {
    16  		Field crds.MappingLabelSpecifier `json:"field,omitempty"`
    17  	}
    18  	type subtest struct {
    19  		inputYAML      string
    20  		expectedStruct TestResource
    21  		expectedJSON   string
    22  		expectedErr    string
    23  	}
    24  	subtests := map[string]subtest{
    25  		"empty":         {`{}`, TestResource{}, `{"field":null}`, ``},
    26  		"explicitEmpty": {`field:`, TestResource{}, `{"field":null}`, ``},
    27  		"explicitnull":  {`field: null`, TestResource{}, `{"field":null}`, ``},
    28  		"explicitNull":  {`field: Null`, TestResource{}, `{"field":null}`, ``},
    29  		"explicitNULL":  {`field: NULL`, TestResource{}, `{"field":null}`, ``},
    30  		"explicitTilde": {`field: ~`, TestResource{}, `{"field":null}`, ``},
    31  		"list": {`field: ["foo"]`, TestResource{}, `{"field":null}`,
    32  			"error unmarshaling JSON: while decoding JSON: could not unmarshal MappingLabelSpecifier: invalid input"},
    33  		"invalid": {``, TestResource{crds.MappingLabelSpecifier{String: stringPtr("foo"), Generic: &crds.MappingLabelSpecGeneric{GenericKey: "foo"}}}, ``,
    34  			"json: error calling MarshalJSON for type v2.MappingLabelSpecifier: invalid MappingLabelSpecifier"},
    35  	}
    36  	for name, info := range subtests {
    37  		info := info // capture loop variable
    38  		t.Run(name, func(t *testing.T) {
    39  			t.Parallel()
    40  			if info.inputYAML == `` {
    41  				jsonbytes, err := json.Marshal(info.expectedStruct)
    42  				assert.Equal(t, info.expectedJSON, string(jsonbytes))
    43  				assert.EqualError(t, err, info.expectedErr)
    44  			} else {
    45  				var parsed TestResource
    46  				err := yaml.Unmarshal([]byte(info.inputYAML), &parsed)
    47  				if info.expectedErr == `` {
    48  					assert.NoError(t, err)
    49  				} else {
    50  					assert.EqualError(t, err, info.expectedErr)
    51  				}
    52  				assert.Equal(t, info.expectedStruct, parsed)
    53  				jsonbytes, err := json.Marshal(parsed)
    54  				assert.NoError(t, err)
    55  				assert.Equal(t, info.expectedJSON, string(jsonbytes))
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestAddedHeader(t *testing.T) {
    62  	t.Parallel()
    63  	var testResource struct {
    64  		Field crds.AddedHeader `json:"field,omitempty"`
    65  	}
    66  	assert.EqualError(t, yaml.Unmarshal([]byte(`field: ["foo"]`), &testResource),
    67  		"error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go struct field .field of type v2.AddedHeaderFull")
    68  }
    69  
    70  func TestOriginList(t *testing.T) {
    71  	t.Parallel()
    72  	type TestResource struct {
    73  		Field crds.OriginList `json:"field,omitempty"`
    74  	}
    75  	type subtest struct {
    76  		inputYAML      string
    77  		expectedStruct TestResource
    78  		expectedJSON   string
    79  		expectedErr    string
    80  	}
    81  	subtests := map[string]subtest{
    82  		"empty":         {`{}`, TestResource{}, `{"field":null}`, ``},
    83  		"explicitEmpty": {`field:`, TestResource{}, `{"field":null}`, ``},
    84  		"explicitnull":  {`field: null`, TestResource{}, `{"field":null}`, ``},
    85  		"explicitNull":  {`field: Null`, TestResource{}, `{"field":null}`, ``},
    86  		"explicitNULL":  {`field: NULL`, TestResource{}, `{"field":null}`, ``},
    87  		"explicitTilde": {`field: ~`, TestResource{}, `{"field":null}`, ``},
    88  		"object": {`field: {"foo": "bar"}`, TestResource{}, `{"field":null}`,
    89  			"error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go struct field TestResource.field of type []string"},
    90  	}
    91  	for name, info := range subtests {
    92  		info := info // capture loop variable
    93  		t.Run(name, func(t *testing.T) {
    94  			t.Parallel()
    95  			var parsed TestResource
    96  			err := yaml.Unmarshal([]byte(info.inputYAML), &parsed)
    97  			if info.expectedErr == `` {
    98  				assert.NoError(t, err)
    99  			} else {
   100  				assert.EqualError(t, err, info.expectedErr)
   101  			}
   102  			assert.Equal(t, info.expectedStruct, parsed)
   103  			jsonbytes, err := json.Marshal(parsed)
   104  			assert.NoError(t, err)
   105  			assert.Equal(t, info.expectedJSON, string(jsonbytes))
   106  		})
   107  	}
   108  }
   109  

View as plain text