conversion.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2015 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. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/fields"
  18. )
  19. // TestSelectableFieldLabelConversions verifies that given resource have field
  20. // label conversion defined for each its selectable field.
  21. // fields contains selectable fields of the resource.
  22. // labelMap maps deprecated labels to their canonical names.
  23. func TestSelectableFieldLabelConversionsOfKind(t *testing.T, apiVersion string, kind string, fields fields.Set, labelMap map[string]string) {
  24. badFieldLabels := []string{
  25. "name",
  26. ".name",
  27. "bad",
  28. "metadata",
  29. "foo.bar",
  30. }
  31. value := "value"
  32. if len(fields) == 0 {
  33. t.Logf("no selectable fields for kind %q, skipping", kind)
  34. }
  35. for label := range fields {
  36. if label == "name" {
  37. t.Logf("FIXME: \"name\" is deprecated by \"metadata.name\", it should be removed from selectable fields of kind=%s", kind)
  38. continue
  39. }
  40. newLabel, newValue, err := api.Scheme.ConvertFieldLabel(apiVersion, kind, label, value)
  41. if err != nil {
  42. t.Errorf("kind=%s label=%s: got unexpected error: %v", kind, label, err)
  43. } else {
  44. expectedLabel := label
  45. if l, exists := labelMap[label]; exists {
  46. expectedLabel = l
  47. }
  48. if newLabel != expectedLabel {
  49. t.Errorf("kind=%s label=%s: got unexpected label name (%q != %q)", kind, label, newLabel, expectedLabel)
  50. }
  51. if newValue != value {
  52. t.Errorf("kind=%s label=%s: got unexpected new value (%q != %q)", kind, label, newValue, value)
  53. }
  54. }
  55. }
  56. for _, label := range badFieldLabels {
  57. _, _, err := api.Scheme.ConvertFieldLabel(apiVersion, kind, label, "value")
  58. if err == nil {
  59. t.Errorf("kind=%s label=%s: got unexpected non-error", kind, label)
  60. }
  61. }
  62. }