mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
0f4e30e9db
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `ioutil.TempDir` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
99 lines
3.0 KiB
Go
99 lines
3.0 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 := t.TempDir()
|
|
|
|
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("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 = ioutil.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())
|
|
})
|
|
}
|