diff --git a/CHANGELOG.md b/CHANGELOG.md index e6fd3700a749..e198cfd08d8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/api/allocations_test.go b/api/allocations_test.go index a18b7e1477a7..b942ee0e0f53 100644 --- a/api/allocations_test.go +++ b/api/allocations_test.go @@ -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()) diff --git a/api/api.go b/api/api.go index 9f4c8ebc589e..a3f2a9c49e91 100644 --- a/api/api.go +++ b/api/api.go @@ -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())