istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/integration/pilot/original_src_addr_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  	"fmt"
    22  	"testing"
    23  
    24  	"istio.io/istio/pkg/test/framework"
    25  	"istio.io/istio/pkg/test/framework/components/echo"
    26  )
    27  
    28  func TestTproxy(t *testing.T) {
    29  	// nolint: staticcheck
    30  	framework.
    31  		NewTest(t).
    32  		RequiresSingleCluster().
    33  		Run(func(t framework.TestContext) {
    34  			if t.Settings().Skip(echo.TProxy) {
    35  				t.Skip()
    36  			}
    37  			workloads, err := apps.A[0].Workloads()
    38  			if err != nil {
    39  				t.Errorf("failed to get Subsets: %v", err)
    40  				return
    41  			}
    42  			// check the server can see the client's original ip
    43  			var srcIps []string
    44  			for _, w := range workloads {
    45  				srcIps = append(srcIps, w.Address())
    46  			}
    47  			checkOriginalSrcIP(t, apps.A[0], apps.Tproxy[0], srcIps)
    48  		})
    49  }
    50  
    51  func checkOriginalSrcIP(t framework.TestContext, from echo.Caller, to echo.Target, expected []string) {
    52  	t.Helper()
    53  	checker := func(result echo.CallResult, inErr error) error {
    54  		// Check that each response saw one of the workload IPs for the src echo instance
    55  		for _, r := range result.Responses {
    56  			found := false
    57  			for _, ip := range expected {
    58  				if r.IP == ip {
    59  					found = true
    60  				}
    61  			}
    62  			if !found {
    63  				return fmt.Errorf("unexpected IP %s, expected to be contained in %v",
    64  					r.IP, expected)
    65  			}
    66  		}
    67  
    68  		return nil
    69  	}
    70  	_ = from.CallOrFail(t, echo.CallOptions{
    71  		To: to,
    72  		Port: echo.Port{
    73  			Name: "http",
    74  		},
    75  		Count: 1,
    76  		Check: checker,
    77  	})
    78  }