istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/integration/pilot/piggyback_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  	"strings"
    23  	"testing"
    24  
    25  	"istio.io/istio/pkg/test/framework"
    26  	"istio.io/istio/pkg/test/framework/components/echo"
    27  	"istio.io/istio/pkg/test/framework/components/istioctl"
    28  	"istio.io/istio/pkg/test/util/retry"
    29  )
    30  
    31  func TestPiggyback(t *testing.T) {
    32  	// nolint: staticcheck
    33  	framework.
    34  		NewTest(t).RequiresSingleCluster().
    35  		RequiresLocalControlPlane().
    36  		RequireIstioVersion("1.10.0").
    37  		Run(func(t framework.TestContext) {
    38  			workloads := []echo.Instances{apps.A, apps.Sotw}
    39  			istioCtl := istioctl.NewOrFail(t, t, istioctl.Config{Cluster: t.Clusters().Default()})
    40  			for _, workload := range workloads {
    41  				podName := workload[0].WorkloadsOrFail(t)[0].PodName()
    42  				namespace := workload.Config().Namespace.Name()
    43  
    44  				retry.UntilSuccessOrFail(t, func() error {
    45  					args := []string{
    46  						"x", "proxy-status", "--xds-via-agents", fmt.Sprintf("%s.%s", podName, namespace),
    47  					}
    48  					output, _, err := istioCtl.Invoke(args)
    49  					if err != nil {
    50  						return err
    51  					}
    52  					return expectSubstrings(output, "Clusters Match", "Listeners Match", "Routes Match")
    53  				})
    54  
    55  				// Test gRPC-based tapped XDS using istioctl.
    56  				retry.UntilSuccessOrFail(t, func() error {
    57  					pf, err := t.Clusters()[0].NewPortForwarder(podName, namespace, "localhost", 0, 15004)
    58  					if err != nil {
    59  						return fmt.Errorf("failed to create the port forwarder: %v", err)
    60  					}
    61  					pf.Start()
    62  					defer pf.Close()
    63  
    64  					argsToTest := []struct {
    65  						args []string
    66  					}{
    67  						{[]string{"x", "proxy-status", "--plaintext", "--xds-address", pf.Address()}},
    68  						{[]string{"proxy-status", "--plaintext", "--xds-address", pf.Address()}},
    69  					}
    70  					for _, args := range argsToTest {
    71  						istioCtl := istioctl.NewOrFail(t, t, istioctl.Config{Cluster: t.Clusters().Default()})
    72  						output, _, err := istioCtl.Invoke(args.args)
    73  						if err != nil {
    74  							return err
    75  						}
    76  
    77  						// Just verify pod A is known to Pilot; implicitly this verifies that
    78  						// the printing code printed it.
    79  						if err := expectSubstrings(output, fmt.Sprintf("%s.%s", podName, namespace)); err != nil {
    80  							return err
    81  						}
    82  					}
    83  					return nil
    84  				})
    85  
    86  				// Test gRPC-based --xds-via-agents
    87  				retry.UntilSuccessOrFail(t, func() error {
    88  					istioCtl := istioctl.NewOrFail(t, t, istioctl.Config{Cluster: t.Clusters().Default()})
    89  					args := []string{"x", "proxy-status", "--xds-via-agents"}
    90  					output, _, err := istioCtl.Invoke(args)
    91  					if err != nil {
    92  						return err
    93  					}
    94  					return expectSubstrings(output, fmt.Sprintf("%s.%s", podName, namespace))
    95  				})
    96  			}
    97  		})
    98  }
    99  
   100  func expectSubstrings(have string, wants ...string) error {
   101  	for _, want := range wants {
   102  		if !strings.Contains(have, want) {
   103  			return fmt.Errorf("substring %q not found; have %q", want, have)
   104  		}
   105  	}
   106  	return nil
   107  }