1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/cnbutils/target_image_test.go
Ralf Pannemans c97625e840
feat(cnbBuild) containerImageName will be defaulted if possible (#3437)
Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>
Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
Co-authored-by: Sumit Kulhadia <sumit.kulhadia@sap.com>
2022-01-14 16:49:45 +01:00

84 lines
2.4 KiB
Go

package cnbutils_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/cnbutils"
"github.com/stretchr/testify/assert"
)
func TestGetImageName(t *testing.T) {
t.Parallel()
t.Run("Registry without protocol will add https", func(t *testing.T) {
t.Parallel()
targetImage, err := cnbutils.GetTargetImage("registry", "image", "tag", "", "")
assert.NoError(t, err)
assert.Equal(t, "https", targetImage.ContainerRegistry.Scheme)
assert.Equal(t, "registry", targetImage.ContainerRegistry.Host)
})
t.Run("Registry with protocol will keep it", func(t *testing.T) {
t.Parallel()
targetImage, err := cnbutils.GetTargetImage("http://registry", "image", "tag", "", "")
assert.NoError(t, err)
assert.Equal(t, "http", targetImage.ContainerRegistry.Scheme)
assert.Equal(t, "registry", targetImage.ContainerRegistry.Host)
})
t.Run("Image name is taken from the configuration", func(t *testing.T) {
t.Parallel()
targetImage, err := cnbutils.GetTargetImage("http://registry", "image", "tag", "", "")
assert.NoError(t, err)
assert.Equal(t, "image", targetImage.ContainerImageName)
assert.Equal(t, "tag", targetImage.ContainerImageTag)
})
t.Run("Image name is taken from project.toml", func(t *testing.T) {
t.Parallel()
targetImage, err := cnbutils.GetTargetImage("http://registry", "", "tag", "project-id.0", "")
assert.NoError(t, err)
assert.Equal(t, "project-id-0", targetImage.ContainerImageName)
assert.Equal(t, "tag", targetImage.ContainerImageTag)
})
t.Run("Image name is taken from git repo", func(t *testing.T) {
t.Parallel()
tmpdir, err := ioutil.TempDir("", "cpe")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)
err = os.MkdirAll(filepath.Join(tmpdir, "commonPipelineEnvironment", "git"), os.ModePerm)
assert.NoError(t, err)
err = ioutil.WriteFile(filepath.Join(tmpdir, "commonPipelineEnvironment", "git", "repository"), []byte("repo-name"), os.ModePerm)
assert.NoError(t, err)
targetImage, err := cnbutils.GetTargetImage("http://registry", "", "tag", "", tmpdir)
assert.NoError(t, err)
assert.Equal(t, "repo-name", targetImage.ContainerImageName)
assert.Equal(t, "tag", targetImage.ContainerImageTag)
})
t.Run("throws an error if unable to find image name", func(t *testing.T) {
t.Parallel()
_, err := cnbutils.GetTargetImage("http://registry", "", "tag", "", "")
assert.Error(t, err)
assert.Equal(t, "failed to derive default for image name", err.Error())
})
}