...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/sidecar/template_config.go

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

     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 sidecar
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/labels"
    24  	"k8s.io/apimachinery/pkg/util/wait"
    25  	"k8s.io/client-go/kubernetes"
    26  	"k8s.io/kubernetes/test/e2e/framework"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	"github.com/chaos-mesh/chaos-mesh/e2e-test/pkg/fixture"
    30  )
    31  
    32  func TestcaseInvalidConfigMapKey(
    33  	ns string,
    34  	cmNamespace string,
    35  	cmName string,
    36  	kubeCli kubernetes.Interface,
    37  	cli client.Client,
    38  ) {
    39  
    40  	ctx, cancel := context.WithCancel(context.Background())
    41  	err := createTemplateConfig(ctx, cli, cmName,
    42  		map[string]string{
    43  			"chaos-pd.yaml": `name: chaosfs-pd
    44  selector:
    45    labelSelectors:
    46      "app.kubernetes.io/component": "pd"`})
    47  	framework.ExpectNoError(err, "failed to create template config")
    48  
    49  	listOptions := metav1.ListOptions{
    50  		LabelSelector: labels.SelectorFromSet(map[string]string{
    51  			"app.kubernetes.io/component": "controller-manager",
    52  		}).String(),
    53  	}
    54  	pods, err := kubeCli.CoreV1().Pods(cmNamespace).List(context.TODO(), listOptions)
    55  	framework.ExpectNoError(err, "get chaos mesh controller pods error")
    56  
    57  	err = wait.Poll(time.Second, 10*time.Second, func() (done bool, err error) {
    58  		newPods, err := kubeCli.CoreV1().Pods(cmNamespace).List(context.TODO(), listOptions)
    59  		framework.ExpectNoError(err, "get chaos mesh controller pods error")
    60  		if !fixture.HaveSameUIDs(pods.Items, newPods.Items) {
    61  			return true, nil
    62  		}
    63  		if len(newPods.Items) > 0 && newPods.Items[0].Status.ContainerStatuses[0].RestartCount > 0 {
    64  			return true, nil
    65  		}
    66  		return false, nil
    67  	})
    68  	framework.ExpectError(err, "wait chaos mesh not dies")
    69  	framework.ExpectEqual(err.Error(), wait.ErrWaitTimeout.Error())
    70  
    71  	cancel()
    72  }
    73  
    74  func TestcaseInvalidConfiguration(
    75  	ns string,
    76  	cmNamespace string,
    77  	cmName string,
    78  	kubeCli kubernetes.Interface,
    79  	cli client.Client,
    80  ) {
    81  
    82  	ctx, cancel := context.WithCancel(context.Background())
    83  	defer cancel()
    84  	err := createTemplateConfig(ctx, cli, cmName,
    85  		map[string]string{
    86  			"data": `name: chaosfs-pd
    87  selector:
    88    labelSelectors:
    89      "app.kubernetes.io/component": "pd"`})
    90  	framework.ExpectNoError(err, "failed to create template config")
    91  
    92  	listOptions := metav1.ListOptions{
    93  		LabelSelector: labels.SelectorFromSet(map[string]string{
    94  			"app.kubernetes.io/component": "controller-manager",
    95  		}).String(),
    96  	}
    97  	pods, err := kubeCli.CoreV1().Pods(cmNamespace).List(context.TODO(), listOptions)
    98  	framework.ExpectNoError(err, "get chaos mesh controller pods error")
    99  
   100  	err = wait.Poll(time.Second, 10*time.Second, func() (done bool, err error) {
   101  		newPods, err := kubeCli.CoreV1().Pods(cmNamespace).List(context.TODO(), listOptions)
   102  		framework.ExpectNoError(err, "get chaos mesh controller pods error")
   103  		if !fixture.HaveSameUIDs(pods.Items, newPods.Items) {
   104  			return true, nil
   105  		}
   106  		if len(newPods.Items) > 0 && newPods.Items[0].Status.ContainerStatuses[0].RestartCount > 0 {
   107  			return true, nil
   108  		}
   109  		return false, nil
   110  	})
   111  	framework.ExpectError(err, "wait chaos mesh not dies")
   112  	framework.ExpectEqual(err.Error(), wait.ErrWaitTimeout.Error())
   113  }
   114