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

decompress response body from websocket error #6650

Merged
merged 3 commits into from
Nov 8, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ BUG FIXES:
* nomad: Multiple connect enabled services in the same taskgroup failed to
register [[GH-6646](https://github.com/hashicorp/nomad/issues/6646)]
* scheduler: Changes to devices in resource stanza should cause rescheduling [[GH-6644](https://github.com/hashicorp/nomad/issues/6644)]
* api: Decompress web socket response body if gzipped on error responses [[GH-6650](https://github.com/hashicorp/nomad/issues/6650)]

## 0.10.1 (November 4, 2019)

Expand Down
62 changes: 50 additions & 12 deletions api/allocations_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package api

import (
"context"
"os"
"reflect"
"sort"
"testing"

"time"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -146,20 +148,12 @@ func TestAllocations_RescheduleInfo(t *testing.T) {
}
job.Canonicalize()

uuidGen := func() string {
ret, err := uuid.GenerateUUID()
if err != nil {
t.Fatal(err)
}
return ret
}

alloc := &Allocation{
ID: uuidGen(),
ID: uuid.Generate(),
Namespace: DefaultNamespace,
EvalID: uuidGen(),
EvalID: uuid.Generate(),
Name: "foo-bar[1]",
NodeID: uuidGen(),
NodeID: uuid.Generate(),
TaskGroup: *job.TaskGroups[0].Name,
JobID: *job.ID,
Job: job,
Expand Down Expand Up @@ -247,6 +241,50 @@ func TestAllocations_RescheduleInfo(t *testing.T) {

}

// TestAllocations_ExecErrors ensures errors are properly formatted
func TestAllocations_ExecErrors(t *testing.T) {
c, s := makeClient(t, nil, nil)
defer s.Stop()
a := c.Allocations()

job := &Job{
Name: stringToPtr("foo"),
Namespace: stringToPtr(DefaultNamespace),
ID: stringToPtr("bar"),
ParentID: stringToPtr("lol"),
TaskGroups: []*TaskGroup{
{
Name: stringToPtr("bar"),
Tasks: []*Task{
{
Name: "task1",
},
},
},
},
}
job.Canonicalize()

alloc := &Allocation{
ID: "",
Namespace: DefaultNamespace,
EvalID: uuid.Generate(),
Name: "foo-bar[1]",
NodeID: uuid.Generate(),
TaskGroup: *job.TaskGroups[0].Name,
JobID: *job.ID,
Job: job,
}
// Querying when no allocs exist returns nothing
sizeCh := make(chan TerminalSize, 1)

// make a request that will result in an error
// ensure the error is what we expect
_, err := a.Exec(context.Background(), alloc, "bar", false, []string{"command"}, os.Stdin, os.Stdout, os.Stderr, sizeCh, nil)
require.Contains(t, err.Error(), "Unexpected response code: 301")
require.Contains(t, err.Error(), "Moved Permanently")
Copy link
Contributor

Choose a reason for hiding this comment

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

Not your change but I find it very sad that we return redirect to /ui if an /v1/ endpoint isn't found.

Copy link
Member

Choose a reason for hiding this comment

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

That is so strange. Filed an issue: #6656

}

func TestAllocations_ShouldMigrate(t *testing.T) {
t.Parallel()
require.True(t, DesiredTransition{Migrate: boolToPtr(true)}.ShouldMigrate())
Expand Down
11 changes: 10 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,16 @@ func (c *Client) websocket(endpoint string, q *QueryOptions) (*websocket.Conn, *
// check resp status code, as it's more informative than handshake error we get from ws library
if resp != nil && resp.StatusCode != 101 {
var buf bytes.Buffer
io.Copy(&buf, resp.Body)

if resp.Header.Get("Content-Encoding") == "gzip" {
greader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, nil, fmt.Errorf("Unexpected response code: %d", resp.StatusCode)
}
io.Copy(&buf, greader)
} else {
io.Copy(&buf, resp.Body)
}
resp.Body.Close()

return nil, nil, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes())
Expand Down