ctl_v3_endpoint_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 endpointStatusTest(cx ctlCtx) {
  38. if err := ctlV3EndpointStatus(cx); err != nil {
  39. cx.t.Fatalf("endpointStatusTest ctlV3EndpointStatus error (%v)", err)
  40. }
  41. }
  42. func ctlV3EndpointStatus(cx ctlCtx) error {
  43. cmdArgs := append(cx.PrefixArgs(), "endpoint", "status")
  44. var eps []string
  45. for _, ep := range cx.epc.endpoints() {
  46. u, _ := url.Parse(ep)
  47. eps = append(eps, u.Host)
  48. }
  49. return spawnWithExpects(cmdArgs, eps...)
  50. }
  51. func ctlV3EndpointHealthFailPermissionDenied(cx ctlCtx) error {
  52. cmdArgs := append(cx.PrefixArgs(), "endpoint", "health")
  53. lines := make([]string, cx.epc.cfg.clusterSize)
  54. for i := range lines {
  55. lines[i] = "is unhealthy: failed to commit proposal: etcdserver: permission denied"
  56. }
  57. return spawnWithExpects(cmdArgs, lines...)
  58. }
  59. func endpointHealthTestWithAuth(cx ctlCtx) {
  60. if err := authEnable(cx); err != nil {
  61. cx.t.Fatal(err)
  62. }
  63. cx.user, cx.pass = "root", "root"
  64. authSetupTestUser(cx)
  65. if err := ctlV3EndpointHealth(cx); err != nil {
  66. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  67. }
  68. // health checking with an ordinal user must fail because the user isn't granted a permission of the key "health"
  69. cx.user, cx.pass = "test-user", "pass"
  70. if err := ctlV3EndpointHealthFailPermissionDenied(cx); err != nil {
  71. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  72. }
  73. }