ctl_v3_user_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 TestCtlV3UserDelete(t *testing.T) { testCtl(t, userDelTest) }
  22. func TestCtlV3UserPasswd(t *testing.T) { testCtl(t, userPasswdTest) }
  23. type userCmdDesc struct {
  24. args []string
  25. expectedStr string
  26. stdIn []string
  27. }
  28. func userAddTest(cx ctlCtx) {
  29. cmdSet := []userCmdDesc{
  30. // Adds a user name.
  31. {
  32. args: []string{"add", "username", "--interactive=false"},
  33. expectedStr: "User username created",
  34. stdIn: []string{"password"},
  35. },
  36. // Tries to add a user name that already exists.
  37. {
  38. args: []string{"add", "username", "--interactive=false"},
  39. expectedStr: "user name already exists",
  40. stdIn: []string{"password"},
  41. },
  42. }
  43. for i, cmd := range cmdSet {
  44. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  45. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  46. cx.t.Fatalf("userAddTest #%d: ctlV3User error (%v)", i, err)
  47. }
  48. }
  49. }
  50. }
  51. func userDelTest(cx ctlCtx) {
  52. cmdSet := []userCmdDesc{
  53. // Adds a user name.
  54. {
  55. args: []string{"add", "username", "--interactive=false"},
  56. expectedStr: "User username created",
  57. stdIn: []string{"password"},
  58. },
  59. // Deletes the user name just added.
  60. {
  61. args: []string{"delete", "username"},
  62. expectedStr: "User username deleted",
  63. },
  64. // Deletes a user name that is not present.
  65. {
  66. args: []string{"delete", "username"},
  67. expectedStr: "user name not found",
  68. },
  69. }
  70. for i, cmd := range cmdSet {
  71. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  72. cx.t.Fatalf("userDelTest #%d: ctlV3User error (%v)", i, err)
  73. }
  74. }
  75. }
  76. func userPasswdTest(cx ctlCtx) {
  77. cmdSet := []userCmdDesc{
  78. // Adds a user name.
  79. {
  80. args: []string{"add", "username", "--interactive=false"},
  81. expectedStr: "User username created",
  82. stdIn: []string{"password"},
  83. },
  84. // Changes the password.
  85. {
  86. args: []string{"passwd", "username", "--interactive=false"},
  87. expectedStr: "Password updated",
  88. stdIn: []string{"password1"},
  89. },
  90. }
  91. for i, cmd := range cmdSet {
  92. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  93. cx.t.Fatalf("userPasswdTest #%d: ctlV3User error (%v)", i, err)
  94. }
  95. }
  96. }
  97. func ctlV3User(cx ctlCtx, args []string, expStr string, stdIn []string) error {
  98. cmdArgs := append(cx.PrefixArgs(), "user")
  99. cmdArgs = append(cmdArgs, args...)
  100. proc, err := spawnCmd(cmdArgs)
  101. if err != nil {
  102. return err
  103. }
  104. // Send 'stdIn' strings as input.
  105. for _, s := range stdIn {
  106. if err = proc.Send(s + "\r"); err != nil {
  107. return err
  108. }
  109. }
  110. _, err = proc.Expect(expStr)
  111. return err
  112. }