interaction_env_handler_raft_log.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 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 rafttest
  15. import (
  16. "fmt"
  17. "math"
  18. "testing"
  19. "github.com/cockroachdb/datadriven"
  20. "go.etcd.io/etcd/raft"
  21. )
  22. func (env *InteractionEnv) writeErr(err error) {
  23. if err != nil {
  24. env.Output.WriteString(err.Error())
  25. }
  26. }
  27. func (env *InteractionEnv) handleRaftLog(t *testing.T, d datadriven.TestData) error {
  28. idx := firstAsNodeIdx(t, d)
  29. return env.RaftLog(idx)
  30. }
  31. // RaftLog pretty prints the raft log to the output buffer.
  32. func (env *InteractionEnv) RaftLog(idx int) error {
  33. s := env.Nodes[idx].Storage
  34. fi, err := s.FirstIndex()
  35. if err != nil {
  36. return err
  37. }
  38. li, err := s.LastIndex()
  39. if err != nil {
  40. return err
  41. }
  42. if li < fi {
  43. // TODO(tbg): this is what MemoryStorage returns, but unclear if it's
  44. // the "correct" thing to do.
  45. fmt.Fprintf(env.Output, "log is empty: first index=%d, last index=%d", fi, li)
  46. return nil
  47. }
  48. ents, err := s.Entries(fi, li+1, math.MaxUint64)
  49. if err != nil {
  50. return err
  51. }
  52. env.Output.WriteString(raft.DescribeEntries(ents, defaultEntryFormatter))
  53. return err
  54. }