open-consul/agent/util_test.go
Frank Schroeder 034ee43cef agent: decouple anti-entropy from local state
The anti-entropy code manages background synchronizations of the local
state on a regular basis or on demand when either the state has changed
or a new consul server has been added.

This patch moves the anti-entropy code into its own package and
decouples it from the local state code since they are performing
two different functions.

To simplify code-review this revision does not make any optimizations,
renames or refactorings. This will happen in subsequent commits.
2017-10-23 08:03:18 +02:00

77 lines
1.5 KiB
Go

package agent
import (
"os"
"runtime"
"testing"
"github.com/hashicorp/consul/testutil"
)
func TestStringHash(t *testing.T) {
t.Parallel()
in := "hello world"
expected := "5eb63bbbe01eeed093cb22bb8f5acdc3"
if out := stringHash(in); out != expected {
t.Fatalf("bad: %s", out)
}
}
func TestSetFilePermissions(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.SkipNow()
}
tempFile := testutil.TempFile(t, "consul")
path := tempFile.Name()
defer os.Remove(path)
// Bad UID fails
if err := setFilePermissions(path, "%", "", ""); err == nil {
t.Fatalf("should fail")
}
// Bad GID fails
if err := setFilePermissions(path, "", "%", ""); err == nil {
t.Fatalf("should fail")
}
// Bad mode fails
if err := setFilePermissions(path, "", "", "%"); err == nil {
t.Fatalf("should fail")
}
// Allows omitting user/group/mode
if err := setFilePermissions(path, "", "", ""); err != nil {
t.Fatalf("err: %s", err)
}
// Doesn't change mode if not given
if err := os.Chmod(path, 0700); err != nil {
t.Fatalf("err: %s", err)
}
if err := setFilePermissions(path, "", "", ""); err != nil {
t.Fatalf("err: %s", err)
}
fi, err := os.Stat(path)
if err != nil {
t.Fatalf("err: %s", err)
}
if fi.Mode().String() != "-rwx------" {
t.Fatalf("bad: %s", fi.Mode())
}
// Changes mode if given
if err := setFilePermissions(path, "", "", "0777"); err != nil {
t.Fatalf("err: %s", err)
}
fi, err = os.Stat(path)
if err != nil {
t.Fatalf("err: %s", err)
}
if fi.Mode().String() != "-rwxrwxrwx" {
t.Fatalf("bad: %s", fi.Mode())
}
}