policy_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2019 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 testing
  14. import (
  15. "fmt"
  16. "testing"
  17. "github.com/google/go-cmp/cmp"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/kubernetes/pkg/scheduler/apis/config"
  20. "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
  21. )
  22. const (
  23. policyTemplate = `
  24. apiVersion: %s
  25. kind: Policy
  26. extenders:
  27. - urlPrefix: http://localhost:8888/
  28. filterVerb: filter
  29. prioritizeVerb: prioritize
  30. weight: 1
  31. enableHttps: false
  32. `
  33. )
  34. func TestSchedulerPolicy(t *testing.T) {
  35. expected := &config.Policy{
  36. Extenders: []config.Extender{
  37. {
  38. URLPrefix: "http://localhost:8888/",
  39. FilterVerb: "filter",
  40. PrioritizeVerb: "prioritize",
  41. Weight: 1,
  42. EnableHTTPS: false,
  43. },
  44. },
  45. }
  46. testcases := []struct {
  47. name string
  48. apiVersion string
  49. expectError bool
  50. expectedObj *config.Policy
  51. }{
  52. // verifies if a Policy YAML with apiVersion 'v1' can be
  53. // serialized into an unversioned Policy object.
  54. {
  55. name: "legacy v1",
  56. apiVersion: "v1",
  57. expectError: false,
  58. expectedObj: expected,
  59. },
  60. // verifies if a Policy YAML with apiVersion 'kubescheduler.config.k8s.io/v1'
  61. // can be serialized into an unversioned Policy object.
  62. {
  63. name: "v1",
  64. apiVersion: "kubescheduler.config.k8s.io/v1",
  65. expectError: false,
  66. expectedObj: expected,
  67. },
  68. // ensures unknown version throws a parsing error.
  69. {
  70. name: "unknown version",
  71. apiVersion: "kubescheduler.config.k8s.io/vunknown",
  72. expectError: true,
  73. },
  74. }
  75. for _, tt := range testcases {
  76. t.Run(tt.name, func(t *testing.T) {
  77. policyStr := fmt.Sprintf(policyTemplate, tt.apiVersion)
  78. got, err := loadPolicy([]byte(policyStr))
  79. if (err != nil) != tt.expectError {
  80. t.Fatalf("Error while parsing Policy. expectErr=%v, but got=%v.", tt.expectError, err)
  81. }
  82. if !tt.expectError {
  83. if diff := cmp.Diff(tt.expectedObj, got); diff != "" {
  84. t.Errorf("Unexpected policy diff (-want, +got): %s", diff)
  85. }
  86. }
  87. })
  88. }
  89. }
  90. // loadPolicy decodes data as a Policy object.
  91. func loadPolicy(data []byte) (*config.Policy, error) {
  92. policy := config.Policy{}
  93. if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), data, &policy); err != nil {
  94. return nil, err
  95. }
  96. return &policy, nil
  97. }