sandboxdata_test.go 3.5 KB

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