conversion_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. "encoding/json"
  16. "math/rand"
  17. "net/url"
  18. "reflect"
  19. "testing"
  20. "time"
  21. appsv1 "k8s.io/api/apps/v1"
  22. "k8s.io/api/core/v1"
  23. "k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
  24. apiequality "k8s.io/apimachinery/pkg/api/equality"
  25. "k8s.io/apimachinery/pkg/api/resource"
  26. metafuzzer "k8s.io/apimachinery/pkg/apis/meta/fuzzer"
  27. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  28. "k8s.io/apimachinery/pkg/runtime"
  29. "k8s.io/apimachinery/pkg/util/diff"
  30. "k8s.io/kubernetes/pkg/api/legacyscheme"
  31. apps "k8s.io/kubernetes/pkg/apis/apps"
  32. "k8s.io/kubernetes/pkg/apis/core"
  33. corefuzzer "k8s.io/kubernetes/pkg/apis/core/fuzzer"
  34. corev1 "k8s.io/kubernetes/pkg/apis/core/v1"
  35. utilpointer "k8s.io/utils/pointer"
  36. // enforce that all types are installed
  37. _ "k8s.io/kubernetes/pkg/api/testapi"
  38. )
  39. func TestPodLogOptions(t *testing.T) {
  40. sinceSeconds := int64(1)
  41. sinceTime := metav1.NewTime(time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC).Local())
  42. tailLines := int64(2)
  43. limitBytes := int64(3)
  44. versionedLogOptions := &v1.PodLogOptions{
  45. Container: "mycontainer",
  46. Follow: true,
  47. Previous: true,
  48. SinceSeconds: &sinceSeconds,
  49. SinceTime: &sinceTime,
  50. Timestamps: true,
  51. TailLines: &tailLines,
  52. LimitBytes: &limitBytes,
  53. }
  54. unversionedLogOptions := &core.PodLogOptions{
  55. Container: "mycontainer",
  56. Follow: true,
  57. Previous: true,
  58. SinceSeconds: &sinceSeconds,
  59. SinceTime: &sinceTime,
  60. Timestamps: true,
  61. TailLines: &tailLines,
  62. LimitBytes: &limitBytes,
  63. }
  64. expectedParameters := url.Values{
  65. "container": {"mycontainer"},
  66. "follow": {"true"},
  67. "previous": {"true"},
  68. "sinceSeconds": {"1"},
  69. "sinceTime": {"2000-01-01T12:34:56Z"},
  70. "timestamps": {"true"},
  71. "tailLines": {"2"},
  72. "limitBytes": {"3"},
  73. }
  74. codec := runtime.NewParameterCodec(legacyscheme.Scheme)
  75. // unversioned -> query params
  76. {
  77. actualParameters, err := codec.EncodeParameters(unversionedLogOptions, v1.SchemeGroupVersion)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if !reflect.DeepEqual(actualParameters, expectedParameters) {
  82. t.Fatalf("Expected\n%#v\ngot\n%#v", expectedParameters, actualParameters)
  83. }
  84. }
  85. // versioned -> query params
  86. {
  87. actualParameters, err := codec.EncodeParameters(versionedLogOptions, v1.SchemeGroupVersion)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. if !reflect.DeepEqual(actualParameters, expectedParameters) {
  92. t.Fatalf("Expected\n%#v\ngot\n%#v", expectedParameters, actualParameters)
  93. }
  94. }
  95. // query params -> versioned
  96. {
  97. convertedLogOptions := &v1.PodLogOptions{}
  98. err := codec.DecodeParameters(expectedParameters, v1.SchemeGroupVersion, convertedLogOptions)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. if !reflect.DeepEqual(convertedLogOptions, versionedLogOptions) {
  103. t.Fatalf("Unexpected deserialization:\n%s", diff.ObjectGoPrintSideBySide(versionedLogOptions, convertedLogOptions))
  104. }
  105. }
  106. // query params -> unversioned
  107. {
  108. convertedLogOptions := &core.PodLogOptions{}
  109. err := codec.DecodeParameters(expectedParameters, v1.SchemeGroupVersion, convertedLogOptions)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. if !reflect.DeepEqual(convertedLogOptions, unversionedLogOptions) {
  114. t.Fatalf("Unexpected deserialization:\n%s", diff.ObjectGoPrintSideBySide(unversionedLogOptions, convertedLogOptions))
  115. }
  116. }
  117. }
  118. // TestPodSpecConversion tests that v1.ServiceAccount is an alias for
  119. // ServiceAccountName.
  120. func TestPodSpecConversion(t *testing.T) {
  121. name, other := "foo", "bar"
  122. // Test internal -> v1. Should have both alias (DeprecatedServiceAccount)
  123. // and new field (ServiceAccountName).
  124. i := &core.PodSpec{
  125. ServiceAccountName: name,
  126. }
  127. v := v1.PodSpec{}
  128. if err := legacyscheme.Scheme.Convert(i, &v, nil); err != nil {
  129. t.Fatalf("unexpected error: %v", err)
  130. }
  131. if v.ServiceAccountName != name {
  132. t.Fatalf("want v1.ServiceAccountName %q, got %q", name, v.ServiceAccountName)
  133. }
  134. if v.DeprecatedServiceAccount != name {
  135. t.Fatalf("want v1.DeprecatedServiceAccount %q, got %q", name, v.DeprecatedServiceAccount)
  136. }
  137. // Test v1 -> internal. Either DeprecatedServiceAccount, ServiceAccountName,
  138. // or both should translate to ServiceAccountName. ServiceAccountName wins
  139. // if both are set.
  140. testCases := []*v1.PodSpec{
  141. // New
  142. {ServiceAccountName: name},
  143. // Alias
  144. {DeprecatedServiceAccount: name},
  145. // Both: same
  146. {ServiceAccountName: name, DeprecatedServiceAccount: name},
  147. // Both: different
  148. {ServiceAccountName: name, DeprecatedServiceAccount: other},
  149. }
  150. for k, v := range testCases {
  151. got := core.PodSpec{}
  152. err := legacyscheme.Scheme.Convert(v, &got, nil)
  153. if err != nil {
  154. t.Fatalf("unexpected error for case %d: %v", k, err)
  155. }
  156. if got.ServiceAccountName != name {
  157. t.Fatalf("want core.ServiceAccountName %q, got %q", name, got.ServiceAccountName)
  158. }
  159. }
  160. }
  161. func TestResourceListConversion(t *testing.T) {
  162. bigMilliQuantity := resource.NewQuantity(resource.MaxMilliValue, resource.DecimalSI)
  163. bigMilliQuantity.Add(resource.MustParse("12345m"))
  164. tests := []struct {
  165. input v1.ResourceList
  166. expected core.ResourceList
  167. }{
  168. { // No changes necessary.
  169. input: v1.ResourceList{
  170. v1.ResourceMemory: resource.MustParse("30M"),
  171. v1.ResourceCPU: resource.MustParse("100m"),
  172. v1.ResourceStorage: resource.MustParse("1G"),
  173. },
  174. expected: core.ResourceList{
  175. core.ResourceMemory: resource.MustParse("30M"),
  176. core.ResourceCPU: resource.MustParse("100m"),
  177. core.ResourceStorage: resource.MustParse("1G"),
  178. },
  179. },
  180. { // Nano-scale values should be rounded up to milli-scale.
  181. input: v1.ResourceList{
  182. v1.ResourceCPU: resource.MustParse("3.000023m"),
  183. v1.ResourceMemory: resource.MustParse("500.000050m"),
  184. },
  185. expected: core.ResourceList{
  186. core.ResourceCPU: resource.MustParse("4m"),
  187. core.ResourceMemory: resource.MustParse("501m"),
  188. },
  189. },
  190. { // Large values should still be accurate.
  191. input: v1.ResourceList{
  192. v1.ResourceCPU: *bigMilliQuantity.Copy(),
  193. v1.ResourceStorage: *bigMilliQuantity.Copy(),
  194. },
  195. expected: core.ResourceList{
  196. core.ResourceCPU: *bigMilliQuantity.Copy(),
  197. core.ResourceStorage: *bigMilliQuantity.Copy(),
  198. },
  199. },
  200. }
  201. for i, test := range tests {
  202. output := core.ResourceList{}
  203. // defaulting is a separate step from conversion that is applied when reading from the API or from etcd.
  204. // perform that step explicitly.
  205. corev1.SetDefaults_ResourceList(&test.input)
  206. err := legacyscheme.Scheme.Convert(&test.input, &output, nil)
  207. if err != nil {
  208. t.Fatalf("unexpected error for case %d: %v", i, err)
  209. }
  210. if !apiequality.Semantic.DeepEqual(test.expected, output) {
  211. t.Errorf("unexpected conversion for case %d: Expected\n%+v;\nGot\n%+v", i, test.expected, output)
  212. }
  213. }
  214. }
  215. func TestReplicationControllerConversion(t *testing.T) {
  216. // If we start with a RC, we should always have round-trip fidelity.
  217. inputs := []*v1.ReplicationController{
  218. {
  219. ObjectMeta: metav1.ObjectMeta{
  220. Name: "name",
  221. Namespace: "namespace",
  222. },
  223. Spec: v1.ReplicationControllerSpec{
  224. Replicas: utilpointer.Int32Ptr(1),
  225. MinReadySeconds: 32,
  226. Selector: map[string]string{"foo": "bar", "bar": "foo"},
  227. Template: &v1.PodTemplateSpec{
  228. ObjectMeta: metav1.ObjectMeta{
  229. Labels: map[string]string{"foo": "bar", "bar": "foo"},
  230. },
  231. Spec: v1.PodSpec{
  232. Containers: []v1.Container{
  233. {
  234. Name: "container",
  235. Image: "image",
  236. },
  237. },
  238. },
  239. },
  240. },
  241. Status: v1.ReplicationControllerStatus{
  242. Replicas: 1,
  243. FullyLabeledReplicas: 2,
  244. ReadyReplicas: 3,
  245. AvailableReplicas: 4,
  246. ObservedGeneration: 5,
  247. Conditions: []v1.ReplicationControllerCondition{
  248. {
  249. Type: v1.ReplicationControllerReplicaFailure,
  250. Status: v1.ConditionTrue,
  251. LastTransitionTime: metav1.NewTime(time.Unix(123456789, 0)),
  252. Reason: "Reason",
  253. Message: "Message",
  254. },
  255. },
  256. },
  257. },
  258. }
  259. // Add some fuzzed RCs.
  260. apiObjectFuzzer := fuzzer.FuzzerFor(fuzzer.MergeFuzzerFuncs(metafuzzer.Funcs, corefuzzer.Funcs), rand.NewSource(152), legacyscheme.Codecs)
  261. for i := 0; i < 100; i++ {
  262. rc := &v1.ReplicationController{}
  263. apiObjectFuzzer.Fuzz(rc)
  264. // Sometimes the fuzzer decides to leave Spec.Template nil.
  265. // We can't support that because Spec.Template is not a pointer in RS,
  266. // so it will round-trip as non-nil but empty.
  267. if rc.Spec.Template == nil {
  268. rc.Spec.Template = &v1.PodTemplateSpec{}
  269. }
  270. // Sometimes the fuzzer decides to insert an empty label key.
  271. // This doesn't round-trip properly because it's invalid.
  272. if rc.Spec.Selector != nil {
  273. delete(rc.Spec.Selector, "")
  274. }
  275. inputs = append(inputs, rc)
  276. }
  277. // Round-trip the input RCs before converting to RS.
  278. for i := range inputs {
  279. inputs[i] = roundTrip(t, inputs[i]).(*v1.ReplicationController)
  280. }
  281. for _, in := range inputs {
  282. rs := &apps.ReplicaSet{}
  283. // Use in.DeepCopy() to avoid sharing pointers with `in`.
  284. if err := corev1.Convert_v1_ReplicationController_To_apps_ReplicaSet(in.DeepCopy(), rs, nil); err != nil {
  285. t.Errorf("can't convert RC to RS: %v", err)
  286. continue
  287. }
  288. // Round-trip RS before converting back to RC.
  289. rs = roundTripRS(t, rs)
  290. out := &v1.ReplicationController{}
  291. if err := corev1.Convert_apps_ReplicaSet_To_v1_ReplicationController(rs, out, nil); err != nil {
  292. t.Errorf("can't convert RS to RC: %v", err)
  293. continue
  294. }
  295. if !apiequality.Semantic.DeepEqual(in, out) {
  296. instr, _ := json.MarshalIndent(in, "", " ")
  297. outstr, _ := json.MarshalIndent(out, "", " ")
  298. t.Errorf("RC-RS conversion round-trip failed:\nin:\n%s\nout:\n%s", instr, outstr)
  299. }
  300. }
  301. }
  302. func roundTripRS(t *testing.T, rs *apps.ReplicaSet) *apps.ReplicaSet {
  303. codec := legacyscheme.Codecs.LegacyCodec(appsv1.SchemeGroupVersion)
  304. data, err := runtime.Encode(codec, rs)
  305. if err != nil {
  306. t.Errorf("%v\n %#v", err, rs)
  307. return nil
  308. }
  309. obj2, err := runtime.Decode(codec, data)
  310. if err != nil {
  311. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), rs)
  312. return nil
  313. }
  314. obj3 := &apps.ReplicaSet{}
  315. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  316. if err != nil {
  317. t.Errorf("%v\nSource: %#v", err, obj2)
  318. return nil
  319. }
  320. return obj3
  321. }