Skip to content

Commit

Permalink
fix: use put (instead of patch) as the method when updating resources. (
Browse files Browse the repository at this point in the history
  • Loading branch information
tokers authored Apr 9, 2021
1 parent c9f04ca commit 438bb0f
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/apisix/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (s *cluster) createResource(ctx context.Context, url string, body io.Reader
}

func (s *cluster) updateResource(ctx context.Context, url string, body io.Reader) (*updateResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, body)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, body)
if err != nil {
return nil, err
}
Expand Down
22 changes: 12 additions & 10 deletions pkg/apisix/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,23 @@ func (r *routeClient) Update(ctx context.Context, obj *v1.Route) (*v1.Route, err
return nil, err
}
body, err := json.Marshal(routeReqBody{
Priority: obj.Priority,
Desc: obj.Name,
Name: obj.Name,
Host: obj.Host,
Hosts: obj.Hosts,
URI: obj.Path,
ServiceId: obj.ServiceId,
Plugins: obj.Plugins,
Vars: obj.Vars,
Priority: obj.Priority,
Desc: obj.Name,
Name: obj.Name,
URI: obj.Path,
Host: obj.Host,
Hosts: obj.Hosts,
ServiceId: obj.ServiceId,
UpstreamId: obj.UpstreamId,
Uris: obj.Uris,
Plugins: obj.Plugins,
Vars: obj.Vars,
})
if err != nil {
return nil, err
}
url := r.url + "/" + obj.ID
log.Debugw("updating route", zap.ByteString("body", body), zap.String("url", r.url))
log.Debugw("updating route", zap.ByteString("body", body), zap.String("url", url))
resp, err := r.cluster.updateResource(ctx, url, bytes.NewReader(body))
if err != nil {
return nil, err
Expand Down
16 changes: 10 additions & 6 deletions pkg/apisix/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,16 @@ func (u *upstreamClient) Update(ctx context.Context, obj *v1.Upstream) (*v1.Upst
})
}
body, err := json.Marshal(upstreamReqBody{
LBType: obj.Type,
HashOn: obj.HashOn,
Key: obj.Key,
Nodes: nodes,
Desc: obj.Name,
Name: obj.Name,
LBType: obj.Type,
HashOn: obj.HashOn,
Key: obj.Key,
Nodes: nodes,
Desc: obj.Name,
Name: obj.Name,
Scheme: obj.Scheme,
Checks: obj.Checks,
Retries: obj.Retries,
Timeout: obj.Timeout,
})
if err != nil {
return nil, err
Expand Down
76 changes: 76 additions & 0 deletions test/e2e/plugins/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,80 @@ spec:
resp.Header("Access-Control-Max-Age").Empty()
resp.Body().Contains("origin")
})
ginkgo.It("enable plugin and then delete it", func() {
backendSvc, backendPorts := s.DefaultHTTPBackend()
ar := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2alpha1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
plugins:
- name: cors
enable: true
`, backendSvc, backendPorts[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ar))
err := s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
err = s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")

resp := s.NewAPISIXClient().GET("/ip").WithHeader("Host", "httpbin.org").Expect()
resp.Status(http.StatusOK)

resp.Header("Access-Control-Allow-Origin").Equal("*")
resp.Header("Access-Control-Allow-Methods").Equal("*")
resp.Header("Access-Control-Allow-Headers").Equal("*")
resp.Header("Access-Control-Expose-Headers").Equal("*")
resp.Header("Access-Control-Max-Age").Equal("5")
resp.Body().Contains("origin")

ar = fmt.Sprintf(`
apiVersion: apisix.apache.org/v2alpha1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
`, backendSvc, backendPorts[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ar))
err = s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
err = s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")

resp = s.NewAPISIXClient().GET("/ip").WithHeader("Host", "httpbin.org").Expect()
resp.Status(http.StatusOK)

// httpbin sets this header by itself.
//resp.Header("Access-Control-Allow-Origin").Empty()
resp.Header("Access-Control-Allow-Methods").Empty()
resp.Header("Access-Control-Allow-Headers").Empty()
resp.Header("Access-Control-Expose-Headers").Empty()
resp.Header("Access-Control-Max-Age").Empty()
resp.Body().Contains("origin")
})
})
111 changes: 111 additions & 0 deletions test/e2e/plugins/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,115 @@ spec:
resp.Status(http.StatusOK)
resp.Body().Equal("my custom body")
})

ginkgo.It("disable plugin", func() {
backendSvc, backendPorts := s.DefaultHTTPBackend()
ar := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2alpha1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
plugins:
- name: echo
enable: false
config:
body: "my custom body"
`, backendSvc, backendPorts[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ar))

err := s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
err = s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")

resp := s.NewAPISIXClient().GET("/ip").WithHeader("Host", "httpbin.org").Expect()
resp.Status(http.StatusOK)
resp.Body().Contains("origin")
resp.Body().NotContains("my custom body")
})

ginkgo.It("enable plugin and then delete it", func() {
backendSvc, backendPorts := s.DefaultHTTPBackend()
ar := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2alpha1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
plugins:
- name: echo
enable: true
config:
body: "my custom body"
`, backendSvc, backendPorts[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ar))

err := s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
err = s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")

resp := s.NewAPISIXClient().GET("/ip").WithHeader("Host", "httpbin.org").Expect()
resp.Status(http.StatusOK)
resp.Body().Equal("my custom body")

ar = fmt.Sprintf(`
apiVersion: apisix.apache.org/v2alpha1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
`, backendSvc, backendPorts[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ar))

err = s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
err = s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")

resp = s.NewAPISIXClient().GET("/ip").WithHeader("Host", "httpbin.org").Expect()
resp.Status(http.StatusOK)
resp.Body().NotContains("my custom body")
resp.Body().Contains("origin")
})
})
68 changes: 68 additions & 0 deletions test/e2e/plugins/fault_injection.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,72 @@ spec:
resp = s.NewAPISIXClient().GET("/ip").WithQuery("name", "bob").WithHeader("Host", "httpbin.org").WithHeader("X-Foo", "bar").Expect()
resp.Status(http.StatusOK)
})
ginkgo.It("enable plugin and then delete it", func() {
backendSvc, backendPorts := s.DefaultHTTPBackend()

ar := fmt.Sprintf(`
apiVersion: apisix.apache.org/v2alpha1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
plugins:
- name: fault-injection
enable: true
config:
abort:
http_status: 500
`, backendSvc, backendPorts[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ar))

err := s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
err = s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")

resp := s.NewAPISIXClient().GET("/ip").WithQuery("name", "bob").WithHeader("Host", "httpbin.org").Expect()
resp.Status(http.StatusInternalServerError)

ar = fmt.Sprintf(`
apiVersion: apisix.apache.org/v2alpha1
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- httpbin.org
paths:
- /ip
backends:
- serviceName: %s
servicePort: %d
weight: 10
`, backendSvc, backendPorts[0])

assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ar))

err = s.EnsureNumApisixUpstreamsCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of upstreams")
err = s.EnsureNumApisixRoutesCreated(1)
assert.Nil(ginkgo.GinkgoT(), err, "Checking number of routes")

resp = s.NewAPISIXClient().GET("/ip").WithQuery("name", "bob").WithHeader("Host", "httpbin.org").Expect()
resp.Status(http.StatusOK)
resp.Body().Contains("origin")
})
})
Loading

0 comments on commit 438bb0f

Please sign in to comment.