github.com/jfrog/jfrog-cli-core/v2@v2.51.0/common/tests/utils.go (about)

     1  package tests
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
    12  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
    13  	testsutils "github.com/jfrog/jfrog-cli-core/v2/utils/config/tests"
    14  	"github.com/jfrog/jfrog-cli-core/v2/utils/progressbar"
    15  	"github.com/jfrog/jfrog-client-go/access"
    16  	"github.com/jfrog/jfrog-client-go/artifactory"
    17  	"github.com/jfrog/jfrog-client-go/distribution"
    18  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    19  	"github.com/jfrog/jfrog-client-go/utils/log"
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func ConfigTestServer(t *testing.T) (cleanUp func(), err error) {
    24  	cleanUp = testsutils.CreateTempEnv(t, false)
    25  	serverDetails := CreateTestServerDetails()
    26  	err = config.SaveServersConf([]*config.ServerDetails{serverDetails})
    27  	return
    28  }
    29  
    30  func CreateTestServerDetails() *config.ServerDetails {
    31  	return &config.ServerDetails{
    32  		Url:               "http://localhost:8080/",
    33  		ArtifactoryUrl:    "http://localhost:8080/artifactory/",
    34  		DistributionUrl:   "http://localhost:8080/distribution/",
    35  		XrayUrl:           "http://localhost:8080/xray/",
    36  		MissionControlUrl: "http://localhost:8080/mc/",
    37  		PipelinesUrl:      "http://localhost:8080/pipelines/",
    38  		ServerId:          "test",
    39  		IsDefault:         true,
    40  		ClientCertPath:    "ClientCertPath", ClientCertKeyPath: "ClientCertKeyPath",
    41  	}
    42  }
    43  
    44  type restsTestHandler func(w http.ResponseWriter, r *http.Request)
    45  
    46  // Create mock server to test REST APIs.
    47  // testHandler - The HTTP handler of the test
    48  func CreateRestsMockServer(testHandler restsTestHandler) *httptest.Server {
    49  	return httptest.NewServer(http.HandlerFunc(testHandler))
    50  }
    51  
    52  func CreateRtRestsMockServer(t *testing.T, testHandler restsTestHandler) (*httptest.Server, *config.ServerDetails, artifactory.ArtifactoryServicesManager) {
    53  	testServer := CreateRestsMockServer(testHandler)
    54  	serverDetails := &config.ServerDetails{Url: testServer.URL + "/", ArtifactoryUrl: testServer.URL + "/"}
    55  
    56  	serviceManager, err := utils.CreateServiceManager(serverDetails, -1, 0, false)
    57  	assert.NoError(t, err)
    58  	return testServer, serverDetails, serviceManager
    59  }
    60  
    61  func CreateAccessRestsMockServer(t *testing.T, testHandler restsTestHandler) (*httptest.Server, *config.ServerDetails, *access.AccessServicesManager) {
    62  	testServer := CreateRestsMockServer(testHandler)
    63  	serverDetails := &config.ServerDetails{Url: testServer.URL + "/", ServerId: "test-server"}
    64  
    65  	serviceManager, err := utils.CreateAccessServiceManager(serverDetails, false)
    66  	assert.NoError(t, err)
    67  	return testServer, serverDetails, serviceManager
    68  }
    69  
    70  func CreateDsRestsMockServer(t *testing.T, testHandler restsTestHandler) (*httptest.Server, *config.ServerDetails, *distribution.DistributionServicesManager) {
    71  	testServer := CreateRestsMockServer(testHandler)
    72  	serverDetails := &config.ServerDetails{DistributionUrl: testServer.URL + "/"}
    73  
    74  	serviceManager, err := utils.CreateDistributionServiceManager(serverDetails, false)
    75  	assert.NoError(t, err)
    76  	return testServer, serverDetails, serviceManager
    77  }
    78  
    79  // Set progressbar.ShouldInitProgressBar func to always return true
    80  // so the progress bar library will be initialized and progress will be displayed.
    81  // The returned callback sets the original func back.
    82  func MockProgressInitialization() func() {
    83  	originFunc := progressbar.ShouldInitProgressBar
    84  	progressbar.ShouldInitProgressBar = func() (bool, error) { return true, nil }
    85  	return func() {
    86  		progressbar.ShouldInitProgressBar = originFunc
    87  	}
    88  }
    89  
    90  // Replace all variables in the form of ${VARIABLE} in the input file, according to the substitution map.
    91  // path - Path to the input file.
    92  // destPath - Path to the output file. If empty, the output file will be under ${CWD}/tmp/.
    93  func ReplaceTemplateVariables(path, destPath string, subMap map[string]string) (string, error) {
    94  	content, err := os.ReadFile(path)
    95  	if err != nil {
    96  		return "", errorutils.CheckError(err)
    97  	}
    98  
    99  	for name, value := range subMap {
   100  		content = bytes.ReplaceAll(content, []byte(name), []byte(value))
   101  	}
   102  	if destPath == "" {
   103  		destPath, err = os.Getwd()
   104  		if err != nil {
   105  			return "", errorutils.CheckError(err)
   106  		}
   107  		destPath = filepath.Join(destPath, "tmp")
   108  	}
   109  	err = os.MkdirAll(destPath, 0700)
   110  	if err != nil {
   111  		return "", errorutils.CheckError(err)
   112  	}
   113  	specPath := filepath.Join(destPath, filepath.Base(path))
   114  	log.Info("Creating spec file at:", specPath)
   115  	err = os.WriteFile(specPath, content, 0700)
   116  	if err != nil {
   117  		return "", errorutils.CheckError(err)
   118  	}
   119  
   120  	return specPath, nil
   121  }