mirror of
https://github.com/mariadb-operator/mariadb-operator.git
synced 2025-07-22 18:27:44 +00:00
Version pkg to use docker pkg
This commit is contained in:
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user