ctl_v3_user_test.go 4.0 KB

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