compare_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package cache
  2. import (
  3. "testing"
  4. "github.com/docker/docker/api/types/container"
  5. "github.com/docker/docker/api/types/strslice"
  6. "github.com/docker/go-connections/nat"
  7. )
  8. // Just to make life easier
  9. func newPortNoError(proto, port string) nat.Port {
  10. p, _ := nat.NewPort(proto, port)
  11. return p
  12. }
  13. func TestCompare(t *testing.T) {
  14. ports1 := make(nat.PortSet)
  15. ports1[newPortNoError("tcp", "1111")] = struct{}{}
  16. ports1[newPortNoError("tcp", "2222")] = struct{}{}
  17. ports2 := make(nat.PortSet)
  18. ports2[newPortNoError("tcp", "3333")] = struct{}{}
  19. ports2[newPortNoError("tcp", "4444")] = struct{}{}
  20. ports3 := make(nat.PortSet)
  21. ports3[newPortNoError("tcp", "1111")] = struct{}{}
  22. ports3[newPortNoError("tcp", "2222")] = struct{}{}
  23. ports3[newPortNoError("tcp", "5555")] = struct{}{}
  24. volumes1 := make(map[string]struct{})
  25. volumes1["/test1"] = struct{}{}
  26. volumes2 := make(map[string]struct{})
  27. volumes2["/test2"] = struct{}{}
  28. volumes3 := make(map[string]struct{})
  29. volumes3["/test1"] = struct{}{}
  30. volumes3["/test3"] = struct{}{}
  31. envs1 := []string{"ENV1=value1", "ENV2=value2"}
  32. envs2 := []string{"ENV1=value1", "ENV3=value3"}
  33. entrypoint1 := strslice.StrSlice{"/bin/sh", "-c"}
  34. entrypoint2 := strslice.StrSlice{"/bin/sh", "-d"}
  35. entrypoint3 := strslice.StrSlice{"/bin/sh", "-c", "echo"}
  36. cmd1 := strslice.StrSlice{"/bin/sh", "-c"}
  37. cmd2 := strslice.StrSlice{"/bin/sh", "-d"}
  38. cmd3 := strslice.StrSlice{"/bin/sh", "-c", "echo"}
  39. labels1 := map[string]string{"LABEL1": "value1", "LABEL2": "value2"}
  40. labels2 := map[string]string{"LABEL1": "value1", "LABEL2": "value3"}
  41. labels3 := map[string]string{"LABEL1": "value1", "LABEL2": "value2", "LABEL3": "value3"}
  42. sameConfigs := map[*container.Config]*container.Config{
  43. // Empty config
  44. &container.Config{}: {},
  45. // Does not compare hostname, domainname & image
  46. &container.Config{
  47. Hostname: "host1",
  48. Domainname: "domain1",
  49. Image: "image1",
  50. User: "user",
  51. }: {
  52. Hostname: "host2",
  53. Domainname: "domain2",
  54. Image: "image2",
  55. User: "user",
  56. },
  57. // only OpenStdin
  58. &container.Config{OpenStdin: false}: {OpenStdin: false},
  59. // only env
  60. &container.Config{Env: envs1}: {Env: envs1},
  61. // only cmd
  62. &container.Config{Cmd: cmd1}: {Cmd: cmd1},
  63. // only labels
  64. &container.Config{Labels: labels1}: {Labels: labels1},
  65. // only exposedPorts
  66. &container.Config{ExposedPorts: ports1}: {ExposedPorts: ports1},
  67. // only entrypoints
  68. &container.Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint1},
  69. // only volumes
  70. &container.Config{Volumes: volumes1}: {Volumes: volumes1},
  71. }
  72. differentConfigs := map[*container.Config]*container.Config{
  73. nil: nil,
  74. &container.Config{
  75. Hostname: "host1",
  76. Domainname: "domain1",
  77. Image: "image1",
  78. User: "user1",
  79. }: {
  80. Hostname: "host1",
  81. Domainname: "domain1",
  82. Image: "image1",
  83. User: "user2",
  84. },
  85. // only OpenStdin
  86. &container.Config{OpenStdin: false}: {OpenStdin: true},
  87. &container.Config{OpenStdin: true}: {OpenStdin: false},
  88. // only env
  89. &container.Config{Env: envs1}: {Env: envs2},
  90. // only cmd
  91. &container.Config{Cmd: cmd1}: {Cmd: cmd2},
  92. // not the same number of parts
  93. &container.Config{Cmd: cmd1}: {Cmd: cmd3},
  94. // only labels
  95. &container.Config{Labels: labels1}: {Labels: labels2},
  96. // not the same number of labels
  97. &container.Config{Labels: labels1}: {Labels: labels3},
  98. // only exposedPorts
  99. &container.Config{ExposedPorts: ports1}: {ExposedPorts: ports2},
  100. // not the same number of ports
  101. &container.Config{ExposedPorts: ports1}: {ExposedPorts: ports3},
  102. // only entrypoints
  103. &container.Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint2},
  104. // not the same number of parts
  105. &container.Config{Entrypoint: entrypoint1}: {Entrypoint: entrypoint3},
  106. // only volumes
  107. &container.Config{Volumes: volumes1}: {Volumes: volumes2},
  108. // not the same number of labels
  109. &container.Config{Volumes: volumes1}: {Volumes: volumes3},
  110. }
  111. for config1, config2 := range sameConfigs {
  112. if !compare(config1, config2) {
  113. t.Fatalf("Compare should be true for [%v] and [%v]", config1, config2)
  114. }
  115. }
  116. for config1, config2 := range differentConfigs {
  117. if compare(config1, config2) {
  118. t.Fatalf("Compare should be false for [%v] and [%v]", config1, config2)
  119. }
  120. }
  121. }