From 3e35c0eb69b0fdae195d8fa372a026977acba737 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 2 Apr 2023 18:57:43 -0600 Subject: btrfsutil: GraphNode: Add a CheckExpectations method --- lib/btrfsutil/graph.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'lib/btrfsutil/graph.go') diff --git a/lib/btrfsutil/graph.go b/lib/btrfsutil/graph.go index db036d8..8e26c08 100644 --- a/lib/btrfsutil/graph.go +++ b/lib/btrfsutil/graph.go @@ -10,6 +10,7 @@ import ( "reflect" "time" + "github.com/datawire/dlib/derror" "github.com/datawire/dlib/dlog" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" @@ -72,6 +73,47 @@ func (n GraphNode) String() string { n.Level, n.Generation, n.Owner, len(n.Items)) } +func (n GraphNode) CheckExpectations(g Graph, exp btrfstree.NodeExpectations) error { + var errs derror.MultiError + if exp.LAddr.OK && n.Addr != exp.LAddr.Val { + errs = append(errs, fmt.Errorf("read from laddr=%v but claims to be at laddr=%v", + exp.LAddr.Val, n.Addr)) + } + if exp.Level.OK && n.Level != exp.Level.Val { + errs = append(errs, fmt.Errorf("expected level=%v but claims to be level=%v", + exp.Level.Val, n.Level)) + } + if n.Level > btrfstree.MaxLevel { + errs = append(errs, fmt.Errorf("maximum level=%v but claims to be level=%v", + btrfstree.MaxLevel, n.Level)) + } + if exp.Generation.OK && n.Generation != exp.Generation.Val { + errs = append(errs, fmt.Errorf("expected generation=%v but claims to be generation=%v", + exp.Generation.Val, n.Generation)) + } + if exp.Owner != nil { + if err := exp.Owner(n.Owner, n.Generation); err != nil { + errs = append(errs, err) + } + } + if n.NumItems(g) == 0 { + errs = append(errs, fmt.Errorf("has no items")) + } else { + if minItem := n.MinItem(g); exp.MinItem.OK && exp.MinItem.Val.Compare(minItem) > 0 { + errs = append(errs, fmt.Errorf("expected minItem>=%v but node has minItem=%v", + exp.MinItem.Val, minItem)) + } + if maxItem := n.MaxItem(g); exp.MaxItem.OK && exp.MaxItem.Val.Compare(maxItem) < 0 { + errs = append(errs, fmt.Errorf("expected maxItem<=%v but node has maxItem=%v", + exp.MaxItem.Val, maxItem)) + } + } + if len(errs) > 0 { + return errs + } + return nil +} + type GraphEdge struct { // It is invalid for both 'FromRoot' and 'FromNode' to be // non-zero. If both are zero, then the GraphEdge is from the -- cgit v1.2.3-54-g00ecf