1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-24 05:37:09 +02:00
sap-jenkins-library/pkg/docker/container_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

144 lines
6.1 KiB
Go

//go:build unit
// +build unit
package docker
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContainerRegistryFromURL(t *testing.T) {
tt := []struct {
url string
expected string
expectedError string
}{
{url: "", expected: "", expectedError: "invalid registry url"},
{url: "invalidUrl", expected: "", expectedError: "invalid registry url"},
{url: "no.protocol.com", expected: "", expectedError: "invalid registry url"},
{url: "no.protocol.com:50000", expected: "", expectedError: "invalid registry url"},
{url: "no.protocol.com:50000/my/path/to/image", expected: "", expectedError: "invalid registry url"},
{url: "no.protocol.com:50000/my/path/to/image:withTag", expected: "", expectedError: "invalid registry url"},
{url: "no.protocol.com:50000/my/path/to/image@withDigest", expected: "", expectedError: "invalid registry url"},
{url: "https://my.registry.com", expected: "my.registry.com"},
{url: "https://my.registry.com:50000", expected: "my.registry.com:50000"},
{url: "https://my.registry.com:50000/", expected: "my.registry.com:50000"},
{url: "https://my.registry.com:50000/my/path/to/image:withTag", expected: "my.registry.com:50000"},
{url: "https://my.registry.com:50000/my/path/to/image@withDigest", expected: "my.registry.com:50000"},
}
for _, test := range tt {
t.Run(test.url, func(t *testing.T) {
got, err := ContainerRegistryFromURL(test.url)
if len(test.expectedError) > 0 {
assert.Contains(t, fmt.Sprint(err), test.expectedError)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expected, got)
})
}
}
func TestContainerRegistryFromImage(t *testing.T) {
tt := []struct {
image string
expected string
expectedError string
}{
{image: "", expected: "", expectedError: "failed to parse image name"},
{image: "onlyImage", expected: "index.docker.io"},
{image: "onlyimage", expected: "index.docker.io"},
{image: "onlyimage:withTag", expected: "index.docker.io"},
{image: "onlyimage@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "index.docker.io"},
{image: "path/to/image", expected: "index.docker.io"},
{image: "my.registry.com/onlyimage", expected: "my.registry.com"},
{image: "my.registry.com:50000/onlyimage", expected: "my.registry.com:50000"},
{image: "my.registry.com:50000/onlyimage:withTag", expected: "my.registry.com:50000"},
{image: "my.registry.com:50000/onlyimage@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "my.registry.com:50000"},
{image: "my.registry.com:50000/path/to/image:withTag", expected: "my.registry.com:50000"},
{image: "my.registry.com:50000/path/to/image@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "my.registry.com:50000"},
}
for _, test := range tt {
t.Run(test.image, func(t *testing.T) {
got, err := ContainerRegistryFromImage(test.image)
if len(test.expectedError) > 0 {
assert.Contains(t, fmt.Sprint(err), test.expectedError)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expected, got)
})
}
}
func TestContainerImageNameTagFromImage(t *testing.T) {
tt := []struct {
image string
expected string
expectedError string
}{
{image: "", expected: "", expectedError: "failed to parse image name"},
{image: "onlyImage", expected: "onlyImage"},
{image: "onlyimage", expected: "onlyimage"},
{image: "onlyimage:withTag", expected: "onlyimage:withTag"},
{image: "onlyimage@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "onlyimage@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205"},
{image: "path/to/image", expected: "path/to/image"},
{image: "my.registry.com/onlyimage", expected: "onlyimage"},
{image: "my.registry.com:50000/onlyimage", expected: "onlyimage"},
{image: "my.registry.com:50000/onlyimage:withTag", expected: "onlyimage:withTag"},
{image: "my.registry.com:50000/onlyimage@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "onlyimage@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205"},
{image: "my.registry.com:50000/path/to/image:withTag", expected: "path/to/image:withTag"},
{image: "my.registry.com:50000/path/to/image@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "path/to/image@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205"},
}
for _, test := range tt {
t.Run(test.image, func(t *testing.T) {
got, err := ContainerImageNameTagFromImage(test.image)
if len(test.expectedError) > 0 {
assert.Contains(t, fmt.Sprint(err), test.expectedError)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expected, got)
})
}
}
func TestContainerImageNameFromImage(t *testing.T) {
tt := []struct {
image string
expected string
expectedError string
}{
{image: "", expected: "", expectedError: "failed to parse image name"},
{image: "onlyImage", expected: "onlyImage"},
{image: "onlyimage", expected: "onlyimage"},
{image: "onlyimage:withTag", expected: "onlyimage"},
{image: "onlyimage@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "onlyimage"},
{image: "path/to/image", expected: "path/to/image"},
{image: "my.registry.com/onlyimage", expected: "onlyimage"},
{image: "my.registry.com:50000/onlyimage", expected: "onlyimage"},
{image: "my.registry.com:50000/onlyimage:withTag", expected: "onlyimage"},
{image: "my.registry.com:50000/onlyimage@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "onlyimage"},
{image: "my.registry.com:50000/path/to/image:withTag", expected: "path/to/image"},
{image: "my.registry.com:50000/path/to/image@sha256:152f65865ae43b143b1e42dacdb5e9c473dd70b3adc5b79af7cf585cc8605205", expected: "path/to/image"},
}
for _, test := range tt {
t.Run(test.image, func(t *testing.T) {
got, err := ContainerImageNameFromImage(test.image)
if len(test.expectedError) > 0 {
assert.Contains(t, fmt.Sprint(err), test.expectedError)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expected, got)
})
}
}