graph_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package node
  14. import (
  15. "sort"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestDeleteEdges_locked(t *testing.T) {
  20. cases := []struct {
  21. desc string
  22. fromType vertexType
  23. toType vertexType
  24. toNamespace string
  25. toName string
  26. start *Graph
  27. expect *Graph
  28. }{
  29. {
  30. // single edge from a configmap to a node, will delete edge and orphaned configmap
  31. desc: "edges and source orphans are deleted, destination orphans are preserved",
  32. fromType: configMapVertexType,
  33. toType: nodeVertexType,
  34. toNamespace: "",
  35. toName: "node1",
  36. start: func() *Graph {
  37. g := NewGraph()
  38. g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap2")
  39. nodeVertex := g.getOrCreateVertex_locked(nodeVertexType, "", "node1")
  40. configmapVertex := g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap1")
  41. g.graph.SetEdge(newDestinationEdge(configmapVertex, nodeVertex, nodeVertex))
  42. return g
  43. }(),
  44. expect: func() *Graph {
  45. g := NewGraph()
  46. g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap2")
  47. g.getOrCreateVertex_locked(nodeVertexType, "", "node1")
  48. return g
  49. }(),
  50. },
  51. {
  52. // two edges from the same configmap to distinct nodes, will delete one of the edges
  53. desc: "edges are deleted, non-orphans and destination orphans are preserved",
  54. fromType: configMapVertexType,
  55. toType: nodeVertexType,
  56. toNamespace: "",
  57. toName: "node2",
  58. start: func() *Graph {
  59. g := NewGraph()
  60. nodeVertex1 := g.getOrCreateVertex_locked(nodeVertexType, "", "node1")
  61. nodeVertex2 := g.getOrCreateVertex_locked(nodeVertexType, "", "node2")
  62. configmapVertex := g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap1")
  63. g.graph.SetEdge(newDestinationEdge(configmapVertex, nodeVertex1, nodeVertex1))
  64. g.graph.SetEdge(newDestinationEdge(configmapVertex, nodeVertex2, nodeVertex2))
  65. return g
  66. }(),
  67. expect: func() *Graph {
  68. g := NewGraph()
  69. nodeVertex1 := g.getOrCreateVertex_locked(nodeVertexType, "", "node1")
  70. g.getOrCreateVertex_locked(nodeVertexType, "", "node2")
  71. configmapVertex := g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap1")
  72. g.graph.SetEdge(newDestinationEdge(configmapVertex, nodeVertex1, nodeVertex1))
  73. return g
  74. }(),
  75. },
  76. {
  77. desc: "no edges to delete",
  78. fromType: configMapVertexType,
  79. toType: nodeVertexType,
  80. toNamespace: "",
  81. toName: "node1",
  82. start: func() *Graph {
  83. g := NewGraph()
  84. g.getOrCreateVertex_locked(nodeVertexType, "", "node1")
  85. g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap1")
  86. return g
  87. }(),
  88. expect: func() *Graph {
  89. g := NewGraph()
  90. g.getOrCreateVertex_locked(nodeVertexType, "", "node1")
  91. g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap1")
  92. return g
  93. }(),
  94. },
  95. {
  96. desc: "destination vertex does not exist",
  97. fromType: configMapVertexType,
  98. toType: nodeVertexType,
  99. toNamespace: "",
  100. toName: "node1",
  101. start: func() *Graph {
  102. g := NewGraph()
  103. g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap1")
  104. return g
  105. }(),
  106. expect: func() *Graph {
  107. g := NewGraph()
  108. g.getOrCreateVertex_locked(configMapVertexType, "namespace1", "configmap1")
  109. return g
  110. }(),
  111. },
  112. {
  113. desc: "source vertex type doesn't exist",
  114. fromType: configMapVertexType,
  115. toType: nodeVertexType,
  116. toNamespace: "",
  117. toName: "node1",
  118. start: func() *Graph {
  119. g := NewGraph()
  120. g.getOrCreateVertex_locked(nodeVertexType, "", "node1")
  121. return g
  122. }(),
  123. expect: func() *Graph {
  124. g := NewGraph()
  125. g.getOrCreateVertex_locked(nodeVertexType, "", "node1")
  126. return g
  127. }(),
  128. },
  129. }
  130. for _, c := range cases {
  131. t.Run(c.desc, func(t *testing.T) {
  132. c.start.deleteEdges_locked(c.fromType, c.toType, c.toNamespace, c.toName)
  133. // Note: We assert on substructures (graph.Nodes(), graph.Edges()) because the graph tracks
  134. // freed IDs for reuse, which results in an irrelevant inequality between start and expect.
  135. // sort the nodes by ID
  136. // (the slices we get back are from map iteration, where order is not guaranteed)
  137. expectNodes := c.expect.graph.Nodes()
  138. sort.Slice(expectNodes, func(i, j int) bool {
  139. return expectNodes[i].ID() < expectNodes[j].ID()
  140. })
  141. startNodes := c.start.graph.Nodes()
  142. sort.Slice(startNodes, func(i, j int) bool {
  143. return startNodes[i].ID() < startNodes[j].ID()
  144. })
  145. assert.Equal(t, expectNodes, startNodes)
  146. // sort the edges by from ID, then to ID
  147. // (the slices we get back are from map iteration, where order is not guaranteed)
  148. expectEdges := c.expect.graph.Edges()
  149. sort.Slice(expectEdges, func(i, j int) bool {
  150. if expectEdges[i].From().ID() == expectEdges[j].From().ID() {
  151. return expectEdges[i].To().ID() < expectEdges[j].To().ID()
  152. }
  153. return expectEdges[i].From().ID() < expectEdges[j].From().ID()
  154. })
  155. startEdges := c.start.graph.Edges()
  156. sort.Slice(startEdges, func(i, j int) bool {
  157. if startEdges[i].From().ID() == startEdges[j].From().ID() {
  158. return startEdges[i].To().ID() < startEdges[j].To().ID()
  159. }
  160. return startEdges[i].From().ID() < startEdges[j].From().ID()
  161. })
  162. assert.Equal(t, expectEdges, startEdges)
  163. // vertices is a recursive map, no need to sort
  164. assert.Equal(t, c.expect.vertices, c.start.vertices)
  165. })
  166. }
  167. }