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
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

101 lines
3.0 KiB
Go

//go:build unit
// +build unit
package cnbutils_test
import (
"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 := t.TempDir()
err := os.MkdirAll(filepath.Join(tmpdir, "commonPipelineEnvironment", "git"), os.ModePerm)
assert.NoError(t, err)
err = os.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("Image name is taken from github repo", func(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()
err := os.MkdirAll(filepath.Join(tmpdir, "commonPipelineEnvironment", "github"), os.ModePerm)
assert.NoError(t, err)
err = os.WriteFile(filepath.Join(tmpdir, "commonPipelineEnvironment", "github", "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())
})
}