...

Source file src/k8s.io/kubernetes/test/integration/defaulttolerationseconds/defaulttolerationseconds_test.go

Documentation: k8s.io/kubernetes/test/integration/defaulttolerationseconds

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package defaulttolerationseconds
    18  
    19  import (
    20  	"testing"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/kubernetes/pkg/apis/core/helper"
    25  	"k8s.io/kubernetes/pkg/controlplane"
    26  	"k8s.io/kubernetes/plugin/pkg/admission/defaulttolerationseconds"
    27  	"k8s.io/kubernetes/test/integration/framework"
    28  	"k8s.io/kubernetes/test/utils/ktesting"
    29  )
    30  
    31  func TestAdmission(t *testing.T) {
    32  	tCtx := ktesting.Init(t)
    33  	client, _, tearDownFn := framework.StartTestServer(tCtx, t, framework.TestServerSetup{
    34  		ModifyServerConfig: func(cfg *controlplane.Config) {
    35  			cfg.GenericConfig.EnableProfiling = true
    36  			cfg.GenericConfig.AdmissionControl = defaulttolerationseconds.NewDefaultTolerationSeconds()
    37  		},
    38  	})
    39  	defer tearDownFn()
    40  
    41  	ns := framework.CreateNamespaceOrDie(client, "default-toleration-seconds", t)
    42  	defer framework.DeleteNamespaceOrDie(client, ns, t)
    43  
    44  	pod := v1.Pod{
    45  		ObjectMeta: metav1.ObjectMeta{
    46  			Namespace: ns.Name,
    47  			Name:      "foo",
    48  		},
    49  		Spec: v1.PodSpec{
    50  			Containers: []v1.Container{
    51  				{
    52  					Name:  "test",
    53  					Image: "an-image",
    54  				},
    55  			},
    56  		},
    57  	}
    58  
    59  	updatedPod, err := client.CoreV1().Pods(pod.Namespace).Create(tCtx, &pod, metav1.CreateOptions{})
    60  	if err != nil {
    61  		t.Fatalf("error creating pod: %v", err)
    62  	}
    63  
    64  	var defaultSeconds int64 = 300
    65  	nodeNotReady := v1.Toleration{
    66  		Key:               v1.TaintNodeNotReady,
    67  		Operator:          v1.TolerationOpExists,
    68  		Effect:            v1.TaintEffectNoExecute,
    69  		TolerationSeconds: &defaultSeconds,
    70  	}
    71  
    72  	nodeUnreachable := v1.Toleration{
    73  		Key:               v1.TaintNodeUnreachable,
    74  		Operator:          v1.TolerationOpExists,
    75  		Effect:            v1.TaintEffectNoExecute,
    76  		TolerationSeconds: &defaultSeconds,
    77  	}
    78  
    79  	found := 0
    80  	tolerations := updatedPod.Spec.Tolerations
    81  	for i := range tolerations {
    82  		if found == 2 {
    83  			break
    84  		}
    85  		if tolerations[i].MatchToleration(&nodeNotReady) {
    86  			if helper.Semantic.DeepEqual(tolerations[i], nodeNotReady) {
    87  				found++
    88  				continue
    89  			}
    90  		}
    91  		if tolerations[i].MatchToleration(&nodeUnreachable) {
    92  			if helper.Semantic.DeepEqual(tolerations[i], nodeUnreachable) {
    93  				found++
    94  				continue
    95  			}
    96  		}
    97  	}
    98  
    99  	if found != 2 {
   100  		t.Fatalf("unexpected tolerations: %v\n", updatedPod.Spec.Tolerations)
   101  	}
   102  }
   103  

View as plain text