Skip to content

Commit

Permalink
Fix test failure; add tests for socket envvars
Browse files Browse the repository at this point in the history
  • Loading branch information
angrycub committed Apr 12, 2023
1 parent 869e19d commit 921dffb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
9 changes: 4 additions & 5 deletions command/operator_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,12 @@ func tlsToCurl(parts []string, tlsConfig *api.TLSConfig) []string {
// all default values are set correctly. Failure to do so will likely result in
// a nil-pointer.
func pathToURL(config *api.Config, path string) (*url.URL, error) {
pPath, err := url.Parse(path)
if err != nil {
return nil, err
}

// If the scheme is missing from the path, it likely means the path is just
// the HTTP handler path. Attempt to infer this.
if pPath.Scheme == "" {
if !strings.HasPrefix(path, "http://") &&
!strings.HasPrefix(path, "https://") &&
!strings.HasPrefix(path, "unix://") {
scheme := "http"

// If the user has set any TLS configuration value, this is a good sign
Expand Down
74 changes: 74 additions & 0 deletions command/operator_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ package command

import (
"bytes"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path"
"testing"
"time"

Expand Down Expand Up @@ -220,3 +223,74 @@ func TestOperatorAPICommand_ContentLength(t *testing.T) {
t.Fatalf("timed out waiting for request")
}
}

func makeSocketListener(t *testing.T) (net.Listener, string) {
td := os.TempDir() // testing.TempDir() on macOS makes paths that are too long
sPath := path.Join(td, t.Name()+".sock")
os.Remove(sPath) // git rid of stale ones now.

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

// Create a Unix domain socket and listen for incoming connections.
socket, err := net.Listen("unix", sPath)
must.NoError(t, err)
return socket, sPath
}

// TestOperatorAPICommand_Socket tests that requests can be routed over a unix
// domain socket
//
// Can not be run in parallel as it modifies the environment.
func TestOperatorAPICommand_Socket(t *testing.T) {

ping := make(chan struct{}, 1)
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ping <- struct{}{}
}))
sock, sockPath := makeSocketListener(t)
ts.Listener = sock
ts.Start()
defer ts.Close()

// Setup command.
ui := cli.NewMockUi()
cmd := &OperatorAPICommand{Meta: Meta{Ui: ui}}

tcs := []struct {
name string
env map[string]string
args []string
exitCode int
}{
{
name: "nomad_socket_path",
env: map[string]string{"NOMAD_SOCKET_PATH": sockPath},
args: []string{"http://example.com/v1/jobs"},
exitCode: 0,
},
{
name: "nomad_addr",
env: map[string]string{"NOMAD_ADDR": "unix://" + sockPath},
args: []string{"http://example.com/v1/jobs"},
exitCode: 0,
},
}
for i, tc := range tcs {
t.Run(fmt.Sprintf("%v_%s", i+1, t.Name()), func(t *testing.T) {
tc := tc
for k, v := range tc.env {
t.Setenv(k, v)
}

exitCode := cmd.Run(tc.args)
must.Eq(t, tc.exitCode, exitCode, must.Sprint(ui.ErrorWriter.String()))

select {
case l := <-ping:
require.Equal(t, struct{}{}, l)
case <-time.After(10 * time.Second):
t.Fatalf("timed out waiting for request")
}
})
}
}

0 comments on commit 921dffb

Please sign in to comment.