Skip to content

Commit

Permalink
Fixed toolkit missing package rebuilds. (microsoft#6417)
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelWMS authored Oct 16, 2023
1 parent dd5832f commit 699cc64
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 17 deletions.
31 changes: 21 additions & 10 deletions toolkit/tools/internal/pkggraph/pkggraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)<TYPE> (ID=x,STATE=state)""
func (n PkgNode) DOTID() string {
Expand Down Expand Up @@ -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
}

Expand Down
91 changes: 84 additions & 7 deletions toolkit/tools/internal/pkggraph/pkggraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -338,27 +360,39 @@ func TestDOTID(t *testing.T) {
for _, n := range allNodes {
assert.NotPanics(t, func() { n.DOTID() })
}
assert.Equal(t, "A-1-RUN<Meta> (ID=0,TYPE=Run,STATE=Meta)", pkgARun.DOTID())
assert.Equal(t, "D--REMOTE<Unresolved> (ID=0,TYPE=Remote,STATE=Unresolved)", pkgD1Unresolved.DOTID())

expectedARunDOTID := fmt.Sprintf("A-1-RUN<Meta> (ID=%d,TYPE=Run,STATE=Meta)", pkgARun.ID())
assert.Equal(t, expectedARunDOTID, pkgARun.DOTID())

expectedD1UnresolvedDOTID := fmt.Sprintf("D--REMOTE<Unresolved> (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() })
}

// TestNodeString tests the built-in String() function for PkgNodes
func TestNodeString(t *testing.T) {
assert.Equal(t, "A(1,):<ID:0 Type:Run State:Meta Rpm:A.rpm> from 'A.src.rpm' in 'test_repo'", pkgARun.String())
assert.Equal(t, "D(<1,):<ID:0 Type:Remote State:Unresolved Rpm:url://D.rpm> from 'url://D.src.rpm' in 'test_repo'", pkgD1Unresolved.String())
expectedARunString := fmt.Sprintf("A(1,):<ID:%d Type:Run State:Meta Rpm:A.rpm> from 'A.src.rpm' in 'test_repo'", pkgARun.ID())
assert.Equal(t, expectedARunString, pkgARun.String())

expectedD1UnresolvedString := fmt.Sprintf("D(<1,):<ID:%d Type:Remote State:Unresolved Rpm:url://D.rpm> 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():<ID:0 Type:Goal State:Meta Rpm:> from '' in ''", goalNode.String())

emptyNode := PkgNode{}
assert.Panics(t, func() { _ = emptyNode.String() })
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

0 comments on commit 699cc64

Please sign in to comment.