liuxiran commented on a change in pull request #1452: URL: https://github.com/apache/apisix-dashboard/pull/1452#discussion_r571816080
##########
File path: api/internal/handler/upstream/upstream_test.go
##########
@@ -19,285 +19,1719 @@ package upstream
import (
"encoding/json"
- "strings"
+ "errors"
+ "fmt"
+ "net/http"
"testing"
- "time"
"github.com/shiningrush/droplet"
+ "github.com/shiningrush/droplet/data"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/mock"
- "github.com/apisix/manager-api/internal/conf"
"github.com/apisix/manager-api/internal/core/entity"
- "github.com/apisix/manager-api/internal/core/storage"
"github.com/apisix/manager-api/internal/core/store"
+ "github.com/apisix/manager-api/internal/handler"
+ "github.com/apisix/manager-api/internal/utils/consts"
)
-var upstreamHandler *Handler
+func TestUpstream_Get(t *testing.T) {
+ tests := []struct {
+ caseDesc string
+ giveInput *GetInput
+ giveRet *entity.Upstream
+ giveErr error
+ wantErr error
+ wantGetKey string
+ wantRet interface{}
+ }{
+ {
+ caseDesc: "upstream: get success",
+ giveInput: &GetInput{ID: "u1"},
+ wantGetKey: "u1",
+ giveRet: &entity.Upstream{
+ BaseInfo: entity.BaseInfo{
+ ID: "u1",
+ },
+ UpstreamDef: entity.UpstreamDef{
+ Name: "upstream1",
+ Timeout: map[string]interface{}{
+ "connect": 15,
+ "send": 15,
+ "read": 15,
+ },
+ Checks: map[string]interface{}{
+ "active":
map[string]interface{}{
+ "timeout": float64(5),
+ "http_path": "/status",
+ "host": "foo.com",
+ "healthy":
map[string]interface{}{
+ "interval": 2,
+ "successes": 1,
+ },
+ "unhealthy":
map[string]interface{}{
+ "interval":
1,
+
"http_failures": 2,
+ },
+ "req_headers":
[]interface{}{"User-Agent: curl/7.29.0"},
+ },
+ "passive":
map[string]interface{}{
+ "healthy":
map[string]interface{}{
+
"http_statuses": []interface{}{float64(200), float64(201)},
+ "successes":
float64(3),
+ },
+ "unhealthy":
map[string]interface{}{
+
"http_statuses": []interface{}{float64(500)},
+
"http_failures": float64(3),
+ "tcp_failures":
float64(3),
+ },
+ },
+ },
+ Key: "server_addr",
+ Nodes: []map[string]interface{}{
+ {
+ "host":
"39.97.63.215",
+ "port": float64(80),
+ "weight": float64(1),
+ },
+ },
+ },
+ },
+ wantRet: &entity.Upstream{
+ BaseInfo: entity.BaseInfo{
+ ID: "u1",
+ },
+ UpstreamDef: entity.UpstreamDef{
+ Name: "upstream1",
+ Timeout: map[string]interface{}{
+ "connect": 15,
+ "send": 15,
+ "read": 15,
+ },
+ Checks: map[string]interface{}{
+ "active":
map[string]interface{}{
+ "timeout": float64(5),
+ "http_path": "/status",
+ "host": "foo.com",
+ "healthy":
map[string]interface{}{
+ "interval": 2,
+ "successes": 1,
+ },
+ "unhealthy":
map[string]interface{}{
+ "interval":
1,
+
"http_failures": 2,
+ },
+ "req_headers":
[]interface{}{"User-Agent: curl/7.29.0"},
+ },
+ "passive":
map[string]interface{}{
+ "healthy":
map[string]interface{}{
+
"http_statuses": []interface{}{float64(200), float64(201)},
+ "successes":
float64(3),
+ },
+ "unhealthy":
map[string]interface{}{
+
"http_statuses": []interface{}{float64(500)},
+
"http_failures": float64(3),
+ "tcp_failures":
float64(3),
+ },
+ },
+ },
+ Key: "server_addr",
+ Nodes: []map[string]interface{}{
+ {
+ "host":
"39.97.63.215",
+ "port": float64(80),
+ "weight": float64(1),
+ },
+ },
+ },
+ },
+ },
+ {
+ caseDesc: "store get failed",
+ giveInput: &GetInput{ID: "failed_key"},
+ wantGetKey: "failed_key",
+ giveErr: fmt.Errorf("get failed"),
+ wantErr: fmt.Errorf("get failed"),
+ wantRet: &data.SpecCodeResponse{
+ StatusCode: http.StatusInternalServerError,
+ },
+ },
+ }
-func TestUpstream(t *testing.T) {
- // init
- err := storage.InitETCDClient(conf.ETCDConfig)
- assert.Nil(t, err)
- err = store.InitStores()
- assert.Nil(t, err)
+ for _, tc := range tests {
+ t.Run(tc.caseDesc, func(t *testing.T) {
+ getCalled := true
+ upstreamStore := &store.MockInterface{}
+ upstreamStore.On("Get", mock.Anything,
mock.Anything).Run(func(args mock.Arguments) {
+ getCalled = true
+ assert.Equal(t, tc.wantGetKey, args.Get(0))
+ }).Return(tc.giveRet, tc.giveErr)
+
+ h := Handler{upstreamStore: upstreamStore}
+ ctx := droplet.NewContext()
+ ctx.SetInput(tc.giveInput)
+ ret, err := h.Get(ctx)
+ assert.True(t, getCalled)
+ assert.Equal(t, tc.wantRet, ret)
+ assert.Equal(t, tc.wantErr, err)
+ })
+ }
+}
+
+func TestUpstreams_List(t *testing.T) {
+ mockData := []*entity.Upstream{
Review comment:
I found that `LIst` support name as filter[1], it would be better add a
case to test it ^_^
reference:
[1]:
https://github.com/apache/apisix-dashboard/blob/a87028a4098afd2fb09cf1724028cff4da568b42/api/internal/handler/upstream/upstream.go#L91
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
