istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/integration/pilot/tcp_probe_test.go (about)

     1  //go:build integ
     2  // +build integ
     3  
     4  //  Copyright Istio Authors
     5  //
     6  //  Licensed under the Apache License, Version 2.0 (the "License");
     7  //  you may not use this file except in compliance with the License.
     8  //  You may obtain a copy of the License at
     9  //
    10  //      http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  //  Unless required by applicable law or agreed to in writing, software
    13  //  distributed under the License is distributed on an "AS IS" BASIS,
    14  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  //  See the License for the specific language governing permissions and
    16  //  limitations under the License.
    17  
    18  package pilot
    19  
    20  import (
    21  	"strconv"
    22  	"testing"
    23  	"time"
    24  
    25  	"istio.io/api/annotation"
    26  	"istio.io/istio/pkg/config/protocol"
    27  	"istio.io/istio/pkg/test/framework"
    28  	"istio.io/istio/pkg/test/framework/components/echo"
    29  	"istio.io/istio/pkg/test/framework/components/echo/deployment"
    30  	"istio.io/istio/pkg/test/framework/components/namespace"
    31  )
    32  
    33  func TestTcpProbe(t *testing.T) {
    34  	framework.NewTest(t).
    35  		Run(func(t framework.TestContext) {
    36  			ns := namespace.NewOrFail(t, t, namespace.Config{Prefix: "tcp-probe", Inject: true})
    37  			for _, testCase := range []struct {
    38  				name     string
    39  				rewrite  bool
    40  				success  bool
    41  				openPort bool
    42  			}{
    43  				{name: "norewrite-success", rewrite: false, success: true, openPort: false},
    44  				{name: "rewrite-success", rewrite: true, success: true, openPort: true},
    45  			} {
    46  				t.NewSubTest(testCase.name).Run(func(t framework.TestContext) {
    47  					runTCPProbeDeployment(t, ns, testCase.name, testCase.rewrite, testCase.success, testCase.openPort)
    48  				})
    49  			}
    50  		})
    51  }
    52  
    53  func runTCPProbeDeployment(ctx framework.TestContext, ns namespace.Instance, //nolint:interfacer
    54  	name string, rewrite bool, wantSuccess bool, openPort bool,
    55  ) {
    56  	ctx.Helper()
    57  
    58  	var tcpProbe echo.Instance
    59  	cfg := echo.Config{
    60  		Namespace:        ns,
    61  		Service:          name,
    62  		ReadinessTCPPort: "1234",
    63  		Subsets: []echo.SubsetConfig{
    64  			{
    65  				Annotations: map[string]string{annotation.SidecarRewriteAppHTTPProbers.Name: strconv.FormatBool(rewrite)},
    66  			},
    67  		},
    68  	}
    69  
    70  	if openPort {
    71  		cfg.Ports = []echo.Port{{
    72  			Name:         "readiness-tcp-port",
    73  			Protocol:     protocol.TCP,
    74  			ServicePort:  1234,
    75  			WorkloadPort: 1234,
    76  		}}
    77  	}
    78  
    79  	// Negative test, we expect the tcp readiness check fails, so set a timeout duration.
    80  	if !wantSuccess {
    81  		cfg.ReadinessTimeout = time.Second * 15
    82  	}
    83  	_, err := deployment.New(ctx).
    84  		With(&tcpProbe, cfg).
    85  		Build()
    86  	gotSuccess := err == nil
    87  	if gotSuccess != wantSuccess {
    88  		ctx.Errorf("tcpProbe app %v, got error %v, want success = %v", name, err, wantSuccess)
    89  	}
    90  }