2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
fuzz "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"
|
2020-06-02 16:41:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
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
|
|
|
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 {
|
2019-12-10 02:26:41 +00:00
|
|
|
if !f.Anonymous {
|
|
|
|
w.fields[f.Name] = v
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return reflectwalk.SkipEntry
|
2017-06-23 08:15:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
func mapFields(t *testing.T, obj interface{}) map[string]reflect.Value {
|
2017-06-23 08:15:48 +00:00
|
|
|
w := &walker{make(map[string]reflect.Value)}
|
|
|
|
if err := reflectwalk.Walk(obj, w); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
t.Fatalf("failed to generate map fields for %+v - %v", obj, err)
|
2017-06-23 08:15:48 +00:00
|
|
|
}
|
|
|
|
return w.fields
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckDefinition_CheckType(t *testing.T) {
|
|
|
|
|
|
|
|
// Fuzz a definition to fill all its fields with data.
|
|
|
|
var def CheckDefinition
|
|
|
|
fuzz.New().Fuzz(&def)
|
2019-12-10 02:26:41 +00:00
|
|
|
orig := mapFields(t, def)
|
2017-06-23 08:15:48 +00:00
|
|
|
|
|
|
|
// 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()
|
2019-12-10 02:26:41 +00:00
|
|
|
copy := mapFields(t, chk)
|
2017-06-23 08:15:48 +00:00
|
|
|
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) {
|
|
|
|
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",
|
2021-04-09 19:12:10 +00:00
|
|
|
H2PING: "somehttp2url",
|
2018-11-07 10:16:03 +00:00
|
|
|
TCP: "host:port",
|
|
|
|
Interval: 1 * time.Second,
|
|
|
|
DockerContainerID: "abc123",
|
|
|
|
Shell: "/bin/ksh",
|
2022-06-07 17:27:14 +00:00
|
|
|
OSService: "myco-svctype-svcname-001",
|
2018-11-07 10:16:03 +00:00
|
|
|
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",
|
2021-04-09 19:12:10 +00:00
|
|
|
H2PING: "somehttp2url",
|
2018-11-07 10:16:03 +00:00
|
|
|
TCP: "host:port",
|
|
|
|
Interval: 1 * time.Second,
|
|
|
|
DockerContainerID: "abc123",
|
|
|
|
Shell: "/bin/ksh",
|
2022-06-07 17:27:14 +00:00
|
|
|
OSService: "myco-svctype-svcname-001",
|
2018-11-07 10:16:03 +00:00
|
|
|
TLSSkipVerify: true,
|
|
|
|
Timeout: 2 * time.Second,
|
|
|
|
TTL: 3 * time.Second,
|
2017-09-25 18:40:42 +00:00
|
|
|
DeregisterCriticalServiceAfter: 4 * time.Second,
|
|
|
|
}
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, want, got.CheckType())
|
2017-09-25 18:40:42 +00:00
|
|
|
}
|