apiserver_etcd_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Copyright 2019 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 gci
  14. import (
  15. "strings"
  16. "testing"
  17. )
  18. type kubeAPIServeETCDEnv struct {
  19. KubeHome string
  20. ETCDServers string
  21. ETCDServersOverride string
  22. CAKey string
  23. CACert string
  24. CACertPath string
  25. APIServerKey string
  26. APIServerCert string
  27. APIServerCertPath string
  28. APIServerKeyPath string
  29. ETCDKey string
  30. ETCDCert string
  31. StorageBackend string
  32. StorageMediaType string
  33. CompactionInterval string
  34. }
  35. func TestServerOverride(t *testing.T) {
  36. testCases := []struct {
  37. desc string
  38. env kubeAPIServeETCDEnv
  39. want []string
  40. }{
  41. {
  42. desc: "ETCD-SERVERS is not set - default override",
  43. want: []string{
  44. "--etcd-servers-overrides=/events#http://127.0.0.1:4002",
  45. },
  46. },
  47. {
  48. desc: "ETCD-SERVERS and ETCD_SERVERS_OVERRIDES iare set",
  49. env: kubeAPIServeETCDEnv{
  50. ETCDServers: "ETCDServers",
  51. ETCDServersOverride: "ETCDServersOverrides",
  52. },
  53. want: []string{
  54. "--etcd-servers-overrides=ETCDServersOverrides",
  55. },
  56. },
  57. }
  58. for _, tc := range testCases {
  59. t.Run(tc.desc, func(t *testing.T) {
  60. c := newManifestTestCase(t, kubeAPIServerManifestFileName, kubeAPIServerStartFuncName, nil)
  61. defer c.tearDown()
  62. tc.env.KubeHome = c.kubeHome
  63. c.mustInvokeFunc(
  64. tc.env,
  65. kubeAPIServerConfigScriptName,
  66. "etcd.template",
  67. "testdata/kube-apiserver/base.template",
  68. "testdata/kube-apiserver/etcd.template",
  69. )
  70. c.mustLoadPodFromManifest()
  71. execArgs := c.pod.Spec.Containers[0].Command[2]
  72. for _, f := range tc.want {
  73. if !strings.Contains(execArgs, f) {
  74. t.Fatalf("Got %q, want it to contain %q", execArgs, f)
  75. }
  76. }
  77. })
  78. }
  79. }
  80. func TestStorageOptions(t *testing.T) {
  81. testCases := []struct {
  82. desc string
  83. env kubeAPIServeETCDEnv
  84. want []string
  85. dontWant []string
  86. }{
  87. {
  88. desc: "storage options are supplied",
  89. env: kubeAPIServeETCDEnv{
  90. StorageBackend: "StorageBackend",
  91. StorageMediaType: "StorageMediaType",
  92. CompactionInterval: "1s",
  93. },
  94. want: []string{
  95. "--storage-backend=StorageBackend",
  96. "--storage-media-type=StorageMediaType",
  97. "--etcd-compaction-interval=1s",
  98. },
  99. },
  100. {
  101. desc: "storage options not not supplied",
  102. env: kubeAPIServeETCDEnv{},
  103. dontWant: []string{
  104. "--storage-backend",
  105. "--storage-media-type",
  106. "--etcd-compaction-interval",
  107. },
  108. },
  109. }
  110. for _, tc := range testCases {
  111. t.Run(tc.desc, func(t *testing.T) {
  112. c := newManifestTestCase(t, kubeAPIServerManifestFileName, kubeAPIServerStartFuncName, nil)
  113. defer c.tearDown()
  114. tc.env.KubeHome = c.kubeHome
  115. c.mustInvokeFunc(
  116. tc.env,
  117. kubeAPIServerConfigScriptName,
  118. "etcd.template",
  119. "testdata/kube-apiserver/base.template",
  120. "testdata/kube-apiserver/etcd.template",
  121. )
  122. c.mustLoadPodFromManifest()
  123. execArgs := c.pod.Spec.Containers[0].Command[2]
  124. for _, f := range tc.want {
  125. if !strings.Contains(execArgs, f) {
  126. t.Fatalf("Got %q, want it to contain %q", execArgs, f)
  127. }
  128. }
  129. for _, f := range tc.dontWant {
  130. if strings.Contains(execArgs, f) {
  131. t.Fatalf("Got %q, but it was not expected it to contain %q", execArgs, f)
  132. }
  133. }
  134. })
  135. }
  136. }
  137. func TestTLSFlags(t *testing.T) {
  138. testCases := []struct {
  139. desc string
  140. env kubeAPIServeETCDEnv
  141. want []string
  142. }{
  143. {
  144. desc: "mTLS enabled",
  145. env: kubeAPIServeETCDEnv{
  146. CAKey: "CAKey",
  147. CACert: "CACert",
  148. CACertPath: "CACertPath",
  149. APIServerKey: "APIServerKey",
  150. APIServerCert: "APIServerCert",
  151. ETCDKey: "ETCDKey",
  152. ETCDCert: "ETCDCert",
  153. ETCDServers: "https://127.0.0.1:2379",
  154. APIServerKeyPath: "APIServerKeyPath",
  155. APIServerCertPath: "APIServerCertPath",
  156. },
  157. want: []string{
  158. "--etcd-servers=https://127.0.0.1:2379",
  159. "--etcd-cafile=CACertPath",
  160. "--etcd-certfile=APIServerCertPath",
  161. "--etcd-keyfile=APIServerKeyPath",
  162. },
  163. },
  164. {
  165. desc: "mTLS disabled",
  166. want: []string{"--etcd-servers=http://127.0.0.1:2379"},
  167. },
  168. }
  169. for _, tc := range testCases {
  170. t.Run(tc.desc, func(t *testing.T) {
  171. c := newManifestTestCase(t, kubeAPIServerManifestFileName, kubeAPIServerStartFuncName, nil)
  172. defer c.tearDown()
  173. tc.env.KubeHome = c.kubeHome
  174. c.mustInvokeFunc(
  175. tc.env,
  176. kubeAPIServerConfigScriptName,
  177. "etcd.template",
  178. "testdata/kube-apiserver/base.template",
  179. "testdata/kube-apiserver/etcd.template",
  180. )
  181. c.mustLoadPodFromManifest()
  182. execArgs := c.pod.Spec.Containers[0].Command[2]
  183. for _, f := range tc.want {
  184. if !strings.Contains(execArgs, f) {
  185. t.Fatalf("Got %q, want it to contain %q", execArgs, f)
  186. }
  187. }
  188. })
  189. }
  190. }