ctl_v2_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Copyright 2016 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package e2e
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/pkg/fileutil"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. )
  24. func TestCtlV2Set(t *testing.T) { testCtlV2Set(t, &configNoTLS, false) }
  25. func TestCtlV2SetQuorum(t *testing.T) { testCtlV2Set(t, &configNoTLS, true) }
  26. func TestCtlV2SetClientTLS(t *testing.T) { testCtlV2Set(t, &configClientTLS, false) }
  27. func TestCtlV2SetPeerTLS(t *testing.T) { testCtlV2Set(t, &configPeerTLS, false) }
  28. func TestCtlV2SetTLS(t *testing.T) { testCtlV2Set(t, &configTLS, false) }
  29. func testCtlV2Set(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
  30. defer testutil.AfterTest(t)
  31. epc := setupEtcdctlTest(t, cfg, quorum)
  32. defer func() {
  33. if errC := epc.Close(); errC != nil {
  34. t.Fatalf("error closing etcd processes (%v)", errC)
  35. }
  36. }()
  37. key, value := "foo", "bar"
  38. if err := etcdctlSet(epc, key, value); err != nil {
  39. t.Fatalf("failed set (%v)", err)
  40. }
  41. if err := etcdctlGet(epc, key, value, quorum); err != nil {
  42. t.Fatalf("failed get (%v)", err)
  43. }
  44. }
  45. func TestCtlV2Mk(t *testing.T) { testCtlV2Mk(t, &configNoTLS, false) }
  46. func TestCtlV2MkQuorum(t *testing.T) { testCtlV2Mk(t, &configNoTLS, true) }
  47. func TestCtlV2MkTLS(t *testing.T) { testCtlV2Mk(t, &configTLS, false) }
  48. func testCtlV2Mk(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
  49. defer testutil.AfterTest(t)
  50. epc := setupEtcdctlTest(t, cfg, quorum)
  51. defer func() {
  52. if errC := epc.Close(); errC != nil {
  53. t.Fatalf("error closing etcd processes (%v)", errC)
  54. }
  55. }()
  56. key, value := "foo", "bar"
  57. if err := etcdctlMk(epc, key, value, true); err != nil {
  58. t.Fatalf("failed mk (%v)", err)
  59. }
  60. if err := etcdctlMk(epc, key, value, false); err != nil {
  61. t.Fatalf("failed mk (%v)", err)
  62. }
  63. if err := etcdctlGet(epc, key, value, quorum); err != nil {
  64. t.Fatalf("failed get (%v)", err)
  65. }
  66. }
  67. func TestCtlV2Rm(t *testing.T) { testCtlV2Rm(t, &configNoTLS) }
  68. func TestCtlV2RmTLS(t *testing.T) { testCtlV2Rm(t, &configTLS) }
  69. func testCtlV2Rm(t *testing.T, cfg *etcdProcessClusterConfig) {
  70. defer testutil.AfterTest(t)
  71. epc := setupEtcdctlTest(t, cfg, true)
  72. defer func() {
  73. if errC := epc.Close(); errC != nil {
  74. t.Fatalf("error closing etcd processes (%v)", errC)
  75. }
  76. }()
  77. key, value := "foo", "bar"
  78. if err := etcdctlSet(epc, key, value); err != nil {
  79. t.Fatalf("failed set (%v)", err)
  80. }
  81. if err := etcdctlRm(epc, key, value, true); err != nil {
  82. t.Fatalf("failed rm (%v)", err)
  83. }
  84. if err := etcdctlRm(epc, key, value, false); err != nil {
  85. t.Fatalf("failed rm (%v)", err)
  86. }
  87. }
  88. func TestCtlV2Ls(t *testing.T) { testCtlV2Ls(t, &configNoTLS, false) }
  89. func TestCtlV2LsQuorum(t *testing.T) { testCtlV2Ls(t, &configNoTLS, true) }
  90. func TestCtlV2LsTLS(t *testing.T) { testCtlV2Ls(t, &configTLS, false) }
  91. func testCtlV2Ls(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) {
  92. defer testutil.AfterTest(t)
  93. epc := setupEtcdctlTest(t, cfg, quorum)
  94. defer func() {
  95. if errC := epc.Close(); errC != nil {
  96. t.Fatalf("error closing etcd processes (%v)", errC)
  97. }
  98. }()
  99. key, value := "foo", "bar"
  100. if err := etcdctlSet(epc, key, value); err != nil {
  101. t.Fatalf("failed set (%v)", err)
  102. }
  103. if err := etcdctlLs(epc, key, quorum); err != nil {
  104. t.Fatalf("failed ls (%v)", err)
  105. }
  106. }
  107. func TestCtlV2Watch(t *testing.T) { testCtlV2Watch(t, &configNoTLS, false) }
  108. func TestCtlV2WatchTLS(t *testing.T) { testCtlV2Watch(t, &configTLS, false) }
  109. func TestCtlV2WatchWithProxy(t *testing.T) { testCtlV2Watch(t, &configWithProxy, false) }
  110. func TestCtlV2WatchWithProxyNoSync(t *testing.T) { testCtlV2Watch(t, &configWithProxy, true) }
  111. func testCtlV2Watch(t *testing.T, cfg *etcdProcessClusterConfig, noSync bool) {
  112. defer testutil.AfterTest(t)
  113. epc := setupEtcdctlTest(t, cfg, true)
  114. defer func() {
  115. if errC := epc.Close(); errC != nil {
  116. t.Fatalf("error closing etcd processes (%v)", errC)
  117. }
  118. }()
  119. key, value := "foo", "bar"
  120. errc := etcdctlWatch(epc, key, value, noSync)
  121. if err := etcdctlSet(epc, key, value); err != nil {
  122. t.Fatalf("failed set (%v)", err)
  123. }
  124. select {
  125. case err := <-errc:
  126. if err != nil {
  127. t.Fatalf("failed watch (%v)", err)
  128. }
  129. case <-time.After(5 * time.Second):
  130. t.Fatalf("watch timed out")
  131. }
  132. }
  133. func TestCtlV2GetRoleUser(t *testing.T) { testCtlV2GetRoleUser(t, &configNoTLS) }
  134. func TestCtlV2GetRoleUserWithProxy(t *testing.T) { testCtlV2GetRoleUser(t, &configWithProxy) }
  135. func testCtlV2GetRoleUser(t *testing.T, cfg *etcdProcessClusterConfig) {
  136. defer testutil.AfterTest(t)
  137. epc := setupEtcdctlTest(t, cfg, true)
  138. defer func() {
  139. if err := epc.Close(); err != nil {
  140. t.Fatalf("error closing etcd processes (%v)", err)
  141. }
  142. }()
  143. if err := etcdctlRoleAdd(epc, "foo"); err != nil {
  144. t.Fatalf("failed to add role (%v)", err)
  145. }
  146. if err := etcdctlUserAdd(epc, "username", "password"); err != nil {
  147. t.Fatalf("failed to add user (%v)", err)
  148. }
  149. if err := etcdctlUserGrant(epc, "username", "foo"); err != nil {
  150. t.Fatalf("failed to grant role (%v)", err)
  151. }
  152. if err := etcdctlUserGet(epc, "username"); err != nil {
  153. t.Fatalf("failed to get user (%v)", err)
  154. }
  155. // ensure double grant gives an error; was crashing in 2.3.1
  156. regrantArgs := etcdctlPrefixArgs(epc)
  157. regrantArgs = append(regrantArgs, "user", "grant", "--roles", "foo", "username")
  158. if err := spawnWithExpect(regrantArgs, "duplicate"); err != nil {
  159. t.Fatalf("missing duplicate error on double grant role (%v)", err)
  160. }
  161. }
  162. func TestCtlV2UserListUsername(t *testing.T) { testCtlV2UserList(t, "username") }
  163. func TestCtlV2UserListRoot(t *testing.T) { testCtlV2UserList(t, "root") }
  164. func testCtlV2UserList(t *testing.T, username string) {
  165. defer testutil.AfterTest(t)
  166. epc := setupEtcdctlTest(t, &configWithProxy, false)
  167. defer func() {
  168. if err := epc.Close(); err != nil {
  169. t.Fatalf("error closing etcd processes (%v)", err)
  170. }
  171. }()
  172. if err := etcdctlUserAdd(epc, username, "password"); err != nil {
  173. t.Fatalf("failed to add user (%v)", err)
  174. }
  175. if err := etcdctlUserList(epc, username); err != nil {
  176. t.Fatalf("failed to list users (%v)", err)
  177. }
  178. }
  179. func TestCtlV2RoleList(t *testing.T) {
  180. defer testutil.AfterTest(t)
  181. epc := setupEtcdctlTest(t, &configWithProxy, false)
  182. defer func() {
  183. if err := epc.Close(); err != nil {
  184. t.Fatalf("error closing etcd processes (%v)", err)
  185. }
  186. }()
  187. if err := etcdctlRoleAdd(epc, "foo"); err != nil {
  188. t.Fatalf("failed to add role (%v)", err)
  189. }
  190. if err := etcdctlRoleList(epc, "foo"); err != nil {
  191. t.Fatalf("failed to list roles (%v)", err)
  192. }
  193. }
  194. func TestCtlV2Backup(t *testing.T) { // For https://github.com/coreos/etcd/issues/5360
  195. defer testutil.AfterTest(t)
  196. backupDir, err := ioutil.TempDir("", "testbackup0.etcd")
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. defer os.RemoveAll(backupDir)
  201. epc1 := setupEtcdctlTest(t, &configNoTLS, false)
  202. if err := etcdctlSet(epc1, "foo1", "bar"); err != nil {
  203. t.Fatal(err)
  204. }
  205. if err := etcdctlBackup(epc1, epc1.procs[0].cfg.dataDirPath, backupDir); err != nil {
  206. t.Fatal(err)
  207. }
  208. if err := epc1.Close(); err != nil {
  209. t.Fatalf("error closing etcd processes (%v)", err)
  210. }
  211. // restart from the backup directory
  212. cfg2 := configNoTLS
  213. cfg2.dataDirPath = backupDir
  214. cfg2.keepDataDir = true
  215. cfg2.forceNewCluster = true
  216. epc2 := setupEtcdctlTest(t, &cfg2, false)
  217. if _, err := epc2.procs[0].proc.Expect("etcdserver: published"); err != nil {
  218. t.Fatal(err)
  219. }
  220. // check if backup went through correctly
  221. if err := etcdctlGet(epc2, "foo1", "bar", false); err != nil {
  222. t.Fatal(err)
  223. }
  224. // check if it can serve client requests
  225. if err := etcdctlSet(epc2, "foo2", "bar"); err != nil {
  226. t.Fatal(err)
  227. }
  228. if err := etcdctlGet(epc2, "foo2", "bar", false); err != nil {
  229. t.Fatal(err)
  230. }
  231. if err := epc2.Close(); err != nil {
  232. t.Fatalf("error closing etcd processes (%v)", err)
  233. }
  234. }
  235. func TestCtlV2AuthWithCommonName(t *testing.T) {
  236. defer testutil.AfterTest(t)
  237. copiedCfg := configClientTLS
  238. copiedCfg.clientCertAuthEnabled = true
  239. epc := setupEtcdctlTest(t, &copiedCfg, false)
  240. defer func() {
  241. if err := epc.Close(); err != nil {
  242. t.Fatalf("error closing etcd processes (%v)", err)
  243. }
  244. }()
  245. if err := etcdctlRoleAdd(epc, "testrole"); err != nil {
  246. t.Fatalf("failed to add role (%v)", err)
  247. }
  248. if err := etcdctlRoleGrant(epc, "testrole", "--rw", "--path=/foo"); err != nil {
  249. t.Fatalf("failed to grant role (%v)", err)
  250. }
  251. if err := etcdctlUserAdd(epc, "root", "123"); err != nil {
  252. t.Fatalf("failed to add user (%v)", err)
  253. }
  254. if err := etcdctlUserAdd(epc, "Autogenerated CA", "123"); err != nil {
  255. t.Fatalf("failed to add user (%v)", err)
  256. }
  257. if err := etcdctlUserGrant(epc, "Autogenerated CA", "testrole"); err != nil {
  258. t.Fatalf("failed to grant role (%v)", err)
  259. }
  260. if err := etcdctlAuthEnable(epc); err != nil {
  261. t.Fatalf("failed to enable auth (%v)", err)
  262. }
  263. if err := etcdctlSet(epc, "foo", "bar"); err != nil {
  264. t.Fatalf("failed to write (%v)", err)
  265. }
  266. }
  267. func etcdctlPrefixArgs(clus *etcdProcessCluster) []string {
  268. endpoints := ""
  269. if proxies := clus.proxies(); len(proxies) != 0 {
  270. endpoints = proxies[0].cfg.acurl
  271. } else if backends := clus.backends(); len(backends) != 0 {
  272. es := []string{}
  273. for _, b := range backends {
  274. es = append(es, b.cfg.acurl)
  275. }
  276. endpoints = strings.Join(es, ",")
  277. }
  278. cmdArgs := []string{"../bin/etcdctl", "--endpoints", endpoints}
  279. if clus.cfg.clientTLS == clientTLS {
  280. cmdArgs = append(cmdArgs, "--ca-file", caPath, "--cert-file", certPath, "--key-file", privateKeyPath)
  281. }
  282. return cmdArgs
  283. }
  284. func etcdctlSet(clus *etcdProcessCluster, key, value string) error {
  285. cmdArgs := append(etcdctlPrefixArgs(clus), "set", key, value)
  286. return spawnWithExpect(cmdArgs, value)
  287. }
  288. func etcdctlMk(clus *etcdProcessCluster, key, value string, first bool) error {
  289. cmdArgs := append(etcdctlPrefixArgs(clus), "mk", key, value)
  290. if first {
  291. return spawnWithExpect(cmdArgs, value)
  292. }
  293. return spawnWithExpect(cmdArgs, "Error: 105: Key already exists")
  294. }
  295. func etcdctlGet(clus *etcdProcessCluster, key, value string, quorum bool) error {
  296. cmdArgs := append(etcdctlPrefixArgs(clus), "get", key)
  297. if quorum {
  298. cmdArgs = append(cmdArgs, "--quorum")
  299. }
  300. return spawnWithExpect(cmdArgs, value)
  301. }
  302. func etcdctlRm(clus *etcdProcessCluster, key, value string, first bool) error {
  303. cmdArgs := append(etcdctlPrefixArgs(clus), "rm", key)
  304. if first {
  305. return spawnWithExpect(cmdArgs, "PrevNode.Value: "+value)
  306. }
  307. return spawnWithExpect(cmdArgs, "Error: 100: Key not found")
  308. }
  309. func etcdctlLs(clus *etcdProcessCluster, key string, quorum bool) error {
  310. cmdArgs := append(etcdctlPrefixArgs(clus), "ls")
  311. if quorum {
  312. cmdArgs = append(cmdArgs, "--quorum")
  313. }
  314. return spawnWithExpect(cmdArgs, key)
  315. }
  316. func etcdctlWatch(clus *etcdProcessCluster, key, value string, noSync bool) <-chan error {
  317. cmdArgs := append(etcdctlPrefixArgs(clus), "watch", "--after-index=1", key)
  318. if noSync {
  319. cmdArgs = append(cmdArgs, "--no-sync")
  320. }
  321. errc := make(chan error, 1)
  322. go func() {
  323. errc <- spawnWithExpect(cmdArgs, value)
  324. }()
  325. return errc
  326. }
  327. func etcdctlRoleAdd(clus *etcdProcessCluster, role string) error {
  328. cmdArgs := append(etcdctlPrefixArgs(clus), "role", "add", role)
  329. return spawnWithExpect(cmdArgs, role)
  330. }
  331. func etcdctlRoleGrant(clus *etcdProcessCluster, role string, perms ...string) error {
  332. cmdArgs := append(etcdctlPrefixArgs(clus), "role", "grant")
  333. cmdArgs = append(cmdArgs, perms...)
  334. cmdArgs = append(cmdArgs, role)
  335. return spawnWithExpect(cmdArgs, role)
  336. }
  337. func etcdctlRoleList(clus *etcdProcessCluster, expectedRole string) error {
  338. cmdArgs := append(etcdctlPrefixArgs(clus), "role", "list")
  339. return spawnWithExpect(cmdArgs, expectedRole)
  340. }
  341. func etcdctlUserAdd(clus *etcdProcessCluster, user, pass string) error {
  342. cmdArgs := append(etcdctlPrefixArgs(clus), "user", "add", user+":"+pass)
  343. return spawnWithExpect(cmdArgs, "User "+user+" created")
  344. }
  345. func etcdctlUserGrant(clus *etcdProcessCluster, user, role string) error {
  346. cmdArgs := append(etcdctlPrefixArgs(clus), "user", "grant", "--roles", role, user)
  347. return spawnWithExpect(cmdArgs, "User "+user+" updated")
  348. }
  349. func etcdctlUserGet(clus *etcdProcessCluster, user string) error {
  350. cmdArgs := append(etcdctlPrefixArgs(clus), "user", "get", user)
  351. return spawnWithExpect(cmdArgs, "User: "+user)
  352. }
  353. func etcdctlUserList(clus *etcdProcessCluster, expectedUser string) error {
  354. cmdArgs := append(etcdctlPrefixArgs(clus), "user", "list")
  355. return spawnWithExpect(cmdArgs, expectedUser)
  356. }
  357. func etcdctlAuthEnable(clus *etcdProcessCluster) error {
  358. cmdArgs := append(etcdctlPrefixArgs(clus), "auth", "enable")
  359. return spawnWithExpect(cmdArgs, "Authentication Enabled")
  360. }
  361. func etcdctlBackup(clus *etcdProcessCluster, dataDir, backupDir string) error {
  362. cmdArgs := append(etcdctlPrefixArgs(clus), "backup", "--data-dir", dataDir, "--backup-dir", backupDir)
  363. return spawnWithExpects(cmdArgs)
  364. }
  365. func mustEtcdctl(t *testing.T) {
  366. if !fileutil.Exist("../bin/etcdctl") {
  367. t.Fatalf("could not find etcdctl binary")
  368. }
  369. }
  370. func setupEtcdctlTest(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) *etcdProcessCluster {
  371. mustEtcdctl(t)
  372. if !quorum {
  373. cfg = configStandalone(*cfg)
  374. }
  375. epc, err := newEtcdProcessCluster(cfg)
  376. if err != nil {
  377. t.Fatalf("could not start etcd process cluster (%v)", err)
  378. }
  379. return epc
  380. }