container_map_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2017 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 containermap
  14. import (
  15. "testing"
  16. )
  17. func TestContainerMap(t *testing.T) {
  18. testCases := []struct {
  19. podUID string
  20. containerNames []string
  21. containerIDs []string
  22. }{
  23. {
  24. "fakePodUID",
  25. []string{"fakeContainerName-1", "fakeContainerName-2"},
  26. []string{"fakeContainerID-1", "fakeContainerName-2"},
  27. },
  28. }
  29. for _, tc := range testCases {
  30. // Build a new containerMap from the testCases, checking proper
  31. // addition, retrieval along the way.
  32. cm := NewContainerMap()
  33. for i := range tc.containerNames {
  34. cm.Add(tc.podUID, tc.containerNames[i], tc.containerIDs[i])
  35. containerID, err := cm.GetContainerID(tc.podUID, tc.containerNames[i])
  36. if err != nil {
  37. t.Errorf("error adding and retrieving containerID: %v", err)
  38. }
  39. if containerID != tc.containerIDs[i] {
  40. t.Errorf("mismatched containerIDs %v, %v", containerID, tc.containerIDs[i])
  41. }
  42. podUID, containerName, err := cm.GetContainerRef(containerID)
  43. if err != nil {
  44. t.Errorf("error retrieving container reference: %v", err)
  45. }
  46. if podUID != tc.podUID {
  47. t.Errorf("mismatched pod UID %v, %v", tc.podUID, podUID)
  48. }
  49. if containerName != tc.containerNames[i] {
  50. t.Errorf("mismatched container Name %v, %v", tc.containerNames[i], containerName)
  51. }
  52. }
  53. // Remove all entries from the containerMap, checking proper removal of
  54. // each along the way.
  55. for i := range tc.containerNames {
  56. cm.RemoveByContainerID(tc.containerIDs[i])
  57. containerID, err := cm.GetContainerID(tc.podUID, tc.containerNames[i])
  58. if err == nil {
  59. t.Errorf("unexpected retrieval of containerID after removal: %v", containerID)
  60. }
  61. cm.Add(tc.podUID, tc.containerNames[i], tc.containerIDs[i])
  62. cm.RemoveByContainerRef(tc.podUID, tc.containerNames[i])
  63. podUID, containerName, err := cm.GetContainerRef(tc.containerIDs[i])
  64. if err == nil {
  65. t.Errorf("unexpected retrieval of container reference after removal: (%v, %v)", podUID, containerName)
  66. }
  67. }
  68. // Verify containerMap now empty.
  69. if len(cm) != 0 {
  70. t.Errorf("unexpected entries still in containerMap: %v", cm)
  71. }
  72. }
  73. }