images_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Copyright 2016 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 images
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
  19. "k8s.io/kubernetes/cmd/kubeadm/app/constants"
  20. )
  21. const (
  22. testversion = "v10.1.2-alpha.1.100+0123456789abcdef+SOMETHING"
  23. expected = "v10.1.2-alpha.1.100_0123456789abcdef_SOMETHING"
  24. gcrPrefix = "k8s.gcr.io"
  25. )
  26. func TestGetGenericImage(t *testing.T) {
  27. const (
  28. prefix = "foo"
  29. image = "bar"
  30. tag = "baz"
  31. )
  32. expected := fmt.Sprintf("%s/%s:%s", prefix, image, tag)
  33. actual := GetGenericImage(prefix, image, tag)
  34. if actual != expected {
  35. t.Errorf("failed GetGenericImage:\n\texpected: %s\n\t actual: %s", expected, actual)
  36. }
  37. }
  38. func TestGetKubernetesImage(t *testing.T) {
  39. var tests = []struct {
  40. image string
  41. expected string
  42. cfg *kubeadmapi.ClusterConfiguration
  43. }{
  44. {
  45. expected: GetGenericImage(gcrPrefix, constants.HyperKube, expected),
  46. cfg: &kubeadmapi.ClusterConfiguration{
  47. ImageRepository: gcrPrefix,
  48. KubernetesVersion: testversion,
  49. UseHyperKubeImage: true,
  50. },
  51. },
  52. {
  53. image: constants.KubeAPIServer,
  54. expected: GetGenericImage(gcrPrefix, "kube-apiserver", expected),
  55. cfg: &kubeadmapi.ClusterConfiguration{
  56. ImageRepository: gcrPrefix,
  57. KubernetesVersion: testversion,
  58. },
  59. },
  60. {
  61. image: constants.KubeControllerManager,
  62. expected: GetGenericImage(gcrPrefix, "kube-controller-manager", expected),
  63. cfg: &kubeadmapi.ClusterConfiguration{
  64. ImageRepository: gcrPrefix,
  65. KubernetesVersion: testversion,
  66. },
  67. },
  68. {
  69. image: constants.KubeScheduler,
  70. expected: GetGenericImage(gcrPrefix, "kube-scheduler", expected),
  71. cfg: &kubeadmapi.ClusterConfiguration{
  72. ImageRepository: gcrPrefix,
  73. KubernetesVersion: testversion,
  74. },
  75. },
  76. }
  77. for _, rt := range tests {
  78. actual := GetKubernetesImage(rt.image, rt.cfg)
  79. if actual != rt.expected {
  80. t.Errorf(
  81. "failed GetKubernetesImage:\n\texpected: %s\n\t actual: %s",
  82. rt.expected,
  83. actual,
  84. )
  85. }
  86. }
  87. }
  88. func TestGetEtcdImage(t *testing.T) {
  89. var tests = []struct {
  90. expected string
  91. cfg *kubeadmapi.ClusterConfiguration
  92. }{
  93. {
  94. cfg: &kubeadmapi.ClusterConfiguration{
  95. ImageRepository: "real.repo",
  96. KubernetesVersion: "1.12.0",
  97. Etcd: kubeadmapi.Etcd{
  98. Local: &kubeadmapi.LocalEtcd{},
  99. },
  100. },
  101. expected: "real.repo/etcd:3.2.24",
  102. },
  103. {
  104. cfg: &kubeadmapi.ClusterConfiguration{
  105. ImageRepository: "real.repo",
  106. KubernetesVersion: "1.12.0",
  107. Etcd: kubeadmapi.Etcd{
  108. Local: &kubeadmapi.LocalEtcd{
  109. ImageMeta: kubeadmapi.ImageMeta{
  110. ImageTag: "override",
  111. },
  112. },
  113. },
  114. },
  115. expected: "real.repo/etcd:override",
  116. },
  117. {
  118. cfg: &kubeadmapi.ClusterConfiguration{
  119. ImageRepository: "real.repo",
  120. KubernetesVersion: "1.12.0",
  121. Etcd: kubeadmapi.Etcd{
  122. Local: &kubeadmapi.LocalEtcd{
  123. ImageMeta: kubeadmapi.ImageMeta{
  124. ImageRepository: "override",
  125. },
  126. },
  127. },
  128. },
  129. expected: "override/etcd:3.2.24",
  130. },
  131. {
  132. expected: GetGenericImage(gcrPrefix, "etcd", constants.DefaultEtcdVersion),
  133. cfg: &kubeadmapi.ClusterConfiguration{
  134. ImageRepository: gcrPrefix,
  135. KubernetesVersion: testversion,
  136. },
  137. },
  138. }
  139. for _, rt := range tests {
  140. actual := GetEtcdImage(rt.cfg)
  141. if actual != rt.expected {
  142. t.Errorf(
  143. "failed GetEtcdImage:\n\texpected: %s\n\t actual: %s",
  144. rt.expected,
  145. actual,
  146. )
  147. }
  148. }
  149. }
  150. func TestGetPauseImage(t *testing.T) {
  151. testcases := []struct {
  152. name string
  153. cfg *kubeadmapi.ClusterConfiguration
  154. expected string
  155. }{
  156. {
  157. name: "pause image defined",
  158. cfg: &kubeadmapi.ClusterConfiguration{
  159. ImageRepository: "test.repo",
  160. },
  161. expected: "test.repo/pause:" + constants.PauseVersion,
  162. },
  163. }
  164. for _, tc := range testcases {
  165. t.Run(tc.name, func(t *testing.T) {
  166. actual := GetPauseImage(tc.cfg)
  167. if actual != tc.expected {
  168. t.Fatalf(
  169. "failed GetPauseImage:\n\texpected: %s\n\t actual: %s",
  170. tc.expected,
  171. actual,
  172. )
  173. }
  174. })
  175. }
  176. }
  177. func TestGetAllImages(t *testing.T) {
  178. testcases := []struct {
  179. name string
  180. expect string
  181. cfg *kubeadmapi.ClusterConfiguration
  182. }{
  183. {
  184. name: "defined CIImageRepository",
  185. cfg: &kubeadmapi.ClusterConfiguration{
  186. CIImageRepository: "test.repo",
  187. },
  188. expect: "test.repo",
  189. },
  190. {
  191. name: "undefined CIImagerRepository should contain the default image prefix",
  192. cfg: &kubeadmapi.ClusterConfiguration{
  193. ImageRepository: "real.repo",
  194. },
  195. expect: "real.repo",
  196. },
  197. {
  198. name: "test that etcd is returned when it is not external",
  199. cfg: &kubeadmapi.ClusterConfiguration{
  200. Etcd: kubeadmapi.Etcd{
  201. Local: &kubeadmapi.LocalEtcd{},
  202. },
  203. },
  204. expect: constants.Etcd,
  205. },
  206. {
  207. name: "CoreDNS image is returned",
  208. cfg: &kubeadmapi.ClusterConfiguration{
  209. DNS: kubeadmapi.DNS{
  210. Type: kubeadmapi.CoreDNS,
  211. },
  212. },
  213. expect: constants.CoreDNSImageName,
  214. },
  215. {
  216. name: "main kube-dns image is returned",
  217. cfg: &kubeadmapi.ClusterConfiguration{
  218. DNS: kubeadmapi.DNS{
  219. Type: kubeadmapi.KubeDNS,
  220. },
  221. },
  222. expect: constants.KubeDNSKubeDNSImageName,
  223. },
  224. {
  225. name: "kube-dns sidecar image is returned",
  226. cfg: &kubeadmapi.ClusterConfiguration{
  227. DNS: kubeadmapi.DNS{
  228. Type: kubeadmapi.KubeDNS,
  229. },
  230. },
  231. expect: constants.KubeDNSSidecarImageName,
  232. },
  233. {
  234. name: "kube-dns dnsmasq-nanny image is returned",
  235. cfg: &kubeadmapi.ClusterConfiguration{
  236. DNS: kubeadmapi.DNS{
  237. Type: kubeadmapi.KubeDNS,
  238. },
  239. },
  240. expect: constants.KubeDNSDnsMasqNannyImageName,
  241. },
  242. }
  243. for _, tc := range testcases {
  244. t.Run(tc.name, func(t *testing.T) {
  245. imgs := GetControlPlaneImages(tc.cfg)
  246. for _, img := range imgs {
  247. if strings.Contains(img, tc.expect) {
  248. return
  249. }
  250. }
  251. t.Fatalf("did not find %q in %q", tc.expect, imgs)
  252. })
  253. }
  254. }