github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/state/autopilot_test.go (about)

     1  package state
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  )
    11  
    12  func TestStateStore_Autopilot(t *testing.T) {
    13  	ci.Parallel(t)
    14  
    15  	s := testStateStore(t)
    16  
    17  	expected := &structs.AutopilotConfig{
    18  		CleanupDeadServers:      true,
    19  		LastContactThreshold:    5 * time.Second,
    20  		MaxTrailingLogs:         500,
    21  		MinQuorum:               3,
    22  		ServerStabilizationTime: 100 * time.Second,
    23  		EnableRedundancyZones:   true,
    24  		DisableUpgradeMigration: true,
    25  		EnableCustomUpgrades:    true,
    26  	}
    27  
    28  	if err := s.AutopilotSetConfig(0, expected); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	idx, config, err := s.AutopilotConfig()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	if idx != 0 {
    37  		t.Fatalf("bad: %d", idx)
    38  	}
    39  	if !reflect.DeepEqual(expected, config) {
    40  		t.Fatalf("bad: %#v, %#v", expected, config)
    41  	}
    42  }
    43  
    44  func TestStateStore_AutopilotCAS(t *testing.T) {
    45  	ci.Parallel(t)
    46  
    47  	s := testStateStore(t)
    48  
    49  	expected := &structs.AutopilotConfig{
    50  		CleanupDeadServers: true,
    51  	}
    52  
    53  	if err := s.AutopilotSetConfig(0, expected); err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	if err := s.AutopilotSetConfig(1, expected); err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	// Do a CAS with an index lower than the entry
    61  	ok, err := s.AutopilotCASConfig(2, 0, &structs.AutopilotConfig{
    62  		CleanupDeadServers: false,
    63  	})
    64  	if ok || err != nil {
    65  		t.Fatalf("expected (false, nil), got: (%v, %#v)", ok, err)
    66  	}
    67  
    68  	// Check that the index is untouched and the entry
    69  	// has not been updated.
    70  	idx, config, err := s.AutopilotConfig()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	if idx != 1 {
    75  		t.Fatalf("bad: %d", idx)
    76  	}
    77  	if !config.CleanupDeadServers {
    78  		t.Fatalf("bad: %#v", config)
    79  	}
    80  
    81  	// Do another CAS, this time with the correct index
    82  	ok, err = s.AutopilotCASConfig(2, 1, &structs.AutopilotConfig{
    83  		CleanupDeadServers: false,
    84  	})
    85  	if !ok || err != nil {
    86  		t.Fatalf("expected (true, nil), got: (%v, %#v)", ok, err)
    87  	}
    88  
    89  	// Make sure the config was updated
    90  	idx, config, err = s.AutopilotConfig()
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	if idx != 2 {
    95  		t.Fatalf("bad: %d", idx)
    96  	}
    97  	if config.CleanupDeadServers {
    98  		t.Fatalf("bad: %#v", config)
    99  	}
   100  }