ctl_v3_endpoint_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "context"
  17. "fmt"
  18. "net/url"
  19. "testing"
  20. "time"
  21. "go.etcd.io/etcd/clientv3"
  22. )
  23. func TestCtlV3EndpointHealth(t *testing.T) { testCtl(t, endpointHealthTest, withQuorum()) }
  24. func TestCtlV3EndpointStatus(t *testing.T) { testCtl(t, endpointStatusTest, withQuorum()) }
  25. func TestCtlV3EndpointHashKV(t *testing.T) { testCtl(t, endpointHashKVTest, withQuorum()) }
  26. func endpointHealthTest(cx ctlCtx) {
  27. if err := ctlV3EndpointHealth(cx); err != nil {
  28. cx.t.Fatalf("endpointStatusTest ctlV3EndpointHealth error (%v)", err)
  29. }
  30. }
  31. func ctlV3EndpointHealth(cx ctlCtx) error {
  32. cmdArgs := append(cx.PrefixArgs(), "endpoint", "health")
  33. lines := make([]string, cx.epc.cfg.clusterSize)
  34. for i := range lines {
  35. lines[i] = "is healthy"
  36. }
  37. return spawnWithExpects(cmdArgs, lines...)
  38. }
  39. func endpointStatusTest(cx ctlCtx) {
  40. if err := ctlV3EndpointStatus(cx); err != nil {
  41. cx.t.Fatalf("endpointStatusTest ctlV3EndpointStatus error (%v)", err)
  42. }
  43. }
  44. func ctlV3EndpointStatus(cx ctlCtx) error {
  45. cmdArgs := append(cx.PrefixArgs(), "endpoint", "status")
  46. var eps []string
  47. for _, ep := range cx.epc.EndpointsV3() {
  48. u, _ := url.Parse(ep)
  49. eps = append(eps, u.Host)
  50. }
  51. return spawnWithExpects(cmdArgs, eps...)
  52. }
  53. func endpointHashKVTest(cx ctlCtx) {
  54. if err := ctlV3EndpointHashKV(cx); err != nil {
  55. cx.t.Fatalf("endpointHashKVTest ctlV3EndpointHashKV error (%v)", err)
  56. }
  57. }
  58. func ctlV3EndpointHashKV(cx ctlCtx) error {
  59. eps := cx.epc.EndpointsV3()
  60. // get latest hash to compare
  61. cli, err := clientv3.New(clientv3.Config{
  62. Endpoints: eps,
  63. DialTimeout: 3 * time.Second,
  64. })
  65. if err != nil {
  66. cx.t.Fatal(err)
  67. }
  68. defer cli.Close()
  69. hresp, err := cli.HashKV(context.TODO(), eps[0], 0)
  70. if err != nil {
  71. cx.t.Fatal(err)
  72. }
  73. cmdArgs := append(cx.PrefixArgs(), "endpoint", "hashkv")
  74. var ss []string
  75. for _, ep := range cx.epc.EndpointsV3() {
  76. u, _ := url.Parse(ep)
  77. ss = append(ss, fmt.Sprintf("%s, %d", u.Host, hresp.Hash))
  78. }
  79. return spawnWithExpects(cmdArgs, ss...)
  80. }