Skip to content

Commit

Permalink
[YUNIKORN-2797] Increase handlers.go test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankert01 committed Sep 16, 2024
1 parent 9e10746 commit ff82d47
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions pkg/webservice/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ func newApplication(appID, partitionName, queueName, rmID string, ugi security.U
return objects.NewApplication(siApp, userGroup, nil, rmID)
}

func TestGetStackInfo(t *testing.T) {
req, err := http.NewRequest("GET", "", strings.NewReader(baseConf))
assert.NilError(t, err, "Error creating request")
resp := &MockResponseWriter{}
getStackInfo(resp, req)

// assert stack trace
assert.Assert(t, strings.Contains(string(resp.outputBytes), "goroutine"), "Stack trace should be present in the response")
assert.Assert(t, strings.Contains(string(resp.outputBytes), "test"), "Stack trace should be present in the response")
assert.Assert(t, strings.Contains(string(resp.outputBytes), "github.com/apache/yunikorn-core/pkg/webservice.getStackInfo"), "Stack trace should be present in the response")
}

func TestValidateConf(t *testing.T) {
confTests := []struct {
content string
Expand Down Expand Up @@ -1332,23 +1344,17 @@ func TestGetPartitionNodes(t *testing.T) {
}

if node.NodeID == node1ID {
assert.Equal(t, node.NodeID, node1ID)
assert.Equal(t, "alloc-1", node.Allocations[0].AllocationKey)
assert.DeepEqual(t, attributesOfnode1, node.Attributes)
assert.DeepEqual(t, map[string]int64{"memory": 50, "vcore": 30}, node.Utilized)
assertNodeInfo(t, node, node1ID, "alloc-1", attributesOfnode1, map[string]int64{"memory": 50, "vcore": 30})
} else {
assert.Equal(t, node.NodeID, node2ID)
assert.Equal(t, "alloc-2", node.Allocations[0].AllocationKey)
assert.DeepEqual(t, attributesOfnode2, node.Attributes)
assert.DeepEqual(t, map[string]int64{"memory": 30, "vcore": 50}, node.Utilized)
assertNodeInfo(t, node, node2ID, "alloc-2", attributesOfnode2, map[string]int64{"memory": 30, "vcore": 50})
}
}

req, err = createRequest(t, "/ws/v1/partition/default/nodes", map[string]string{"partition": "notexists"})
assert.NilError(t, err, "Get Nodes for PartitionNodes Handler request failed")
resp1 := &MockResponseWriter{}
getPartitionNodes(resp1, req)
assertPartitionNotExists(t, resp1)
resp = &MockResponseWriter{}
getPartitionNodes(resp, req)
assertPartitionNotExists(t, resp)

// test params name missing
req, err = http.NewRequest("GET", "/ws/v1/partition/default/nodes", strings.NewReader(""))
Expand All @@ -1358,17 +1364,42 @@ func TestGetPartitionNodes(t *testing.T) {
assertParamsMissing(t, resp)

// Test specific node
req, err = createRequest(t, "/ws/v1/partition/default/node/node-1", map[string]string{"node": "node-1"})
req, err = createRequest(t, "/ws/v1/partition/default/node/node-1", map[string]string{"partition": "default", "node": "node-1"})
assert.NilError(t, err, "Get Node for PartitionNode Handler request failed")
resp = &MockResponseWriter{}
getPartitionNode(resp, req)
var nodeInfo dao.NodeDAOInfo
err = json.Unmarshal(resp.outputBytes, &nodeInfo)
assert.NilError(t, err, unmarshalError)
assertNodeInfo(t, &nodeInfo, node1ID, "alloc-1", attributesOfnode1, map[string]int64{"memory": 50, "vcore": 30})

// Test node id is missing
req, err = createRequest(t, "/ws/v1/partition/default/node/node-1", map[string]string{"partition": "default", "node": ""})
assert.NilError(t, err, "Get Node for PartitionNode Handler request failed")
resp = &MockResponseWriter{}
getPartitionNode(resp, req)
assertNodeIDNotExists(t, resp)

// Test param missing
req, err = http.NewRequest("GET", "/ws/v1/partition/default/node", strings.NewReader(""))
assert.NilError(t, err, "Get Node for PartitionNode Handler request failed")
resp = &MockResponseWriter{}
getPartitionNode(resp, req)
assertParamsMissing(t, resp)

// Test partition not exist
req, err = createRequest(t, "/ws/v1/partition/notexists/node/node-1", map[string]string{"partition": "notexists"})
assert.NilError(t, err, "Get Nodes for PartitionNode Handler request failed")
resp = &MockResponseWriter{}
getPartitionNodes(resp, req)
assertPartitionNotExists(t, resp)
}

func assertNodeInfo(t *testing.T, node *dao.NodeDAOInfo, expectedID string, expectedAllocationKey string, expectedAttibute map[string]string, expectedUtilized map[string]int64) {
assert.Equal(t, expectedID, node.NodeID)
assert.Equal(t, expectedAllocationKey, node.Allocations[0].AllocationKey)
assert.DeepEqual(t, expectedAttibute, node.Attributes)
assert.DeepEqual(t, expectedUtilized, node.Utilized)
}

// addApp Add app to the given partition and assert the app count, state etc
Expand Down

0 comments on commit ff82d47

Please sign in to comment.