Skip to content

Commit

Permalink
Adds a fuzz utility and test to make sure copy is complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
slackpad committed Jun 22, 2017
1 parent ff47d1f commit 1fe25f5
Show file tree
Hide file tree
Showing 7 changed files with 906 additions and 1 deletion.
56 changes: 55 additions & 1 deletion agent/consul/structs/check_defintion_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package structs

import (
"reflect"
"testing"

"github.com/google/gofuzz"
"github.com/hashicorp/consul/api"
"github.com/mitchellh/reflectwalk"
)

func TestAgentStructs_HealthCheck(t *testing.T) {
func TestCheckDefinition_Defaults(t *testing.T) {
t.Parallel()
def := CheckDefinition{}
check := def.HealthCheck("node1")
Expand All @@ -16,3 +19,54 @@ func TestAgentStructs_HealthCheck(t *testing.T) {
t.Fatalf("bad: %v", check.Status)
}
}

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)
}
}
}
67 changes: 67 additions & 0 deletions vendor/github.com/google/gofuzz/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

202 changes: 202 additions & 0 deletions vendor/github.com/google/gofuzz/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1fe25f5

Please sign in to comment.