This is an automated email from the ASF dual-hosted git repository.

chia7712 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/yunikorn-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 639901e1 [YUNIKORN-2412] Avoid returning potential nil in handlers.go 
(#798)
639901e1 is described below

commit 639901e1e44fcfbc5ba4801a9553c4e1a482fc1b
Author: Ryan Lo <[email protected]>
AuthorDate: Wed Feb 21 16:35:49 2024 +0800

    [YUNIKORN-2412] Avoid returning potential nil in handlers.go (#798)
    
    Closes: #798
    
    Signed-off-by: Chia-Ping Tsai <[email protected]>
---
 pkg/webservice/handlers.go      | 12 ++++++------
 pkg/webservice/handlers_test.go | 37 ++++++++++++++++++++++++++++++-------
 2 files changed, 36 insertions(+), 13 deletions(-)

diff --git a/pkg/webservice/handlers.go b/pkg/webservice/handlers.go
index 90f11bb8..80feb68f 100644
--- a/pkg/webservice/handlers.go
+++ b/pkg/webservice/handlers.go
@@ -811,7 +811,7 @@ func getApplication(w http.ResponseWriter, r *http.Request) 
{
 }
 
 func getPartitionInfoDAO(lists map[string]*scheduler.PartitionContext) 
[]*dao.PartitionInfo {
-       var result []*dao.PartitionInfo
+       result := make([]*dao.PartitionInfo, 0, len(lists))
 
        for _, partitionContext := range lists {
                partitionInfo := &dao.PartitionInfo{}
@@ -869,7 +869,7 @@ func getAppHistoryDAO(records []*history.MetricsRecord) 
[]*dao.ApplicationHistor
 }
 
 func getPartitionNodesDAO(lists map[string]*scheduler.PartitionContext) 
[]*dao.NodesDAOInfo {
-       var result []*dao.NodesDAOInfo
+       result := make([]*dao.NodesDAOInfo, 0, len(lists))
 
        for _, partition := range lists {
                nodesDao := getNodesDAO(partition.GetNodes())
@@ -900,7 +900,7 @@ func getContainerHistoryDAO(records 
[]*history.MetricsRecord) []*dao.ContainerHi
 }
 
 func getApplicationsDAO(lists map[string]*scheduler.PartitionContext) 
[]*dao.ApplicationDAOInfo {
-       var result []*dao.ApplicationDAOInfo
+       result := make([]*dao.ApplicationDAOInfo, 0, len(lists))
 
        for _, partition := range lists {
                var appList []*objects.Application
@@ -917,7 +917,7 @@ func getApplicationsDAO(lists 
map[string]*scheduler.PartitionContext) []*dao.App
 }
 
 func getPartitionQueuesDAO(lists map[string]*scheduler.PartitionContext) 
[]dao.PartitionQueueDAOInfo {
-       var result []dao.PartitionQueueDAOInfo
+       result := make([]dao.PartitionQueueDAOInfo, 0, len(lists))
 
        for _, partition := range lists {
                result = append(result, partition.GetPartitionQueues())
@@ -927,7 +927,7 @@ func getPartitionQueuesDAO(lists 
map[string]*scheduler.PartitionContext) []dao.P
 }
 
 func getClusterDAO(lists map[string]*scheduler.PartitionContext) 
[]*dao.ClusterDAOInfo {
-       var result []*dao.ClusterDAOInfo
+       result := make([]*dao.ClusterDAOInfo, 0, len(lists))
 
        for _, partition := range lists {
                result = append(result, getClusterJSON(partition))
@@ -937,7 +937,7 @@ func getClusterDAO(lists 
map[string]*scheduler.PartitionContext) []*dao.ClusterD
 }
 
 func getRMBuildInformation(lists map[string]*scheduler.RMInformation) 
[]map[string]string {
-       var result []map[string]string
+       result := make([]map[string]string, 0, len(lists))
 
        for _, rmInfo := range lists {
                result = append(result, rmInfo.RMBuildInformation)
diff --git a/pkg/webservice/handlers_test.go b/pkg/webservice/handlers_test.go
index c598dc4f..462cab80 100644
--- a/pkg/webservice/handlers_test.go
+++ b/pkg/webservice/handlers_test.go
@@ -390,6 +390,7 @@ func TestApplicationHistory(t *testing.T) {
        err = json.Unmarshal(resp.outputBytes, &appHist)
        assert.NilError(t, err, unmarshalError)
        assert.Equal(t, resp.statusCode, 0, "app response should have no 
status")
+       assert.Assert(t, appHist != nil, "appHist should not be nil")
        assert.Equal(t, len(appHist), 0, "empty response must have no records")
 
        // add new history records
@@ -445,6 +446,7 @@ func TestContainerHistory(t *testing.T) {
        err = json.Unmarshal(resp.outputBytes, &contHist)
        assert.NilError(t, err, unmarshalError)
        assert.Equal(t, resp.statusCode, 0, "container response should have no 
status")
+       assert.Check(t, contHist != nil, "contHist should not be nil")
        assert.Equal(t, len(contHist), 0, "empty response must have no records")
 
        // add new history records
@@ -551,9 +553,11 @@ func TestGetClusterUtilJSON(t *testing.T) {
        buildInfoMap["buildVersion"] = "latest"
        buildInfoMap["isPluginVersion"] = "false"
        schedulerContext.SetRMInfo(rmID, buildInfoMap)
+       rmBuildInformationMaps := getRMBuildInformation(nil)
+       assert.Equal(t, 0, len(rmBuildInformationMaps))
        rmInfo := schedulerContext.GetRMInfoMapClone()
        assert.Equal(t, 1, len(rmInfo))
-       rmBuildInformationMaps := getRMBuildInformation(rmInfo)
+       rmBuildInformationMaps = getRMBuildInformation(rmInfo)
        assert.Equal(t, 1, len(rmBuildInformationMaps))
        assert.Equal(t, rmBuildInformationMaps[0]["buildDate"], 
buildInfoMap["buildDate"])
        assert.Equal(t, rmBuildInformationMaps[0]["buildVersion"], 
buildInfoMap["buildVersion"])
@@ -921,6 +925,20 @@ func getNodesUtilByType(t *testing.T, nodesUtilList 
[]*dao.NodesUtilDAOInfo, res
 }
 
 func TestPartitions(t *testing.T) {
+       schedulerContext = &scheduler.ClusterContext{}
+
+       var req *http.Request
+       req, err := http.NewRequest("GET", "/ws/v1/partitions", 
strings.NewReader(""))
+       assert.NilError(t, err, "App Handler request failed")
+
+       resp := &MockResponseWriter{}
+       var partitionInfo []*dao.PartitionInfo
+       getPartitions(resp, req)
+       err = json.Unmarshal(resp.outputBytes, &partitionInfo)
+       assert.NilError(t, err, unmarshalError)
+       assert.Check(t, partitionInfo != nil, "partitionInfo should not be nil")
+       assert.Equal(t, len(partitionInfo), 0)
+
        defaultPartition := setup(t, configMultiPartitions, 2)
        partitionName := defaultPartition.Name
 
@@ -970,17 +988,15 @@ func TestPartitions(t *testing.T) {
        ask1 := objects.NewAllocationAsk("alloc-1", app6.ApplicationID, 
resAlloc1)
        ask2 := objects.NewAllocationAsk("alloc-2", app3.ApplicationID, 
resAlloc2)
        allocs := []*objects.Allocation{objects.NewAllocation(node1ID, ask1)}
-       err := defaultPartition.AddNode(node1, allocs)
+       err = defaultPartition.AddNode(node1, allocs)
        assert.NilError(t, err, "add node to partition should not have failed")
        allocs = []*objects.Allocation{objects.NewAllocation(node2ID, ask2)}
        err = defaultPartition.AddNode(node2, allocs)
        assert.NilError(t, err, "add node to partition should not have failed")
 
-       var req *http.Request
        req, err = http.NewRequest("GET", "/ws/v1/partitions", 
strings.NewReader(""))
        assert.NilError(t, err, "App Handler request failed")
-       resp := &MockResponseWriter{}
-       var partitionInfo []*dao.PartitionInfo
+       resp = &MockResponseWriter{}
        getPartitions(resp, req)
        err = json.Unmarshal(resp.outputBytes, &partitionInfo)
        assert.NilError(t, err, unmarshalError)
@@ -1148,13 +1164,20 @@ func TestGetPartitionQueuesHandler(t *testing.T) {
 }
 
 func TestGetClusterInfo(t *testing.T) {
-       setup(t, configTwoLevelQueues, 2)
-
+       schedulerContext = &scheduler.ClusterContext{}
        resp := &MockResponseWriter{}
        getClusterInfo(resp, nil)
        var data []*dao.ClusterDAOInfo
        err := json.Unmarshal(resp.outputBytes, &data)
        assert.NilError(t, err)
+       assert.Equal(t, 0, len(data))
+
+       setup(t, configTwoLevelQueues, 2)
+
+       resp = &MockResponseWriter{}
+       getClusterInfo(resp, nil)
+       err = json.Unmarshal(resp.outputBytes, &data)
+       assert.NilError(t, err)
        assert.Equal(t, 2, len(data))
 
        cs := make(map[string]*dao.ClusterDAOInfo, 2)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to