agent: test mdb dir protection

This commit is contained in:
Ryan Uber 2015-10-15 14:15:41 -07:00
parent 2a7609d6bc
commit 8bc51eb237

View file

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path/filepath"
"strings" "strings"
"testing" "testing"
@ -299,3 +300,39 @@ func TestSetupScadaConn(t *testing.T) {
t.Fatalf("should be closed") t.Fatalf("should be closed")
} }
} }
func TestProtectDataDir(t *testing.T) {
dir, err := ioutil.TempDir("", "consul")
if err != nil {
t.Fatalf("err: %v", err)
}
defer os.RemoveAll(dir)
if err := os.MkdirAll(filepath.Join(dir, "mdb"), 0700); err != nil {
t.Fatalf("err: %v", err)
}
cfgFile, err := ioutil.TempFile("", "consul")
if err != nil {
t.Fatalf("err: %v", err)
}
defer os.Remove(cfgFile.Name())
content := fmt.Sprintf(`{"server": true, "data_dir": "%s"}`, dir)
_, err = cfgFile.Write([]byte(content))
if err != nil {
t.Fatalf("err: %v", err)
}
ui := new(cli.MockUi)
cmd := &Command{
Ui: ui,
args: []string{"-config-file=" + cfgFile.Name()},
}
if conf := cmd.readConfig(); conf != nil {
t.Fatalf("should fail")
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, dir) {
t.Fatalf("expected mdb dir error, got: %s", out)
}
}