2017-06-15 16:46:06 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
2017-06-23 08:15:48 +00:00
|
|
|
"reflect"
|
2017-06-15 16:46:06 +00:00
|
|
|
"testing"
|
2017-09-25 18:40:42 +00:00
|
|
|
"time"
|
2017-06-15 16:46:06 +00:00
|
|
|
|
2017-06-23 08:15:48 +00:00
|
|
|
"github.com/google/gofuzz"
|
2017-06-15 16:46:06 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2017-06-23 08:15:48 +00:00
|
|
|
"github.com/mitchellh/reflectwalk"
|
2017-09-25 18:40:42 +00:00
|
|
|
"github.com/pascaldekloe/goe/verify"
|
2017-06-15 16:46:06 +00:00
|
|
|
)
|
|
|
|
|
2017-06-23 08:15:48 +00:00
|
|
|
func TestCheckDefinition_Defaults(t *testing.T) {
|
2017-06-15 16:46:06 +00:00
|
|
|
t.Parallel()
|
|
|
|
def := CheckDefinition{}
|
|
|
|
check := def.HealthCheck("node1")
|
|
|
|
|
|
|
|
// Health checks default to critical state
|
|
|
|
if check.Status != api.HealthCritical {
|
|
|
|
t.Fatalf("bad: %v", check.Status)
|
|
|
|
}
|
|
|
|
}
|
2017-06-23 08:15:48 +00:00
|
|
|
|
|
|
|
type walker struct {
|
|
|
|
fields map[string]reflect.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *walker) Struct(reflect.Value) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *walker) StructField(f reflect.StructField, v reflect.Value) error {
|
|
|
|
w.fields[f.Name] = v
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapFields(obj interface{}) map[string]reflect.Value {
|
|
|
|
w := &walker{make(map[string]reflect.Value)}
|
|
|
|
if err := reflectwalk.Walk(obj, w); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return w.fields
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckDefinition_CheckType(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Fuzz a definition to fill all its fields with data.
|
|
|
|
var def CheckDefinition
|
|
|
|
fuzz.New().Fuzz(&def)
|
|
|
|
orig := mapFields(def)
|
|
|
|
|
|
|
|
// Remap the ID field which changes name, and redact fields we don't
|
|
|
|
// expect in the copy.
|
|
|
|
orig["CheckID"] = orig["ID"]
|
|
|
|
delete(orig, "ID")
|
|
|
|
delete(orig, "ServiceID")
|
|
|
|
delete(orig, "Token")
|
|
|
|
|
|
|
|
// Now convert to a check type and ensure that all fields left match.
|
|
|
|
chk := def.CheckType()
|
|
|
|
copy := mapFields(chk)
|
|
|
|
for f, vo := range orig {
|
|
|
|
vc, ok := copy[f]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("struct is missing field %q", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(vo.Interface(), vc.Interface()) {
|
|
|
|
t.Fatalf("copy skipped field %q", f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-25 18:40:42 +00:00
|
|
|
|
|
|
|
func TestCheckDefinitionToCheckType(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
got := &CheckDefinition{
|
|
|
|
ID: "id",
|
|
|
|
Name: "name",
|
|
|
|
Status: "green",
|
|
|
|
Notes: "notes",
|
|
|
|
|
2018-11-07 10:16:03 +00:00
|
|
|
ServiceID: "svcid",
|
|
|
|
Token: "tok",
|
|
|
|
ScriptArgs: []string{"/bin/foo"},
|
|
|
|
HTTP: "someurl",
|
|
|
|
TCP: "host:port",
|
|
|
|
Interval: 1 * time.Second,
|
|
|
|
DockerContainerID: "abc123",
|
|
|
|
Shell: "/bin/ksh",
|
|
|
|
TLSSkipVerify: true,
|
|
|
|
Timeout: 2 * time.Second,
|
|
|
|
TTL: 3 * time.Second,
|
2017-09-25 18:40:42 +00:00
|
|
|
DeregisterCriticalServiceAfter: 4 * time.Second,
|
|
|
|
}
|
|
|
|
want := &CheckType{
|
|
|
|
CheckID: "id",
|
|
|
|
Name: "name",
|
|
|
|
Status: "green",
|
|
|
|
Notes: "notes",
|
|
|
|
|
2018-11-07 10:16:03 +00:00
|
|
|
ScriptArgs: []string{"/bin/foo"},
|
|
|
|
HTTP: "someurl",
|
|
|
|
TCP: "host:port",
|
|
|
|
Interval: 1 * time.Second,
|
|
|
|
DockerContainerID: "abc123",
|
|
|
|
Shell: "/bin/ksh",
|
|
|
|
TLSSkipVerify: true,
|
|
|
|
Timeout: 2 * time.Second,
|
|
|
|
TTL: 3 * time.Second,
|
2017-09-25 18:40:42 +00:00
|
|
|
DeregisterCriticalServiceAfter: 4 * time.Second,
|
|
|
|
}
|
|
|
|
verify.Values(t, "", got.CheckType(), want)
|
|
|
|
}
|