github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/cnbutils/target_image_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cnbutils_test
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/SAP/jenkins-library/pkg/cnbutils"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestGetImageName(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	t.Run("Registry without protocol will add https", func(t *testing.T) {
    19  		t.Parallel()
    20  
    21  		targetImage, err := cnbutils.GetTargetImage("registry", "image", "tag", "", "")
    22  
    23  		assert.NoError(t, err)
    24  		assert.Equal(t, "https", targetImage.ContainerRegistry.Scheme)
    25  		assert.Equal(t, "registry", targetImage.ContainerRegistry.Host)
    26  	})
    27  
    28  	t.Run("Registry with protocol will keep it", func(t *testing.T) {
    29  		t.Parallel()
    30  
    31  		targetImage, err := cnbutils.GetTargetImage("http://registry", "image", "tag", "", "")
    32  
    33  		assert.NoError(t, err)
    34  		assert.Equal(t, "http", targetImage.ContainerRegistry.Scheme)
    35  		assert.Equal(t, "registry", targetImage.ContainerRegistry.Host)
    36  	})
    37  
    38  	t.Run("Image name is taken from the configuration", func(t *testing.T) {
    39  		t.Parallel()
    40  
    41  		targetImage, err := cnbutils.GetTargetImage("http://registry", "image", "tag", "", "")
    42  
    43  		assert.NoError(t, err)
    44  		assert.Equal(t, "image", targetImage.ContainerImageName)
    45  		assert.Equal(t, "tag", targetImage.ContainerImageTag)
    46  	})
    47  
    48  	t.Run("Image name is taken from project.toml", func(t *testing.T) {
    49  		t.Parallel()
    50  
    51  		targetImage, err := cnbutils.GetTargetImage("http://registry", "", "tag", "project-id.0", "")
    52  
    53  		assert.NoError(t, err)
    54  		assert.Equal(t, "project-id-0", targetImage.ContainerImageName)
    55  		assert.Equal(t, "tag", targetImage.ContainerImageTag)
    56  	})
    57  
    58  	t.Run("Image name is taken from git repo", func(t *testing.T) {
    59  		t.Parallel()
    60  
    61  		tmpdir := t.TempDir()
    62  
    63  		err := os.MkdirAll(filepath.Join(tmpdir, "commonPipelineEnvironment", "git"), os.ModePerm)
    64  		assert.NoError(t, err)
    65  
    66  		err = os.WriteFile(filepath.Join(tmpdir, "commonPipelineEnvironment", "git", "repository"), []byte("repo-name"), os.ModePerm)
    67  		assert.NoError(t, err)
    68  
    69  		targetImage, err := cnbutils.GetTargetImage("http://registry", "", "tag", "", tmpdir)
    70  		assert.NoError(t, err)
    71  		assert.Equal(t, "repo-name", targetImage.ContainerImageName)
    72  		assert.Equal(t, "tag", targetImage.ContainerImageTag)
    73  	})
    74  
    75  	t.Run("Image name is taken from github repo", func(t *testing.T) {
    76  		t.Parallel()
    77  
    78  		tmpdir := t.TempDir()
    79  
    80  		err := os.MkdirAll(filepath.Join(tmpdir, "commonPipelineEnvironment", "github"), os.ModePerm)
    81  		assert.NoError(t, err)
    82  
    83  		err = os.WriteFile(filepath.Join(tmpdir, "commonPipelineEnvironment", "github", "repository"), []byte("repo-name"), os.ModePerm)
    84  		assert.NoError(t, err)
    85  
    86  		targetImage, err := cnbutils.GetTargetImage("http://registry", "", "tag", "", tmpdir)
    87  		assert.NoError(t, err)
    88  		assert.Equal(t, "repo-name", targetImage.ContainerImageName)
    89  		assert.Equal(t, "tag", targetImage.ContainerImageTag)
    90  	})
    91  
    92  	t.Run("throws an error if unable to find image name", func(t *testing.T) {
    93  		t.Parallel()
    94  
    95  		_, err := cnbutils.GetTargetImage("http://registry", "", "tag", "", "")
    96  
    97  		assert.Error(t, err)
    98  		assert.Equal(t, "failed to derive default for image name", err.Error())
    99  	})
   100  }