pod_container_dependencies_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package utils
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. type MockContainer struct {
  7. ID string
  8. Name string
  9. Deps []string
  10. }
  11. func TestTopologicalSortContainers(t *testing.T) {
  12. containers := []MockContainer{
  13. {ID: "c1", Name: "container1"},
  14. {ID: "c2", Name: "container2", Deps: []string{"container1"}},
  15. {ID: "c3", Name: "container3", Deps: []string{"container2"}},
  16. }
  17. mockGetDependencies := func(c MockContainer) []string {
  18. return c.Deps
  19. }
  20. err := TopologicalSortContainers(containers, func(c MockContainer) string { return c.Name }, mockGetDependencies)
  21. if err != nil {
  22. t.Fatalf("Expected no error, got %v", err)
  23. }
  24. }
  25. func TestNewDependencyTopoGraph(t *testing.T) {
  26. containers := []MockContainer{
  27. {ID: "c1", Name: "container1"},
  28. {ID: "c2", Name: "container2", Deps: []string{"container1"}},
  29. {ID: "c3", Name: "container3", Deps: []string{"container2"}},
  30. }
  31. mockGetDependencies := func(c MockContainer) []string {
  32. return c.Deps
  33. }
  34. graph, err := NewDependencyTopoGraph(
  35. containers,
  36. func(c MockContainer) string { return c.ID },
  37. func(c MockContainer) string { return c.Name },
  38. mockGetDependencies,
  39. )
  40. if err != nil {
  41. t.Fatalf("Expected no error, got %v", err)
  42. }
  43. if len(graph.Leafs) != 1 || graph.Leafs[0] != "c1" {
  44. t.Errorf("Expected leafs [c1], got %v", graph.Leafs)
  45. }
  46. }
  47. func TestGetNextBatch(t *testing.T) {
  48. containers := []MockContainer{
  49. {ID: "c1", Name: "container1"},
  50. {ID: "c2", Name: "container2", Deps: []string{"container1"}},
  51. {ID: "c3", Name: "container3", Deps: []string{"container2"}},
  52. }
  53. mockGetDependencies := func(c MockContainer) []string {
  54. return c.Deps
  55. }
  56. mockFetchById := func(id string) MockContainer {
  57. for _, c := range containers {
  58. if c.ID == id {
  59. return c
  60. }
  61. }
  62. return MockContainer{}
  63. }
  64. graph, err := NewDependencyTopoGraph(
  65. containers,
  66. func(c MockContainer) string { return c.ID },
  67. func(c MockContainer) string { return c.Name },
  68. mockGetDependencies,
  69. )
  70. if err != nil {
  71. t.Fatalf("Expected no error, got %v", err)
  72. }
  73. // First batch should be container1
  74. batch1 := graph.GetNextBatch(mockFetchById)
  75. if len(batch1) != 1 || batch1[0].Name != "container1" {
  76. t.Errorf("Expected first batch [container1], got %v", batch1)
  77. }
  78. // Second batch should be container2
  79. batch2 := graph.GetNextBatch(mockFetchById)
  80. if len(batch2) != 1 || batch2[0].Name != "container2" {
  81. t.Errorf("Expected second batch [container2], got %v", batch2)
  82. }
  83. // Third batch should be container3
  84. batch3 := graph.GetNextBatch(mockFetchById)
  85. if len(batch3) != 1 || batch3[0].Name != "container3" {
  86. t.Errorf("Expected third batch [container3], got %v", batch3)
  87. }
  88. // No more batches
  89. batch4 := graph.GetNextBatch(mockFetchById)
  90. if batch4 != nil {
  91. t.Errorf("Expected nil, got %v", batch4)
  92. }
  93. }
  94. func TestCircularDependency(t *testing.T) {
  95. containers := []MockContainer{
  96. {ID: "c1", Name: "container1", Deps: []string{"container2"}},
  97. {ID: "c2", Name: "container2", Deps: []string{"container1"}},
  98. }
  99. mockGetDependencies := func(c MockContainer) []string {
  100. return c.Deps
  101. }
  102. err := TopologicalSortContainers(containers, func(c MockContainer) string { return c.Name }, mockGetDependencies)
  103. if err == nil {
  104. t.Fatal("Expected circular dependency error, got nil")
  105. }
  106. if !strings.Contains(err.Error(), "circular dependency") {
  107. t.Errorf("Expected circular dependency error, got %v", err)
  108. }
  109. }