github.com/proppy/fleet@v0.1.4/agent/state_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // Assert that an existing conflict is triggered against the potential job name
     8  func TestHasConflictExistingMatch(t *testing.T) {
     9  	state := NewState()
    10  	state.TrackJobConflicts("a", []string{"b"})
    11  
    12  	matched, name := state.HasConflict("b", []string{})
    13  	if !matched || name != "a" {
    14  		t.Errorf("Expected conflict with 'a'")
    15  	}
    16  }
    17  
    18  // Assert that a potential conflict is triggered against the existing job name
    19  func TestHasConflictPotentialMatch(t *testing.T) {
    20  	state := NewState()
    21  	state.TrackJobConflicts("a", []string{})
    22  
    23  	matched, name := state.HasConflict("b", []string{"a"})
    24  	if !matched || name != "a" {
    25  		t.Errorf("Expected conflict with 'a'")
    26  	}
    27  }
    28  
    29  // Assert that a existing jobs and potential jobs that do not conflict do not
    30  // trigger a match
    31  func TestHasConflictNoMatch(t *testing.T) {
    32  	state := NewState()
    33  	state.TrackJobConflicts("a", []string{"b"})
    34  
    35  	matched, _ := state.HasConflict("c", []string{"d"})
    36  	if matched {
    37  		t.Errorf("Expected no match")
    38  	}
    39  }
    40  
    41  // Assert that our glob-parser can handle relatively-complex matching
    42  func TestHasConflictComplexGlob(t *testing.T) {
    43  	state := NewState()
    44  	state.TrackJobConflicts("a", []string{"*.[1-9].service"})
    45  
    46  	matched, name := state.HasConflict("web.2.service", []string{})
    47  	if !matched || name != "a" {
    48  		t.Errorf("Expected conflict with 'a'")
    49  	}
    50  
    51  	matched, _ = state.HasConflict("app.99.service", []string{})
    52  	if matched {
    53  		t.Errorf("Expected no conflict")
    54  	}
    55  }
    56  
    57  // Assert that a conflict is truly gone when DropJobConflicts is called
    58  func TestHasConflictDropped(t *testing.T) {
    59  	state := NewState()
    60  	state.TrackJobConflicts("a", []string{"b"})
    61  
    62  	matched, name := state.HasConflict("b", []string{})
    63  	if !matched || name != "a" {
    64  		t.Errorf("Expected conflict with 'a'")
    65  	}
    66  
    67  	state.DropJobConflicts("a")
    68  	matched, _ = state.HasConflict("b", []string{})
    69  	if matched {
    70  		t.Errorf("Expected no conflict")
    71  	}
    72  }
    73  
    74  // Assert that jobs and their peers are properly indexed
    75  func TestGetJobsByPeer(t *testing.T) {
    76  	state := NewState()
    77  	state.TrackJobPeers("a", []string{"b", "c"})
    78  	state.TrackJobPeers("d", []string{"c"})
    79  
    80  	peers := state.GetJobsByPeer("b")
    81  	if len(peers) != 1 || peers[0] != "a" {
    82  		t.Fatalf("Unexpected index of job peers %v", peers)
    83  	}
    84  
    85  	peers = state.GetJobsByPeer("c")
    86  	if len(peers) != 2 || peers[0] != "a" || peers[1] != "d" {
    87  		t.Fatalf("Unexpected index of job peers %v", peers)
    88  	}
    89  }
    90  
    91  // Assert that no jobs are returned for unknown peers
    92  func TestGetJobsByPeerUnknown(t *testing.T) {
    93  	state := NewState()
    94  	state.TrackJobPeers("a", []string{"b"})
    95  
    96  	peers := state.GetJobsByPeer("c")
    97  	if len(peers) != 0 {
    98  		t.Fatalf("Unexpected index of job peers %v", peers)
    99  	}
   100  }
   101  
   102  // Assert that peers indexes are properly cleared after
   103  // calling DropPeersJob
   104  func TestDropPeersJob(t *testing.T) {
   105  	state := NewState()
   106  	state.TrackJobPeers("a", []string{"b", "c"})
   107  	state.TrackJobPeers("d", []string{"c"})
   108  	state.DropPeersJob("a")
   109  
   110  	peers := state.GetJobsByPeer("c")
   111  	if len(peers) != 1 || peers[0] != "d" {
   112  		t.Fatalf("Unexpected index of job peers %v", peers)
   113  	}
   114  }