ctl_v3_user_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. }
  77. for i, cmd := range cmdSet {
  78. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  79. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  80. cx.t.Fatalf("userAddTest #%d: ctlV3User error (%v)", i, err)
  81. }
  82. }
  83. }
  84. }
  85. func userListTest(cx ctlCtx) {
  86. cmdSet := []userCmdDesc{
  87. // Adds a user name.
  88. {
  89. args: []string{"add", "username", "--interactive=false"},
  90. expectedStr: "User username created",
  91. stdIn: []string{"password"},
  92. },
  93. // List user name
  94. {
  95. args: []string{"list"},
  96. expectedStr: "username",
  97. },
  98. }
  99. for i, cmd := range cmdSet {
  100. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  101. cx.t.Fatalf("userListTest #%d: ctlV3User error (%v)", i, err)
  102. }
  103. }
  104. }
  105. func userDelTest(cx ctlCtx) {
  106. cmdSet := []userCmdDesc{
  107. // Adds a user name.
  108. {
  109. args: []string{"add", "username", "--interactive=false"},
  110. expectedStr: "User username created",
  111. stdIn: []string{"password"},
  112. },
  113. // Deletes the user name just added.
  114. {
  115. args: []string{"delete", "username"},
  116. expectedStr: "User username deleted",
  117. },
  118. // Deletes a user name that is not present.
  119. {
  120. args: []string{"delete", "username"},
  121. expectedStr: "user name not found",
  122. },
  123. }
  124. for i, cmd := range cmdSet {
  125. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  126. cx.t.Fatalf("userDelTest #%d: ctlV3User error (%v)", i, err)
  127. }
  128. }
  129. }
  130. func userPasswdTest(cx ctlCtx) {
  131. cmdSet := []userCmdDesc{
  132. // Adds a user name.
  133. {
  134. args: []string{"add", "username", "--interactive=false"},
  135. expectedStr: "User username created",
  136. stdIn: []string{"password"},
  137. },
  138. // Changes the password.
  139. {
  140. args: []string{"passwd", "username", "--interactive=false"},
  141. expectedStr: "Password updated",
  142. stdIn: []string{"password1"},
  143. },
  144. }
  145. for i, cmd := range cmdSet {
  146. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  147. cx.t.Fatalf("userPasswdTest #%d: ctlV3User error (%v)", i, err)
  148. }
  149. }
  150. }
  151. func ctlV3User(cx ctlCtx, args []string, expStr string, stdIn []string) error {
  152. cmdArgs := append(cx.PrefixArgs(), "user")
  153. cmdArgs = append(cmdArgs, args...)
  154. proc, err := spawnCmd(cmdArgs)
  155. if err != nil {
  156. return err
  157. }
  158. // Send 'stdIn' strings as input.
  159. for _, s := range stdIn {
  160. if err = proc.Send(s + "\r"); err != nil {
  161. return err
  162. }
  163. }
  164. _, err = proc.Expect(expStr)
  165. return err
  166. }