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: /quitquitquit api now responds to HTTP GET and POST requests. #1947

Merged
merged 4 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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
}
Expand Down
39 changes: 37 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ func TestPProfServer(t *testing.T) {
}
}

func TestQuitQuitQuit(t *testing.T) {
func TestQuitQuitQuitHttpPost(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

c := NewCommand(WithDialer(&spyDialer{}))
c.SilenceUsage = true
c.SilenceErrors = true
Expand All @@ -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)
}
Expand Down Expand Up @@ -1348,6 +1348,41 @@ func TestQuitQuitQuit(t *testing.T) {
}
}

func TestQuitQuitQuitHttpGet(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

c := NewCommand(WithDialer(&spyDialer{}))
c.SilenceUsage = true
c.SilenceErrors = true
c.SetArgs([]string{"--quitquitquit", "--admin-port", "9192", "my-project:my-region:my-instance"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use 9193 or some other port to avoid conflicts with the test above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we pull the port out as a constant to avoid this kind of mistake (9192 here, 9193 above)

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
}
Expand Down
Loading