istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/integration/pilot/cni/cniversionskew_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 cni
    19  
    20  import (
    21  	"fmt"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"istio.io/istio/pkg/test/env"
    27  	"istio.io/istio/pkg/test/framework"
    28  	"istio.io/istio/pkg/test/framework/components/echo/common/deployment"
    29  	"istio.io/istio/pkg/test/framework/components/istio"
    30  	"istio.io/istio/pkg/test/framework/label"
    31  	"istio.io/istio/pkg/test/kube"
    32  	"istio.io/istio/pkg/test/util/file"
    33  	"istio.io/istio/pkg/test/util/retry"
    34  	"istio.io/istio/tests/integration/pilot/common"
    35  )
    36  
    37  var (
    38  	i istio.Instance
    39  
    40  	apps = deployment.SingleNamespaceView{}
    41  )
    42  
    43  const (
    44  	// TODO: replace this with official 1.11 release once available.
    45  	NMinusOne    = "1.11.0-beta.1"
    46  	CNIConfigDir = "tests/integration/pilot/testdata/upgrade"
    47  )
    48  
    49  // Currently only test CNI with one version behind.
    50  var versions = []string{NMinusOne}
    51  
    52  // TestCNIVersionSkew runs all traffic tests with older versions of CNI and lastest Istio.
    53  // This is to simulate the case where CNI and Istio control plane versions are out of sync during upgrade.
    54  func TestCNIVersionSkew(t *testing.T) {
    55  	framework.
    56  		NewTest(t).
    57  		Run(func(t framework.TestContext) {
    58  			if !i.Settings().EnableCNI {
    59  				t.Skip("CNI version skew test is only tested when CNI is enabled.")
    60  			}
    61  			for _, v := range versions {
    62  				installCNIOrFail(t, v)
    63  				podFetchFn := kube.NewSinglePodFetch(t.Clusters().Default(), i.Settings().SystemNamespace, "k8s-app=istio-cni-node")
    64  				// Make sure CNI pod is using image with applied version.
    65  				retry.UntilSuccessOrFail(t, func() error {
    66  					pods, err := podFetchFn()
    67  					if err != nil {
    68  						return fmt.Errorf("failed to get CNI pods %v", err)
    69  					}
    70  					if len(pods) == 0 {
    71  						return fmt.Errorf("cannot find any CNI pods")
    72  					}
    73  					for _, p := range pods {
    74  						if !strings.Contains(p.Spec.Containers[0].Image, v) {
    75  							return fmt.Errorf("pods image does not match wanted CNI version")
    76  						}
    77  					}
    78  					return nil
    79  				})
    80  
    81  				// Make sure CNI pod is ready
    82  				if _, err := kube.WaitUntilPodsAreReady(podFetchFn); err != nil {
    83  					t.Fatal(err)
    84  				}
    85  				if err := apps.All.Instances().Restart(); err != nil {
    86  					t.Fatalf("Failed to restart apps %v", err)
    87  				}
    88  				common.RunAllTrafficTests(t, i, apps)
    89  			}
    90  		})
    91  }
    92  
    93  func TestMain(m *testing.M) {
    94  	// nolint: staticcheck
    95  	framework.
    96  		NewSuite(m).
    97  		Label(label.Postsubmit).
    98  		Label(label.CustomSetup).
    99  		RequireMultiPrimary().
   100  		Setup(istio.Setup(&i, nil)).
   101  		Setup(deployment.SetupSingleNamespace(&apps, deployment.Config{})).
   102  		Run()
   103  }
   104  
   105  // installCNIOrFail installs CNI DaemonSet for the given version.
   106  // It looks for tar compressed CNI manifest and apply that in the cluster.
   107  func installCNIOrFail(t framework.TestContext, ver string) {
   108  	cniFilePath := filepath.Join(env.IstioSrc, CNIConfigDir,
   109  		fmt.Sprintf("%s-cni-install.yaml.tar", ver))
   110  	config, err := file.ReadTarFile(cniFilePath)
   111  	if err != nil {
   112  		t.Fatalf("Failed to read CNI manifest %v", err)
   113  	}
   114  	config = strings.ReplaceAll(config, "kube-system", i.Settings().SystemNamespace)
   115  	t.ConfigIstio().YAML("", config).ApplyOrFail(t)
   116  }