Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/rhevin/apple-compose

go 1.26.4
go 1.27.0

require (
github.com/compose-spec/compose-go/v2 v2.16.2
Expand Down
24 changes: 20 additions & 4 deletions internal/backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ func serviceConfigHash(project string, svc types.ServiceConfig) string {
hashWrite(h, "envfile:"+path)
}
if svc.Entrypoint != nil {
hashWrite(h, "entrypoint:"+strings.Join(svc.Entrypoint, " "))
if len(svc.Entrypoint) <= 1 {
hashWrite(h, "entrypoint:"+strings.Join(svc.Entrypoint, " "))
} else {
// First element is --entrypoint; the rest are args. A space-join hashed the same as one string.
hashWrite(h, "entrypoint:"+svc.Entrypoint[0])
for _, a := range svc.Entrypoint[1:] {
hashWrite(h, "entryarg:"+a)
}
}
}
hashWrite(h, "user:"+svc.User)
hashWrite(h, "workdir:"+svc.WorkingDir)
Expand Down Expand Up @@ -128,10 +136,10 @@ func sortedVolumeKeys(project string, vols []types.ServiceVolumeConfig) []string
for _, v := range vols {
switch v.Type {
case "bind":
keys = append(keys, fmt.Sprintf("bind:%s:%s", v.Source, v.Target))
keys = append(keys, volumeHashKey("bind:"+v.Source, v.Target, v.ReadOnly))
case "volume", "":
if v.Source != "" {
keys = append(keys, fmt.Sprintf("named:%s:%s:%s", project, v.Source, v.Target))
keys = append(keys, volumeHashKey("named:"+project+":"+v.Source, v.Target, v.ReadOnly))
}
default:
keys = append(keys, fmt.Sprintf("%s:%s:%s", v.Type, v.Source, v.Target))
Expand Down Expand Up @@ -188,10 +196,18 @@ func sortedEnvPairs(env types.MappingWithEquals) []string {
return pairs
}

func volumeHashKey(source, target string, readOnly bool) string {
key := source + ":" + target
if readOnly {
key += ":ro"
}
return key
}

func composePortKeys(ports []types.ServicePortConfig) []string {
keys := make([]string, 0, len(ports))
for _, p := range ports {
keys = append(keys, fmt.Sprintf("%s:%d", p.Published, p.Target))
keys = append(keys, publishSpec(p))
}
sort.Strings(keys)
return keys
Expand Down
69 changes: 69 additions & 0 deletions internal/backend/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,75 @@ func TestServiceConfigHash_ChangesOnPlatform(t *testing.T) {
}
}

func TestServiceConfigHash_ChangesOnHostIP(t *testing.T) {
a := types.ServiceConfig{
Name: "web",
Image: "nginx:alpine",
Ports: []types.ServicePortConfig{{Published: "8080", Target: 80}},
}
b := a
b.Ports = []types.ServicePortConfig{{HostIP: "127.0.0.1", Published: "8080", Target: 80}}
if serviceConfigHash("myapp", a) == serviceConfigHash("myapp", b) {
t.Fatal("expected different hash when host IP is set")
}
}

func TestServiceConfigHash_TCPPortDoesNotChangeHash(t *testing.T) {
a := types.ServiceConfig{
Name: "web",
Image: "nginx:alpine",
Ports: []types.ServicePortConfig{{Published: "8080", Target: 80}},
}
b := a
b.Ports = []types.ServicePortConfig{{Published: "8080", Target: 80, Protocol: "tcp"}}
if serviceConfigHash("myapp", a) != serviceConfigHash("myapp", b) {
t.Fatal("tcp is the CLI default and must not change the hash")
}
}

func TestServiceConfigHash_ChangesOnProtocol(t *testing.T) {
a := types.ServiceConfig{
Name: "dns",
Image: "nginx:alpine",
Ports: []types.ServicePortConfig{{Published: "5353", Target: 53, Protocol: "tcp"}},
}
b := a
b.Ports = []types.ServicePortConfig{{Published: "5353", Target: 53, Protocol: "udp"}}
if serviceConfigHash("myapp", a) == serviceConfigHash("myapp", b) {
t.Fatal("expected different hash for udp")
}
}

func TestServiceConfigHash_ChangesOnVolumeReadOnly(t *testing.T) {
a := types.ServiceConfig{
Name: "db",
Image: "postgres:16",
Volumes: []types.ServiceVolumeConfig{
{Type: "bind", Source: "/host/data", Target: "/data"},
},
}
b := a
b.Volumes = []types.ServiceVolumeConfig{
{Type: "bind", Source: "/host/data", Target: "/data", ReadOnly: true},
}
if serviceConfigHash("myapp", a) == serviceConfigHash("myapp", b) {
t.Fatal("expected different hash for a read-only mount")
}
}

func TestServiceConfigHash_EntrypointArgsNotJoined(t *testing.T) {
split := types.ServiceConfig{
Name: "web",
Image: "nginx:alpine",
Entrypoint: types.ShellCommand{"sh", "-c", "echo hi"},
}
joined := split
joined.Entrypoint = types.ShellCommand{"sh -c echo hi"}
if serviceConfigHash("myapp", split) == serviceConfigHash("myapp", joined) {
t.Fatal("multi-element entrypoint must not hash like one joined string")
}
}

func TestServiceConfigHash_ChangesOnDNS(t *testing.T) {
a := types.ServiceConfig{Name: "web", Image: "nginx:alpine", DNS: types.StringList{"1.1.1.1"}}
b := types.ServiceConfig{Name: "web", Image: "nginx:alpine", DNS: types.StringList{"8.8.8.8"}}
Expand Down
Loading