github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/transferconfig/utils_test.go (about)

     1  package transferconfig
     2  
     3  import (
     4  	"archive/zip"
     5  	"bytes"
     6  	biutils "github.com/jfrog/build-info-go/utils"
     7  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
     8  	"golang.org/x/exp/slices"
     9  	"io"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestArchiveConfig(t *testing.T) {
    18  	expectedConfigXml := "<config></config>"
    19  	exportPath := filepath.Join("..", "testdata", "artifactory_export")
    20  	buf, err := archiveConfig(exportPath, expectedConfigXml)
    21  	assert.NoError(t, err)
    22  
    23  	zipReader, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
    24  	assert.NoError(t, err)
    25  
    26  	expectedFiles := append(slices.Clone(neededFiles), "artifactory.config.xml")
    27  	assert.Len(t, zipReader.File, len(expectedFiles))
    28  	for _, zipFile := range zipReader.File {
    29  		assert.Contains(t, expectedFiles, zipFile.Name)
    30  		if zipFile.Name == "artifactory.config.xml" {
    31  			f, err := zipFile.Open()
    32  			assert.NoError(t, err)
    33  			defer func(file io.ReadCloser) {
    34  				assert.NoError(t, file.Close())
    35  			}(f)
    36  			actualConfigXml, err := io.ReadAll(f)
    37  			assert.NoError(t, err)
    38  			assert.Equal(t, expectedConfigXml, string(actualConfigXml))
    39  		}
    40  	}
    41  }
    42  
    43  func initHandleTypoInAccessBootstrapTest(t *testing.T) (exportDir string, cleanupFunc func()) {
    44  	exportDir, err := fileutils.CreateTempDir()
    45  	assert.NoError(t, err)
    46  	testDataPath := filepath.Join("..", "testdata", "artifactory_export")
    47  	assert.NoError(t, biutils.CopyDir(testDataPath, exportDir, true, nil))
    48  	cleanupFunc = func() {
    49  		assert.NoError(t, fileutils.RemoveTempDir(exportDir), "Couldn't remove temp dir")
    50  	}
    51  	return
    52  }
    53  
    54  func TestHandleTypoInAccessBootstrapNoTypo(t *testing.T) {
    55  	exportDir, cleanupFunc := initHandleTypoInAccessBootstrapTest(t)
    56  	defer cleanupFunc()
    57  
    58  	// Test access.bootstrap.json
    59  	assert.NoError(t, handleTypoInAccessBootstrap(exportDir))
    60  	assert.FileExists(t, filepath.Join(exportDir, "etc", "access.bootstrap.json"))
    61  }
    62  
    63  func TestHandleTypoInAccessBootstrapWithTypo(t *testing.T) {
    64  	exportDir, cleanupFunc := initHandleTypoInAccessBootstrapTest(t)
    65  	defer cleanupFunc()
    66  
    67  	accessBootstrapPath := filepath.Join(exportDir, "etc", "access.bootstrap.json")
    68  	assert.NoError(t, fileutils.MoveFile(accessBootstrapPath, filepath.Join(exportDir, "etc", "access.boostrap.json")))
    69  
    70  	assert.NoError(t, handleTypoInAccessBootstrap(exportDir))
    71  	assert.FileExists(t, accessBootstrapPath)
    72  }
    73  
    74  func TestHandleTypoInAccessBootstrapNotExist(t *testing.T) {
    75  	exportDir, cleanupFunc := initHandleTypoInAccessBootstrapTest(t)
    76  	defer cleanupFunc()
    77  
    78  	assert.NoError(t, os.Remove(filepath.Join(exportDir, "etc", "access.bootstrap.json")))
    79  	assert.Error(t, handleTypoInAccessBootstrap(exportDir))
    80  }