ctl_v3_role_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 TestCtlV3RoleAdd(t *testing.T) { testCtl(t, roleAddTest) }
  17. func TestCtlV3RoleAddNoTLS(t *testing.T) { testCtl(t, roleAddTest, withCfg(configNoTLS)) }
  18. func TestCtlV3RoleAddClientTLS(t *testing.T) { testCtl(t, roleAddTest, withCfg(configClientTLS)) }
  19. func TestCtlV3RoleAddPeerTLS(t *testing.T) { testCtl(t, roleAddTest, withCfg(configPeerTLS)) }
  20. func TestCtlV3RoleAddTimeout(t *testing.T) { testCtl(t, roleAddTest, withDialTimeout(0)) }
  21. func TestCtlV3RoleGrant(t *testing.T) { testCtl(t, roleGrantTest) }
  22. func roleAddTest(cx ctlCtx) {
  23. cmdSet := []struct {
  24. args []string
  25. expectedStr string
  26. }{
  27. // Add a role.
  28. {
  29. args: []string{"add", "root"},
  30. expectedStr: "Role root created",
  31. },
  32. // Try adding the same role.
  33. {
  34. args: []string{"add", "root"},
  35. expectedStr: "role name already exists",
  36. },
  37. }
  38. for i, cmd := range cmdSet {
  39. if err := ctlV3Role(cx, cmd.args, cmd.expectedStr); err != nil {
  40. if cx.dialTimeout > 0 && !isGRPCTimedout(err) {
  41. cx.t.Fatalf("roleAddTest #%d: ctlV3Role error (%v)", i, err)
  42. }
  43. }
  44. }
  45. }
  46. func roleGrantTest(cx ctlCtx) {
  47. cmdSet := []struct {
  48. args []string
  49. expectedStr string
  50. }{
  51. // Add a role.
  52. {
  53. args: []string{"add", "root"},
  54. expectedStr: "Role root created",
  55. },
  56. // Grant read permission to the role.
  57. {
  58. args: []string{"grant", "root", "read", "foo"},
  59. expectedStr: "Role root updated",
  60. },
  61. // Grant write permission to the role.
  62. {
  63. args: []string{"grant", "root", "write", "foo"},
  64. expectedStr: "Role root updated",
  65. },
  66. // Grant rw permission to the role.
  67. {
  68. args: []string{"grant", "root", "readwrite", "foo"},
  69. expectedStr: "Role root updated",
  70. },
  71. // Try granting invalid permission to the role.
  72. {
  73. args: []string{"grant", "root", "123", "foo"},
  74. expectedStr: "invalid permission type",
  75. },
  76. }
  77. for i, cmd := range cmdSet {
  78. if err := ctlV3Role(cx, cmd.args, cmd.expectedStr); err != nil {
  79. cx.t.Fatalf("roleGrantTest #%d: ctlV3Role error (%v)", i, err)
  80. }
  81. }
  82. }
  83. func ctlV3Role(cx ctlCtx, args []string, expStr string) error {
  84. cmdArgs := append(cx.PrefixArgs(), "role")
  85. cmdArgs = append(cmdArgs, args...)
  86. return spawnWithExpect(cmdArgs, expStr)
  87. }