test: add some basic unit tests for new utility methods

This commit is contained in:
3wc
2025-09-03 13:33:06 -04:00
parent 90e9e9b5aa
commit 117f64a9d6
3 changed files with 235 additions and 0 deletions

88
pkg/deploy/utils_test.go Normal file
View File

@ -0,0 +1,88 @@
package deploy
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetImageNameAndTag(t *testing.T) {
tests := []struct {
name string
imageName string
expectedName string
expectedTag string
expectError bool
description string
}{
{
name: "standard image with tag",
imageName: "nginx:1.23",
expectedName: "nginx",
expectedTag: "1.23",
expectError: false,
description: "should parse standard image name with tag",
},
{
name: "image with digest",
imageName: "nginx:1.23@sha256:abc123",
expectedName: "nginx",
expectedTag: "1.23",
expectError: false,
description: "should parse image with digest, ignoring digest part",
},
{
name: "image with latest tag",
imageName: "redis:latest",
expectedName: "redis",
expectedTag: "latest",
expectError: false,
description: "should parse image with latest tag",
},
{
name: "image with numeric tag",
imageName: "postgres:14",
expectedName: "postgres",
expectedTag: "14",
expectError: false,
description: "should parse image with numeric tag",
},
{
name: "image with complex name",
imageName: "registry.example.com/myapp/api:v1.2.3",
expectedName: "registry.example.com/myapp/api",
expectedTag: "v1.2.3",
expectError: false,
description: "should parse image with registry prefix and complex name",
},
{
name: "image without tag",
imageName: "nginx",
expectError: true,
description: "should error when no tag present",
},
{
name: "empty image name",
imageName: "",
expectError: true,
description: "should error on empty image name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
name, tag, err := GetImageNameAndTag(tt.imageName)
if tt.expectError {
assert.Error(t, err)
assert.Empty(t, name)
assert.Empty(t, tag)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedName, name)
assert.Equal(t, tt.expectedTag, tag)
}
})
}
}