defaulttolerationseconds_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package defaulttolerationseconds
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. clientset "k8s.io/client-go/kubernetes"
  20. restclient "k8s.io/client-go/rest"
  21. "k8s.io/kubernetes/pkg/apis/core/helper"
  22. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  23. "k8s.io/kubernetes/plugin/pkg/admission/defaulttolerationseconds"
  24. "k8s.io/kubernetes/test/integration/framework"
  25. )
  26. func TestAdmission(t *testing.T) {
  27. masterConfig := framework.NewMasterConfig()
  28. masterConfig.GenericConfig.EnableProfiling = true
  29. masterConfig.GenericConfig.AdmissionControl = defaulttolerationseconds.NewDefaultTolerationSeconds()
  30. _, s, closeFn := framework.RunAMaster(masterConfig)
  31. defer closeFn()
  32. client := clientset.NewForConfigOrDie(&restclient.Config{Host: s.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
  33. ns := framework.CreateTestingNamespace("default-toleration-seconds", s, t)
  34. defer framework.DeleteTestingNamespace(ns, s, t)
  35. pod := v1.Pod{
  36. ObjectMeta: metav1.ObjectMeta{
  37. Namespace: ns.Name,
  38. Name: "foo",
  39. },
  40. Spec: v1.PodSpec{
  41. Containers: []v1.Container{
  42. {
  43. Name: "test",
  44. Image: "an-image",
  45. },
  46. },
  47. },
  48. }
  49. updatedPod, err := client.CoreV1().Pods(pod.Namespace).Create(&pod)
  50. if err != nil {
  51. t.Fatalf("error creating pod: %v", err)
  52. }
  53. var defaultSeconds int64 = 300
  54. nodeNotReady := v1.Toleration{
  55. Key: schedulerapi.TaintNodeNotReady,
  56. Operator: v1.TolerationOpExists,
  57. Effect: v1.TaintEffectNoExecute,
  58. TolerationSeconds: &defaultSeconds,
  59. }
  60. nodeUnreachable := v1.Toleration{
  61. Key: schedulerapi.TaintNodeUnreachable,
  62. Operator: v1.TolerationOpExists,
  63. Effect: v1.TaintEffectNoExecute,
  64. TolerationSeconds: &defaultSeconds,
  65. }
  66. found := 0
  67. tolerations := updatedPod.Spec.Tolerations
  68. for i := range tolerations {
  69. if found == 2 {
  70. break
  71. }
  72. if tolerations[i].MatchToleration(&nodeNotReady) {
  73. if helper.Semantic.DeepEqual(tolerations[i], nodeNotReady) {
  74. found++
  75. continue
  76. }
  77. }
  78. if tolerations[i].MatchToleration(&nodeUnreachable) {
  79. if helper.Semantic.DeepEqual(tolerations[i], nodeUnreachable) {
  80. found++
  81. continue
  82. }
  83. }
  84. }
  85. if found != 2 {
  86. t.Fatalf("unexpected tolerations: %v\n", updatedPod.Spec.Tolerations)
  87. }
  88. }