-
Notifications
You must be signed in to change notification settings - Fork 37
*: add more unit tests #4017
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
Open
wk989898
wants to merge
16
commits into
pingcap:master
Choose a base branch
from
wk989898:ut-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
*: add more unit tests #4017
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
67ae0c2
init
wk989898 a82e615
.
wk989898 e411d14
.
wk989898 af94308
.
wk989898 85ff9c6
update
wk989898 49b6007
Merge branch 'master' into ut-fix
wk989898 027c30b
fix ut
wk989898 c12f052
update test
wk989898 ef4564c
update
wk989898 9e21902
fmt
wk989898 9cb41a0
update
wk989898 7558840
.
wk989898 35d19ed
update
wk989898 7e77a8d
update test
wk989898 3a67c63
Merge branch 'master' into ut-fix
wk989898 cd2a51e
Merge branch 'master' into ut-fix
wk989898 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright 2021 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package rest | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func restClient(testServer *httptest.Server) (*CDCRESTClient, error) { | ||
| c, err := CDCRESTClientFromConfig(&Config{ | ||
| Host: testServer.URL, | ||
| APIPath: "/api", | ||
| Version: "v1", | ||
| }) | ||
| return c, err | ||
| } | ||
|
|
||
| func TestRestRequestSuccess(t *testing.T) { | ||
| testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
| rw.Header().Set("Content-Type", "application/json") | ||
| rw.WriteHeader(http.StatusOK) | ||
| if r.URL.Path == "/api/v1/test" { | ||
| _, _ = rw.Write([]byte(`{"cdc": "hello world"}`)) | ||
| } | ||
| })) | ||
| defer testServer.Close() | ||
|
|
||
| c, err := restClient(testServer) | ||
| require.Nil(t, err) | ||
| body, err := c.Get().WithPrefix("test").Do(context.Background()).Raw() | ||
| require.Equal(t, `{"cdc": "hello world"}`, string(body)) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestRestRequestFailed(t *testing.T) { | ||
| testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
| rw.WriteHeader(http.StatusNotFound) | ||
| _, _ = rw.Write([]byte(`{ | ||
| "error_msg": "test rest request failed", | ||
| "error_code": "test rest request failed" | ||
| }`)) | ||
| })) | ||
| defer testServer.Close() | ||
|
|
||
| c, err := restClient(testServer) | ||
| require.Nil(t, err) | ||
| err = c.Get().WithMaxRetries(1).Do(context.Background()).Error() | ||
| require.NotNil(t, err) | ||
| } | ||
|
|
||
| func TestRestRawRequestFailed(t *testing.T) { | ||
| testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
| rw.WriteHeader(http.StatusNotFound) | ||
| _, _ = rw.Write([]byte(`{ | ||
| "error_msg": "test rest request failed", | ||
| "error_code": "test rest request failed" | ||
| }`)) | ||
| })) | ||
| defer testServer.Close() | ||
|
|
||
| c, err := restClient(testServer) | ||
| require.Nil(t, err) | ||
| body, err := c.Get().WithMaxRetries(1).Do(context.Background()).Raw() | ||
| require.NotNil(t, body) | ||
| require.NotNil(t, err) | ||
| } | ||
|
|
||
| func TestHTTPMethods(t *testing.T) { | ||
| testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
| rw.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer testServer.Close() | ||
|
|
||
| c, _ := restClient(testServer) | ||
|
|
||
| req := c.Post() | ||
| require.NotNil(t, req) | ||
|
|
||
| req = c.Get() | ||
| require.NotNil(t, req) | ||
|
|
||
| req = c.Put() | ||
| require.NotNil(t, req) | ||
|
|
||
| req = c.Delete() | ||
| require.NotNil(t, req) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2021 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package rest | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/pingcap/ticdc/pkg/security" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestCDCRESTClientCommonConfigs(t *testing.T) { | ||
| _, err := CDCRESTClientFromConfig(&Config{Host: "127.0.0.1"}) | ||
| require.NotNil(t, err) | ||
|
|
||
| _, err = CDCRESTClientFromConfig(&Config{Host: "127.0.0.1", Version: "v1"}) | ||
| require.NotNil(t, err) | ||
|
|
||
| _, err = CDCRESTClientFromConfig(&Config{Host: "127.0.0.1", APIPath: "/api"}) | ||
| require.NotNil(t, err) | ||
|
|
||
| _, err = CDCRESTClientFromConfig(&Config{Host: "http://127.0.0.1:2379", APIPath: "/api", Version: "v1"}) | ||
| require.Nil(t, err) | ||
|
|
||
| _, err = CDCRESTClientFromConfig(&Config{Host: "127.0.0.1:2379", APIPath: "/api", Version: "v2"}) | ||
| require.Nil(t, err) | ||
| } | ||
|
|
||
| func checkTLS(config *Config) bool { | ||
| baseURL, _, err := defaultServerURLFromConfig(config) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| return baseURL.Scheme == "https" | ||
| } | ||
|
|
||
| func TestCDCRESTClientUsingTLS(t *testing.T) { | ||
| testCases := []struct { | ||
| Config *Config | ||
| UsingTLS bool | ||
| }{ | ||
| { | ||
| Config: &Config{}, | ||
| UsingTLS: false, | ||
| }, | ||
| { | ||
| Config: &Config{ | ||
| Host: "https://127.0.0.1", | ||
| }, | ||
| UsingTLS: true, | ||
| }, | ||
| { | ||
| Config: &Config{ | ||
| Host: "127.0.0.1", | ||
| Credential: &security.Credential{ | ||
| CAPath: "foo", | ||
| CertPath: "bar", | ||
| KeyPath: "test", | ||
| }, | ||
| }, | ||
| UsingTLS: true, | ||
| }, | ||
| { | ||
| Config: &Config{ | ||
| Host: "///:://127.0.0.1", | ||
| Credential: &security.Credential{ | ||
| CAPath: "foo", | ||
| CertPath: "bar", | ||
| KeyPath: "test", | ||
| }, | ||
| }, | ||
| UsingTLS: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| usingTLS := checkTLS(tc.Config) | ||
| require.Equal(t, usingTLS, tc.UsingTLS) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| // Copyright 2021 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package rest | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/pingcap/ticdc/pkg/httputil" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRequestParams(t *testing.T) { | ||
| req := (&Request{}).WithParam("foo", "bar") | ||
| require.Equal(t, req.params, url.Values{"foo": []string{"bar"}}) | ||
|
|
||
| req.WithParam("hello", "world") | ||
| require.Equal(t, req.params, url.Values{"foo": []string{"bar"}, "hello": []string{"world"}}) | ||
| } | ||
|
|
||
| func TestRequestURI(t *testing.T) { | ||
| req := (&Request{}).WithParam("foo", "bar").WithPrefix("test") | ||
| req.WithURI("/production?foo=hello&val=1024") | ||
| require.Equal(t, req.pathPrefix, "test/production") | ||
| require.Equal(t, req.params, url.Values{"foo": []string{"hello"}, "val": []string{"1024"}}) | ||
| } | ||
|
|
||
| type testStruct struct { | ||
| Foo string `json:"foo"` | ||
| Bar int `json:"bar"` | ||
| } | ||
|
|
||
| func TestRequestBody(t *testing.T) { | ||
| // test unsupported data type | ||
| req := (&Request{}).WithBody(func() {}) | ||
| require.NotNil(t, req.err) | ||
| require.Nil(t, req.body) | ||
|
|
||
| // test data type which can be json marshalled | ||
| p := &testStruct{Foo: "hello", Bar: 10} | ||
| req = (&Request{}).WithBody(p) | ||
| require.Nil(t, req.err) | ||
| require.NotNil(t, req.body) | ||
|
|
||
| // test data type io.Reader | ||
| req = (&Request{}).WithBody(bytes.NewReader([]byte(`{"hello": "world"}`))) | ||
| require.Nil(t, req.err) | ||
| require.NotNil(t, req.body) | ||
| } | ||
|
|
||
| type clientFunc func(req *http.Request) (*http.Response, error) | ||
|
|
||
| func (f clientFunc) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| return f(req) | ||
| } | ||
|
|
||
| func TestRequestHeader(t *testing.T) { | ||
| cli := httputil.NewTestClient(clientFunc(func(req *http.Request) (*http.Response, error) { | ||
| require.Equal(t, req.Header.Get("signature"), "test-header1") | ||
|
|
||
| return &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Body: io.NopCloser(bytes.NewReader([]byte{})), | ||
| }, nil | ||
| })) | ||
| req := newRequestWithClient(&url.URL{Path: "/test"}, "", nil).WithMethod(HTTPMethodGet) | ||
| req.WithHeader("signature", "test-header2") | ||
| req.WithHeader("signature", "test-header1") | ||
| req.c.Client = cli | ||
|
|
||
| _ = req.Do(context.Background()) | ||
| } | ||
|
|
||
| func TestRequestDoContext(t *testing.T) { | ||
| received := make(chan struct{}) | ||
| blocked := make(chan struct{}) | ||
| testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
| close(received) | ||
| <-blocked | ||
| rw.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer testServer.Close() | ||
| defer close(blocked) | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
|
|
||
| go func() { | ||
| <-received | ||
| cancel() | ||
| }() | ||
| c, err := CDCRESTClientFromConfig(&Config{ | ||
| Host: testServer.URL, | ||
| APIPath: "/api", | ||
| Version: "v1", | ||
| }) | ||
| require.Nil(t, err) | ||
| err = c.Get(). | ||
| WithPrefix("/test"). | ||
| WithTimeout(time.Second). | ||
| Do(ctx). | ||
| Error() | ||
| require.NotNil(t, err) | ||
| } | ||
|
|
||
| func TestRequestDoContextTimeout(t *testing.T) { | ||
| testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
| time.Sleep(200 * time.Millisecond) | ||
| rw.WriteHeader(http.StatusOK) | ||
| })) | ||
| defer testServer.Close() | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| c, err := CDCRESTClientFromConfig(&Config{ | ||
| Host: testServer.URL, | ||
| APIPath: "/api", | ||
| Version: "v1", | ||
| }) | ||
| require.Nil(t, err) | ||
| err = c.Get(). | ||
| WithPrefix("/test"). | ||
| WithTimeout(50 * time.Millisecond). | ||
| Do(ctx). | ||
| Error() | ||
| require.NotNil(t, err) | ||
| } | ||
|
|
||
| func TestResultIntoError(t *testing.T) { | ||
| result := Result{err: errors.New("test-error")} | ||
| err := result.Into(&testStruct{}) | ||
| require.Equal(t, result.err, err) | ||
|
|
||
| result = Result{ | ||
| body: []byte(`{"foo": "hello", "bar": 10}`), | ||
| } | ||
|
|
||
| var res testStruct | ||
| err = result.Into(&res) | ||
| require.Nil(t, err) | ||
| require.Equal(t, res.Foo, "hello") | ||
| require.Equal(t, res.Bar, 10) | ||
| } | ||
|
|
||
| func TestResultZeroLengthBody(t *testing.T) { | ||
| result := Result{ | ||
| body: []byte{}, | ||
| } | ||
| err := result.Into(&testStruct{}) | ||
| require.NotNil(t, err) | ||
| require.Equal(t, strings.Contains(err.Error(), "0-length"), true) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle
restClienterrors before dereferencing.If
restClientfails,cmay be nil and cause a panic.✅ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents