v0.33.7_docker_hostnames

This commit is contained in:
Mateusz Gruszczyński committed 2026-10-22 19:00:26 +02:00
1 parent f462ca7194
commit 7e625c5876
6 files changed
+90 -73

No files matched your search

+37 -1
View File
@@ -41,7 +41,7 @@ func TestServicesFromUsesStableComposeIdentity(t *testing.T) {
if len(got) != 1 {
t.Fatalf("got %d services", len(got))
}
if got[0].Key != "compose:lab/web/1:8080/tcp" || got[0].TechnicalName != "lab-web-8080" {
if got[0].Key != "compose:lab/web/1:8080/tcp" || got[0].TechnicalName != "web-1" {
t.Fatalf("unexpected service %#v", got[0])
}
if len(got[0].Bindings) != 1 || got[0].Bindings[0].PublicPort != 18080 {
@@ -72,6 +72,42 @@ func TestServicesFromKeepsComposeReplicasAndAllBindings(t *testing.T) {
}
}
func TestServiceNamesPreferContainerAndOnlyAppendPortsForCollisions(t *testing.T) {
items := []dockerContainer{
{ID: "a", Names: []string{"/grafana"}, Labels: map[string]string{}, Ports: []dockerPort{{PrivatePort: 3000, PublicPort: 3000, Type: "tcp", IP: "0.0.0.0"}}},
{ID: "b", Names: []string{"/frontend"}, Labels: map[string]string{"com.docker.compose.project": "example", "com.docker.compose.service": "web", "com.docker.compose.container-number": "1"}, Ports: []dockerPort{{PrivatePort: 80, PublicPort: 8080, Type: "tcp", IP: "0.0.0.0"}}},
{ID: "c", Names: []string{"/shared"}, Ports: []dockerPort{{PrivatePort: 80, PublicPort: 8081, Type: "tcp", IP: "0.0.0.0"}, {PrivatePort: 443, PublicPort: 8443, Type: "tcp", IP: "0.0.0.0"}}},
}
got := servicesFrom(items)
names := map[string]string{}
for _, item := range got {
names[item.Key] = item.TechnicalName
}
if names["container:grafana:3000/tcp"] != "grafana" || names["compose:example/web/1:80/tcp"] != "frontend" {
t.Fatalf("incorrect short names: %#v", names)
}
if names["container:shared:80/tcp"] != "shared-80" || names["container:shared:443/tcp"] != "shared-443" {
t.Fatalf("incorrect collision names: %#v", names)
}
}
func TestServiceNamesRemainUniqueAfterDNSNormalization(t *testing.T) {
items := []dockerContainer{
{Names: []string{"/A_B"}, Ports: []dockerPort{{PrivatePort: 80, PublicPort: 18080, Type: "tcp"}}},
{Names: []string{"/a-b"}, Ports: []dockerPort{{PrivatePort: 80, PublicPort: 18081, Type: "tcp"}}},
}
got := servicesFrom(items)
if len(got) != 2 || got[0].TechnicalName == got[1].TechnicalName {
t.Fatalf("names not unique: %#v", got)
}
reversed := servicesFrom([]dockerContainer{items[1], items[0]})
for i := range got {
if got[i].TechnicalName != reversed[i].TechnicalName {
t.Fatalf("unstable names: %#v vs %#v", got, reversed)
}
}
}
func TestServicesFromSkipsLoopbackOnlyBindings(t *testing.T) {
items := []dockerContainer{{ID: "abc", Names: []string{"/app"}, Labels: map[string]string{}, Ports: []dockerPort{
{IP: "127.0.0.1", PrivatePort: 8080, PublicPort: 18080, Type: "tcp"},