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

juzhiyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 49c3a92  fix: retries field to support zero value (#2298)
49c3a92 is described below

commit 49c3a923bbd2133fbb25627dc9d96530c206699e
Author: Chunxiang Yan (Chauncey) <[email protected]>
AuthorDate: Tue Feb 1 18:00:16 2022 +0800

    fix: retries field to support zero value (#2298)
---
 api/internal/core/entity/entity.go         |  2 +-
 api/internal/core/entity/format_test.go    | 25 +++++++++++
 api/test/e2enew/upstream/upstream_retry.go | 69 ++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 1 deletion(-)

diff --git a/api/internal/core/entity/entity.go 
b/api/internal/core/entity/entity.go
index 7472c14..4f06770 100644
--- a/api/internal/core/entity/entity.go
+++ b/api/internal/core/entity/entity.go
@@ -163,7 +163,7 @@ type UpstreamKeepalivePool struct {
 
 type UpstreamDef struct {
        Nodes         interface{}            `json:"nodes,omitempty"`
-       Retries       int                    `json:"retries,omitempty"`
+       Retries       *int                   `json:"retries,omitempty"`
        Timeout       *Timeout               `json:"timeout,omitempty"`
        Type          string                 `json:"type,omitempty"`
        Checks        interface{}            `json:"checks,omitempty"`
diff --git a/api/internal/core/entity/format_test.go 
b/api/internal/core/entity/format_test.go
index ec285a0..56e2458 100644
--- a/api/internal/core/entity/format_test.go
+++ b/api/internal/core/entity/format_test.go
@@ -179,3 +179,28 @@ func TestNodesFormat_no_nodes(t *testing.T) {
        jsonStr := string(res)
        assert.Contains(t, jsonStr, `null`)
 }
+
+func TestUpstream_nil_and_zero_retries(t *testing.T) {
+       ud0 := UpstreamDef{}
+       // Unmarshal from zero value
+       err := json.Unmarshal([]byte(`{"retries":0}`), &ud0)
+       assert.Nil(t, err)
+       assert.Equal(t, *ud0.Retries, 0)
+
+       // Marshal with zero value
+       marshaled, err := json.Marshal(ud0)
+       assert.Nil(t, err)
+       assert.Contains(t, string(marshaled), `"retries":0`)
+
+       udNull := UpstreamDef{}
+
+       // Unmarshal from null value
+       err = json.Unmarshal([]byte(`{}`), &udNull)
+       assert.Nil(t, err)
+       assert.Nil(t, udNull.Retries)
+
+       // Marshal to null value
+       marshaledNull, err := json.Marshal(udNull)
+       assert.Nil(t, err)
+       assert.Equal(t, string(marshaledNull), `{}`)
+}
diff --git a/api/test/e2enew/upstream/upstream_retry.go 
b/api/test/e2enew/upstream/upstream_retry.go
index adb81bb..9805624 100644
--- a/api/test/e2enew/upstream/upstream_retry.go
+++ b/api/test/e2enew/upstream/upstream_retry.go
@@ -60,4 +60,73 @@ var _ = ginkgo.Describe("Upstream keepalive pool", func() {
                        ExpectStatus: http.StatusOK,
                })
        })
+       ginkgo.It("zero retry field", func() {
+               createUpstreamBody := make(map[string]interface{})
+               createUpstreamBody["nodes"] = []map[string]interface{}{
+                       {
+                               "host":   base.UpstreamIp,
+                               "port":   1980,
+                               "weight": 1,
+                       }}
+               createUpstreamBody["type"] = "roundrobin"
+               createUpstreamBody["retries"] = 0
+               createUpstreamBody["retry_timeout"] = 5.5
+               _createUpstreamBody, err := json.Marshal(createUpstreamBody)
+               gomega.Expect(err).To(gomega.BeNil())
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodPut,
+                       Path:         "/apisix/admin/upstreams/zero-retry",
+                       Body:         string(_createUpstreamBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+               })
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/apisix/admin/upstreams/zero-retry",
+                       Body:         string(_createUpstreamBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+                       ExpectBody:   `"retries":0`,
+               })
+       })
+       ginkgo.It("nil retry field", func() {
+               createUpstreamBody := make(map[string]interface{})
+               createUpstreamBody["nodes"] = []map[string]interface{}{
+                       {
+                               "host":   base.UpstreamIp,
+                               "port":   1980,
+                               "weight": 1,
+                       }}
+               createUpstreamBody["type"] = "roundrobin"
+               _createUpstreamBody, err := json.Marshal(createUpstreamBody)
+               gomega.Expect(err).To(gomega.BeNil())
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodPut,
+                       Path:         "/apisix/admin/upstreams/zero-retry",
+                       Body:         string(_createUpstreamBody),
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+               })
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodGet,
+                       Path:         "/apisix/admin/upstreams/zero-retry",
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+                       UnexpectBody: `"retries"`,
+               })
+       })
+       ginkgo.It("delete upstream", func() {
+               base.RunTestCase(base.HttpTestCase{
+                       Object:       base.ManagerApiExpect(),
+                       Method:       http.MethodDelete,
+                       Path:         "/apisix/admin/upstreams/zero-retry",
+                       Headers:      map[string]string{"Authorization": 
base.GetToken()},
+                       ExpectStatus: http.StatusOK,
+               })
+       })
+
 })

Reply via email to