...

Source file src/github.com/chaos-mesh/chaos-mesh/test/pkg/fixture/fixture.go

Documentation: github.com/chaos-mesh/chaos-mesh/test/pkg/fixture

     1  // Copyright 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package fixture
    15  
    16  import (
    17  	"sort"
    18  
    19  	appsv1 "k8s.io/api/apps/v1"
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/apimachinery/pkg/util/intstr"
    23  	"k8s.io/utils/pointer"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/test/e2e/config"
    26  )
    27  
    28  // NewCommonNginxPod describe that we use common nginx pod to be tested in our chaos-operator test
    29  func NewCommonNginxPod(name, namespace string) *corev1.Pod {
    30  	return &corev1.Pod{
    31  		ObjectMeta: metav1.ObjectMeta{
    32  			Name:      name,
    33  			Namespace: namespace,
    34  			Labels: map[string]string{
    35  				"app": "nginx",
    36  			},
    37  		},
    38  		Spec: corev1.PodSpec{
    39  			Containers: []corev1.Container{
    40  				{
    41  					Image:           "nginx:latest",
    42  					ImagePullPolicy: corev1.PullIfNotPresent,
    43  					Name:            "nginx",
    44  				},
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  // NewCommonNginxDeployment would create a nginx deployment
    51  func NewCommonNginxDeployment(name, namespace string, replicas int32) *appsv1.Deployment {
    52  	return &appsv1.Deployment{
    53  		ObjectMeta: metav1.ObjectMeta{
    54  			Name:      name,
    55  			Namespace: namespace,
    56  			Labels: map[string]string{
    57  				"app": "nginx",
    58  			},
    59  		},
    60  		Spec: appsv1.DeploymentSpec{
    61  			Replicas: &replicas,
    62  			Selector: &metav1.LabelSelector{
    63  				MatchLabels: map[string]string{
    64  					"app": "nginx",
    65  				},
    66  			},
    67  			Template: corev1.PodTemplateSpec{
    68  				ObjectMeta: metav1.ObjectMeta{
    69  					Labels: map[string]string{
    70  						"app": "nginx",
    71  					},
    72  				},
    73  				Spec: corev1.PodSpec{
    74  					Containers: []corev1.Container{
    75  						{
    76  							Image:           "nginx:latest",
    77  							ImagePullPolicy: corev1.PullIfNotPresent,
    78  							Name:            "nginx",
    79  						},
    80  					},
    81  				},
    82  			},
    83  		},
    84  	}
    85  }
    86  
    87  // NewTimerDeployment creates a timer deployment
    88  func NewTimerDeployment(name, namespace string) *appsv1.Deployment {
    89  	return &appsv1.Deployment{
    90  		ObjectMeta: metav1.ObjectMeta{
    91  			Name:      name,
    92  			Namespace: namespace,
    93  			Labels: map[string]string{
    94  				"app": name,
    95  			},
    96  		},
    97  		Spec: appsv1.DeploymentSpec{
    98  			Replicas: pointer.Int32Ptr(1),
    99  			Selector: &metav1.LabelSelector{
   100  				MatchLabels: map[string]string{
   101  					"app": name,
   102  				},
   103  			},
   104  			Template: corev1.PodTemplateSpec{
   105  				ObjectMeta: metav1.ObjectMeta{
   106  					Labels: map[string]string{
   107  						"app": name,
   108  					},
   109  				},
   110  				Spec: corev1.PodSpec{
   111  					Containers: []corev1.Container{
   112  						{
   113  							Image:           config.TestConfig.E2EImage,
   114  							ImagePullPolicy: corev1.PullIfNotPresent,
   115  							Name:            name,
   116  							Command:         []string{"/bin/test"},
   117  						},
   118  					},
   119  				},
   120  			},
   121  		},
   122  	}
   123  }
   124  
   125  // NewNetworkTestDeployment creates a deployment for e2e test
   126  func NewNetworkTestDeployment(name, namespace string, extraLabels map[string]string) *appsv1.Deployment {
   127  	labels := map[string]string{
   128  		"app": name,
   129  	}
   130  	for key, val := range extraLabels {
   131  		labels[key] = val
   132  	}
   133  	return &appsv1.Deployment{
   134  		ObjectMeta: metav1.ObjectMeta{
   135  			Name:      name,
   136  			Namespace: namespace,
   137  			Labels:    labels,
   138  		},
   139  		Spec: appsv1.DeploymentSpec{
   140  			Replicas: pointer.Int32Ptr(1),
   141  			Selector: &metav1.LabelSelector{
   142  				MatchLabels: labels,
   143  			},
   144  			Template: corev1.PodTemplateSpec{
   145  				ObjectMeta: metav1.ObjectMeta{
   146  					Labels: labels,
   147  				},
   148  				Spec: corev1.PodSpec{
   149  					Containers: []corev1.Container{
   150  						{
   151  							Image:           config.TestConfig.E2EImage,
   152  							ImagePullPolicy: corev1.PullIfNotPresent,
   153  							Name:            "network",
   154  							Command:         []string{"/bin/test"},
   155  						},
   156  					},
   157  				},
   158  			},
   159  		},
   160  	}
   161  }
   162  
   163  // NewIOTestDeployment creates a deployment for e2e test
   164  func NewIOTestDeployment(name, namespace string) *appsv1.Deployment {
   165  	return &appsv1.Deployment{
   166  		ObjectMeta: metav1.ObjectMeta{
   167  			Name:      name,
   168  			Namespace: namespace,
   169  			Labels: map[string]string{
   170  				"app": "io",
   171  			},
   172  		},
   173  		Spec: appsv1.DeploymentSpec{
   174  			Replicas: pointer.Int32Ptr(1),
   175  			Selector: &metav1.LabelSelector{
   176  				MatchLabels: map[string]string{
   177  					"app": "io",
   178  				},
   179  			},
   180  			Template: corev1.PodTemplateSpec{
   181  				ObjectMeta: metav1.ObjectMeta{
   182  					Labels: map[string]string{
   183  						"app": "io",
   184  					},
   185  					Annotations: map[string]string{
   186  						"admission-webhook.chaos-mesh.org/request": "chaosfs-io",
   187  					},
   188  				},
   189  				Spec: corev1.PodSpec{
   190  					Containers: []corev1.Container{
   191  						{
   192  							Image:           config.TestConfig.E2EImage,
   193  							ImagePullPolicy: corev1.PullIfNotPresent,
   194  							Name:            "io",
   195  							Command:         []string{"/bin/test"},
   196  							VolumeMounts: []corev1.VolumeMount{
   197  								{
   198  									Name:      "datadir",
   199  									MountPath: "/var/run/data",
   200  								},
   201  							},
   202  						},
   203  					},
   204  					Volumes: []corev1.Volume{
   205  						{
   206  							Name: "datadir",
   207  							VolumeSource: corev1.VolumeSource{
   208  								EmptyDir: &corev1.EmptyDirVolumeSource{},
   209  							},
   210  						},
   211  					},
   212  				},
   213  			},
   214  		},
   215  	}
   216  }
   217  
   218  // NewE2EService creates a service for the E2E helper deployment
   219  func NewE2EService(name, namespace string) *corev1.Service {
   220  	return &corev1.Service{
   221  		ObjectMeta: metav1.ObjectMeta{
   222  			Namespace: namespace,
   223  			Name:      name,
   224  		},
   225  		Spec: corev1.ServiceSpec{
   226  			Type: corev1.ServiceTypeNodePort,
   227  			Selector: map[string]string{
   228  				"app": name,
   229  			},
   230  			Ports: []corev1.ServicePort{
   231  				{
   232  					Name:       "http",
   233  					Port:       8080,
   234  					TargetPort: intstr.IntOrString{IntVal: 8080},
   235  				},
   236  				// Only used in network chaos
   237  				{
   238  					Name:       "nc-port",
   239  					Port:       1070,
   240  					TargetPort: intstr.IntOrString{IntVal: 8000},
   241  				},
   242  				// Only used in io chaos
   243  				{
   244  					Name:       "chaosfs",
   245  					Port:       65534,
   246  					TargetPort: intstr.IntOrString{IntVal: 65534},
   247  				},
   248  			},
   249  		},
   250  	}
   251  }
   252  
   253  // HaveSameUIDs returns if pods1 and pods2 are same based on their UIDs
   254  func HaveSameUIDs(pods1 []corev1.Pod, pods2 []corev1.Pod) bool {
   255  	count := len(pods1)
   256  	if count != len(pods2) {
   257  		return false
   258  	}
   259  	ids1, ids2 := make([]string, count), make([]string, count)
   260  	for i := 0; i < count; i++ {
   261  		ids1[i], ids2[i] = string(pods1[i].UID), string(pods2[i].UID)
   262  	}
   263  	sort.Strings(ids1)
   264  	sort.Strings(ids2)
   265  	for i := 0; i < count; i++ {
   266  		if ids1[i] != ids2[i] {
   267  			return false
   268  		}
   269  	}
   270  	return true
   271  }
   272