|
| 1 | +package compose |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + composetypes "github.com/compose-spec/compose-go/v2/types" |
| 7 | +) |
| 8 | + |
| 9 | +func portProject() *composetypes.Project { |
| 10 | + return &composetypes.Project{ |
| 11 | + Services: composetypes.Services{ |
| 12 | + "app": { |
| 13 | + Name: "app", |
| 14 | + Image: "app:dev", |
| 15 | + Ports: []composetypes.ServicePortConfig{ |
| 16 | + {Target: 8080, Published: "8080", Protocol: "tcp"}, |
| 17 | + {Target: 9229, Published: "9229", HostIP: "127.0.0.1", Protocol: "tcp"}, |
| 18 | + }, |
| 19 | + Environment: composetypes.MappingWithEquals{}, |
| 20 | + }, |
| 21 | + "db": { |
| 22 | + Name: "db", |
| 23 | + Image: "postgres:17", |
| 24 | + Ports: []composetypes.ServicePortConfig{ |
| 25 | + // `ports: ["5432"]` — no published side; the daemon |
| 26 | + // picks an ephemeral host port, in the same namespace. |
| 27 | + {Target: 5432}, |
| 28 | + }, |
| 29 | + }, |
| 30 | + "cache": { |
| 31 | + Name: "cache", |
| 32 | + Image: "redis:7", |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Every entry goes, on every service, and the caller learns what was |
| 39 | +// taken so it can say so. Service order is sorted: the engine turns |
| 40 | +// each entry into a warning, and warning order must not depend on map |
| 41 | +// iteration. |
| 42 | +func TestApplyDropHostPorts_DropsEveryEntry(t *testing.T) { |
| 43 | + proj := portProject() |
| 44 | + |
| 45 | + dropped := ApplyDropHostPorts(proj) |
| 46 | + |
| 47 | + for name, svc := range proj.Services { |
| 48 | + if len(svc.Ports) != 0 { |
| 49 | + t.Errorf("service %q kept %d ports, want 0", name, len(svc.Ports)) |
| 50 | + } |
| 51 | + } |
| 52 | + if len(dropped) != 3 { |
| 53 | + t.Fatalf("dropped = %d entries, want 3; got %+v", len(dropped), dropped) |
| 54 | + } |
| 55 | + want := []string{"app", "app", "db"} |
| 56 | + for i, svc := range want { |
| 57 | + if dropped[i].Service != svc { |
| 58 | + t.Errorf("dropped[%d].Service = %q, want %q (sorted by service name)", i, dropped[i].Service, svc) |
| 59 | + } |
| 60 | + } |
| 61 | + if got := dropped[0].String(); got != "8080:8080/tcp" { |
| 62 | + t.Errorf("dropped[0] = %q, want 8080:8080/tcp", got) |
| 63 | + } |
| 64 | + if got := dropped[1].String(); got != "127.0.0.1:9229:9229/tcp" { |
| 65 | + t.Errorf("dropped[1] = %q, want 127.0.0.1:9229:9229/tcp", got) |
| 66 | + } |
| 67 | + // Published unset: still dropped, and rendered without a host side |
| 68 | + // rather than as ":5432/tcp". |
| 69 | + if got := dropped[2].String(); got != "5432/tcp" { |
| 70 | + t.Errorf("dropped[2] = %q, want 5432/tcp", got) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// The drop is surgical: nothing but ports: moves. A service that |
| 75 | +// declared none is not rewritten at all. |
| 76 | +func TestApplyDropHostPorts_LeavesTheRestOfTheServiceAlone(t *testing.T) { |
| 77 | + proj := portProject() |
| 78 | + |
| 79 | + ApplyDropHostPorts(proj) |
| 80 | + |
| 81 | + if got := proj.Services["app"].Image; got != "app:dev" { |
| 82 | + t.Errorf("app image = %q, want app:dev", got) |
| 83 | + } |
| 84 | + if got := proj.Services["cache"].Image; got != "redis:7" { |
| 85 | + t.Errorf("cache image = %q, want redis:7", got) |
| 86 | + } |
| 87 | + if len(proj.Services) != 3 { |
| 88 | + t.Errorf("services = %d, want 3", len(proj.Services)) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestApplyDropHostPorts_NoPortsIsANoOp(t *testing.T) { |
| 93 | + proj := &composetypes.Project{ |
| 94 | + Services: composetypes.Services{"app": {Name: "app", Image: "app:dev"}}, |
| 95 | + } |
| 96 | + if dropped := ApplyDropHostPorts(proj); dropped != nil { |
| 97 | + t.Errorf("dropped = %+v, want nil", dropped) |
| 98 | + } |
| 99 | + if dropped := ApplyDropHostPorts(nil); dropped != nil { |
| 100 | + t.Errorf("nil project: dropped = %+v, want nil", dropped) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// The reason the drop happens here and not inside the orchestrator's |
| 105 | +// portsOf: ConfigHash reads the project's ServiceConfig, so removing |
| 106 | +// the entries is visible to the recreate check. A container created |
| 107 | +// before the option was turned on published the ports and must be |
| 108 | +// replaced, not reused. |
| 109 | +func TestApplyDropHostPorts_ChangesConfigHash(t *testing.T) { |
| 110 | + proj := portProject() |
| 111 | + before := ConfigHash("sha256:x", proj.Services["app"]) |
| 112 | + |
| 113 | + ApplyDropHostPorts(proj) |
| 114 | + |
| 115 | + after := ConfigHash("sha256:x", proj.Services["app"]) |
| 116 | + if before == after { |
| 117 | + t.Error("ApplyDropHostPorts did not change ConfigHash; a container created with the publishes would be reused with them intact") |
| 118 | + } |
| 119 | +} |
0 commit comments