Skip to content

Commit

Permalink
Merge pull request #6650 from hashicorp/b-better-ws-error
Browse files Browse the repository at this point in the history
decompress response body from websocket error
  • Loading branch information
drewbailey committed Nov 8, 2019
2 parents 43e3570 + 69751a7 commit 5e117ec
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
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")
}

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

0 comments on commit 5e117ec

Please sign in to comment.