ctl_v3_user_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "testing"
  16. func TestCtlV3UserAdd(t *testing.T) { testCtl(t, userAddTest) }
  17. func TestCtlV3UserAddNoTLS(t *testing.T) { testCtl(t, userAddTest, withCfg(configNoTLS)) }
  18. func TestCtlV3UserAddClientTLS(t *testing.T) { testCtl(t, userAddTest, withCfg(configClientTLS)) }
  19. func TestCtlV3UserAddPeerTLS(t *testing.T) { testCtl(t, userAddTest, withCfg(configPeerTLS)) }
  20. func TestCtlV3UserAddTimeout(t *testing.T) { testCtl(t, userAddTest, withDialTimeout(0)) }
  21. func TestCtlV3UserAddClientAutoTLS(t *testing.T) {
  22. testCtl(t, userAddTest, withCfg(configClientAutoTLS))
  23. }
  24. func TestCtlV3UserList(t *testing.T) { testCtl(t, userListTest) }
  25. func TestCtlV3UserListNoTLS(t *testing.T) { testCtl(t, userListTest, withCfg(configNoTLS)) }
  26. func TestCtlV3UserListClientTLS(t *testing.T) { testCtl(t, userListTest, withCfg(configClientTLS)) }
  27. func TestCtlV3UserListPeerTLS(t *testing.T) { testCtl(t, userListTest, withCfg(configPeerTLS)) }
  28. func TestCtlV3UserListClientAutoTLS(t *testing.T) {
  29. testCtl(t, userListTest, withCfg(configClientAutoTLS))
  30. }
  31. func TestCtlV3UserDelete(t *testing.T) { testCtl(t, userDelTest) }
  32. func TestCtlV3UserDeleteNoTLS(t *testing.T) { testCtl(t, userDelTest, withCfg(configNoTLS)) }
  33. func TestCtlV3UserDeleteClientTLS(t *testing.T) { testCtl(t, userDelTest, withCfg(configClientTLS)) }
  34. func TestCtlV3UserDeletePeerTLS(t *testing.T) { testCtl(t, userDelTest, withCfg(configPeerTLS)) }
  35. func TestCtlV3UserDeleteClientAutoTLS(t *testing.T) {
  36. testCtl(t, userDelTest, withCfg(configClientAutoTLS))
  37. }
  38. func TestCtlV3UserPasswd(t *testing.T) { testCtl(t, userPasswdTest) }
  39. func TestCtlV3UserPasswdNoTLS(t *testing.T) { testCtl(t, userPasswdTest, withCfg(configNoTLS)) }
  40. func TestCtlV3UserPasswdClientTLS(t *testing.T) { testCtl(t, userPasswdTest, withCfg(configClientTLS)) }
  41. func TestCtlV3UserPasswdPeerTLS(t *testing.T) { testCtl(t, userPasswdTest, withCfg(configPeerTLS)) }
  42. func TestCtlV3UserPasswdClientAutoTLS(t *testing.T) {
  43. testCtl(t, userPasswdTest, withCfg(configClientAutoTLS))
  44. }
  45. type userCmdDesc struct {
  46. args []string
  47. expectedStr string
  48. stdIn []string
  49. }
  50. func userAddTest(cx ctlCtx) {
  51. cmdSet := []userCmdDesc{
  52. // Adds a user name.
  53. {
  54. args: []string{"add", "username", "--interactive=false"},
  55. expectedStr: "User username created",
  56. stdIn: []string{"password"},
  57. },
  58. // Adds a user name using the usertest:password syntax.
  59. {
  60. args: []string{"add", "usertest:password"},
  61. expectedStr: "User usertest created",
  62. stdIn: []string{},
  63. },
  64. // Tries to add a user with empty username.
  65. {
  66. args: []string{"add", ":password"},
  67. expectedStr: "empty user name is not allowed",
  68. stdIn: []string{},
  69. },
  70. // Tries to add a user name that already exists.
  71. {
  72. args: []string{"add", "username", "--interactive=false"},
  73. expectedStr: "user name already exists",
  74. stdIn: []string{"password"},
  75. },
  76. // Adds a user without password.
  77. {
  78. args: []string{"add", "userwopasswd", "--no-password"},
  79. expectedStr: "User userwopasswd created",
  80. stdIn: []string{},
  81. },
  82. }
  83. for i, cmd := range cmdSet {
  84. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  85. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  86. cx.t.Fatalf("userAddTest #%d: ctlV3User error (%v)", i, err)
  87. }
  88. }
  89. }
  90. }
  91. func userListTest(cx ctlCtx) {
  92. cmdSet := []userCmdDesc{
  93. // Adds a user name.
  94. {
  95. args: []string{"add", "username", "--interactive=false"},
  96. expectedStr: "User username created",
  97. stdIn: []string{"password"},
  98. },
  99. // List user name
  100. {
  101. args: []string{"list"},
  102. expectedStr: "username",
  103. },
  104. }
  105. for i, cmd := range cmdSet {
  106. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  107. cx.t.Fatalf("userListTest #%d: ctlV3User error (%v)", i, err)
  108. }
  109. }
  110. }
  111. func userDelTest(cx ctlCtx) {
  112. cmdSet := []userCmdDesc{
  113. // Adds a user name.
  114. {
  115. args: []string{"add", "username", "--interactive=false"},
  116. expectedStr: "User username created",
  117. stdIn: []string{"password"},
  118. },
  119. // Deletes the user name just added.
  120. {
  121. args: []string{"delete", "username"},
  122. expectedStr: "User username deleted",
  123. },
  124. // Deletes a user name that is not present.
  125. {
  126. args: []string{"delete", "username"},
  127. expectedStr: "user name not found",
  128. },
  129. }
  130. for i, cmd := range cmdSet {
  131. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  132. cx.t.Fatalf("userDelTest #%d: ctlV3User error (%v)", i, err)
  133. }
  134. }
  135. }
  136. func userPasswdTest(cx ctlCtx) {
  137. cmdSet := []userCmdDesc{
  138. // Adds a user name.
  139. {
  140. args: []string{"add", "username", "--interactive=false"},
  141. expectedStr: "User username created",
  142. stdIn: []string{"password"},
  143. },
  144. // Changes the password.
  145. {
  146. args: []string{"passwd", "username", "--interactive=false"},
  147. expectedStr: "Password updated",
  148. stdIn: []string{"password1"},
  149. },
  150. }
  151. for i, cmd := range cmdSet {
  152. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  153. cx.t.Fatalf("userPasswdTest #%d: ctlV3User error (%v)", i, err)
  154. }
  155. }
  156. }
  157. func ctlV3User(cx ctlCtx, args []string, expStr string, stdIn []string) error {
  158. cmdArgs := append(cx.PrefixArgs(), "user")
  159. cmdArgs = append(cmdArgs, args...)
  160. proc, err := spawnCmd(cmdArgs)
  161. if err != nil {
  162. return err
  163. }
  164. // Send 'stdIn' strings as input.
  165. for _, s := range stdIn {
  166. if err = proc.Send(s + "\r"); err != nil {
  167. return err
  168. }
  169. }
  170. _, err = proc.Expect(expStr)
  171. return err
  172. }