datadriven_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 confchange
  15. import (
  16. "errors"
  17. "fmt"
  18. "strconv"
  19. "strings"
  20. "testing"
  21. "github.com/cockroachdb/datadriven"
  22. pb "go.etcd.io/etcd/raft/raftpb"
  23. "go.etcd.io/etcd/raft/tracker"
  24. )
  25. func TestConfChangeDataDriven(t *testing.T) {
  26. datadriven.Walk(t, "testdata", func(t *testing.T, path string) {
  27. tr := tracker.MakeProgressTracker(10)
  28. c := Changer{
  29. Tracker: tr,
  30. LastIndex: 0, // incremented in this test with each cmd
  31. }
  32. // The test files use the commands
  33. // - simple: run a simple conf change (i.e. no joint consensus),
  34. // - enter-joint: enter a joint config, and
  35. // - leave-joint: leave a joint config.
  36. // The first two take a list of config changes, which have the following
  37. // syntax:
  38. // - vn: make n a voter,
  39. // - ln: make n a learner,
  40. // - rn: remove n, and
  41. // - un: update n.
  42. datadriven.RunTest(t, path, func(d *datadriven.TestData) string {
  43. defer func() {
  44. c.LastIndex++
  45. }()
  46. var ccs []pb.ConfChangeSingle
  47. toks := strings.Split(strings.TrimSpace(d.Input), " ")
  48. if toks[0] == "" {
  49. toks = nil
  50. }
  51. for _, tok := range toks {
  52. if len(tok) < 2 {
  53. return fmt.Sprintf("unknown token %s", tok)
  54. }
  55. var cc pb.ConfChangeSingle
  56. switch tok[0] {
  57. case 'v':
  58. cc.Type = pb.ConfChangeAddNode
  59. case 'l':
  60. cc.Type = pb.ConfChangeAddLearnerNode
  61. case 'r':
  62. cc.Type = pb.ConfChangeRemoveNode
  63. case 'u':
  64. cc.Type = pb.ConfChangeUpdateNode
  65. default:
  66. return fmt.Sprintf("unknown input: %s", tok)
  67. }
  68. id, err := strconv.ParseUint(tok[1:], 10, 64)
  69. if err != nil {
  70. return err.Error()
  71. }
  72. cc.NodeID = id
  73. ccs = append(ccs, cc)
  74. }
  75. var cfg tracker.Config
  76. var prs tracker.ProgressMap
  77. var err error
  78. switch d.Cmd {
  79. case "simple":
  80. cfg, prs, err = c.Simple(ccs...)
  81. case "enter-joint":
  82. var autoLeave bool
  83. if len(d.CmdArgs) > 0 {
  84. d.ScanArgs(t, "autoleave", &autoLeave)
  85. }
  86. cfg, prs, err = c.EnterJoint(autoLeave, ccs...)
  87. case "leave-joint":
  88. if len(ccs) > 0 {
  89. err = errors.New("this command takes no input")
  90. } else {
  91. cfg, prs, err = c.LeaveJoint()
  92. }
  93. default:
  94. return "unknown command"
  95. }
  96. if err != nil {
  97. return err.Error() + "\n"
  98. }
  99. c.Tracker.Config, c.Tracker.Progress = cfg, prs
  100. return fmt.Sprintf("%s\n%s", c.Tracker.Config, c.Tracker.Progress)
  101. })
  102. })
  103. }