Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YUNIKORN-2797] Increase handlers.go test coverage #970

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
ryankert01 marked this conversation as resolved.
Show resolved Hide resolved

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
ryankert01 marked this conversation as resolved.
Show resolved Hide resolved
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)
}
ryankert01 marked this conversation as resolved.
Show resolved Hide resolved

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
Loading