merge_conflict_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Copyright 2018 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 strategy_test
  14. import (
  15. . "github.com/onsi/ginkgo"
  16. "k8s.io/kubernetes/pkg/kubectl/apply/strategy"
  17. )
  18. var _ = Describe("Comparing fields of remote and recorded ", func() {
  19. Context("Test conflict in map fields of remote and recorded", func() {
  20. It("If conflicts found, expected return error", func() {
  21. recorded := create(`
  22. apiVersion: apps/v1
  23. kind: Deployment
  24. spec:
  25. foo1: "key1"
  26. `)
  27. local := create(`
  28. apiVersion: apps/v1
  29. kind: Deployment
  30. spec:
  31. foo2: "baz2-1"
  32. `)
  33. remote := create(`
  34. apiVersion: apps/v1
  35. kind: Deployment
  36. spec:
  37. foo1: "baz1-0"
  38. `)
  39. expect := hasConflict
  40. // map fields have conflict : recorded {foo1 : "key1"}, remote {foo1 : "baz1-0"}
  41. runConflictTest(strategy.Create(strategy.Options{FailOnConflict: true}), recorded, local, remote, expect)
  42. })
  43. })
  44. Context("Test conflict in list fields of remote and recorded ", func() {
  45. It("If conflicts found, expected return false", func() {
  46. recorded := create(`
  47. apiVersion: apps/v1
  48. kind: Deployment
  49. metadata:
  50. finalizers:
  51. - "a"
  52. - "b"
  53. - "d"
  54. `)
  55. local := create(`
  56. apiVersion: apps/v1
  57. kind: Deployment
  58. metadata:
  59. finalizers:
  60. - "a"
  61. - "b"
  62. `)
  63. remote := create(`
  64. apiVersion: apps/v1
  65. kind: Deployment
  66. metadata:
  67. finalizers:
  68. - "a"
  69. - "b"
  70. - "c"
  71. `)
  72. expect := hasConflict
  73. // primatie lists have conflicts: recorded [a, b, d], remote [a, b, c]
  74. runConflictTest(strategy.Create(strategy.Options{FailOnConflict: true}), recorded, local, remote, expect)
  75. })
  76. })
  77. Context("Test conflict in Map-List fields of remote and recorded ", func() {
  78. It("should leave the item", func() {
  79. recorded := create(`
  80. apiVersion: apps/v1
  81. kind: Deployment
  82. spec:
  83. template:
  84. spec:
  85. containers:
  86. - name: item1
  87. image: image1
  88. `)
  89. local := create(`
  90. apiVersion: apps/v1
  91. kind: Deployment
  92. spec:
  93. template:
  94. spec:
  95. containers:
  96. - name: item2
  97. image: image2
  98. `)
  99. remote := create(`
  100. apiVersion: apps/v1
  101. kind: Deployment
  102. spec:
  103. template:
  104. spec:
  105. containers:
  106. - name: item1
  107. image: image3
  108. `)
  109. expect := hasConflict
  110. // map list has conflict : recorded {containers: [ {name: item1, image: image1} ]} , remote {containers: [ {name: item1, image: image3} ]}
  111. runConflictTest(strategy.Create(strategy.Options{FailOnConflict: true}), recorded, local, remote, expect)
  112. })
  113. })
  114. Context("Test conflicts in nested map field", func() {
  115. It("If conflicts found, expected return error", func() {
  116. recorded := create(`
  117. apiVersion: apps/v1
  118. kind: Deployment
  119. spec:
  120. foo1:
  121. name: "key1"
  122. `)
  123. local := create(`
  124. apiVersion: apps/v1
  125. kind: Deployment
  126. spec:
  127. foo1:
  128. name: "baz1-0"
  129. `)
  130. remote := create(`
  131. apiVersion: apps/v1
  132. kind: Deployment
  133. spec:
  134. foo1:
  135. name: "baz1-1"
  136. `)
  137. expect := hasConflict
  138. // nested map has conflict : recorded {foo1: {name: "key1"}}, remote {foo1: {name : "baz1-1"}}
  139. runConflictTest(strategy.Create(strategy.Options{FailOnConflict: true}), recorded, local, remote, expect)
  140. })
  141. })
  142. Context("Test conflicts in complicated map, list", func() {
  143. It("Should catch conflict in key-value in map element", func() {
  144. recorded := create(`
  145. apiVersion: apps/v1
  146. kind: Deployment
  147. spec:
  148. template:
  149. spec:
  150. containers:
  151. - name: container
  152. ports:
  153. - containerPort: 8080
  154. protocol: TCP
  155. hostPort: 2020
  156. - containerPort: 8080
  157. protocol: UDP
  158. hostPort: 2022
  159. `)
  160. local := create(`
  161. apiVersion: apps/v1
  162. kind: Deployment
  163. spec:
  164. template:
  165. spec:
  166. containers:
  167. - name: container
  168. ports:
  169. - containerPort: 8080
  170. protocol: TCP
  171. hostPort: 2020
  172. `)
  173. remote := create(`
  174. apiVersion: apps/v1
  175. kind: Deployment
  176. spec:
  177. template:
  178. spec:
  179. containers:
  180. - name: container
  181. ports:
  182. - containerPort: 8080
  183. protocol: TCP
  184. hostPort: 2020
  185. - containerPort: 8080
  186. protocol: UDP
  187. hostPort: 2022
  188. hostIP: "127.0.0.1"
  189. `)
  190. expect := noConflict
  191. runConflictTest(strategy.Create(strategy.Options{FailOnConflict: true}), recorded, local, remote, expect)
  192. })
  193. })
  194. })