ctl_v3_endpoint_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 endpointHealthTestWithAuth(cx ctlCtx) {
  52. if err := authEnable(cx); err != nil {
  53. cx.t.Fatal(err)
  54. }
  55. cx.user, cx.pass = "root", "root"
  56. authSetupTestUser(cx)
  57. if err := ctlV3EndpointHealth(cx); err != nil {
  58. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  59. }
  60. // health checking with an ordinal user "succeeds" since permission denial goes through consensus
  61. cx.user, cx.pass = "test-user", "pass"
  62. if err := ctlV3EndpointHealth(cx); err != nil {
  63. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  64. }
  65. // succeed if permissions granted for ordinary user
  66. cx.user, cx.pass = "root", "root"
  67. if err := ctlV3RoleGrantPermission(cx, "test-role", grantingPerm{true, true, "health", "", false}); err != nil {
  68. cx.t.Fatal(err)
  69. }
  70. cx.user, cx.pass = "test-user", "pass"
  71. if err := ctlV3EndpointHealth(cx); err != nil {
  72. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  73. }
  74. }