From 1e9736b71f92969816d9bc8f3d17119adc6071c4 Mon Sep 17 00:00:00 2001 From: Jonathan Hess Date: Wed, 13 Sep 2023 14:12:20 -0600 Subject: [PATCH 1/4] feat: /quitquitquit api now responds to HTTP GET and POST requests. --- cmd/root.go | 2 +- cmd/root_test.go | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index c7f931e9e..f1c63cec8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -913,7 +913,7 @@ func runSignalWrapper(cmd *Command) (err error) { func quitquitquit(quitOnce *sync.Once, shutdownCh chan<- error) http.HandlerFunc { return func(rw http.ResponseWriter, req *http.Request) { - if req.Method != http.MethodPost { + if req.Method != http.MethodPost && req.Method != http.MethodGet { rw.WriteHeader(400) return } diff --git a/cmd/root_test.go b/cmd/root_test.go index b2f60b02c..528fe81d6 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1307,7 +1307,7 @@ func TestPProfServer(t *testing.T) { } } -func TestQuitQuitQuit(t *testing.T) { +func TestQuitQuitQuitHttpPost(t *testing.T) { c := NewCommand(WithDialer(&spyDialer{})) c.SilenceUsage = true c.SilenceErrors = true @@ -1320,7 +1320,7 @@ func TestQuitQuitQuit(t *testing.T) { err := c.ExecuteContext(ctx) errCh <- err }() - resp, err := tryDial("GET", "http://localhost:9192/quitquitquit") + resp, err := tryDial("HEAD", "http://localhost:9192/quitquitquit") if err != nil { t.Fatalf("failed to dial endpoint: %v", err) } @@ -1348,6 +1348,41 @@ func TestQuitQuitQuit(t *testing.T) { } } +func TestQuitQuitQuitHttpGet(t *testing.T) { + c := NewCommand(WithDialer(&spyDialer{})) + c.SilenceUsage = true + c.SilenceErrors = true + c.SetArgs([]string{"--quitquitquit", "--admin-port", "9192", "my-project:my-region:my-instance"}) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + errCh := make(chan error) + go func() { + err := c.ExecuteContext(ctx) + errCh <- err + }() + + resp, err := tryDial("GET", "http://localhost:9192/quitquitquit") + if err != nil { + t.Fatalf("failed to dial endpoint: %v", err) + } + if resp.StatusCode != http.StatusOK { + t.Fatalf("expected a 200 status, got = %v", resp.StatusCode) + } + + var gotErr error + select { + case err := <-errCh: + gotErr = err + case <-time.After(30 * time.Second): + t.Fatal("timeout waiting for error") + } + + if !errors.Is(gotErr, errQuitQuitQuit) { + t.Fatalf("want = %v, got = %v", errQuitQuitQuit, gotErr) + } +} + type errorDialer struct { spyDialer } From 30f6a1d74330b0ae6e3ba3080b56d6af8a7e45b0 Mon Sep 17 00:00:00 2001 From: Jonathan Hess Date: Thu, 14 Sep 2023 12:19:31 -0600 Subject: [PATCH 2/4] chore: code review comments. --- cmd/root_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 528fe81d6..980f4d81c 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1307,7 +1307,7 @@ func TestPProfServer(t *testing.T) { } } -func TestQuitQuitQuitHttpPost(t *testing.T) { +func TestQuitQuitQuitHTTPPost(t *testing.T) { c := NewCommand(WithDialer(&spyDialer{})) c.SilenceUsage = true c.SilenceErrors = true @@ -1348,11 +1348,11 @@ func TestQuitQuitQuitHttpPost(t *testing.T) { } } -func TestQuitQuitQuitHttpGet(t *testing.T) { +func TestQuitQuitQuitHTTPGet(t *testing.T) { c := NewCommand(WithDialer(&spyDialer{})) c.SilenceUsage = true c.SilenceErrors = true - c.SetArgs([]string{"--quitquitquit", "--admin-port", "9192", "my-project:my-region:my-instance"}) + c.SetArgs([]string{"--quitquitquit", "--admin-port", "9193", "my-project:my-region:my-instance"}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() From 0b5375791fd52c012cead2b490ca1eaf9da011c3 Mon Sep 17 00:00:00 2001 From: Jonathan Hess Date: Thu, 14 Sep 2023 13:09:49 -0600 Subject: [PATCH 3/4] fix: deconflict the admin port used in integration tests. --- cmd/root_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 980f4d81c..883c1cc3a 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1352,7 +1352,7 @@ func TestQuitQuitQuitHTTPGet(t *testing.T) { c := NewCommand(WithDialer(&spyDialer{})) c.SilenceUsage = true c.SilenceErrors = true - c.SetArgs([]string{"--quitquitquit", "--admin-port", "9193", "my-project:my-region:my-instance"}) + c.SetArgs([]string{"--quitquitquit", "--admin-port", "9194", "my-project:my-region:my-instance"}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() From 55a22c1db7e2408c05fa463f2f82ed2104d37393 Mon Sep 17 00:00:00 2001 From: Jonathan Hess Date: Thu, 14 Sep 2023 13:12:09 -0600 Subject: [PATCH 4/4] fix: deconflict the admin port used in integration tests. --- cmd/root_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 883c1cc3a..1607e7164 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1362,7 +1362,7 @@ func TestQuitQuitQuitHTTPGet(t *testing.T) { errCh <- err }() - resp, err := tryDial("GET", "http://localhost:9192/quitquitquit") + resp, err := tryDial("GET", "http://localhost:9194/quitquitquit") if err != nil { t.Fatalf("failed to dial endpoint: %v", err) }