ctl_v3_user_test.go 3.7 KB

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