github.phpd.cn/hashicorp/consul@v1.4.5/agent/consul/autopilot/promotion_test.go (about)

     1  package autopilot
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hashicorp/raft"
     8  	"github.com/pascaldekloe/goe/verify"
     9  )
    10  
    11  func TestPromotion(t *testing.T) {
    12  	config := &Config{
    13  		LastContactThreshold:    5 * time.Second,
    14  		MaxTrailingLogs:         100,
    15  		ServerStabilizationTime: 3 * time.Second,
    16  	}
    17  
    18  	cases := []struct {
    19  		name       string
    20  		conf       *Config
    21  		health     OperatorHealthReply
    22  		servers    []raft.Server
    23  		promotions []raft.Server
    24  	}{
    25  		{
    26  			name: "one stable voter, no promotions",
    27  			conf: config,
    28  			health: OperatorHealthReply{
    29  				Servers: []ServerHealth{
    30  					{
    31  						ID:          "a",
    32  						Healthy:     true,
    33  						StableSince: time.Now().Add(-10 * time.Second),
    34  					},
    35  				},
    36  			},
    37  			servers: []raft.Server{
    38  				{ID: "a", Suffrage: raft.Voter},
    39  			},
    40  			promotions: []raft.Server{},
    41  		},
    42  		{
    43  			name: "one stable nonvoter, should be promoted",
    44  			conf: config,
    45  			health: OperatorHealthReply{
    46  				Servers: []ServerHealth{
    47  					{
    48  						ID:          "a",
    49  						Healthy:     true,
    50  						StableSince: time.Now().Add(-10 * time.Second),
    51  					},
    52  					{
    53  						ID:          "b",
    54  						Healthy:     true,
    55  						StableSince: time.Now().Add(-10 * time.Second),
    56  					},
    57  				},
    58  			},
    59  			servers: []raft.Server{
    60  				{ID: "a", Suffrage: raft.Voter},
    61  				{ID: "b", Suffrage: raft.Nonvoter},
    62  			},
    63  			promotions: []raft.Server{
    64  				{ID: "b", Suffrage: raft.Nonvoter},
    65  			},
    66  		},
    67  		{
    68  			name: "unstable servers, neither should be promoted",
    69  			conf: config,
    70  			health: OperatorHealthReply{
    71  				Servers: []ServerHealth{
    72  					{
    73  						ID:          "a",
    74  						Healthy:     true,
    75  						StableSince: time.Now().Add(-10 * time.Second),
    76  					},
    77  					{
    78  						ID:          "b",
    79  						Healthy:     false,
    80  						StableSince: time.Now().Add(-10 * time.Second),
    81  					},
    82  					{
    83  						ID:          "c",
    84  						Healthy:     true,
    85  						StableSince: time.Now().Add(-1 * time.Second),
    86  					},
    87  				},
    88  			},
    89  			servers: []raft.Server{
    90  				{ID: "a", Suffrage: raft.Voter},
    91  				{ID: "b", Suffrage: raft.Nonvoter},
    92  				{ID: "c", Suffrage: raft.Nonvoter},
    93  			},
    94  			promotions: []raft.Server{},
    95  		},
    96  	}
    97  
    98  	for _, tc := range cases {
    99  		promotions := PromoteStableServers(tc.conf, tc.health, tc.servers)
   100  		verify.Values(t, tc.name, tc.promotions, promotions)
   101  	}
   102  }