up_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2020 Docker Compose CLI 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 compose
  14. import (
  15. "testing"
  16. "gotest.tools/v3/assert"
  17. "github.com/docker/compose/v5/pkg/api"
  18. )
  19. func TestShouldFollowStartEvent(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. event api.ContainerEvent
  23. attached []string
  24. attachTo []string
  25. want bool
  26. }{
  27. {
  28. name: "ignores non-start events",
  29. event: api.ContainerEvent{
  30. Type: api.ContainerEventExited,
  31. Service: "validator",
  32. },
  33. attachTo: []string{"validator"},
  34. want: false,
  35. },
  36. {
  37. name: "ignores services outside explicit attach selection",
  38. event: api.ContainerEvent{
  39. Type: api.ContainerEventStarted,
  40. Service: "event-bus-validator",
  41. ID: "event-bus-validator-1",
  42. },
  43. attachTo: []string{"validator"},
  44. want: false,
  45. },
  46. {
  47. name: "ignores containers already attached unless restarting",
  48. event: api.ContainerEvent{
  49. Type: api.ContainerEventStarted,
  50. Service: "validator",
  51. ID: "validator-1",
  52. },
  53. attached: []string{"validator-1"},
  54. attachTo: []string{"validator"},
  55. want: false,
  56. },
  57. {
  58. name: "follows restarts for attached service",
  59. event: api.ContainerEvent{
  60. Type: api.ContainerEventStarted,
  61. Service: "validator",
  62. ID: "validator-1",
  63. Restarting: true,
  64. },
  65. attached: []string{"validator-1"},
  66. attachTo: []string{"validator"},
  67. want: true,
  68. },
  69. {
  70. name: "follows selected service when not already attached",
  71. event: api.ContainerEvent{
  72. Type: api.ContainerEventStarted,
  73. Service: "validator",
  74. ID: "validator-2",
  75. },
  76. attachTo: []string{"validator"},
  77. want: true,
  78. },
  79. {
  80. name: "follows service when no explicit attach filter exists",
  81. event: api.ContainerEvent{
  82. Type: api.ContainerEventStarted,
  83. Service: "validator",
  84. ID: "validator-2",
  85. },
  86. want: true,
  87. },
  88. }
  89. for _, tt := range tests {
  90. t.Run(tt.name, func(t *testing.T) {
  91. got := shouldFollowStartEvent(tt.event, tt.attached, tt.attachTo)
  92. assert.Equal(t, got, tt.want)
  93. })
  94. }
  95. }