Files
velero/pkg/builder/container_builder_test.go
Michal Pryc 4b6708de2c
Some checks failed
Run the E2E test on kind / get-go-version (push) Failing after 1m5s
Run the E2E test on kind / build (push) Has been skipped
Run the E2E test on kind / setup-test-matrix (push) Successful in 4s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / get-go-version (push) Successful in 18s
Main CI / Build (push) Failing after 40s
Close stale issues and PRs / stale (push) Successful in 11s
Trivy Nightly Scan / Trivy nightly scan (velero, main) (push) Failing after 1m45s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-aws, main) (push) Failing after 1m14s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-gcp, main) (push) Failing after 1m7s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-microsoft-azure, main) (push) Failing after 1m6s
Fix plugin init container names exceeding DNS-1123 limit (#9445)
Ensure plugin init container names satisfy DNS-1123 label constraints
(max 63 chars). Long names are truncated with an 8-char hash suffix to
maintain uniqueness.

Fixes: #9444

Signed-off-by: Michal Pryc <mpryc@redhat.com>
2025-12-19 13:39:27 -05:00

150 lines
4.8 KiB
Go

/*
Copyright 2018, 2019 the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package builder
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetName(t *testing.T) {
tests := []struct {
name string
image string
expected string
}{
{
name: "image name with registry hostname and tag",
image: "gcr.io/my-repo/my-image:latest",
expected: "my-repo-my-image",
},
{
name: "image name with registry hostname, without tag",
image: "gcr.io/my-repo/my-image",
expected: "my-repo-my-image",
},
{
name: "image name without registry hostname, with tag",
image: "my-repo/my-image:latest",
expected: "my-repo-my-image",
},
{
name: "image name without registry hostname, without tag",
image: "my-repo/my-image",
expected: "my-repo-my-image",
},
{
name: "image name with registry hostname and port, and tag",
image: "mycustomregistry.io:8080/my-repo/my-image:latest",
expected: "my-repo-my-image",
},
{
name: "image name with no / in it",
image: "my-image",
expected: "my-image",
},
{
name: "image name starting with / in it",
image: "/my-image",
expected: "my-image",
},
{
name: "image name with repo starting with a / as first char",
image: "/my-repo/my-image",
expected: "my-repo-my-image",
},
{
name: "image name with registry hostname, etoomany slashes, without tag",
image: "gcr.io/my-repo/mystery/another/my-image",
expected: "my-repo-mystery-another-my-image",
},
{
name: "image name with registry hostname starting with a / will include the registry name ¯\\_(ツ)_/¯",
image: "/gcr.io/my-repo/mystery/another/my-image",
expected: "gcr-io-my-repo-mystery-another-my-image",
},
{
name: "image repository names containing _ ",
image: "projects.registry.vmware.com/tanzu_migrator/route-2-httpproxy:myTag",
expected: "tanzu-migrator-route-2-httpproxy",
},
{
name: "image repository names containing . ",
image: "projects.registry.vmware.com/tanzu.migrator/route-2-httpproxy:myTag",
expected: "tanzu-migrator-route-2-httpproxy",
},
{
name: "pull by digest",
image: "quay.io/vmware-tanzu/velero@sha256:a75f9e8c3ced3943515f249597be389f8233e1258d289b11184796edceaa7dab",
expected: "vmware-tanzu-velero",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, getName(test.image))
})
}
}
func TestGetNameWithLongPaths(t *testing.T) {
tests := []struct {
name string
image string
validate func(t *testing.T, result string)
}{
{
name: "plugin with deeply nested repository path exceeding 63 characters",
image: "arohcpsvcdev.azurecr.io/redhat-user-workloads/ocp-art-tenant/oadp-hypershift-oadp-plugin-main@sha256:adb840bf3890b4904a8cdda1a74c82cf8d96c52eba9944ac10e795335d6fd450",
validate: func(t *testing.T, result string) {
t.Helper()
// Should not exceed DNS-1123 label limit of 63 characters
assert.LessOrEqual(t, len(result), 63, "Container name must satisfy DNS-1123 label constraints (max 63 chars)")
// Should be exactly 63 characters (truncated with hash)
assert.Len(t, result, 63)
// Should be deterministic
result2 := getName("arohcpsvcdev.azurecr.io/redhat-user-workloads/ocp-art-tenant/oadp-hypershift-oadp-plugin-main@sha256:adb840bf3890b4904a8cdda1a74c82cf8d96c52eba9944ac10e795335d6fd450")
assert.Equal(t, result, result2)
},
},
{
name: "plugin with normal path length (should remain unchanged)",
image: "arohcpsvcdev.azurecr.io/konveyor/velero-plugin-for-microsoft-azure@sha256:b2db5f09da514e817a74c992dcca5f90b77c2ab0b2797eba947d224271d6070e",
validate: func(t *testing.T, result string) {
t.Helper()
assert.Equal(t, "konveyor-velero-plugin-for-microsoft-azure", result)
assert.LessOrEqual(t, len(result), 63)
},
},
{
name: "very long nested path",
image: "registry.example.com/org/team/project/subproject/component/service/application-name-with-many-words:v1.2.3",
validate: func(t *testing.T, result string) {
t.Helper()
assert.LessOrEqual(t, len(result), 63)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := getName(test.image)
test.validate(t, result)
})
}
}