...

Source file src/github.com/emissary-ingress/emissary/v3/pkg/kates/manifests_test.go

Documentation: github.com/emissary-ingress/emissary/v3/pkg/kates

     1  package kates
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  	gw "sigs.k8s.io/gateway-api/apis/v1alpha1"
     9  
    10  	amb "github.com/emissary-ingress/emissary/v3/pkg/api/getambassador.io/v3alpha1"
    11  )
    12  
    13  func TestMergeUpdate(t *testing.T) {
    14  	a := &Unstructured{}
    15  	a.Object = map[string]interface{}{
    16  		"apiVersion": "vtest",
    17  		"kind":       "Foo",
    18  		"metadata": map[string]interface{}{
    19  			"name":            "foo",
    20  			"namespace":       "default",
    21  			"resourceVersion": "1234",
    22  			"labels": map[string]interface{}{
    23  				"foo": "bar",
    24  			},
    25  			"annotations": map[string]interface{}{
    26  				"moo": "arf",
    27  			},
    28  		},
    29  		"spec": map[string]interface{}{
    30  			"foo": "bar",
    31  		},
    32  	}
    33  
    34  	b := &Unstructured{}
    35  	b.Object = map[string]interface{}{
    36  		"apiVersion": "vtest",
    37  		"kind":       "Foo",
    38  		"metadata": map[string]interface{}{
    39  			"labels": map[string]interface{}{
    40  				"foo": "bar",
    41  				"moo": "arf",
    42  			},
    43  			"annotations": map[string]interface{}{
    44  				"foo": "bar",
    45  				"moo": "arf",
    46  			},
    47  		},
    48  		"spec": map[string]interface{}{
    49  			"key": "value",
    50  		},
    51  	}
    52  
    53  	assert.NotEqual(t, a.Object["spec"], b.Object["spec"])
    54  
    55  	MergeUpdate(a, b)
    56  
    57  	assert.Equal(t, "arf", a.GetLabels()["moo"])
    58  	assert.Equal(t, "bar", a.GetAnnotations()["foo"])
    59  	assert.Equal(t, b.Object["spec"], a.Object["spec"])
    60  }
    61  
    62  const mapping = `---
    63  apiVersion: getambassador.io/v3alpha1
    64  kind: Mapping
    65  metadata:
    66    name:  mapping-name
    67  spec:
    68    prefix: /mapping-prefix/
    69    service: http://mapping-service
    70  `
    71  
    72  func TestParseManifestsResultTypes(t *testing.T) {
    73  	objs, err := ParseManifests(mapping)
    74  	require.NoError(t, err)
    75  	require.Equal(t, 1, len(objs))
    76  
    77  	m := objs[0]
    78  	t.Logf("value = %v", m)
    79  	t.Logf("type = %T", m)
    80  	_, ok := m.(*amb.Mapping)
    81  	require.True(t, ok)
    82  }
    83  
    84  // TestGatewayResources checks that the kates schema includes the gateway resources and will
    85  // therefore parse into the proper types as opposed to falling back to the Unstructured type.
    86  func TestGatewayResources(t *testing.T) {
    87  	objs, err := ParseManifests(gatewayResources)
    88  	require.NoError(t, err)
    89  	require.Len(t, objs, 3)
    90  	assert.IsType(t, &gw.GatewayClass{}, objs[0])
    91  	assert.IsType(t, &gw.Gateway{}, objs[1])
    92  	assert.IsType(t, &gw.HTTPRoute{}, objs[2])
    93  }
    94  
    95  const gatewayResources = `
    96  ---
    97  kind: GatewayClass
    98  apiVersion: networking.x-k8s.io/v1alpha1
    99  metadata:
   100    name: acme-lb
   101  spec:
   102    controller: acme.io/gateway-controller
   103    parametersRef:
   104      name: acme-lb
   105      group: acme.io
   106      kind: Parameters
   107  ---
   108  kind: Gateway
   109  apiVersion: networking.x-k8s.io/v1alpha1
   110  metadata:
   111    name: my-gateway
   112  spec:
   113    gatewayClassName: acme-lb
   114    listeners:  # Use GatewayClass defaults for listener definition.
   115    - protocol: HTTP
   116      port: 80
   117      routes:
   118        kind: HTTPRoute
   119        selector:
   120          matchLabels:
   121            app: foo
   122        namespaces:
   123          from: "Same"
   124  ---
   125  kind: HTTPRoute
   126  apiVersion: networking.x-k8s.io/v1alpha1
   127  metadata:
   128    name: http-app-1
   129    labels:
   130      app: foo
   131  spec:
   132    hostnames:
   133    - "foo.com"
   134    rules:
   135    - matches:
   136      - path:
   137          type: Prefix
   138          value: /bar
   139      forwardTo:
   140      - serviceName: my-service1
   141        port: 8080
   142    - matches:
   143      - headers:
   144          type: Exact
   145          values:
   146            magic: foo
   147        path:
   148          type: Prefix
   149          value: /some/thing
   150      forwardTo:
   151      - serviceName: my-service2
   152        port: 8080
   153  `
   154  

View as plain text