ctl_v2_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. // check if backup went through correctly
  218. if err := etcdctlGet(epc2, "foo1", "bar", false); err != nil {
  219. t.Fatal(err)
  220. }
  221. // check if it can serve client requests
  222. if err := etcdctlSet(epc2, "foo2", "bar"); err != nil {
  223. t.Fatal(err)
  224. }
  225. if err := etcdctlGet(epc2, "foo2", "bar", false); err != nil {
  226. t.Fatal(err)
  227. }
  228. if err := epc2.Close(); err != nil {
  229. t.Fatalf("error closing etcd processes (%v)", err)
  230. }
  231. }
  232. func TestCtlV2AuthWithCommonName(t *testing.T) {
  233. defer testutil.AfterTest(t)
  234. copiedCfg := configClientTLS
  235. copiedCfg.clientCertAuthEnabled = true
  236. epc := setupEtcdctlTest(t, &copiedCfg, false)
  237. defer func() {
  238. if err := epc.Close(); err != nil {
  239. t.Fatalf("error closing etcd processes (%v)", err)
  240. }
  241. }()
  242. if err := etcdctlRoleAdd(epc, "testrole"); err != nil {
  243. t.Fatalf("failed to add role (%v)", err)
  244. }
  245. if err := etcdctlRoleGrant(epc, "testrole", "--rw", "--path=/foo"); err != nil {
  246. t.Fatalf("failed to grant role (%v)", err)
  247. }
  248. if err := etcdctlUserAdd(epc, "root", "123"); err != nil {
  249. t.Fatalf("failed to add user (%v)", err)
  250. }
  251. if err := etcdctlUserAdd(epc, "Autogenerated CA", "123"); err != nil {
  252. t.Fatalf("failed to add user (%v)", err)
  253. }
  254. if err := etcdctlUserGrant(epc, "Autogenerated CA", "testrole"); err != nil {
  255. t.Fatalf("failed to grant role (%v)", err)
  256. }
  257. if err := etcdctlAuthEnable(epc); err != nil {
  258. t.Fatalf("failed to enable auth (%v)", err)
  259. }
  260. if err := etcdctlSet(epc, "foo", "bar"); err != nil {
  261. t.Fatalf("failed to write (%v)", err)
  262. }
  263. }
  264. func TestCtlV2ClusterHealth(t *testing.T) {
  265. defer testutil.AfterTest(t)
  266. epc := setupEtcdctlTest(t, &configNoTLS, true)
  267. defer func() {
  268. if err := epc.Close(); err != nil {
  269. t.Fatalf("error closing etcd processes (%v)", err)
  270. }
  271. }()
  272. // has quorum
  273. if err := etcdctlClusterHealth(epc, "cluster is healthy"); err != nil {
  274. t.Fatalf("cluster-health expected to be healthy (%v)", err)
  275. }
  276. // cut quorum
  277. epc.procs[0].Stop()
  278. epc.procs[1].Stop()
  279. if err := etcdctlClusterHealth(epc, "cluster is unhealthy"); err != nil {
  280. t.Fatalf("cluster-health expected to be unhealthy (%v)", err)
  281. }
  282. epc.procs[0], epc.procs[1] = nil, nil
  283. }
  284. func etcdctlPrefixArgs(clus *etcdProcessCluster) []string {
  285. endpoints := ""
  286. if proxies := clus.proxies(); len(proxies) != 0 {
  287. endpoints = proxies[0].cfg.acurl
  288. } else if processes := clus.processes(); len(processes) != 0 {
  289. es := []string{}
  290. for _, b := range processes {
  291. es = append(es, b.cfg.acurl)
  292. }
  293. endpoints = strings.Join(es, ",")
  294. }
  295. cmdArgs := []string{ctlBinPath, "--endpoints", endpoints}
  296. if clus.cfg.clientTLS == clientTLS {
  297. cmdArgs = append(cmdArgs, "--ca-file", caPath, "--cert-file", certPath, "--key-file", privateKeyPath)
  298. }
  299. return cmdArgs
  300. }
  301. func etcdctlClusterHealth(clus *etcdProcessCluster, val string) error {
  302. cmdArgs := append(etcdctlPrefixArgs(clus), "cluster-health")
  303. return spawnWithExpect(cmdArgs, val)
  304. }
  305. func etcdctlSet(clus *etcdProcessCluster, key, value string) error {
  306. cmdArgs := append(etcdctlPrefixArgs(clus), "set", key, value)
  307. return spawnWithExpect(cmdArgs, value)
  308. }
  309. func etcdctlMk(clus *etcdProcessCluster, key, value string, first bool) error {
  310. cmdArgs := append(etcdctlPrefixArgs(clus), "mk", key, value)
  311. if first {
  312. return spawnWithExpect(cmdArgs, value)
  313. }
  314. return spawnWithExpect(cmdArgs, "Error: 105: Key already exists")
  315. }
  316. func etcdctlGet(clus *etcdProcessCluster, key, value string, quorum bool) error {
  317. cmdArgs := append(etcdctlPrefixArgs(clus), "get", key)
  318. if quorum {
  319. cmdArgs = append(cmdArgs, "--quorum")
  320. }
  321. return spawnWithExpect(cmdArgs, value)
  322. }
  323. func etcdctlRm(clus *etcdProcessCluster, key, value string, first bool) error {
  324. cmdArgs := append(etcdctlPrefixArgs(clus), "rm", key)
  325. if first {
  326. return spawnWithExpect(cmdArgs, "PrevNode.Value: "+value)
  327. }
  328. return spawnWithExpect(cmdArgs, "Error: 100: Key not found")
  329. }
  330. func etcdctlLs(clus *etcdProcessCluster, key string, quorum bool) error {
  331. cmdArgs := append(etcdctlPrefixArgs(clus), "ls")
  332. if quorum {
  333. cmdArgs = append(cmdArgs, "--quorum")
  334. }
  335. return spawnWithExpect(cmdArgs, key)
  336. }
  337. func etcdctlWatch(clus *etcdProcessCluster, key, value string, noSync bool) <-chan error {
  338. cmdArgs := append(etcdctlPrefixArgs(clus), "watch", "--after-index=1", key)
  339. if noSync {
  340. cmdArgs = append(cmdArgs, "--no-sync")
  341. }
  342. errc := make(chan error, 1)
  343. go func() {
  344. errc <- spawnWithExpect(cmdArgs, value)
  345. }()
  346. return errc
  347. }
  348. func etcdctlRoleAdd(clus *etcdProcessCluster, role string) error {
  349. cmdArgs := append(etcdctlPrefixArgs(clus), "role", "add", role)
  350. return spawnWithExpect(cmdArgs, role)
  351. }
  352. func etcdctlRoleGrant(clus *etcdProcessCluster, role string, perms ...string) error {
  353. cmdArgs := append(etcdctlPrefixArgs(clus), "role", "grant")
  354. cmdArgs = append(cmdArgs, perms...)
  355. cmdArgs = append(cmdArgs, role)
  356. return spawnWithExpect(cmdArgs, role)
  357. }
  358. func etcdctlRoleList(clus *etcdProcessCluster, expectedRole string) error {
  359. cmdArgs := append(etcdctlPrefixArgs(clus), "role", "list")
  360. return spawnWithExpect(cmdArgs, expectedRole)
  361. }
  362. func etcdctlUserAdd(clus *etcdProcessCluster, user, pass string) error {
  363. cmdArgs := append(etcdctlPrefixArgs(clus), "user", "add", user+":"+pass)
  364. return spawnWithExpect(cmdArgs, "User "+user+" created")
  365. }
  366. func etcdctlUserGrant(clus *etcdProcessCluster, user, role string) error {
  367. cmdArgs := append(etcdctlPrefixArgs(clus), "user", "grant", "--roles", role, user)
  368. return spawnWithExpect(cmdArgs, "User "+user+" updated")
  369. }
  370. func etcdctlUserGet(clus *etcdProcessCluster, user string) error {
  371. cmdArgs := append(etcdctlPrefixArgs(clus), "user", "get", user)
  372. return spawnWithExpect(cmdArgs, "User: "+user)
  373. }
  374. func etcdctlUserList(clus *etcdProcessCluster, expectedUser string) error {
  375. cmdArgs := append(etcdctlPrefixArgs(clus), "user", "list")
  376. return spawnWithExpect(cmdArgs, expectedUser)
  377. }
  378. func etcdctlAuthEnable(clus *etcdProcessCluster) error {
  379. cmdArgs := append(etcdctlPrefixArgs(clus), "auth", "enable")
  380. return spawnWithExpect(cmdArgs, "Authentication Enabled")
  381. }
  382. func etcdctlBackup(clus *etcdProcessCluster, dataDir, backupDir string) error {
  383. cmdArgs := append(etcdctlPrefixArgs(clus), "backup", "--data-dir", dataDir, "--backup-dir", backupDir)
  384. return spawnWithExpects(cmdArgs)
  385. }
  386. func mustEtcdctl(t *testing.T) {
  387. if !fileutil.Exist(binDir + "/etcdctl") {
  388. t.Fatalf("could not find etcdctl binary")
  389. }
  390. }
  391. func setupEtcdctlTest(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) *etcdProcessCluster {
  392. mustEtcdctl(t)
  393. if !quorum {
  394. cfg = configStandalone(*cfg)
  395. }
  396. epc, err := newEtcdProcessCluster(cfg)
  397. if err != nil {
  398. t.Fatalf("could not start etcd process cluster (%v)", err)
  399. }
  400. return epc
  401. }