Merge pull request #6650 from hashicorp/b-better-ws-error

decompress response body from websocket error
This commit is contained in:
Drew Bailey 2019-11-08 10:31:13 -05:00 committed by GitHub
commit 0d9486b660
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 13 deletions

View File

@ -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)

View File

@ -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"
)
@ -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,
@ -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())

View File

@ -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())