ctl_v3_endpoint_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 (
  16. "net/url"
  17. "testing"
  18. )
  19. func TestCtlV3EndpointHealth(t *testing.T) { testCtl(t, endpointHealthTest, withQuorum()) }
  20. func TestCtlV3EndpointStatus(t *testing.T) { testCtl(t, endpointStatusTest, withQuorum()) }
  21. func TestCtlV3EndpointHealthWithAuth(t *testing.T) {
  22. testCtl(t, endpointHealthTestWithAuth, withQuorum())
  23. }
  24. func endpointHealthTest(cx ctlCtx) {
  25. if err := ctlV3EndpointHealth(cx); err != nil {
  26. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  27. }
  28. }
  29. func ctlV3EndpointHealth(cx ctlCtx) error {
  30. cmdArgs := append(cx.PrefixArgs(), "endpoint", "health")
  31. lines := make([]string, cx.epc.cfg.clusterSize)
  32. for i := range lines {
  33. lines[i] = "is healthy"
  34. }
  35. return spawnWithExpects(cmdArgs, lines...)
  36. }
  37. func ctlV3EndpointHealthWithKey(cx ctlCtx, key string) error {
  38. cmdArgs := append(cx.PrefixArgs(), "endpoint", "health", "--health-check-key", key)
  39. lines := make([]string, cx.epc.cfg.clusterSize)
  40. for i := range lines {
  41. lines[i] = "is healthy"
  42. }
  43. return spawnWithExpects(cmdArgs, lines...)
  44. }
  45. func endpointStatusTest(cx ctlCtx) {
  46. if err := ctlV3EndpointStatus(cx); err != nil {
  47. cx.t.Fatalf("endpointStatusTest ctlV3EndpointStatus error (%v)", err)
  48. }
  49. }
  50. func ctlV3EndpointStatus(cx ctlCtx) error {
  51. cmdArgs := append(cx.PrefixArgs(), "endpoint", "status")
  52. var eps []string
  53. for _, ep := range cx.epc.endpoints() {
  54. u, _ := url.Parse(ep)
  55. eps = append(eps, u.Host)
  56. }
  57. return spawnWithExpects(cmdArgs, eps...)
  58. }
  59. func ctlV3EndpointHealthFailPermissionDenied(cx ctlCtx) error {
  60. cmdArgs := append(cx.PrefixArgs(), "endpoint", "health")
  61. lines := make([]string, cx.epc.cfg.clusterSize)
  62. for i := range lines {
  63. lines[i] = "is unhealthy: failed to commit proposal: etcdserver: permission denied"
  64. }
  65. return spawnWithExpects(cmdArgs, lines...)
  66. }
  67. func endpointHealthTestWithAuth(cx ctlCtx) {
  68. if err := authEnable(cx); err != nil {
  69. cx.t.Fatal(err)
  70. }
  71. cx.user, cx.pass = "root", "root"
  72. authSetupTestUser(cx)
  73. if err := ctlV3EndpointHealth(cx); err != nil {
  74. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  75. }
  76. // health checking with an ordinal user must fail because the user isn't granted a permission of the key "health"
  77. cx.user, cx.pass = "test-user", "pass"
  78. if err := ctlV3EndpointHealthFailPermissionDenied(cx); err != nil {
  79. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  80. }
  81. cx.user, cx.pass = "root", "root"
  82. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "custom-key", "", false}); err != nil {
  83. cx.t.Fatal(err)
  84. }
  85. cx.user, cx.pass = "test-user", "pass"
  86. if err := ctlV3EndpointHealthWithKey(cx, "custom-key"); err != nil {
  87. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  88. }
  89. }