sandboxdata_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(sandbox.GenerateKey("sandbox1"), true, ep); err != nil {
  20. t.Fatal(err)
  21. }
  22. ctrlr.sandboxRm(sandbox.GenerateKey("sandbox1"), ep)
  23. ctrlr.LeaveAll("sandbox1")
  24. if len(ctrlr.sandboxes) != 0 {
  25. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  26. }
  27. sandbox.GC()
  28. }
  29. func TestSandboxAddMultiPrio(t *testing.T) {
  30. ctrlr := createEmptyCtrlr()
  31. ep1 := createEmptyEndpoint()
  32. ep2 := createEmptyEndpoint()
  33. ep3 := createEmptyEndpoint()
  34. ep1.container.config.prio = 1
  35. ep2.container.config.prio = 2
  36. ep3.container.config.prio = 3
  37. sKey := sandbox.GenerateKey("sandbox1")
  38. if _, err := ctrlr.sandboxAdd(sKey, true, ep1); err != nil {
  39. t.Fatal(err)
  40. }
  41. if _, err := ctrlr.sandboxAdd(sKey, true, ep2); err != nil {
  42. t.Fatal(err)
  43. }
  44. if _, err := ctrlr.sandboxAdd(sKey, true, ep3); err != nil {
  45. t.Fatal(err)
  46. }
  47. if ctrlr.sandboxes[sKey].endpoints[0] != ep3 {
  48. t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
  49. }
  50. ctrlr.sandboxRm(sKey, ep3)
  51. if ctrlr.sandboxes[sKey].endpoints[0] != ep2 {
  52. 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")
  53. }
  54. ctrlr.sandboxRm(sKey, ep2)
  55. if ctrlr.sandboxes[sKey].endpoints[0] != ep1 {
  56. 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")
  57. }
  58. // Re-add ep3 back
  59. if _, err := ctrlr.sandboxAdd(sKey, true, ep3); err != nil {
  60. t.Fatal(err)
  61. }
  62. if ctrlr.sandboxes[sKey].endpoints[0] != ep3 {
  63. 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")
  64. }
  65. ctrlr.sandboxRm(sKey, ep3)
  66. ctrlr.sandboxRm(sKey, ep1)
  67. if err := ctrlr.LeaveAll("sandbox1"); err != nil {
  68. t.Fatal(err)
  69. }
  70. if len(ctrlr.sandboxes) != 0 {
  71. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  72. }
  73. sandbox.GC()
  74. }
  75. func TestSandboxAddSamePrio(t *testing.T) {
  76. ctrlr := createEmptyCtrlr()
  77. ep1 := createEmptyEndpoint()
  78. ep2 := createEmptyEndpoint()
  79. ep1.network = &network{name: "aaa"}
  80. ep2.network = &network{name: "bbb"}
  81. sKey := sandbox.GenerateKey("sandbox1")
  82. if _, err := ctrlr.sandboxAdd(sKey, true, ep1); err != nil {
  83. t.Fatal(err)
  84. }
  85. if _, err := ctrlr.sandboxAdd(sKey, true, ep2); err != nil {
  86. t.Fatal(err)
  87. }
  88. if ctrlr.sandboxes[sKey].endpoints[0] != ep1 {
  89. t.Fatal("Expected ep1 to be at the top of the heap. But did not find ep1 at the top of the heap")
  90. }
  91. ctrlr.sandboxRm(sKey, ep1)
  92. if ctrlr.sandboxes[sKey].endpoints[0] != ep2 {
  93. 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")
  94. }
  95. ctrlr.sandboxRm(sKey, ep2)
  96. if err := ctrlr.LeaveAll("sandbox1"); err != nil {
  97. t.Fatal(err)
  98. }
  99. if len(ctrlr.sandboxes) != 0 {
  100. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  101. }
  102. sandbox.GC()
  103. }