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

feat: accept GET method at /quitquitquit #726

Merged
merged 2 commits into from
Dec 2, 2024
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
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ func runSignalWrapper(cmd *Command) (err error) {

func quitquitquit(quitOnce *sync.Once, shutdownCh chan<- error) http.HandlerFunc {
return http.HandlerFunc(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
}
Expand Down
48 changes: 45 additions & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,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
Expand All @@ -1206,14 +1206,56 @@ 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)
}
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("expected a 400 status, got = %v", resp.StatusCode)
}
resp, err = http.Post("http://localhost:9192/quitquitquit", "", nil)
resp, err = tryDial("POST", "http://localhost:9192/quitquitquit")
if err != nil {
t.Fatalf("failed to dial endpoint: %v", err)
}
if resp.StatusCode != http.StatusOK {
nancynh marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}

func TestQuitQuitQuitGet(t *testing.T) {
c := NewCommand(WithDialer(&spyDialer{}))
c.SilenceUsage = true
c.SilenceErrors = true
c.SetArgs([]string{"--quitquitquit", "--admin-port", "9192",
"projects/proj/locations/region/clusters/clust/instances/inst"})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

errCh := make(chan error)
go func() {
err := c.ExecuteContext(ctx)
errCh <- err
}()
resp, err := tryDial("HEAD", "http://localhost:9192/quitquitquit")
if err != nil {
t.Fatalf("failed to dial endpoint: %v", err)
}
if resp.StatusCode != http.StatusBadRequest {
t.Fatalf("expected a 400 status, got = %v", resp.StatusCode)
}
resp, err = tryDial("GET", "http://localhost:9192/quitquitquit")
if err != nil {
t.Fatalf("failed to dial endpoint: %v", err)
}
Expand Down
Loading