hub.fastgit.org/hashicorp/consul.git@v1.4.5/agent/consul/autopilot/structs_test.go (about)

     1  package autopilot
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hashicorp/serf/serf"
     8  )
     9  
    10  func TestServerHealth_IsHealthy(t *testing.T) {
    11  	cases := []struct {
    12  		health    ServerHealth
    13  		lastTerm  uint64
    14  		lastIndex uint64
    15  		conf      Config
    16  		expected  bool
    17  	}{
    18  		// Healthy server, all values within allowed limits
    19  		{
    20  			health:    ServerHealth{SerfStatus: serf.StatusAlive, LastTerm: 1, LastIndex: 0},
    21  			lastTerm:  1,
    22  			lastIndex: 10,
    23  			conf:      Config{MaxTrailingLogs: 20},
    24  			expected:  true,
    25  		},
    26  		// Serf status failed
    27  		{
    28  			health:   ServerHealth{SerfStatus: serf.StatusFailed},
    29  			expected: false,
    30  		},
    31  		// Old value for lastTerm
    32  		{
    33  			health:   ServerHealth{SerfStatus: serf.StatusAlive, LastTerm: 0},
    34  			lastTerm: 1,
    35  			expected: false,
    36  		},
    37  		// Too far behind on logs
    38  		{
    39  			health:    ServerHealth{SerfStatus: serf.StatusAlive, LastIndex: 0},
    40  			lastIndex: 10,
    41  			conf:      Config{MaxTrailingLogs: 5},
    42  			expected:  false,
    43  		},
    44  	}
    45  
    46  	for index, tc := range cases {
    47  		actual := tc.health.IsHealthy(tc.lastTerm, tc.lastIndex, &tc.conf)
    48  		if actual != tc.expected {
    49  			t.Fatalf("bad value for case %d: %v", index, actual)
    50  		}
    51  	}
    52  }
    53  
    54  func TestServerHealth_IsStable(t *testing.T) {
    55  	start := time.Now()
    56  	cases := []struct {
    57  		health   *ServerHealth
    58  		now      time.Time
    59  		conf     Config
    60  		expected bool
    61  	}{
    62  		// Healthy server, all values within allowed limits
    63  		{
    64  			health:   &ServerHealth{Healthy: true, StableSince: start},
    65  			now:      start.Add(15 * time.Second),
    66  			conf:     Config{ServerStabilizationTime: 10 * time.Second},
    67  			expected: true,
    68  		},
    69  		// Unhealthy server
    70  		{
    71  			health:   &ServerHealth{Healthy: false},
    72  			expected: false,
    73  		},
    74  		// Healthy server, hasn't reached stabilization time
    75  		{
    76  			health:   &ServerHealth{Healthy: true, StableSince: start},
    77  			now:      start.Add(5 * time.Second),
    78  			conf:     Config{ServerStabilizationTime: 10 * time.Second},
    79  			expected: false,
    80  		},
    81  		// Nil struct
    82  		{
    83  			health:   nil,
    84  			expected: false,
    85  		},
    86  	}
    87  
    88  	for index, tc := range cases {
    89  		actual := tc.health.IsStable(tc.now, &tc.conf)
    90  		if actual != tc.expected {
    91  			t.Fatalf("bad value for case %d: %v", index, actual)
    92  		}
    93  	}
    94  }