config_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package docker
  2. import (
  3. "testing"
  4. )
  5. func TestCompareConfig(t *testing.T) {
  6. volumes1 := make(map[string]struct{})
  7. volumes1["/test1"] = struct{}{}
  8. config1 := Config{
  9. Dns: []string{"1.1.1.1", "2.2.2.2"},
  10. PortSpecs: []string{"1111:1111", "2222:2222"},
  11. Env: []string{"VAR1=1", "VAR2=2"},
  12. VolumesFrom: "11111111",
  13. Volumes: volumes1,
  14. }
  15. config2 := Config{
  16. Dns: []string{"0.0.0.0", "2.2.2.2"},
  17. PortSpecs: []string{"1111:1111", "2222:2222"},
  18. Env: []string{"VAR1=1", "VAR2=2"},
  19. VolumesFrom: "11111111",
  20. Volumes: volumes1,
  21. }
  22. config3 := Config{
  23. Dns: []string{"1.1.1.1", "2.2.2.2"},
  24. PortSpecs: []string{"0000:0000", "2222:2222"},
  25. Env: []string{"VAR1=1", "VAR2=2"},
  26. VolumesFrom: "11111111",
  27. Volumes: volumes1,
  28. }
  29. config4 := Config{
  30. Dns: []string{"1.1.1.1", "2.2.2.2"},
  31. PortSpecs: []string{"0000:0000", "2222:2222"},
  32. Env: []string{"VAR1=1", "VAR2=2"},
  33. VolumesFrom: "22222222",
  34. Volumes: volumes1,
  35. }
  36. volumes2 := make(map[string]struct{})
  37. volumes2["/test2"] = struct{}{}
  38. config5 := Config{
  39. Dns: []string{"1.1.1.1", "2.2.2.2"},
  40. PortSpecs: []string{"0000:0000", "2222:2222"},
  41. Env: []string{"VAR1=1", "VAR2=2"},
  42. VolumesFrom: "11111111",
  43. Volumes: volumes2,
  44. }
  45. if CompareConfig(&config1, &config2) {
  46. t.Fatalf("CompareConfig should return false, Dns are different")
  47. }
  48. if CompareConfig(&config1, &config3) {
  49. t.Fatalf("CompareConfig should return false, PortSpecs are different")
  50. }
  51. if CompareConfig(&config1, &config4) {
  52. t.Fatalf("CompareConfig should return false, VolumesFrom are different")
  53. }
  54. if CompareConfig(&config1, &config5) {
  55. t.Fatalf("CompareConfig should return false, Volumes are different")
  56. }
  57. if !CompareConfig(&config1, &config1) {
  58. t.Fatalf("CompareConfig should return true")
  59. }
  60. }
  61. func TestMergeConfig(t *testing.T) {
  62. volumesImage := make(map[string]struct{})
  63. volumesImage["/test1"] = struct{}{}
  64. volumesImage["/test2"] = struct{}{}
  65. configImage := &Config{
  66. Dns: []string{"1.1.1.1", "2.2.2.2"},
  67. PortSpecs: []string{"1111:1111", "2222:2222"},
  68. Env: []string{"VAR1=1", "VAR2=2"},
  69. VolumesFrom: "1111",
  70. Volumes: volumesImage,
  71. }
  72. volumesUser := make(map[string]struct{})
  73. volumesUser["/test3"] = struct{}{}
  74. configUser := &Config{
  75. Dns: []string{"3.3.3.3"},
  76. PortSpecs: []string{"3333:2222", "3333:3333"},
  77. Env: []string{"VAR2=3", "VAR3=3"},
  78. Volumes: volumesUser,
  79. }
  80. if err := MergeConfig(configUser, configImage); err != nil {
  81. t.Error(err)
  82. }
  83. if len(configUser.Dns) != 3 {
  84. t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
  85. }
  86. for _, dns := range configUser.Dns {
  87. if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
  88. t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
  89. }
  90. }
  91. if len(configUser.ExposedPorts) != 3 {
  92. t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
  93. }
  94. for portSpecs := range configUser.ExposedPorts {
  95. if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
  96. t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
  97. }
  98. }
  99. if len(configUser.Env) != 3 {
  100. t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
  101. }
  102. for _, env := range configUser.Env {
  103. if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
  104. t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
  105. }
  106. }
  107. if len(configUser.Volumes) != 3 {
  108. t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
  109. }
  110. for v := range configUser.Volumes {
  111. if v != "/test1" && v != "/test2" && v != "/test3" {
  112. t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
  113. }
  114. }
  115. if configUser.VolumesFrom != "1111" {
  116. t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom)
  117. }
  118. ports, _, err := parsePortSpecs([]string{"0000"})
  119. if err != nil {
  120. t.Error(err)
  121. }
  122. configImage2 := &Config{
  123. ExposedPorts: ports,
  124. }
  125. if err := MergeConfig(configUser, configImage2); err != nil {
  126. t.Error(err)
  127. }
  128. if len(configUser.ExposedPorts) != 4 {
  129. t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
  130. }
  131. for portSpecs := range configUser.ExposedPorts {
  132. if portSpecs.Port() != "0000" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
  133. t.Fatalf("Expected 0000 or 1111 or 2222 or 3333, found %s", portSpecs)
  134. }
  135. }
  136. }