Version pkg to use docker pkg

This commit is contained in:
mmontes11
2024-09-17 20:02:05 +02:00
committed by Martin Montes
parent de47247ccd
commit e913fead9a
3 changed files with 80 additions and 5 deletions

View File

@ -1,11 +1,24 @@
package docker
import (
"errors"
"fmt"
"github.com/distribution/reference"
)
func GetTag(image string) (string, error) {
ref, err := reference.Parse(image)
if err != nil {
return "", fmt.Errorf("error parsing reference: %v", err)
}
if tagged, ok := ref.(reference.NamedTagged); ok {
return tagged.Tag(), nil
}
return "", errors.New("image does not have a tag")
}
func SetTagOrDigest(sourceImage, targetImage string) (string, error) {
sourceRef, err := reference.Parse(sourceImage)
if err != nil {

View File

@ -2,6 +2,68 @@ package docker
import "testing"
// nolint:lll
func TestGetTag(t *testing.T) {
tests := []struct {
name string
image string
wantTag string
wantErr bool
}{
{
name: "invalid image",
image: "foo",
wantTag: "",
wantErr: true,
},
{
name: "empty tag",
image: "mariadb:",
wantTag: "",
wantErr: true,
},
{
name: "image",
image: "mariadb:10.6",
wantTag: "10.6",
wantErr: false,
},
{
name: "image with namespace",
image: "mariadb/maxscale:23.08.5",
wantTag: "23.08.5",
wantErr: false,
},
{
name: "image with namespace and host",
image: "docker-registry3.mariadb.com/mariadb-operator/mariadb-operator:v0.0.1",
wantTag: "v0.0.1",
wantErr: false,
},
{
name: "digest",
image: "docker-registry3.mariadb.com/mariadb-operator/mariadb-operator@sha256:3f48454b6a33e094af6d23ced54645ec0533cb11854d07738920852ca48e390d",
wantTag: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tag, err := GetTag(tt.image)
if tt.wantTag != tag {
t.Errorf("unexpected image, expected: %v got: %v", tt.wantTag, tag)
}
if tt.wantErr && err == nil {
t.Error("expect error to have occurred, got nil")
}
if !tt.wantErr && err != nil {
t.Errorf("expect error to not have occurred, got: %v", err)
}
})
}
}
// nolint:lll
func TestSetTagOrDigest(t *testing.T) {
tests := []struct {

View File

@ -2,18 +2,18 @@ package version
import (
"fmt"
"strings"
"github.com/hashicorp/go-version"
"github.com/mariadb-operator/mariadb-operator/pkg/docker"
)
func GetMinorVersion(image string) (string, error) {
parts := strings.SplitN(image, ":", 2)
if len(parts) != 2 {
return "", fmt.Errorf("invalid image format: %s", image)
tag, err := docker.GetTag(image)
if err != nil {
return "", fmt.Errorf("invalid image: %v", err)
}
v, err := version.NewSemver(parts[1])
v, err := version.NewSemver(tag)
if err != nil {
return "", fmt.Errorf("error parsing version: %v", err)
}