...

Source file src/sigs.k8s.io/gateway-api/apis/v1alpha2/validation/tlsroute_test.go

Documentation: sigs.k8s.io/gateway-api/apis/v1alpha2/validation

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package validation
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/util/validation/field"
    23  
    24  	gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
    25  )
    26  
    27  func TestValidateTLSRoute(t *testing.T) {
    28  	t.Parallel()
    29  
    30  	var portNumber int32 = 9080
    31  
    32  	tests := []struct {
    33  		name  string
    34  		rules []gatewayv1a2.TLSRouteRule
    35  		errs  field.ErrorList
    36  	}{
    37  		{
    38  			name:  "valid TLSRoute with 1 backendRef",
    39  			rules: makeRouteRules[gatewayv1a2.TLSRouteRule](&portNumber),
    40  		},
    41  		{
    42  			name:  "invalid TLSRoute with 1 backendRef (missing port)",
    43  			rules: makeRouteRules[gatewayv1a2.TLSRouteRule](nil),
    44  			errs: field.ErrorList{
    45  				{
    46  					Type:   field.ErrorTypeRequired,
    47  					Field:  "spec.rules[0].backendRefs[0].port",
    48  					Detail: "missing port for Service reference",
    49  				},
    50  			},
    51  		},
    52  	}
    53  
    54  	for _, tc := range tests {
    55  		tc := tc
    56  		t.Run(tc.name, func(t *testing.T) {
    57  			t.Parallel()
    58  
    59  			route := gatewayv1a2.TLSRoute{Spec: gatewayv1a2.TLSRouteSpec{Rules: tc.rules}}
    60  			errs := ValidateTLSRoute(&route)
    61  			if len(errs) != len(tc.errs) {
    62  				t.Fatalf("got %d errors, want %d errors: %s", len(errs), len(tc.errs), errs)
    63  			}
    64  			for i := 0; i < len(errs); i++ {
    65  				realErr := errs[i].Error()
    66  				expectedErr := tc.errs[i].Error()
    67  				if realErr != expectedErr {
    68  					t.Fatalf("expect error message: %s, but got: %s", expectedErr, realErr)
    69  				}
    70  			}
    71  		})
    72  	}
    73  }
    74  

View as plain text