...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/networkchaos/network_peers_crossover.go

Documentation: github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/networkchaos

     1  // Copyright 2021 Chaos Mesh 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  
    16  package networkchaos
    17  
    18  import (
    19  	"context"
    20  	"net/http"
    21  	"time"
    22  
    23  	. "github.com/onsi/ginkgo/v2"
    24  	corev1 "k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/util/wait"
    26  	"k8s.io/kubernetes/test/e2e/framework"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    30  	"github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/util"
    31  )
    32  
    33  // This test case is for https://github.com/chaos-mesh/chaos-mesh/issues/1450
    34  // For example, if the source is A, B, and the target is C, D, and the direction is both,
    35  // now the connection between A and B will also be affected by this chaos, this is unexpected.
    36  func TestcasePeersCrossover(
    37  	ns string,
    38  	cli client.Client,
    39  	networkPeers []*corev1.Pod,
    40  	ports []uint16,
    41  	c http.Client,
    42  ) {
    43  	ctx, cancel := context.WithCancel(context.Background())
    44  	defer cancel()
    45  
    46  	By("prepare experiment playground")
    47  	for index := range networkPeers {
    48  		err := util.WaitE2EHelperReady(c, ports[index])
    49  
    50  		framework.ExpectNoError(err, "wait e2e helper ready error")
    51  	}
    52  
    53  	result := probeNetworkCondition(c, networkPeers, ports, false)
    54  	framework.ExpectEqual(len(result[networkConditionBlocked]), 0)
    55  	framework.ExpectEqual(len(result[networkConditionSlow]), 0)
    56  
    57  	var (
    58  		testDelayTcParam = v1alpha1.TcParameter{
    59  			Delay: &v1alpha1.DelaySpec{
    60  				Latency:     "200ms",
    61  				Correlation: "25",
    62  				Jitter:      "0ms",
    63  			},
    64  		}
    65  	)
    66  
    67  	By("injecting network chaos between partition 0 and 1")
    68  	networkDelay := makeNetworkDelayChaos(
    69  		ns, "network-chaos-1",
    70  		map[string]string{"partition": "0"},
    71  		map[string]string{"partition": "1"},
    72  		v1alpha1.AllMode,
    73  		v1alpha1.AllMode,
    74  		v1alpha1.Both,
    75  		testDelayTcParam,
    76  		nil,
    77  	)
    78  	// that's important
    79  	networkDelay.Spec.Direction = v1alpha1.Both
    80  
    81  	By("Injecting delay between partition 0 (peer 0,2) with partition 1 (peer 1,3)")
    82  	err := cli.Create(ctx, networkDelay.DeepCopy())
    83  	framework.ExpectNoError(err, "create network chaos error")
    84  
    85  	err = wait.Poll(time.Second, 15*time.Second, func() (done bool, err error) {
    86  		result = probeNetworkCondition(c, networkPeers, ports, false)
    87  		if len(result[networkConditionBlocked]) != 0 || len(result[networkConditionSlow]) != 4 {
    88  			return false, nil
    89  		}
    90  		return true, nil
    91  	})
    92  
    93  	framework.ExpectNoError(err, "failed to waiting condition for chaos injection")
    94  	framework.ExpectEqual(len(result[networkConditionBlocked]), 0)
    95  	framework.ExpectEqual(result[networkConditionSlow], [][]int{{0, 1}, {0, 3}, {1, 2}, {2, 3}})
    96  
    97  	By("recover")
    98  	err = cli.Delete(ctx, networkDelay.DeepCopy())
    99  	framework.ExpectNoError(err, "delete network chaos error")
   100  
   101  	err = wait.Poll(time.Second, 15*time.Second, func() (done bool, err error) {
   102  		result = probeNetworkCondition(c, networkPeers, ports, false)
   103  		if len(result[networkConditionBlocked]) != 0 || len(result[networkConditionSlow]) != 0 {
   104  			return false, nil
   105  		}
   106  		return true, nil
   107  	})
   108  
   109  	framework.ExpectNoError(err, "failed to waiting condition for chaos recover")
   110  	framework.ExpectEqual(len(result[networkConditionBlocked]), 0)
   111  	framework.ExpectEqual(len(result[networkConditionSlow]), 0)
   112  
   113  }
   114