istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/kube/conversion_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kube
    16  
    17  import (
    18  	"testing"
    19  
    20  	corev1 "k8s.io/api/core/v1"
    21  
    22  	"istio.io/istio/pkg/config/protocol"
    23  	"istio.io/istio/pkg/ptr"
    24  )
    25  
    26  func TestConvertProtocol(t *testing.T) {
    27  	https := "https"
    28  	cases := []struct {
    29  		name          string
    30  		port          int32
    31  		portName      string
    32  		proto         corev1.Protocol
    33  		appProto      *string
    34  		expectedProto protocol.Instance
    35  	}{
    36  		{
    37  			name:          "resolves empty",
    38  			expectedProto: protocol.Unsupported,
    39  		},
    40  		{
    41  			name:          "resolves from protocol directly",
    42  			proto:         corev1.ProtocolUDP,
    43  			expectedProto: protocol.UDP,
    44  		},
    45  		{
    46  			name:          "resolves from port name",
    47  			portName:      "http-something",
    48  			expectedProto: protocol.HTTP,
    49  		},
    50  		{
    51  			name:          "prefers appProto over portName",
    52  			portName:      "http-something",
    53  			appProto:      &https,
    54  			expectedProto: protocol.HTTPS,
    55  		},
    56  		{
    57  			name:          "resolves from appProto",
    58  			portName:      "something-httpx",
    59  			appProto:      &https,
    60  			expectedProto: protocol.HTTPS,
    61  		},
    62  		{
    63  			name:          "resolves grpc-web",
    64  			portName:      "grpc-web-x",
    65  			expectedProto: protocol.GRPCWeb,
    66  		},
    67  		{
    68  			name:          "makes sure grpc-web is not resolved incorrectly",
    69  			portName:      "grpcweb-x",
    70  			expectedProto: protocol.Unsupported,
    71  		},
    72  		{
    73  			name:          "resolves based on known ports",
    74  			port:          3306, // mysql
    75  			portName:      "random-name",
    76  			expectedProto: protocol.TCP,
    77  		},
    78  		{
    79  			name:          "standard app protocol",
    80  			port:          3306, // mysql
    81  			portName:      "random-name",
    82  			appProto:      ptr.Of("kubernetes.io/h2c"),
    83  			expectedProto: protocol.HTTP2,
    84  		},
    85  	}
    86  
    87  	for _, tc := range cases {
    88  		t.Run(tc.name, func(t *testing.T) {
    89  			actualProto := ConvertProtocol(tc.port, tc.portName, tc.proto, tc.appProto)
    90  			if actualProto != tc.expectedProto {
    91  				t.Errorf("ConvertProtocol(%d, %s, %s, %v) => %s, want %s",
    92  					tc.port, tc.portName, tc.proto, tc.appProto, actualProto, tc.expectedProto)
    93  			}
    94  		})
    95  	}
    96  }