...

Source file src/github.com/emissary-ingress/emissary/v3/pkg/kates/validation_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  )
     9  
    10  func TestValidation(t *testing.T) {
    11  	ctx, client := testClient(t, nil)
    12  
    13  	version, err := client.ServerVersion()
    14  	require.NoError(t, err)
    15  	if version.Major == "1" && version.Minor == "10" {
    16  		t.Skip("skipping test because kubernetes version is too old")
    17  		return
    18  	} else {
    19  		t.Log("Kubernetes Version", version)
    20  	}
    21  
    22  	// create a crd with schema validations so we can test that they work
    23  	objs, err := ParseManifests(CRD)
    24  	require.NoError(t, err)
    25  	require.Equal(t, len(objs), 1)
    26  	crd := objs[0].(*CustomResourceDefinition)
    27  
    28  	defer func() {
    29  		assert.NoError(t, client.Delete(ctx, crd, nil))
    30  	}()
    31  
    32  	require.NoError(t, client.Create(ctx, crd, crd))
    33  
    34  	require.NoError(t, client.WaitFor(ctx, crd.GetName()))
    35  
    36  	// check that an invalid crd errors out
    37  	validator, err := NewValidator(client, nil)
    38  	require.NoError(t, err)
    39  	err = validator.Validate(ctx, map[string]interface{}{
    40  		"apiVersion": "test.io/v1",
    41  		"kind":       "TestValidation",
    42  		"spec": map[string]interface{}{
    43  			"ambassador_id": "three", //[]interface{}{"one", "two", 3},
    44  			"foo":           "bar",
    45  			"circuit_breakers": []map[string]interface{}{{
    46  				"priority": "blah",
    47  			}},
    48  		},
    49  		"foo": "bar",
    50  	})
    51  	assert.Error(t, err)
    52  	t.Log(err)
    53  
    54  	// check that a valid crd passes
    55  	err = validator.Validate(ctx, map[string]interface{}{
    56  		"apiVersion": "test.io/v1",
    57  		"kind":       "TestValidation",
    58  		"spec": map[string]interface{}{
    59  			"ambassador_id": []interface{}{"one", "two", "three"},
    60  			"circuit_breakers": []map[string]interface{}{{
    61  				"priority": "high",
    62  			}},
    63  		},
    64  	})
    65  	assert.NoError(t, err)
    66  
    67  	// check that non-crds validate
    68  	err = validator.Validate(ctx, map[string]interface{}{
    69  		"apiVersion": "v1",
    70  		"kind":       "Service",
    71  	})
    72  	assert.NoError(t, err)
    73  }
    74  
    75  var CRD = `
    76  apiVersion: apiextensions.k8s.io/v1
    77  kind: CustomResourceDefinition
    78  metadata:
    79    name: testvalidations.test.io
    80  spec:
    81    conversion:
    82      strategy: None
    83    group: test.io
    84    names:
    85      kind: TestValidation
    86      plural: testvalidations
    87      singular: testvalidation
    88    scope: Namespaced
    89    versions:
    90    - name: v1
    91      schema:
    92        openAPIV3Schema:
    93          description: Mapping is the Schema for the mappings API
    94          properties:
    95            apiVersion:
    96              description: 'APIVersion defines the versioned schema of this representation
    97                of an object. Servers should convert recognized schemas to the latest
    98                internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
    99              type: string
   100            kind:
   101              description: 'Kind is a string value representing the REST resource this
   102                object represents. Servers may infer this from the endpoint the client
   103                submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
   104              type: string
   105            metadata:
   106              type: object
   107            spec:
   108              properties:
   109                add_linkerd_headers:
   110                  type: boolean
   111                add_request_headers:
   112                  items:
   113                    type: string
   114                  type: array
   115                add_response_headers:
   116                  items:
   117                    type: string
   118                  type: array
   119                ambassador_id:
   120                  description: '"metadata_labels": {     "type": "object",     "additionalProperties":
   121                    { "type": [ "string", "boolean" ] } },'
   122                  items:
   123                    type: string
   124                  type: array
   125                auto_host_rewrite:
   126                  type: boolean
   127                bypass_auth:
   128                  type: boolean
   129                case_sensitive:
   130                  type: boolean
   131                circuit_breakers:
   132                  items:
   133                    properties:
   134                      max_connections:
   135                        format: int32
   136                        type: integer
   137                      max_pending_requests:
   138                        format: int32
   139                        type: integer
   140                      max_requests:
   141                        format: int32
   142                        type: integer
   143                      max_retries:
   144                        format: int32
   145                        type: integer
   146                      priority:
   147                        enum:
   148                        - default
   149                        - high
   150                        type: string
   151                    type: object
   152                  type: array
   153                cluster_idle_timeout_ms:
   154                  format: int32
   155                  type: integer
   156                connect_timeout_ms:
   157                  format: int32
   158                  type: integer
   159                cors:
   160                  properties:
   161                    credentials:
   162                      type: boolean
   163                    exposed_headers:
   164                      items:
   165                        type: string
   166                      type: array
   167                    headers:
   168                      items:
   169                        type: string
   170                      type: array
   171                    max_age:
   172                      type: string
   173                    methods:
   174                      items:
   175                        type: string
   176                      type: array
   177                    origins:
   178                      items:
   179                        type: string
   180                      type: array
   181                  type: object
   182                enable_ipv4:
   183                  type: boolean
   184                enable_ipv6:
   185                  type: boolean
   186                generation:
   187                  format: int32
   188                  type: integer
   189                grpc:
   190                  type: boolean
   191                headers:
   192                  additionalProperties:
   193                    type: string
   194                  type: object
   195                host:
   196                  type: string
   197                host_redirect:
   198                  type: boolean
   199                host_regex:
   200                  type: boolean
   201                host_rewrite:
   202                  type: string
   203                idle_timeout_ms:
   204                  format: int32
   205                  type: integer
   206                labels:
   207                  additionalProperties:
   208                    type: string
   209                  type: object
   210                load_balancer:
   211                  properties:
   212                    cookie:
   213                      properties:
   214                        name:
   215                          type: string
   216                        path:
   217                          type: string
   218                        ttl:
   219                          type: string
   220                      type: object
   221                    header:
   222                      type: string
   223                    policy:
   224                      enum:
   225                      - round_robin
   226                      - ring_hash
   227                      - maglev
   228                      - least_request
   229                      type: string
   230                    source_ip:
   231                      type: boolean
   232                  type: object
   233                method:
   234                  type: string
   235                method_regex:
   236                  type: boolean
   237                outlier_detection:
   238                  type: string
   239                path_redirect:
   240                  type: string
   241                precedence:
   242                  format: int32
   243                  type: integer
   244                prefix:
   245                  type: string
   246                prefix_regex:
   247                  type: boolean
   248                priority:
   249                  type: string
   250                regex_headers:
   251                  additionalProperties:
   252                    type: string
   253                  type: object
   254                remove_request_headers:
   255                  items:
   256                    type: string
   257                  type: array
   258                remove_response_headers:
   259                  items:
   260                    type: string
   261                  type: array
   262                resolver:
   263                  type: string
   264                retry_policy:
   265                  properties:
   266                    num_retries:
   267                      format: int32
   268                      type: integer
   269                    per_try_timeout:
   270                      type: string
   271                    retry_on:
   272                      enum:
   273                      - 5xx
   274                      - gateway-error
   275                      - connect-failure
   276                      - retriable-4xx
   277                      - refused-stream
   278                      - retriable-status-codes
   279                      type: string
   280                  type: object
   281                rewrite:
   282                  type: string
   283                service:
   284                  type: string
   285                shadow:
   286                  type: boolean
   287                timeout_ms:
   288                  format: int32
   289                  type: integer
   290                tls:
   291                  type: string
   292                use_websocket:
   293                  type: boolean
   294                weight:
   295                  format: int32
   296                  type: integer
   297              type: object
   298            status:
   299              description: MappingStatus defines the observed state of Mapping
   300              type: object
   301          type: object
   302      served: true
   303      storage: true
   304  `
   305  

View as plain text