sandboxdata_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package libnetwork
  2. import (
  3. "testing"
  4. "github.com/docker/libnetwork/sandbox"
  5. )
  6. func createEmptyCtrlr() *controller {
  7. return &controller{sandboxes: sandboxTable{}}
  8. }
  9. func createEmptyEndpoint() *endpoint {
  10. return &endpoint{
  11. container: &containerInfo{},
  12. joinInfo: &endpointJoinInfo{},
  13. iFaces: []*endpointInterface{},
  14. }
  15. }
  16. func TestSandboxAddEmpty(t *testing.T) {
  17. ctrlr := createEmptyCtrlr()
  18. ep := createEmptyEndpoint()
  19. if _, err := ctrlr.sandboxAdd("sandbox1", true, ep); err != nil {
  20. t.Fatal(err)
  21. }
  22. if ctrlr.sandboxes["sandbox1"].refCnt != 1 {
  23. t.Fatalf("Unexpected sandbox ref count. Expected 1, got %d",
  24. ctrlr.sandboxes["sandbox1"].refCnt)
  25. }
  26. ctrlr.sandboxRm("sandbox1", ep)
  27. if len(ctrlr.sandboxes) != 0 {
  28. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  29. }
  30. sandbox.GC()
  31. }
  32. func TestSandboxAddMultiPrio(t *testing.T) {
  33. ctrlr := createEmptyCtrlr()
  34. ep1 := createEmptyEndpoint()
  35. ep2 := createEmptyEndpoint()
  36. ep3 := createEmptyEndpoint()
  37. ep1.container.config.prio = 1
  38. ep2.container.config.prio = 2
  39. ep3.container.config.prio = 3
  40. if _, err := ctrlr.sandboxAdd("sandbox1", true, ep1); err != nil {
  41. t.Fatal(err)
  42. }
  43. if _, err := ctrlr.sandboxAdd("sandbox1", true, ep2); err != nil {
  44. t.Fatal(err)
  45. }
  46. if _, err := ctrlr.sandboxAdd("sandbox1", true, ep3); err != nil {
  47. t.Fatal(err)
  48. }
  49. if ctrlr.sandboxes["sandbox1"].refCnt != 3 {
  50. t.Fatalf("Unexpected sandbox ref count. Expected 3, got %d",
  51. ctrlr.sandboxes["sandbox1"].refCnt)
  52. }
  53. if ctrlr.sandboxes["sandbox1"].endpoints[0] != ep3 {
  54. t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
  55. }
  56. ctrlr.sandboxRm("sandbox1", ep3)
  57. if ctrlr.sandboxes["sandbox1"].endpoints[0] != ep2 {
  58. t.Fatal("Expected ep2 to be at the top of the heap after removing ep3. But did not find ep2 at the top of the heap")
  59. }
  60. ctrlr.sandboxRm("sandbox1", ep2)
  61. if ctrlr.sandboxes["sandbox1"].endpoints[0] != ep1 {
  62. t.Fatal("Expected ep1 to be at the top of the heap after removing ep2. But did not find ep1 at the top of the heap")
  63. }
  64. // Re-add ep3 back
  65. if _, err := ctrlr.sandboxAdd("sandbox1", true, ep3); err != nil {
  66. t.Fatal(err)
  67. }
  68. if ctrlr.sandboxes["sandbox1"].endpoints[0] != ep3 {
  69. t.Fatal("Expected ep3 to be at the top of the heap after adding ep3 back. But did not find ep3 at the top of the heap")
  70. }
  71. ctrlr.sandboxRm("sandbox1", ep3)
  72. ctrlr.sandboxRm("sandbox1", ep1)
  73. if len(ctrlr.sandboxes) != 0 {
  74. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  75. }
  76. sandbox.GC()
  77. }
  78. func TestSandboxAddSamePrio(t *testing.T) {
  79. ctrlr := createEmptyCtrlr()
  80. ep1 := createEmptyEndpoint()
  81. ep2 := createEmptyEndpoint()
  82. ep1.network = &network{name: "aaa"}
  83. ep2.network = &network{name: "bbb"}
  84. if _, err := ctrlr.sandboxAdd("sandbox1", true, ep1); err != nil {
  85. t.Fatal(err)
  86. }
  87. if _, err := ctrlr.sandboxAdd("sandbox1", true, ep2); err != nil {
  88. t.Fatal(err)
  89. }
  90. if ctrlr.sandboxes["sandbox1"].refCnt != 2 {
  91. t.Fatalf("Unexpected sandbox ref count. Expected 2, got %d",
  92. ctrlr.sandboxes["sandbox1"].refCnt)
  93. }
  94. if ctrlr.sandboxes["sandbox1"].endpoints[0] != ep1 {
  95. t.Fatal("Expected ep1 to be at the top of the heap. But did not find ep1 at the top of the heap")
  96. }
  97. ctrlr.sandboxRm("sandbox1", ep1)
  98. if ctrlr.sandboxes["sandbox1"].endpoints[0] != ep2 {
  99. t.Fatal("Expected ep2 to be at the top of the heap after removing ep3. But did not find ep2 at the top of the heap")
  100. }
  101. ctrlr.sandboxRm("sandbox1", ep2)
  102. if len(ctrlr.sandboxes) != 0 {
  103. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  104. }
  105. sandbox.GC()
  106. }