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

Backport of api: only set url field in config if previously unset into release/1.8.x #23787

Merged
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
3 changes: 3 additions & 0 deletions .changelog/23785.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api: Fixed a bug where an `api.Config` targeting a unix domain socket could not be reused between clients
```
10 changes: 7 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,13 @@ func NewClient(config *Config) (*Client, error) {
}

// we have to test the address that comes from DefaultConfig, because it
// could be the value of NOMAD_ADDR which is applied without testing
if config.url, err = url.Parse(config.Address); err != nil {
return nil, fmt.Errorf("invalid address '%s': %v", config.Address, err)
// could be the value of NOMAD_ADDR which is applied without testing. But
// only on the first use of this Config, otherwise we'll have mutated the
// address
if config.url == nil {
if config.url, err = url.Parse(config.Address); err != nil {
return nil, fmt.Errorf("invalid address '%s': %v", config.Address, err)
}
}

httpClient := config.HttpClient
Expand Down
44 changes: 44 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -595,3 +598,44 @@ func TestClient_autoUnzip(t *testing.T) {
Body: io.NopCloser(&b),
}, nil)
}

func TestUnixSocketConfig(t *testing.T) {

td := os.TempDir() // testing.TempDir() on macOS makes paths that are too long
socketPath := path.Join(td, t.Name()+".sock")
os.Remove(socketPath) // git rid of stale ones now.

t.Cleanup(func() { os.Remove(socketPath) })

ts := httptest.NewUnstartedServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`"10.1.1.1"`))
}))

// Create a Unix domain socket and listen for incoming connections.
socket, err := net.Listen("unix", socketPath)
must.NoError(t, err)
t.Cleanup(func() { socket.Close() })

ts.Listener = socket
ts.Start()
defer ts.Close()

cfg := &Config{Address: "unix://" + socketPath}

client1, err := NewClient(cfg)
must.NoError(t, err)
t.Cleanup(client1.Close)

resp, err := client1.Status().Leader()
must.NoError(t, err)
must.Eq(t, "10.1.1.1", resp)

client2, err := NewClient(cfg)
must.NoError(t, err)
t.Cleanup(client2.Close)

_, err = client2.Status().Leader()
must.NoError(t, err)
must.Eq(t, "10.1.1.1", resp)
}