...

Source file src/go.etcd.io/etcd/raft/v3/confchange/datadriven_test.go

Documentation: go.etcd.io/etcd/raft/v3/confchange

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

View as plain text