Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix #414

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,17 +1096,15 @@ func TestRuntimeHandler(t *testing.T) {
}

func TestClusterRefersFilterHandler(t *testing.T) {
completeConfig := cluster.RefersFilterConfigList{
{
Group: "g1",
Service: "s1",
Mode: cluster.FilterModeExclude,
Rule: "127.0",
},
}
completeConfigBody := `[{"service":"com.weibo.api.test1","mode":"exclude","rule":"127.93.0.0/16"},{"service":"com.weibo.api.test2","mode":"exclude","rule":"127.93.0.0/16"}]`
var completeConfig cluster.RefersFilterConfigList
_ = json.Unmarshal([]byte(completeConfigBody), &completeConfig)
completeConfigExpect, _ := json.Marshal(completeConfig)
emptyConfig := cluster.RefersFilterConfigList{}
emptyConfigExpect, _ := json.Marshal(emptyConfig)
rewriteConfigBody := `[{"mode":"exclude","rule":"127.93.0.0/16"},{"service":"com.weibo.api.test2","mode":"exclude","rule":"127.93.0.0/16"}]`
var rewriteConfig cluster.RefersFilterConfigList
_ = json.Unmarshal([]byte(rewriteConfigBody), &rewriteConfig)
rewriteConfigExpect, _ := json.Marshal(rewriteConfig)
emptyConfigExpect := `[]`
cases := []struct {
desc string
request *http.Request
Expand Down Expand Up @@ -1164,7 +1162,7 @@ func TestClusterRefersFilterHandler(t *testing.T) {
request: func() *http.Request {
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
_ = writer.WriteField("config", string(completeConfigExpect))
_ = writer.WriteField("config", completeConfigBody)
_ = writer.Close()
req, _ := http.NewRequest("POST", "http://127.0.0.1:8002/refers/filter/set", payload)
req.Header.Set("Content-Type", writer.FormDataContentType())
Expand Down Expand Up @@ -1192,6 +1190,39 @@ func TestClusterRefersFilterHandler(t *testing.T) {
assert.Equal(t, fmt.Sprintf(`{"result":"ok","data":%s}`, completeConfigExpect), string(bodyBytes))
},
},
{
desc: "rewrite config",
request: func() *http.Request {
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
_ = writer.WriteField("config", string(rewriteConfigBody))
_ = writer.Close()
req, _ := http.NewRequest("POST", "http://127.0.0.1:8002/refers/filter/set", payload)
req.Header.Set("Content-Type", writer.FormDataContentType())
return req
}(),
assertFunc: func(t *testing.T, resp *http.Response, respErr error) {
assert.Nil(t, respErr)
assert.Equal(t, http.StatusOK, resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, `{"result":"ok","data":"ok"}`, string(bodyBytes))
},
},
{
desc: "get rewrite config",
request: func() *http.Request {
req, _ := http.NewRequest("POST", "http://127.0.0.1:8002/refers/filter/get", nil)
return req
}(),
assertFunc: func(t *testing.T, resp *http.Response, respErr error) {
assert.Nil(t, respErr)
assert.Equal(t, http.StatusOK, resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
assert.Equal(t, fmt.Sprintf(`{"result":"ok","data":%s}`, rewriteConfigExpect), string(bodyBytes))
},
},
}

for _, c := range cases {
Expand Down
4 changes: 3 additions & 1 deletion manageHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,12 +942,14 @@ func (h *RefersFilterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
JSONError(w, "empty config")
return
}
err := json.Unmarshal([]byte(configContent), &h.config)
var newConfig cluster.RefersFilterConfigList
err := json.Unmarshal([]byte(configContent), &newConfig)
if err != nil {
vlog.Errorf("update refers filter fail, config: %s", configContent)
JSONError(w, "parse config failed")
return
}
h.config = newConfig
err = h.config.Verify()
if err != nil {
vlog.Errorf("update refers filter fail, config: %s", configContent)
Expand Down
Loading