Skip to content

Commit

Permalink
Adding unit tests to node handler
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos G. Yedro <[email protected]>
  • Loading branch information
marcosy committed Jan 25, 2019
1 parent 06c3692 commit 98babed
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pkg/server/endpoints/node/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"io"
"io/ioutil"
"net"
Expand Down Expand Up @@ -195,6 +196,74 @@ func TestAttestChallengeResponse(t *testing.T) {
suite.NoError(suite.handler.Attest(stream))
}

func TestEvictSuccessCase(t *testing.T) {
suite := SetupHandlerTest(t)
spiffeIDToRemove := "spiffe://example.org/spire/agent/join_token/token_a"
evictRequest := &node.EvictRequest{SpiffeID: spiffeIDToRemove}
ctx := context.Background()

// Set Evict expectations
suite.mockDataStore.EXPECT().
DeleteAttestedNode(ctx, &datastore.DeleteAttestedNodeRequest{SpiffeId: spiffeIDToRemove}).
Return(&datastore.DeleteAttestedNodeResponse{}, nil)

evictResponse, err := suite.handler.Evict(ctx, evictRequest)
suite.NoError(err)
suite.True(evictResponse.DeleteSucceed, "Evict did not remove spiffeID: %q", spiffeIDToRemove)
}

func TestEvictFailureCase(t *testing.T) {
suite := SetupHandlerTest(t)
spiffeIDToRemove := "spiffe://example.org/spire/agent/join_token/token_a"
evictRequest := &node.EvictRequest{SpiffeID: spiffeIDToRemove}
ctx := context.Background()

// Set Evict expectations
suite.mockDataStore.EXPECT().
DeleteAttestedNode(ctx, &datastore.DeleteAttestedNodeRequest{SpiffeId: spiffeIDToRemove}).
Return(&datastore.DeleteAttestedNodeResponse{}, errors.New("Some error"))

evictResponse, err := suite.handler.Evict(ctx, evictRequest)
suite.Error(err, "Evict should have failed")
suite.False(evictResponse.DeleteSucceed, "Evict shouldn't have removed entry %q", spiffeIDToRemove)
}
func TestListSuccessCase(t *testing.T) {
suite := SetupHandlerTest(t)
ctx := context.Background()
nodesList := []*common.AttestedNode{
{SpiffeId: "spiffe://example.org/spire/agent/join_token/token_a"},
{SpiffeId: "spiffe://example.org/spire/agent/join_token/token_b"},
}

// Set List expectations
suite.mockDataStore.EXPECT().
ListAttestedNodes(ctx, &datastore.ListAttestedNodesRequest{}).
Return(&datastore.ListAttestedNodesResponse{Nodes: nodesList}, nil)

listResponse, err := suite.handler.List(ctx, &common.Empty{})
suite.NoError(err)
suite.Len(listResponse.Nodes, 2)
suite.Equal(listResponse.Nodes, nodesList)
}

func TestListFailureCase(t *testing.T) {
suite := SetupHandlerTest(t)
ctx := context.Background()
nodesList := []*common.AttestedNode{
{SpiffeId: "spiffe://example.org/spire/agent/join_token/token_a"},
{SpiffeId: "spiffe://example.org/spire/agent/join_token/token_b"},
}

// Set List expectations
suite.mockDataStore.EXPECT().
ListAttestedNodes(ctx, &datastore.ListAttestedNodesRequest{}).
Return(&datastore.ListAttestedNodesResponse{Nodes: nodesList}, errors.New("Some error"))

listResponse, err := suite.handler.List(ctx, &common.Empty{})
suite.Error(err)
suite.Nil(listResponse)
}

func TestFetchX509SVID(t *testing.T) {
suite := SetupHandlerTest(t)
defer suite.ctrl.Finish()
Expand Down

0 comments on commit 98babed

Please sign in to comment.