ctl_v3_user_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. func TestCtlV3UserGrant(t *testing.T) { testCtl(t, userGrantTest) }
  24. type userCmdDesc struct {
  25. args []string
  26. expectedStr string
  27. stdIn []string
  28. }
  29. func userAddTest(cx ctlCtx) {
  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. cmdSet := []userCmdDesc{
  54. // Adds a user name.
  55. {
  56. args: []string{"add", "username", "--interactive=false"},
  57. expectedStr: "User username created",
  58. stdIn: []string{"password"},
  59. },
  60. // Deletes the user name just added.
  61. {
  62. args: []string{"delete", "username"},
  63. expectedStr: "User username deleted",
  64. },
  65. // Deletes a user name that is not present.
  66. {
  67. args: []string{"delete", "username"},
  68. expectedStr: "user name not found",
  69. },
  70. }
  71. for i, cmd := range cmdSet {
  72. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  73. cx.t.Fatalf("userDelTest #%d: ctlV3User error (%v)", i, err)
  74. }
  75. }
  76. }
  77. func userPasswdTest(cx ctlCtx) {
  78. cmdSet := []userCmdDesc{
  79. // Adds a user name.
  80. {
  81. args: []string{"add", "username", "--interactive=false"},
  82. expectedStr: "User username created",
  83. stdIn: []string{"password"},
  84. },
  85. // Changes the password.
  86. {
  87. args: []string{"passwd", "username", "--interactive=false"},
  88. expectedStr: "Password updated",
  89. stdIn: []string{"password1"},
  90. },
  91. }
  92. for i, cmd := range cmdSet {
  93. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  94. cx.t.Fatalf("userPasswdTest #%d: ctlV3User error (%v)", i, err)
  95. }
  96. }
  97. }
  98. func userGrantTest(cx ctlCtx) {
  99. // Add a role.
  100. if err := ctlV3Role(cx, []string{"add", "root"}, "Role root created"); err != nil {
  101. cx.t.Fatalf("userGrantTest: ctlV3Role error (%v)", err)
  102. }
  103. cmdSet := []userCmdDesc{
  104. // Add a user name.
  105. {
  106. args: []string{"add", "username", "--interactive=false"},
  107. expectedStr: "User username created",
  108. stdIn: []string{"password"},
  109. },
  110. // Grant the previously added user a role.
  111. {
  112. args: []string{"grant", "username", "root"},
  113. expectedStr: "Role root is granted to user username",
  114. },
  115. // Supply a wrong user name.
  116. {
  117. args: []string{"grant", "username1", "root"},
  118. expectedStr: "user name not found",
  119. },
  120. // Supply a wrong role name.
  121. {
  122. args: []string{"grant", "username", "root1"},
  123. expectedStr: "role name not found",
  124. },
  125. }
  126. for i, cmd := range cmdSet {
  127. if err := ctlV3User(cx, cmd.args, cmd.expectedStr, cmd.stdIn); err != nil {
  128. cx.t.Fatalf("userPasswdTest #%d: ctlV3User error (%v)", i, err)
  129. }
  130. }
  131. }
  132. func ctlV3User(cx ctlCtx, args []string, expStr string, stdIn []string) error {
  133. cmdArgs := append(cx.PrefixArgs(), "user")
  134. cmdArgs = append(cmdArgs, args...)
  135. proc, err := spawnCmd(cmdArgs)
  136. if err != nil {
  137. return err
  138. }
  139. // Send 'stdIn' strings as input.
  140. for _, s := range stdIn {
  141. if err = proc.Send(s + "\r"); err != nil {
  142. return err
  143. }
  144. }
  145. _, err = proc.Expect(expStr)
  146. return err
  147. }