Skip to content

Commit

Permalink
[YUNIKORN-2590] Handler tests should check for nil request on create (a…
Browse files Browse the repository at this point in the history
…pache#865)

Closes: apache#865

Signed-off-by: Chia-Ping Tsai <[email protected]>
  • Loading branch information
ryankert01 authored and chia7712 committed May 11, 2024
1 parent 40e0dbc commit 677bf90
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/webservice/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ func TestGetPartitionQueuesHandler(t *testing.T) {

var req *http.Request
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queues", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID}}))
assert.NilError(t, err, "Get Queues for PartitionQueues Handler request failed")
resp := &MockResponseWriter{}
Expand Down Expand Up @@ -1149,6 +1150,7 @@ func TestGetPartitionQueuesHandler(t *testing.T) {

// test partition not exists
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queues", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: "notexists"}}))
assert.NilError(t, err)
resp = &MockResponseWriter{}
Expand All @@ -1165,6 +1167,7 @@ func TestGetPartitionQueuesHandler(t *testing.T) {
// test specific queue
var partitionQueueDao1 dao.PartitionQueueDAOInfo
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.a", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: "default"}, httprouter.Param{Key: "queue", Value: "root.a"}}))
assert.NilError(t, err)
resp = &MockResponseWriter{}
Expand All @@ -1179,6 +1182,7 @@ func TestGetPartitionQueuesHandler(t *testing.T) {
// test hierarchy queue
var partitionQueueDao2 dao.PartitionQueueDAOInfo
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.a?subtree", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: "default"}, httprouter.Param{Key: "queue", Value: "root.a"}}))
assert.NilError(t, err)
resp = &MockResponseWriter{}
Expand All @@ -1193,6 +1197,7 @@ func TestGetPartitionQueuesHandler(t *testing.T) {

// test partition not exists
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.a", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: "notexists"}}))
assert.NilError(t, err)
resp = &MockResponseWriter{}
Expand All @@ -1208,6 +1213,7 @@ func TestGetPartitionQueuesHandler(t *testing.T) {

// test invalid queue name
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.a", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: "default"}, httprouter.Param{Key: "queue", Value: "root.notexists@"}}))
assert.NilError(t, err)
resp = &MockResponseWriter{}
Expand All @@ -1216,6 +1222,7 @@ func TestGetPartitionQueuesHandler(t *testing.T) {

// test queue is not exists
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.a", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: "default"}, httprouter.Param{Key: "queue", Value: "notexists"}}))
assert.NilError(t, err)
resp = &MockResponseWriter{}
Expand Down Expand Up @@ -1283,6 +1290,7 @@ func TestGetPartitionNodes(t *testing.T) {

var req *http.Request
req, err = http.NewRequest("GET", "/ws/v1/partition/default/nodes", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID}}))
assert.NilError(t, err, "Get Nodes for PartitionNodes Handler request failed")
resp := &MockResponseWriter{}
Expand Down Expand Up @@ -1313,6 +1321,7 @@ func TestGetPartitionNodes(t *testing.T) {
}

req, err = http.NewRequest("GET", "/ws/v1/partition/default/nodes", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: "notexists"}}))
assert.NilError(t, err, "Get Nodes for PartitionNodes Handler request failed")
resp1 := &MockResponseWriter{}
Expand All @@ -1328,13 +1337,15 @@ func TestGetPartitionNodes(t *testing.T) {

// Test specific node
req, err = http.NewRequest("GET", "/ws/v1/partition/default/node/node-1", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "node", Value: "node-1"}}))
assert.NilError(t, err, "Get Node for PartitionNode Handler request failed")
resp = &MockResponseWriter{}
getPartitionNode(resp, req)

// Test node id is missing
req, err = http.NewRequest("GET", "/ws/v1/partition/default/node/node-1", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{httprouter.Param{Key: "partition", Value: "default"}, httprouter.Param{Key: "node", Value: ""}}))
assert.NilError(t, err, "Get Node for PartitionNode Handler request failed")
resp = &MockResponseWriter{}
Expand Down Expand Up @@ -1394,6 +1405,7 @@ func TestGetQueueApplicationsHandler(t *testing.T) {

var req *http.Request
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/applications", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID},
httprouter.Param{Key: "queue", Value: "root.default"},
Expand Down Expand Up @@ -1423,6 +1435,7 @@ func TestGetQueueApplicationsHandler(t *testing.T) {
// test nonexistent partition
var req1 *http.Request
req1, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/applications", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req1 = req1.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: "notexists"},
httprouter.Param{Key: "queue", Value: "root.default"},
Expand All @@ -1435,6 +1448,7 @@ func TestGetQueueApplicationsHandler(t *testing.T) {
// test nonexistent queue
var req2 *http.Request
req2, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/applications", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req2 = req2.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID},
httprouter.Param{Key: "queue", Value: "notexists"},
Expand All @@ -1447,6 +1461,7 @@ func TestGetQueueApplicationsHandler(t *testing.T) {
// test queue without applications
var req3 *http.Request
req3, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.noapps/applications", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req3 = req3.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID},
httprouter.Param{Key: "queue", Value: "root.noapps"},
Expand All @@ -1469,6 +1484,7 @@ func TestGetQueueApplicationsHandler(t *testing.T) {

func checkLegalGetAppsRequest(t *testing.T, url string, params httprouter.Params, expected []*dao.ApplicationDAOInfo) {
req, err := http.NewRequest("GET", url, strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, params))
assert.NilError(t, err)
resp := &MockResponseWriter{}
Expand All @@ -1481,6 +1497,7 @@ func checkLegalGetAppsRequest(t *testing.T, url string, params httprouter.Params

func checkIllegalGetAppsRequest(t *testing.T, url string, params httprouter.Params, assertFunc func(t *testing.T, resp *MockResponseWriter)) {
req, err := http.NewRequest("GET", url, strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, params))
assert.NilError(t, err)
resp := &MockResponseWriter{}
Expand Down Expand Up @@ -1570,6 +1587,7 @@ func TestGetApplicationHandler(t *testing.T) {

var req *http.Request
req, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/application/app-1", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID},
httprouter.Param{Key: "queue", Value: "root.default"},
Expand All @@ -1591,6 +1609,7 @@ func TestGetApplicationHandler(t *testing.T) {
// test nonexistent partition
var req1 *http.Request
req1, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/application/app-1", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req1 = req1.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: "notexists"},
httprouter.Param{Key: "queue", Value: "root.default"},
Expand All @@ -1604,6 +1623,7 @@ func TestGetApplicationHandler(t *testing.T) {
// test nonexistent queue
var req2 *http.Request
req2, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/application/app-1", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req2 = req2.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID},
httprouter.Param{Key: "queue", Value: "notexists"},
Expand All @@ -1617,6 +1637,7 @@ func TestGetApplicationHandler(t *testing.T) {
// test nonexistent application
var req3 *http.Request
req3, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.noapps/application/app-1", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req3 = req3.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID},
httprouter.Param{Key: "queue", Value: "root.noapps"},
Expand All @@ -1630,6 +1651,7 @@ func TestGetApplicationHandler(t *testing.T) {
// test without queue
var req4 *http.Request
req4, err = http.NewRequest("GET", "/ws/v1/partition/default/application/app-1", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req4 = req4.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID},
httprouter.Param{Key: "application", Value: "app-1"},
Expand All @@ -1644,6 +1666,7 @@ func TestGetApplicationHandler(t *testing.T) {
// test invalid queue name
var req5 *http.Request
req5, err = http.NewRequest("GET", "/ws/v1/partition/default/queue/root.default/application/app-1", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req5 = req5.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "partition", Value: partitionNameWithoutClusterID},
httprouter.Param{Key: "queue", Value: "root.test.test123@"},
Expand Down Expand Up @@ -1836,6 +1859,7 @@ func TestSpecificUserAndGroupResourceUsage(t *testing.T) {
prepareUserAndGroupContext(t, groupsLimitsConfig)
// Test user name is missing
req, err := http.NewRequest("GET", "/ws/v1/partition/default/usage/user/", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "group", Value: "testgroup"},
}))
Expand All @@ -1846,6 +1870,7 @@ func TestSpecificUserAndGroupResourceUsage(t *testing.T) {

// Test group name is missing
req, err = http.NewRequest("GET", "/ws/v1/partition/default/usage/group/", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "user", Value: "testuser"},
httprouter.Param{Key: "group", Value: ""},
Expand All @@ -1857,6 +1882,7 @@ func TestSpecificUserAndGroupResourceUsage(t *testing.T) {

// Test existed user query
req, err = http.NewRequest("GET", "/ws/v1/partition/default/usage/user/", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "user", Value: "testuser"},
httprouter.Param{Key: "group", Value: "testgroup"},
Expand Down Expand Up @@ -1884,6 +1910,7 @@ func TestSpecificUserAndGroupResourceUsage(t *testing.T) {

// Test non-existing user query
req, err = http.NewRequest("GET", "/ws/v1/partition/default/usage/user/", strings.NewReader(""))
assert.NilError(t, err, "HTTP request create failed")
req = req.WithContext(context.WithValue(req.Context(), httprouter.ParamsKey, httprouter.Params{
httprouter.Param{Key: "user", Value: "testNonExistingUser"},
httprouter.Param{Key: "group", Value: "testgroup"},
Expand Down Expand Up @@ -2133,6 +2160,7 @@ func TestGetStream_Count(t *testing.T) {
getStream(resp, req)
output := make([]byte, 256)
n, err := resp.Body.Read(output)
assert.NilError(t, err)
lines := strings.Split(string(output[:n]), "\n")
assertInstanceUUID(t, lines[0])

Expand Down

0 comments on commit 677bf90

Please sign in to comment.