From 699cc6491372df5d86e810db4bdf59153396603d Mon Sep 17 00:00:00 2001 From: Pawel Winogrodzki Date: Mon, 16 Oct 2023 14:22:34 -0700 Subject: [PATCH] Fixed toolkit missing package rebuilds. (#6417) --- toolkit/tools/internal/pkggraph/pkggraph.go | 31 +++++-- .../tools/internal/pkggraph/pkggraph_test.go | 91 +++++++++++++++++-- 2 files changed, 105 insertions(+), 17 deletions(-) diff --git a/toolkit/tools/internal/pkggraph/pkggraph.go b/toolkit/tools/internal/pkggraph/pkggraph.go index 2a6d5f15270..88d0e4775c3 100644 --- a/toolkit/tools/internal/pkggraph/pkggraph.go +++ b/toolkit/tools/internal/pkggraph/pkggraph.go @@ -672,6 +672,20 @@ func (g *PkgGraph) AllTestNodes() []*PkgNode { }) } +// NodesMatchingFilter returns a list of all nodes satisfying the input filter. +func (g *PkgGraph) NodesMatchingFilter(filter func(*PkgNode) bool) (nodes []*PkgNode) { + foundNodes := 0 + nodes = make([]*PkgNode, 0, g.Nodes().Len()) + for _, n := range graph.NodesOf(g.Nodes()) { + pkgNode := n.(*PkgNode).This + if filter(pkgNode) { + nodes = append(nodes, pkgNode) + foundNodes++ + } + } + return nodes[:foundNodes] +} + // DOTID generates an id for a DOT graph of the form // "pkg(ver:=xyz) (ID=x,STATE=state)"" func (n PkgNode) DOTID() string { @@ -1598,17 +1612,14 @@ func rpmsProvidedBySRPM(srpmPath string, pkgGraph *PkgGraph, graphMutex *sync.RW defer graphMutex.RUnlock() } - rpmsMap := make(map[string]bool) - runNodes := pkgGraph.AllPreferredRunNodes() - for _, node := range runNodes { - if node.SrpmPath != srpmPath { - continue - } - - if node.RpmPath == "" || node.RpmPath == NoRPMPath { - continue - } + filteredNodes := pkgGraph.NodesMatchingFilter(func(node *PkgNode) bool { + return (node.SrpmPath == srpmPath) && + (node.RpmPath != "") && + (node.RpmPath != NoRPMPath) + }) + rpmsMap := make(map[string]bool) + for _, node := range filteredNodes { rpmsMap[node.RpmPath] = true } diff --git a/toolkit/tools/internal/pkggraph/pkggraph_test.go b/toolkit/tools/internal/pkggraph/pkggraph_test.go index 1fb4df53401..fa98d319ff0 100644 --- a/toolkit/tools/internal/pkggraph/pkggraph_test.go +++ b/toolkit/tools/internal/pkggraph/pkggraph_test.go @@ -163,6 +163,9 @@ func addNodeToGraphHelper(g *PkgGraph, node *PkgNode) (newNode *PkgNode, err err node.Architecture, node.SourceRepo, ) + + // Updating node's ID for the sake of equality testing. + node.nodeID = newNode.nodeID return } @@ -222,6 +225,25 @@ func buildTestGraphHelper() (g *PkgGraph, err error) { return } +// Checks if two lists of nodes are equivalent (ignoring order and graph edges) +func checkEqualComponents(t *testing.T, expected, actual []*PkgNode) { + t.Helper() + for _, mustHave := range expected { + foundPackage := false + for _, n := range actual { + foundPackage = foundPackage || mustHave.Equal(n) + } + assert.True(t, foundPackage, "expected to find %s in actual", mustHave.String()) + } + for _, doHave := range actual { + foundPackage := false + for _, n := range expected { + foundPackage = foundPackage || doHave.Equal(n) + } + assert.True(t, foundPackage, "found %s in actual, but it was unexpected", doHave.String()) + } +} + func checkTestGraph(t *testing.T, g *PkgGraph) { // Make sure we got the same graph back! assert.Equal(t, len(allNodes), len(g.AllNodes())) @@ -338,16 +360,23 @@ func TestDOTID(t *testing.T) { for _, n := range allNodes { assert.NotPanics(t, func() { n.DOTID() }) } - assert.Equal(t, "A-1-RUN (ID=0,TYPE=Run,STATE=Meta)", pkgARun.DOTID()) - assert.Equal(t, "D--REMOTE (ID=0,TYPE=Remote,STATE=Unresolved)", pkgD1Unresolved.DOTID()) + + expectedARunDOTID := fmt.Sprintf("A-1-RUN (ID=%d,TYPE=Run,STATE=Meta)", pkgARun.ID()) + assert.Equal(t, expectedARunDOTID, pkgARun.DOTID()) + + expectedD1UnresolvedDOTID := fmt.Sprintf("D--REMOTE (ID=%d,TYPE=Remote,STATE=Unresolved)", pkgD1Unresolved.ID()) + assert.Equal(t, expectedD1UnresolvedDOTID, pkgD1Unresolved.DOTID()) g := NewPkgGraph() goal, err := g.AddGoalNode("test", nil, nil, false) assert.NoError(t, err) - assert.Equal(t, "test (ID=0,TYPE=Goal,STATE=Meta)", goal.DOTID()) + + expectedGoalDOTID := fmt.Sprintf("test (ID=%d,TYPE=Goal,STATE=Meta)", goal.ID()) + assert.Equal(t, expectedGoalDOTID, goal.DOTID()) meta := g.AddMetaNode([]*PkgNode{}, []*PkgNode{}) - assert.Equal(t, "Meta(1) (ID=1,TYPE=PureMeta,STATE=Meta)", meta.DOTID()) + expectedMetaDOTID := fmt.Sprintf("Meta(1) (ID=%d,TYPE=PureMeta,STATE=Meta)", meta.ID()) + assert.Equal(t, expectedMetaDOTID, meta.DOTID()) junk := PkgNode{State: -1, Type: -1} assert.Panics(t, func() { junk.DOTID() }) @@ -355,10 +384,15 @@ func TestDOTID(t *testing.T) { // TestNodeString tests the built-in String() function for PkgNodes func TestNodeString(t *testing.T) { - assert.Equal(t, "A(1,): from 'A.src.rpm' in 'test_repo'", pkgARun.String()) - assert.Equal(t, "D(<1,): from 'url://D.src.rpm' in 'test_repo'", pkgD1Unresolved.String()) + expectedARunString := fmt.Sprintf("A(1,): from 'A.src.rpm' in 'test_repo'", pkgARun.ID()) + assert.Equal(t, expectedARunString, pkgARun.String()) + + expectedD1UnresolvedString := fmt.Sprintf("D(<1,): from 'url://D.src.rpm' in 'test_repo'", pkgD1Unresolved.ID()) + assert.Equal(t, expectedD1UnresolvedString, pkgD1Unresolved.String()) + goalNode := PkgNode{GoalName: "goal", Type: TypeGoal, State: StateMeta} assert.Equal(t, "goal(): from '' in ''", goalNode.String()) + emptyNode := PkgNode{} assert.Panics(t, func() { _ = emptyNode.String() }) } @@ -422,7 +456,6 @@ func TestAddMissingVersion(t *testing.T) { n, err := addNodeToGraphHelper(g, pkgD2Unresolved) assert.NoError(t, err) assert.NotNil(t, n) - } // Add a run node with an invalid version (for a run node) @@ -1070,3 +1103,47 @@ func TestShouldGetSRPMNameFromEmptySRPMPath(t *testing.T) { assert.Equal(t, ".", node.SRPMFileName()) } + +func TestShouldGetAllBuildNodesWithFilter(t *testing.T) { + gOut, err := buildTestGraphHelper() + assert.NoError(t, err) + assert.NotNil(t, gOut) + + foundNodes := gOut.NodesMatchingFilter(func(node *PkgNode) bool { + return node.Type == TypeLocalBuild + }) + checkEqualComponents(t, buildNodes, foundNodes) +} + +func TestShouldGetAllNodesWithFilter(t *testing.T) { + gOut, err := buildTestGraphHelper() + assert.NoError(t, err) + assert.NotNil(t, gOut) + + foundNodes := gOut.NodesMatchingFilter(func(node *PkgNode) bool { + return true + }) + checkEqualComponents(t, allNodes, foundNodes) +} + +func TestShouldGetAllRunNodesWithFilter(t *testing.T) { + gOut, err := buildTestGraphHelper() + assert.NoError(t, err) + assert.NotNil(t, gOut) + + foundNodes := gOut.NodesMatchingFilter(func(node *PkgNode) bool { + return node.Type == TypeLocalRun + }) + checkEqualComponents(t, runNodes, foundNodes) +} + +func TestShouldGetAllUnresolvedNodesWithFilter(t *testing.T) { + gOut, err := buildTestGraphHelper() + assert.NoError(t, err) + assert.NotNil(t, gOut) + + foundNodes := gOut.NodesMatchingFilter(func(node *PkgNode) bool { + return node.State == StateUnresolved + }) + checkEqualComponents(t, unresolvedNodes, foundNodes) +}