2020-05-18 08:55:25 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-02-28 14:57:23 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/mock"
|
2020-05-18 08:55:25 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
2022-03-23 11:02:00 +02:00
|
|
|
"github.com/google/go-containerregistry/pkg/v1/fake"
|
|
|
|
)
|
2020-05-18 08:55:25 +02:00
|
|
|
|
|
|
|
func TestRunContainerSaveImage(t *testing.T) {
|
|
|
|
telemetryData := telemetry.CustomData{}
|
|
|
|
|
|
|
|
t.Run("success case", func(t *testing.T) {
|
|
|
|
config := containerSaveImageOptions{}
|
2022-03-23 11:02:00 +02:00
|
|
|
config.FilePath = "testfile.tar"
|
2020-05-18 08:55:25 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
dClient := mock.DownloadMock{}
|
2022-02-28 14:57:23 +02:00
|
|
|
files := mock.FilesMock{}
|
2020-05-18 08:55:25 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
cacheFolder, err := files.TempDir("", "containerSaveImage-")
|
2020-05-18 08:55:25 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
dClient.Stub = func(imgRef string, dest string) (v1.Image, error) {
|
|
|
|
files.AddFile(dest, []byte("This is a test"))
|
|
|
|
return &fake.FakeImage{}, nil
|
|
|
|
}
|
2020-05-18 08:55:25 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
filePath, err := runContainerSaveImage(&config, &telemetryData, cacheFolder, cacheFolder, &dClient, &files)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
content, err := files.FileRead(filepath.Join(cacheFolder, "testfile.tar"))
|
2020-05-18 08:55:25 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "This is a test", string(content))
|
2022-01-24 10:48:01 +02:00
|
|
|
|
|
|
|
assert.Contains(t, filePath, "testfile.tar")
|
2020-05-18 08:55:25 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("failure - download image", func(t *testing.T) {
|
|
|
|
config := containerSaveImageOptions{}
|
2022-07-12 15:19:12 +02:00
|
|
|
tmpFolder := t.TempDir()
|
2020-05-18 08:55:25 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
dClient := mock.DownloadMock{ReturnError: "download error"}
|
2022-02-28 14:57:23 +02:00
|
|
|
files := mock.FilesMock{}
|
2022-07-12 15:19:12 +02:00
|
|
|
_, err := runContainerSaveImage(&config, &telemetryData, filepath.Join(tmpFolder, "cache"), tmpFolder, &dClient, &files)
|
2020-05-18 08:55:25 +02:00
|
|
|
assert.EqualError(t, err, "failed to download docker image: download error")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilenameFromContainer(t *testing.T) {
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
rootPath string
|
|
|
|
containerImage string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{rootPath: "", containerImage: "image:tag", expected: "image_tag.tar"},
|
|
|
|
{rootPath: "root/path", containerImage: "image:tag", expected: filepath.Join("root/path", "image_tag.tar")},
|
|
|
|
{rootPath: "", containerImage: "my.registry.com:55555/path/to/my/image:tag", expected: "my_registry_com_55555_path_to_my_image_tag.tar"},
|
|
|
|
{rootPath: "root/path", containerImage: "my.registry.com:55555/path/to/my/image:tag", expected: filepath.Join("root/path", "my_registry_com_55555_path_to_my_image_tag.tar")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tt {
|
|
|
|
assert.Equal(t, test.expected, filenameFromContainer(test.rootPath, test.containerImage))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-02-28 14:57:23 +02:00
|
|
|
|
|
|
|
func TestCorrectContainerDockerConfigEnvVar(t *testing.T) {
|
|
|
|
t.Run("with credentials", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
utilsMock := mock.FilesMock{}
|
|
|
|
utilsMock.CurrentDir = "/tmp/test"
|
|
|
|
|
|
|
|
dockerConfigFile := "myConfig/docker.json"
|
|
|
|
utilsMock.AddFile(dockerConfigFile, []byte("{}"))
|
|
|
|
|
|
|
|
resetValue := os.Getenv("DOCKER_CONFIG")
|
|
|
|
os.Setenv("DOCKER_CONFIG", "")
|
|
|
|
defer os.Setenv("DOCKER_CONFIG", resetValue)
|
|
|
|
|
|
|
|
// test
|
2022-07-21 09:04:21 +02:00
|
|
|
err := correctContainerDockerConfigEnvVar(&containerSaveImageOptions{DockerConfigJSON: dockerConfigFile}, &utilsMock)
|
2022-02-28 14:57:23 +02:00
|
|
|
// assert
|
2022-07-21 09:04:21 +02:00
|
|
|
assert.NoError(t, err)
|
2022-02-28 14:57:23 +02:00
|
|
|
assert.NotNil(t, os.Getenv("DOCKER_CONFIG"))
|
|
|
|
})
|
|
|
|
t.Run("with added credentials", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
utilsMock := mock.FilesMock{}
|
|
|
|
utilsMock.CurrentDir = "/tmp/test"
|
|
|
|
|
|
|
|
dockerConfigFile := "myConfig/docker.json"
|
|
|
|
utilsMock.AddFile(dockerConfigFile, []byte("{}"))
|
|
|
|
|
|
|
|
resetValue := os.Getenv("DOCKER_CONFIG")
|
|
|
|
os.Setenv("DOCKER_CONFIG", "")
|
|
|
|
defer os.Setenv("DOCKER_CONFIG", resetValue)
|
|
|
|
|
|
|
|
// test
|
2022-07-21 09:04:21 +02:00
|
|
|
err := correctContainerDockerConfigEnvVar(&containerSaveImageOptions{DockerConfigJSON: dockerConfigFile, ContainerRegistryURL: "https://test.registry", ContainerRegistryUser: "testuser", ContainerRegistryPassword: "testPassword"}, &utilsMock)
|
2022-02-28 14:57:23 +02:00
|
|
|
// assert
|
2022-07-21 09:04:21 +02:00
|
|
|
assert.NoError(t, err)
|
2022-02-28 14:57:23 +02:00
|
|
|
assert.NotNil(t, os.Getenv("DOCKER_CONFIG"))
|
|
|
|
absoluteFilePath, _ := utilsMock.Abs(fmt.Sprintf("%s/%s", os.Getenv("DOCKER_CONFIG"), "config.json"))
|
|
|
|
content, _ := utilsMock.FileRead(absoluteFilePath)
|
|
|
|
assert.Contains(t, string(content), "https://test.registry")
|
|
|
|
})
|
|
|
|
t.Run("without credentials", func(t *testing.T) {
|
|
|
|
// init
|
|
|
|
utilsMock := mock.FilesMock{}
|
|
|
|
resetValue := os.Getenv("DOCKER_CONFIG")
|
|
|
|
os.Setenv("DOCKER_CONFIG", "")
|
|
|
|
defer os.Setenv("DOCKER_CONFIG", resetValue)
|
|
|
|
// test
|
2022-07-21 09:04:21 +02:00
|
|
|
err := correctContainerDockerConfigEnvVar(&containerSaveImageOptions{}, &utilsMock)
|
2022-02-28 14:57:23 +02:00
|
|
|
// assert
|
2022-07-21 09:04:21 +02:00
|
|
|
assert.NoError(t, err)
|
2022-02-28 14:57:23 +02:00
|
|
|
assert.NotNil(t, os.Getenv("DOCKER_CONFIG"))
|
|
|
|
})
|
|
|
|
}
|