2014-01-15 22:17:40 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2015-01-20 22:13:36 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-05-29 19:03:55 +00:00
|
|
|
"runtime"
|
2014-01-15 22:17:40 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAEScale(t *testing.T) {
|
|
|
|
intv := time.Minute
|
|
|
|
if v := aeScale(intv, 100); v != intv {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := aeScale(intv, 200); v != 2*intv {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := aeScale(intv, 1000); v != 4*intv {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := aeScale(intv, 10000); v != 8*intv {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 22:34:17 +00:00
|
|
|
|
2015-06-30 19:02:05 +00:00
|
|
|
func TestRateScaledInterval(t *testing.T) {
|
|
|
|
min := 1*time.Second
|
|
|
|
rate := 200.0
|
|
|
|
if v := rateScaledInterval(rate, min, 0); v != min {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := rateScaledInterval(rate, min, 100); v != min {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := rateScaledInterval(rate, min, 200); v != 1*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := rateScaledInterval(rate, min, 1000); v != 5*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := rateScaledInterval(rate, min, 5000); v != 25*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
if v := rateScaledInterval(rate, min, 10000); v != 50*time.Second {
|
|
|
|
t.Fatalf("Bad: %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:34:17 +00:00
|
|
|
func TestRandomStagger(t *testing.T) {
|
|
|
|
intv := time.Minute
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
stagger := randomStagger(intv)
|
|
|
|
if stagger < 0 || stagger >= intv {
|
|
|
|
t.Fatalf("Bad: %v", stagger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 03:11:21 +00:00
|
|
|
|
|
|
|
func TestStringHash(t *testing.T) {
|
|
|
|
in := "hello world"
|
|
|
|
expected := "5eb63bbbe01eeed093cb22bb8f5acdc3"
|
|
|
|
|
|
|
|
if out := stringHash(in); out != expected {
|
|
|
|
t.Fatalf("bad: %s", out)
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 22:13:36 +00:00
|
|
|
|
|
|
|
func TestSetFilePermissions(t *testing.T) {
|
2015-05-29 19:03:55 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2015-01-20 22:13:36 +00:00
|
|
|
tempFile, err := ioutil.TempFile("", "consul")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
path := tempFile.Name()
|
|
|
|
defer os.Remove(path)
|
|
|
|
|
|
|
|
// Bad UID fails
|
2015-01-21 04:30:59 +00:00
|
|
|
if err := setFilePermissions(path, UnixSocketPermissions{Usr: "%"}); err == nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad GID fails
|
2015-01-21 04:30:59 +00:00
|
|
|
if err := setFilePermissions(path, UnixSocketPermissions{Grp: "%"}); err == nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad mode fails
|
2015-01-21 04:30:59 +00:00
|
|
|
if err := setFilePermissions(path, UnixSocketPermissions{Perms: "%"}); err == nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allows omitting user/group/mode
|
2015-01-21 04:30:59 +00:00
|
|
|
if err := setFilePermissions(path, UnixSocketPermissions{}); err != nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-01-21 04:30:59 +00:00
|
|
|
if err := setFilePermissions(path, UnixSocketPermissions{}); err != nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
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
|
2015-01-21 04:30:59 +00:00
|
|
|
if err := setFilePermissions(path, UnixSocketPermissions{Perms: "0777"}); err != nil {
|
2015-01-20 22:13:36 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|