conversion_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 v1_test
  14. import (
  15. "net/url"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/api/resource"
  21. "k8s.io/kubernetes/pkg/api/unversioned"
  22. versioned "k8s.io/kubernetes/pkg/api/v1"
  23. "k8s.io/kubernetes/pkg/runtime"
  24. "k8s.io/kubernetes/pkg/util/diff"
  25. )
  26. func TestPodLogOptions(t *testing.T) {
  27. sinceSeconds := int64(1)
  28. sinceTime := unversioned.NewTime(time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC).Local())
  29. tailLines := int64(2)
  30. limitBytes := int64(3)
  31. versionedLogOptions := &versioned.PodLogOptions{
  32. Container: "mycontainer",
  33. Follow: true,
  34. Previous: true,
  35. SinceSeconds: &sinceSeconds,
  36. SinceTime: &sinceTime,
  37. Timestamps: true,
  38. TailLines: &tailLines,
  39. LimitBytes: &limitBytes,
  40. }
  41. unversionedLogOptions := &api.PodLogOptions{
  42. Container: "mycontainer",
  43. Follow: true,
  44. Previous: true,
  45. SinceSeconds: &sinceSeconds,
  46. SinceTime: &sinceTime,
  47. Timestamps: true,
  48. TailLines: &tailLines,
  49. LimitBytes: &limitBytes,
  50. }
  51. expectedParameters := url.Values{
  52. "container": {"mycontainer"},
  53. "follow": {"true"},
  54. "previous": {"true"},
  55. "sinceSeconds": {"1"},
  56. "sinceTime": {"2000-01-01T12:34:56Z"},
  57. "timestamps": {"true"},
  58. "tailLines": {"2"},
  59. "limitBytes": {"3"},
  60. }
  61. codec := runtime.NewParameterCodec(api.Scheme)
  62. // unversioned -> query params
  63. {
  64. actualParameters, err := codec.EncodeParameters(unversionedLogOptions, versioned.SchemeGroupVersion)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if !reflect.DeepEqual(actualParameters, expectedParameters) {
  69. t.Fatalf("Expected\n%#v\ngot\n%#v", expectedParameters, actualParameters)
  70. }
  71. }
  72. // versioned -> query params
  73. {
  74. actualParameters, err := codec.EncodeParameters(versionedLogOptions, versioned.SchemeGroupVersion)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if !reflect.DeepEqual(actualParameters, expectedParameters) {
  79. t.Fatalf("Expected\n%#v\ngot\n%#v", expectedParameters, actualParameters)
  80. }
  81. }
  82. // query params -> versioned
  83. {
  84. convertedLogOptions := &versioned.PodLogOptions{}
  85. err := codec.DecodeParameters(expectedParameters, versioned.SchemeGroupVersion, convertedLogOptions)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if !reflect.DeepEqual(convertedLogOptions, versionedLogOptions) {
  90. t.Fatalf("Unexpected deserialization:\n%s", diff.ObjectGoPrintSideBySide(versionedLogOptions, convertedLogOptions))
  91. }
  92. }
  93. // query params -> unversioned
  94. {
  95. convertedLogOptions := &api.PodLogOptions{}
  96. err := codec.DecodeParameters(expectedParameters, versioned.SchemeGroupVersion, convertedLogOptions)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if !reflect.DeepEqual(convertedLogOptions, unversionedLogOptions) {
  101. t.Fatalf("Unexpected deserialization:\n%s", diff.ObjectGoPrintSideBySide(unversionedLogOptions, convertedLogOptions))
  102. }
  103. }
  104. }
  105. // TestPodSpecConversion tests that ServiceAccount is an alias for
  106. // ServiceAccountName.
  107. func TestPodSpecConversion(t *testing.T) {
  108. name, other := "foo", "bar"
  109. // Test internal -> v1. Should have both alias (DeprecatedServiceAccount)
  110. // and new field (ServiceAccountName).
  111. i := &api.PodSpec{
  112. ServiceAccountName: name,
  113. }
  114. v := versioned.PodSpec{}
  115. if err := api.Scheme.Convert(i, &v, nil); err != nil {
  116. t.Fatalf("unexpected error: %v", err)
  117. }
  118. if v.ServiceAccountName != name {
  119. t.Fatalf("want v1.ServiceAccountName %q, got %q", name, v.ServiceAccountName)
  120. }
  121. if v.DeprecatedServiceAccount != name {
  122. t.Fatalf("want v1.DeprecatedServiceAccount %q, got %q", name, v.DeprecatedServiceAccount)
  123. }
  124. // Test v1 -> internal. Either DeprecatedServiceAccount, ServiceAccountName,
  125. // or both should translate to ServiceAccountName. ServiceAccountName wins
  126. // if both are set.
  127. testCases := []*versioned.PodSpec{
  128. // New
  129. {ServiceAccountName: name},
  130. // Alias
  131. {DeprecatedServiceAccount: name},
  132. // Both: same
  133. {ServiceAccountName: name, DeprecatedServiceAccount: name},
  134. // Both: different
  135. {ServiceAccountName: name, DeprecatedServiceAccount: other},
  136. }
  137. for k, v := range testCases {
  138. got := api.PodSpec{}
  139. err := api.Scheme.Convert(v, &got, nil)
  140. if err != nil {
  141. t.Fatalf("unexpected error for case %d: %v", k, err)
  142. }
  143. if got.ServiceAccountName != name {
  144. t.Fatalf("want api.ServiceAccountName %q, got %q", name, got.ServiceAccountName)
  145. }
  146. }
  147. }
  148. func TestResourceListConversion(t *testing.T) {
  149. bigMilliQuantity := resource.NewQuantity(resource.MaxMilliValue, resource.DecimalSI)
  150. bigMilliQuantity.Add(resource.MustParse("12345m"))
  151. tests := []struct {
  152. input versioned.ResourceList
  153. expected api.ResourceList
  154. }{
  155. { // No changes necessary.
  156. input: versioned.ResourceList{
  157. versioned.ResourceMemory: resource.MustParse("30M"),
  158. versioned.ResourceCPU: resource.MustParse("100m"),
  159. versioned.ResourceStorage: resource.MustParse("1G"),
  160. },
  161. expected: api.ResourceList{
  162. api.ResourceMemory: resource.MustParse("30M"),
  163. api.ResourceCPU: resource.MustParse("100m"),
  164. api.ResourceStorage: resource.MustParse("1G"),
  165. },
  166. },
  167. { // Nano-scale values should be rounded up to milli-scale.
  168. input: versioned.ResourceList{
  169. versioned.ResourceCPU: resource.MustParse("3.000023m"),
  170. versioned.ResourceMemory: resource.MustParse("500.000050m"),
  171. },
  172. expected: api.ResourceList{
  173. api.ResourceCPU: resource.MustParse("4m"),
  174. api.ResourceMemory: resource.MustParse("501m"),
  175. },
  176. },
  177. { // Large values should still be accurate.
  178. input: versioned.ResourceList{
  179. versioned.ResourceCPU: *bigMilliQuantity.Copy(),
  180. versioned.ResourceStorage: *bigMilliQuantity.Copy(),
  181. },
  182. expected: api.ResourceList{
  183. api.ResourceCPU: *bigMilliQuantity.Copy(),
  184. api.ResourceStorage: *bigMilliQuantity.Copy(),
  185. },
  186. },
  187. }
  188. for i, test := range tests {
  189. output := api.ResourceList{}
  190. err := api.Scheme.Convert(&test.input, &output, nil)
  191. if err != nil {
  192. t.Fatalf("unexpected error for case %d: %v", i, err)
  193. }
  194. if !api.Semantic.DeepEqual(test.expected, output) {
  195. t.Errorf("unexpected conversion for case %d: Expected %+v; Got %+v", i, test.expected, output)
  196. }
  197. }
  198. }