Skip to content

Commit

Permalink
Add unit-test for ykclient.GetApplication().
Browse files Browse the repository at this point in the history
Verify that GetApplication() uses default partition and queue names
when not given as arguments, per its comment description.
  • Loading branch information
richscott committed Jun 5, 2024
1 parent 7927a9d commit c3a3e73
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ require (
github.com/knadh/koanf/providers/file v0.1.0
github.com/knadh/koanf/v2 v2.1.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -24,6 +26,7 @@ require (
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
Expand Down
122 changes: 122 additions & 0 deletions internal/ykclient/api_handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package ykclient

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"strconv"
"testing"

"github.com/apache/yunikorn-core/pkg/webservice/dao"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_GetApplication(t *testing.T) {
tests := []struct {
name string
partName string
queueName string
appId string
expected dao.ApplicationDAOInfo
}{
{
name: "All Fields Specified",
partName: "specialPartition",
queueName: "myQueue",
appId: "app1",
expected: dao.ApplicationDAOInfo{
ApplicationID: "app1",
Partition: "specialPartition",
QueueName: "myQueue",
},
},
{
name: "Empty Partition Name",
partName: "",
queueName: "default",
appId: "app1",
expected: dao.ApplicationDAOInfo{
ApplicationID: "app1",
Partition: "default",
QueueName: "default",
},
},
{
name: "Empty Queue Name",
partName: "myPartition",
queueName: "",
appId: "app1",
expected: dao.ApplicationDAOInfo{
ApplicationID: "app1",
Partition: "myPartition",
QueueName: "myPartition.default",
},
},
}

partQueueAppRe := regexp.MustCompile(`/ws/v1/partition/(\w+)/queue/(\w+)/application/(\w+)`)
partAppRe := regexp.MustCompile(`/ws/v1/partition/(\w+)/application/(\w+)`)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
responseApp := dao.ApplicationDAOInfo{}

matches := partQueueAppRe.FindStringSubmatch(r.URL.Path)
if matches != nil {
responseApp = dao.ApplicationDAOInfo{
Partition: matches[1],
QueueName: matches[2],
ApplicationID: matches[3],
}
} else {
matches = partAppRe.FindStringSubmatch(r.URL.Path)
if matches == nil {
http.Error(w, errors.New("invalid request path").Error(), http.StatusNotFound)
return
}

responseApp = dao.ApplicationDAOInfo{
Partition: matches[1],
QueueName: fmt.Sprintf("%s.%s", matches[1], "default"),
ApplicationID: matches[2],
}

}
err := json.NewEncoder(w).Encode(&responseApp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}

}))

defer ts.Close()
serverURL, err := url.Parse(ts.URL)
require.NoError(t, err)

portNum, err := strconv.Atoi(serverURL.Port())
require.NoError(t, err)

ykclient := Client{
httpProto: serverURL.Scheme,
ykHost: serverURL.Hostname(),
ykPort: portNum,
repo: nil,
appMap: map[string]*dao.ApplicationDAOInfo{},
}

app, err := ykclient.GetApplication(context.Background(), tt.partName, tt.queueName, tt.appId)
require.NoError(t, err)
assert.NotNil(t, app)
assert.Equal(t, tt.expected.Partition, app.Partition)
assert.Equal(t, tt.expected.QueueName, app.QueueName)
assert.Equal(t, tt.expected.ApplicationID, app.ApplicationID)
})
}
}

0 comments on commit c3a3e73

Please sign in to comment.