conversion_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. Copyright 2014 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 runtime_test
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/runtime"
  19. )
  20. type InternalComplex struct {
  21. runtime.TypeMeta
  22. String string
  23. Integer int
  24. Integer64 int64
  25. Int64 int64
  26. Bool bool
  27. }
  28. type ExternalComplex struct {
  29. runtime.TypeMeta `json:",inline"`
  30. String string `json:"string" description:"testing"`
  31. Integer int `json:"int"`
  32. Integer64 int64 `json:",omitempty"`
  33. Int64 int64
  34. Bool bool `json:"bool"`
  35. }
  36. func (obj *InternalComplex) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
  37. func (obj *ExternalComplex) GetObjectKind() unversioned.ObjectKind { return &obj.TypeMeta }
  38. func TestStringMapConversion(t *testing.T) {
  39. internalGV := unversioned.GroupVersion{Group: "test.group", Version: runtime.APIVersionInternal}
  40. externalGV := unversioned.GroupVersion{Group: "test.group", Version: "external"}
  41. scheme := runtime.NewScheme()
  42. scheme.Log(t)
  43. scheme.AddKnownTypeWithName(internalGV.WithKind("Complex"), &InternalComplex{})
  44. scheme.AddKnownTypeWithName(externalGV.WithKind("Complex"), &ExternalComplex{})
  45. testCases := map[string]struct {
  46. input map[string][]string
  47. errFn func(error) bool
  48. expected runtime.Object
  49. }{
  50. "ignores omitempty": {
  51. input: map[string][]string{
  52. "String": {"not_used"},
  53. "string": {"value"},
  54. "int": {"1"},
  55. "Integer64": {"2"},
  56. },
  57. expected: &ExternalComplex{String: "value", Integer: 1},
  58. },
  59. "returns error on bad int": {
  60. input: map[string][]string{
  61. "int": {"a"},
  62. },
  63. errFn: func(err error) bool { return err != nil },
  64. expected: &ExternalComplex{},
  65. },
  66. "parses int64": {
  67. input: map[string][]string{
  68. "Int64": {"-1"},
  69. },
  70. expected: &ExternalComplex{Int64: -1},
  71. },
  72. "returns error on bad int64": {
  73. input: map[string][]string{
  74. "Int64": {"a"},
  75. },
  76. errFn: func(err error) bool { return err != nil },
  77. expected: &ExternalComplex{},
  78. },
  79. "parses boolean true": {
  80. input: map[string][]string{
  81. "bool": {"true"},
  82. },
  83. expected: &ExternalComplex{Bool: true},
  84. },
  85. "parses boolean any value": {
  86. input: map[string][]string{
  87. "bool": {"foo"},
  88. },
  89. expected: &ExternalComplex{Bool: true},
  90. },
  91. "parses boolean false": {
  92. input: map[string][]string{
  93. "bool": {"false"},
  94. },
  95. expected: &ExternalComplex{Bool: false},
  96. },
  97. "parses boolean empty value": {
  98. input: map[string][]string{
  99. "bool": {""},
  100. },
  101. expected: &ExternalComplex{Bool: true},
  102. },
  103. "parses boolean no value": {
  104. input: map[string][]string{
  105. "bool": {},
  106. },
  107. expected: &ExternalComplex{Bool: false},
  108. },
  109. }
  110. for k, tc := range testCases {
  111. out := &ExternalComplex{}
  112. if err := scheme.Convert(&tc.input, out, nil); (tc.errFn == nil && err != nil) || (tc.errFn != nil && !tc.errFn(err)) {
  113. t.Errorf("%s: unexpected error: %v", k, err)
  114. continue
  115. } else if err != nil {
  116. continue
  117. }
  118. if !reflect.DeepEqual(out, tc.expected) {
  119. t.Errorf("%s: unexpected output: %#v", k, out)
  120. }
  121. }
  122. }