github.com/anuvu/nomad@v0.8.7-atom1/command/agent/helpers_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/helper/uuid"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestHTTP_rpcHandlerForAlloc(t *testing.T) {
    11  	t.Parallel()
    12  	require := require.New(t)
    13  	agent := NewTestAgent(t, t.Name(), nil)
    14  
    15  	a := mockFSAlloc(agent.client.NodeID(), nil)
    16  	addAllocToClient(agent, a, terminalClientAlloc)
    17  
    18  	// Case 1: Client has allocation
    19  	// Outcome: Use local client
    20  	lc, rc, s := agent.Server.rpcHandlerForAlloc(a.ID)
    21  	require.True(lc)
    22  	require.False(rc)
    23  	require.False(s)
    24  
    25  	// Case 2: Client doesn't have allocation and there is a server
    26  	// Outcome: Use server
    27  	lc, rc, s = agent.Server.rpcHandlerForAlloc(uuid.Generate())
    28  	require.False(lc)
    29  	require.False(rc)
    30  	require.True(s)
    31  
    32  	// Case 3: Client doesn't have allocation and there is no server
    33  	// Outcome: Use client RPC to server
    34  	srv := agent.server
    35  	agent.server = nil
    36  	lc, rc, s = agent.Server.rpcHandlerForAlloc(uuid.Generate())
    37  	require.False(lc)
    38  	require.True(rc)
    39  	require.False(s)
    40  	agent.server = srv
    41  
    42  	// Case 4: No client
    43  	// Outcome: Use server
    44  	client := agent.client
    45  	agent.client = nil
    46  	lc, rc, s = agent.Server.rpcHandlerForAlloc(uuid.Generate())
    47  	require.False(lc)
    48  	require.False(rc)
    49  	require.True(s)
    50  	agent.client = client
    51  }
    52  
    53  func TestHTTP_rpcHandlerForNode(t *testing.T) {
    54  	t.Parallel()
    55  	require := require.New(t)
    56  	agent := NewTestAgent(t, t.Name(), nil)
    57  	cID := agent.client.NodeID()
    58  
    59  	// Case 1: Node running, no node ID given
    60  	// Outcome: Use local node
    61  	lc, rc, s := agent.Server.rpcHandlerForNode("")
    62  	require.True(lc)
    63  	require.False(rc)
    64  	require.False(s)
    65  
    66  	// Case 2: Node running, it's ID given
    67  	// Outcome: Use local node
    68  	lc, rc, s = agent.Server.rpcHandlerForNode(cID)
    69  	require.True(lc)
    70  	require.False(rc)
    71  	require.False(s)
    72  
    73  	// Case 3: Local node but wrong ID and there is no server
    74  	// Outcome: Use client RPC to server
    75  	srv := agent.server
    76  	agent.server = nil
    77  	lc, rc, s = agent.Server.rpcHandlerForNode(uuid.Generate())
    78  	require.False(lc)
    79  	require.True(rc)
    80  	require.False(s)
    81  	agent.server = srv
    82  
    83  	// Case 4: No client
    84  	// Outcome: Use server
    85  	client := agent.client
    86  	agent.client = nil
    87  	lc, rc, s = agent.Server.rpcHandlerForNode(uuid.Generate())
    88  	require.False(lc)
    89  	require.False(rc)
    90  	require.True(s)
    91  	agent.client = client
    92  }