Merge pull request #1884 from mtchavez/1541-data-dir-perms

command: Data directory permission error message
This commit is contained in:
James Phillips 2016-04-12 22:06:49 -07:00
commit 79153c3014
5 changed files with 48 additions and 3 deletions

View File

@ -203,6 +203,12 @@ func (c *Command) readConfig() *Config {
if config.Server { if config.Server {
mdbPath := filepath.Join(config.DataDir, "mdb") mdbPath := filepath.Join(config.DataDir, "mdb")
if _, err := os.Stat(mdbPath); !os.IsNotExist(err) { if _, err := os.Stat(mdbPath); !os.IsNotExist(err) {
if os.IsPermission(err) {
c.Ui.Error(fmt.Sprintf("CRITICAL: Permission denied for data folder at %q!", mdbPath))
c.Ui.Error("Consul will refuse to boot without access to this directory.")
c.Ui.Error("Please correct permissions and try starting again.")
return nil
}
c.Ui.Error(fmt.Sprintf("CRITICAL: Deprecated data folder found at %q!", mdbPath)) c.Ui.Error(fmt.Sprintf("CRITICAL: Deprecated data folder found at %q!", mdbPath))
c.Ui.Error("Consul will refuse to boot with this directory present.") c.Ui.Error("Consul will refuse to boot with this directory present.")
c.Ui.Error("See https://www.consul.io/docs/upgrade-specific.html for more information.") c.Ui.Error("See https://www.consul.io/docs/upgrade-specific.html for more information.")

View File

@ -403,3 +403,29 @@ func TestProtectDataDir(t *testing.T) {
t.Fatalf("expected mdb dir error, got: %s", out) t.Fatalf("expected mdb dir error, got: %s", out)
} }
} }
func TestBadDataDirPermissions(t *testing.T) {
dir, err := ioutil.TempDir("", "consul")
if err != nil {
t.Fatalf("err: %v", err)
}
defer os.RemoveAll(dir)
dataDir := filepath.Join(dir, "mdb")
if err := os.MkdirAll(dataDir, 0400); err != nil {
t.Fatalf("err: %v", err)
}
defer os.RemoveAll(dataDir)
ui := new(cli.MockUi)
cmd := &Command{
Ui: ui,
args: []string{"-data-dir=" + dataDir, "-server=true"},
}
if conf := cmd.readConfig(); conf != nil {
t.Fatalf("Should fail with bad data directory permissions")
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Permission denied") {
t.Fatalf("expected permission denied error, got: %s", out)
}
}

View File

@ -28,6 +28,9 @@ func makeHTTPServer(t *testing.T) (string, *HTTPServer) {
} }
func makeHTTPServerWithConfig(t *testing.T, cb func(c *Config)) (string, *HTTPServer) { func makeHTTPServerWithConfig(t *testing.T, cb func(c *Config)) (string, *HTTPServer) {
configTry := 0
RECONF:
configTry += 1
conf := nextConfig() conf := nextConfig()
if cb != nil { if cb != nil {
cb(conf) cb(conf)
@ -36,6 +39,9 @@ func makeHTTPServerWithConfig(t *testing.T, cb func(c *Config)) (string, *HTTPSe
dir, agent := makeAgent(t, conf) dir, agent := makeAgent(t, conf)
servers, err := NewHTTPServers(agent, conf, agent.logOutput) servers, err := NewHTTPServers(agent, conf, agent.logOutput)
if err != nil { if err != nil {
if configTry < 3 {
goto RECONF
}
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if len(servers) == 0 { if len(servers) == 0 {

View File

@ -3,8 +3,6 @@ package agent
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/serf/serf"
"io" "io"
"io/ioutil" "io/ioutil"
"net" "net"
@ -14,6 +12,9 @@ import (
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/hashicorp/consul/testutil"
"github.com/hashicorp/serf/serf"
) )
type rpcParts struct { type rpcParts struct {
@ -40,6 +41,9 @@ func testRPCClientWithConfig(t *testing.T, cb func(c *Config)) *rpcParts {
lw := NewLogWriter(512) lw := NewLogWriter(512)
mult := io.MultiWriter(os.Stderr, lw) mult := io.MultiWriter(os.Stderr, lw)
configTry := 0
RECONF:
configTry += 1
conf := nextConfig() conf := nextConfig()
cb(conf) cb(conf)
@ -50,6 +54,9 @@ func testRPCClientWithConfig(t *testing.T, cb func(c *Config)) *rpcParts {
l, err := net.Listen(rpcAddr.Network(), rpcAddr.String()) l, err := net.Listen(rpcAddr.Network(), rpcAddr.String())
if err != nil { if err != nil {
if configTry < 3 {
goto RECONF
}
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -212,7 +212,7 @@ func TestClient_RPC_Pool(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if len(s1.LANMembers()) != 2 || len(c1.LANMembers()) != 2 { if len(s1.LANMembers()) != 2 || len(c1.LANMembers()) != 2 {
t.Fatalf("bad len") t.Fatalf("Server has %v of %v expected members; Client has %v of %v expected members.", len(s1.LANMembers()), 2, len(c1.LANMembers()), 2)
} }
// Blast out a bunch of RPC requests at the same time to try to get // Blast out a bunch of RPC requests at the same time to try to get