plugins_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2020 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 scheduler
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/api/resource"
  18. utilfeature "k8s.io/apiserver/pkg/util/feature"
  19. featuregatetesting "k8s.io/component-base/featuregate/testing"
  20. "k8s.io/kubernetes/pkg/features"
  21. )
  22. func TestNodeResourceLimits(t *testing.T) {
  23. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ResourceLimitsPriorityFunction, true)()
  24. testCtx := initTest(t, "node-resource-limits")
  25. defer cleanupTest(t, testCtx)
  26. // Add one node
  27. expectedNode, err := createNode(testCtx.clientSet, "test-node1", &v1.ResourceList{
  28. v1.ResourcePods: *resource.NewQuantity(32, resource.DecimalSI),
  29. v1.ResourceCPU: *resource.NewMilliQuantity(2000, resource.DecimalSI),
  30. v1.ResourceMemory: *resource.NewQuantity(2000, resource.DecimalSI),
  31. })
  32. if err != nil {
  33. t.Fatalf("Cannot create node: %v", err)
  34. }
  35. // Add another node with less resource
  36. _, err = createNode(testCtx.clientSet, "test-node2", &v1.ResourceList{
  37. v1.ResourcePods: *resource.NewQuantity(32, resource.DecimalSI),
  38. v1.ResourceCPU: *resource.NewMilliQuantity(1000, resource.DecimalSI),
  39. v1.ResourceMemory: *resource.NewQuantity(1000, resource.DecimalSI),
  40. })
  41. if err != nil {
  42. t.Fatalf("Cannot create node: %v", err)
  43. }
  44. podName := "pod-with-resource-limits"
  45. pod, err := runPausePod(testCtx.clientSet, initPausePod(testCtx.clientSet, &pausePodConfig{
  46. Name: podName,
  47. Namespace: testCtx.ns.Name,
  48. Resources: &v1.ResourceRequirements{Requests: v1.ResourceList{
  49. v1.ResourceCPU: *resource.NewMilliQuantity(500, resource.DecimalSI),
  50. v1.ResourceMemory: *resource.NewQuantity(500, resource.DecimalSI)},
  51. },
  52. }))
  53. if err != nil {
  54. t.Fatalf("Error running pause pod: %v", err)
  55. }
  56. if pod.Spec.NodeName != expectedNode.Name {
  57. t.Errorf("pod %v got scheduled on an unexpected node: %v. Expected node: %v.", podName, pod.Spec.NodeName, expectedNode.Name)
  58. } else {
  59. t.Logf("pod %v got successfully scheduled on node %v.", podName, pod.Spec.NodeName)
  60. }
  61. }