agent: test agent rpc unix socket create failure

This commit is contained in:
Ryan Uber 2015-01-19 09:54:12 -08:00
parent b30af9536d
commit 2530e5ff05
1 changed files with 46 additions and 0 deletions

View File

@ -1,10 +1,12 @@
package agent
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"testing"
"github.com/hashicorp/consul/testutil"
@ -163,3 +165,47 @@ func TestRetryJoinWanFail(t *testing.T) {
t.Fatalf("bad: %d", code)
}
}
func TestSetupAgent_UnixSocket_Fails(t *testing.T) {
conf := nextConfig()
tmpDir, err := ioutil.TempDir("", "consul")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(tmpDir)
tmpFile, err := ioutil.TempFile("", "consul")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(tmpFile.Name())
socketPath := tmpFile.Name()
conf.DataDir = tmpDir
conf.Server = true
conf.Bootstrap = true
// Set socket address to an existing file. Consul should fail to
// start and return an error.
conf.Addresses.RPC = "unix://" + socketPath
shutdownCh := make(chan struct{})
defer close(shutdownCh)
cmd := &Command{
ShutdownCh: shutdownCh,
Ui: new(cli.MockUi),
}
logWriter := NewLogWriter(512)
logOutput := new(bytes.Buffer)
// Ensure we got an error mentioning the socket file
err = cmd.setupAgent(conf, logOutput, logWriter)
if err == nil {
t.Fatalf("should have failed")
}
if !strings.Contains(err.Error(), socketPath) {
t.Fatalf("expected socket file error, got: %q", err)
}
}