From 49d2fa78d95535af662a50d9bc3affd4d8d061d4 Mon Sep 17 00:00:00 2001 From: Tao Wan Date: Mon, 1 Mar 2021 18:56:13 +0800 Subject: [PATCH 1/3] client.Populate assign client to proxy --- client/client.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/client/client.go b/client/client.go index f061ce97..0419dc4b 100644 --- a/client/client.go +++ b/client/client.go @@ -159,6 +159,13 @@ func (client *Client) Populate(config []Proxy) ([]*Proxy, error) { resp.Body = ioutil.NopCloser(&body) err = checkError(resp, http.StatusCreated, "Populate") + if err != nil { + return nil, err + } + + for _, proxy := range proxies.Proxies { + proxy.client = client + } return proxies.Proxies, err } From 778b08e069fea06cbb1178df0cc746454a8e7840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E9=9F=AC?= Date: Tue, 2 Nov 2021 19:31:17 +0800 Subject: [PATCH 2/3] add test case --- api_test.go | 31 +++++++++++++++++++++++++++++++ client/client.go | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/api_test.go b/api_test.go index c2f9a9fe..2730eaf5 100644 --- a/api_test.go +++ b/api_test.go @@ -369,6 +369,37 @@ func TestPopulateProxyWithBadDataShouldReturnError(t *testing.T) { }) } +func TestPopulateAddToxic(t *testing.T) { + WithServer(t, func(addr string) { + testProxies, err := client.Populate([]tclient.Proxy{ + { + Name: "one", + Listen: "localhost:7070", + Upstream: "localhost:7171", + Enabled: true, + }, + }) + + if err != nil { + t.Fatal("Unable to populate:", err) + } + + if len(testProxies) != 1 { + t.Fatalf("Wrong number of proxies returned: %d != %d", len(testProxies), 1) + } + + if testProxies[0].Name != "one" { + t.Fatalf("Wrong proxy name returned: %s != one", testProxies[0].Name) + } + + _, err = testProxies[0].AddToxic("", "latency", "downstream", 1, nil) + if err != nil { + t.Fatal("Failed to AddToxic.") + } + + }) +} + func TestListingProxies(t *testing.T) { WithServer(t, func(addr string) { _, err := client.CreateProxy("mysql_master", "localhost:3310", "localhost:20001") diff --git a/client/client.go b/client/client.go index 0fa17cce..87c5833c 100644 --- a/client/client.go +++ b/client/client.go @@ -162,7 +162,7 @@ func (client *Client) Populate(config []Proxy) ([]*Proxy, error) { resp.Body = ioutil.NopCloser(&body) err = checkError(resp, http.StatusCreated, "Populate") if err != nil { - return nil, err + return proxies.Proxies, err } for _, proxy := range proxies.Proxies { From 95505bf9bfc1f14b8fd082bd25ab9b68e259b66d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=87=E9=9F=AC?= Date: Tue, 2 Nov 2021 21:56:16 +0800 Subject: [PATCH 3/3] changelog and lint fix --- CHANGELOG.md | 1 + api_test.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b452738a..fbc66c69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Store all the executable `main` packages in `cmd` folder. (#335, @miry) * Extract common test helpers to own files. (#336, @miry) * Client: Allow HTTPS endpoints. (#338, @chen-anders) +* client.Populate assign client to proxy. (#291, @hellodudu) # [2.2.0] diff --git a/api_test.go b/api_test.go index 2730eaf5..175fc91d 100644 --- a/api_test.go +++ b/api_test.go @@ -396,7 +396,6 @@ func TestPopulateAddToxic(t *testing.T) { if err != nil { t.Fatal("Failed to AddToxic.") } - }) }