...

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

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

     1  package v2
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"path"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"sigs.k8s.io/yaml"
    12  )
    13  
    14  func TestAuthSvcRoundTrip(t *testing.T) {
    15  	var a []AuthService
    16  	checkRoundtrip(t, "authsvc.yaml", &a)
    17  }
    18  
    19  func TestDevPortalRoundTrip(t *testing.T) {
    20  	var d []DevPortal
    21  	checkRoundtrip(t, "devportals.yaml", &d)
    22  }
    23  
    24  func TestHostRoundTrip(t *testing.T) {
    25  	var h []Host
    26  	checkRoundtrip(t, "hosts.yaml", &h)
    27  }
    28  
    29  func TestLogSvcRoundTrip(t *testing.T) {
    30  	var l []LogService
    31  	checkRoundtrip(t, "logsvc.yaml", &l)
    32  }
    33  
    34  func TestMappingRoundTrip(t *testing.T) {
    35  	var m []Mapping
    36  	checkRoundtrip(t, "mappings.yaml", &m)
    37  }
    38  
    39  func TestModuleRoundTrip(t *testing.T) {
    40  	var m []Module
    41  	checkRoundtrip(t, "modules.yaml", &m)
    42  }
    43  
    44  func TestRateLimitSvcRoundTrip(t *testing.T) {
    45  	var r []RateLimitService
    46  	checkRoundtrip(t, "ratelimitsvc.yaml", &r)
    47  }
    48  
    49  func TestTCPMappingRoundTrip(t *testing.T) {
    50  	var tm []TCPMapping
    51  	checkRoundtrip(t, "tcpmappings.yaml", &tm)
    52  }
    53  
    54  func TestTLSContextRoundTrip(t *testing.T) {
    55  	var tc []TLSContext
    56  	checkRoundtrip(t, "tlscontexts.yaml", &tc)
    57  }
    58  
    59  func TestTracingSvcRoundTrip(t *testing.T) {
    60  	var tr []TracingService
    61  	checkRoundtrip(t, "tracingsvc.yaml", &tr)
    62  }
    63  
    64  func checkRoundtrip(t *testing.T, filename string, ptr interface{}) {
    65  	bytes, err := ioutil.ReadFile(path.Join("testdata", filename))
    66  	require.NoError(t, err)
    67  
    68  	canonical := func() string {
    69  		var untyped interface{}
    70  		require.NoError(t, yaml.Unmarshal(bytes, &untyped))
    71  		canonical, err := json.MarshalIndent(untyped, "", "\t")
    72  		require.NoError(t, err)
    73  		return string(canonical)
    74  	}()
    75  
    76  	actual := func() string {
    77  		// Round-trip twice, to get map field ordering, instead of struct field ordering.
    78  
    79  		// first
    80  		require.NoError(t, yaml.Unmarshal(bytes, ptr))
    81  		first, err := json.Marshal(ptr)
    82  		require.NoError(t, err)
    83  
    84  		// second
    85  		var untyped interface{}
    86  		require.NoError(t, json.Unmarshal(first, &untyped))
    87  		second, err := json.MarshalIndent(untyped, "", "\t")
    88  		require.NoError(t, err)
    89  
    90  		return string(second)
    91  	}()
    92  
    93  	assert.Equal(t, canonical, actual)
    94  }
    95  

View as plain text