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

     1  package build
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/jfrog/build-info-go/utils"
    10  	"github.com/jfrog/jfrog-cli-core/v2/common/project"
    11  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    12  	testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
    13  
    14  	"github.com/spf13/viper"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  const (
    19  	host              = "proxy.mydomain"
    20  	port              = "8888"
    21  	username          = "login"
    22  	password          = "password"
    23  	httpProxyForTest  = "http://" + username + ":" + password + "@" + host + ":" + port
    24  	httpsHost         = "proxy.mydomains"
    25  	httpsPort         = "8889"
    26  	httpsUsername     = "logins"
    27  	httpsPassword     = "passwords"
    28  	httpsProxyForTest = "http://" + httpsUsername + ":" + httpsPassword + "@" + httpsHost + ":" + httpsPort
    29  )
    30  
    31  func GetTestDataPath() (string, error) {
    32  	dir, err := os.Getwd()
    33  	if err != nil {
    34  		return "", errorutils.CheckError(err)
    35  	}
    36  	return filepath.Join(dir, "testdata"), nil
    37  }
    38  
    39  func TestCreateDefaultPropertiesFile(t *testing.T) {
    40  	proxyOrg := getOriginalProxyValue()
    41  	setAndAssertProxy("", t)
    42  	testdataPath, err := GetTestDataPath()
    43  	assert.NoError(t, err)
    44  	data := []struct {
    45  		projectType   project.ProjectType
    46  		expectedProps string
    47  	}{
    48  		{project.Maven, filepath.Join(testdataPath, "expected_maven_test_create_default_properties_file.json")},
    49  		{project.Gradle, filepath.Join(testdataPath, "expected_gradle_test_create_default_properties_file.json")},
    50  	}
    51  	for _, d := range data {
    52  		testCreateDefaultPropertiesFile(d.projectType, d.expectedProps, t)
    53  	}
    54  	setAndAssertProxy(proxyOrg, t)
    55  }
    56  
    57  func testCreateDefaultPropertiesFile(projectType project.ProjectType, expectedPropsFilePath string, t *testing.T) {
    58  	providedConfig := viper.New()
    59  	providedConfig.Set("type", projectType.String())
    60  	expectedProps := map[string]string{}
    61  	assert.NoError(t, utils.Unmarshal(expectedPropsFilePath, &expectedProps))
    62  	props, err := CreateBuildInfoProps("", providedConfig, projectType)
    63  	if err != nil {
    64  		t.Error(err)
    65  	}
    66  	assert.True(t, fmt.Sprint(expectedProps) == fmt.Sprint(props), "unexpected "+projectType.String()+" props. got:\n"+fmt.Sprint(props)+"\nwant: "+fmt.Sprint(expectedProps)+"\n")
    67  }
    68  
    69  func TestCreateSimplePropertiesFileWithHttpProxy(t *testing.T) {
    70  	// Save old configuration
    71  	proxyOrg := getOriginalProxyValue()
    72  
    73  	// Prepare
    74  	setAndAssertProxy(httpProxyForTest, t)
    75  	testdataPath, err := GetTestDataPath()
    76  	assert.NoError(t, err)
    77  
    78  	// Run
    79  	createSimplePropertiesFile(t, filepath.Join(testdataPath, "expected_test_create_simple_properties_file_with_proxy.json"))
    80  
    81  	// Cleanup
    82  	setAndAssertProxy(proxyOrg, t)
    83  }
    84  
    85  func TestCreateSimplePropertiesFileWithNoProxy(t *testing.T) {
    86  	// Save old configuration
    87  	proxyOrg := getOriginalNoProxyValue()
    88  
    89  	// Prepare
    90  	setAndAssertNoProxy(httpProxyForTest, t)
    91  	testdataPath, err := GetTestDataPath()
    92  	assert.NoError(t, err)
    93  
    94  	// Run
    95  	createSimplePropertiesFile(t, filepath.Join(testdataPath, "expected_test_create_simple_properties_file_with_no_proxy.json"))
    96  
    97  	// Cleanup
    98  	setAndAssertNoProxy(proxyOrg, t)
    99  }
   100  
   101  func TestCreateSimplePropertiesFileWithHttpsProxy(t *testing.T) {
   102  	// Save old configuration
   103  	oldProxy := getOriginalHttpsProxyValue()
   104  
   105  	// Prepare
   106  	setAndAssertHttpsProxy(httpsProxyForTest, t)
   107  	testdataPath, err := GetTestDataPath()
   108  	assert.NoError(t, err)
   109  
   110  	// Run
   111  	createSimplePropertiesFile(t, filepath.Join(testdataPath, "expected_test_create_simple_properties_file_with_https_proxy.json"))
   112  
   113  	// Cleanup
   114  	setAndAssertHttpsProxy(oldProxy, t)
   115  }
   116  
   117  func TestCreateSimplePropertiesFileWithHttpAndHttpsProxy(t *testing.T) {
   118  	// Save old configuration
   119  	oldProxy := getOriginalProxyValue()
   120  	oldHttpsProxy := getOriginalHttpsProxyValue()
   121  
   122  	// Prepare
   123  	setAndAssertProxy(httpProxyForTest, t)
   124  	setAndAssertHttpsProxy(httpsProxyForTest, t)
   125  	testdataPath, err := GetTestDataPath()
   126  	assert.NoError(t, err)
   127  
   128  	// Run
   129  	createSimplePropertiesFile(t, filepath.Join(testdataPath, "expected_test_create_simple_properties_file_with_http_https_proxy.json"))
   130  
   131  	// Cleanup
   132  	setAndAssertProxy(oldProxy, t)
   133  	setAndAssertHttpsProxy(oldHttpsProxy, t)
   134  }
   135  
   136  func TestCreateSimplePropertiesFileWithoutProxy(t *testing.T) {
   137  	proxyOrg := getOriginalProxyValue()
   138  	setAndAssertProxy("", t)
   139  	testdataPath, err := GetTestDataPath()
   140  	assert.NoError(t, err)
   141  	createSimplePropertiesFile(t, filepath.Join(testdataPath, "expected_test_create_simple_properties_file_without_proxy.json"))
   142  	setAndAssertProxy(proxyOrg, t)
   143  }
   144  
   145  func createSimplePropertiesFile(t *testing.T, expectedPropsFilePath string) {
   146  	var yamlConfig = map[string]string{
   147  		ResolverPrefix + Url: "http://some.url.com",
   148  		DeployerPrefix + Url: "http://some.other.url.com",
   149  	}
   150  	var expectedProps map[string]interface{}
   151  	assert.NoError(t, utils.Unmarshal(expectedPropsFilePath, &expectedProps))
   152  	vConfig := viper.New()
   153  	vConfig.Set("type", project.Maven.String())
   154  	for k, v := range yamlConfig {
   155  		vConfig.Set(k, v)
   156  	}
   157  	props, err := CreateBuildInfoProps("", vConfig, project.Maven)
   158  	if err != nil {
   159  		t.Error(err)
   160  	}
   161  	assert.True(t, fmt.Sprint(props) == fmt.Sprint(expectedProps))
   162  }
   163  
   164  func compareViperConfigs(t *testing.T, actual, expected *viper.Viper, projectType project.ProjectType) {
   165  	for _, key := range expected.AllKeys() {
   166  		value := expected.GetString(key)
   167  		if !actual.IsSet(key) {
   168  			t.Error("["+projectType.String()+"]: Expected key was not found:", "'"+key+"'")
   169  			continue
   170  		}
   171  		if actual.GetString(key) != value {
   172  			t.Error("["+projectType.String()+"]: Expected:", "('"+key+"','"+value+"'),", "found:", "('"+key+"','"+actual.GetString(key)+"').")
   173  		}
   174  	}
   175  
   176  	for _, key := range actual.AllKeys() {
   177  		value := actual.GetString(key)
   178  		if !expected.IsSet(key) {
   179  			t.Error("["+projectType.String()+"]: Unexpected key, value found:", "('"+key+"','"+value+"')")
   180  		}
   181  	}
   182  }
   183  
   184  func TestSetHttpProxy(t *testing.T) {
   185  	// Save old configuration
   186  	proxyOrg := getOriginalProxyValue()
   187  	backupProxyPass := os.Getenv(httpProxy + Password)
   188  
   189  	// Prepare
   190  	setAndAssertProxy(httpProxyForTest, t)
   191  	vConfig := viper.New()
   192  	expectedConfig := viper.New()
   193  	expectedConfig.Set(httpProxy+Host, host)
   194  	expectedConfig.Set(httpProxy+Port, port)
   195  	expectedConfig.Set(httpProxy+Username, username)
   196  
   197  	// Run
   198  	err := setProxyIfDefined(vConfig)
   199  	assert.NoError(t, err)
   200  
   201  	// Assert
   202  	compareViperConfigs(t, vConfig, expectedConfig, project.Maven)
   203  	assert.Equal(t, password, os.Getenv(httpProxy+Password))
   204  
   205  	// Cleanup
   206  	setAndAssertProxy(proxyOrg, t)
   207  	assert.NoError(t, os.Setenv(httpProxy+Password, backupProxyPass))
   208  }
   209  
   210  func TestSetHttpsProxy(t *testing.T) {
   211  	// Save old configuration
   212  	oldHttpsProxy := getOriginalHttpsProxyValue()
   213  	backupProxyPass := os.Getenv(httpsProxy + Password)
   214  
   215  	// Prepare
   216  	setAndAssertHttpsProxy(httpsProxyForTest, t)
   217  	vConfig := viper.New()
   218  	expectedConfig := viper.New()
   219  	expectedConfig.Set(httpsProxy+Host, httpsHost)
   220  	expectedConfig.Set(httpsProxy+Port, httpsPort)
   221  	expectedConfig.Set(httpsProxy+Username, httpsUsername)
   222  
   223  	// Run
   224  	assert.NoError(t, setProxyIfDefined(vConfig))
   225  
   226  	// Assert
   227  	compareViperConfigs(t, vConfig, expectedConfig, project.Maven)
   228  	assert.Equal(t, httpsPassword, os.Getenv(httpsProxy+Password))
   229  
   230  	// Cleanup
   231  	setAndAssertHttpsProxy(oldHttpsProxy, t)
   232  	assert.NoError(t, os.Setenv(httpsProxy+Password, backupProxyPass))
   233  }
   234  
   235  func getOriginalProxyValue() string {
   236  	return os.Getenv(HttpProxyEnvKey)
   237  }
   238  
   239  func getOriginalNoProxyValue() string {
   240  	return os.Getenv(NoProxyEnvKey)
   241  }
   242  
   243  func getOriginalHttpsProxyValue() string {
   244  	return os.Getenv(HttpsProxyEnvKey)
   245  }
   246  
   247  func setAndAssertProxy(proxy string, t *testing.T) {
   248  	testsutils.SetEnvAndAssert(t, HttpProxyEnvKey, proxy)
   249  }
   250  
   251  func setAndAssertHttpsProxy(proxy string, t *testing.T) {
   252  	testsutils.SetEnvAndAssert(t, HttpsProxyEnvKey, proxy)
   253  }
   254  
   255  func setAndAssertNoProxy(proxy string, t *testing.T) {
   256  	testsutils.SetEnvAndAssert(t, NoProxyEnvKey, proxy)
   257  }
   258  
   259  func TestCreateDefaultConfigWithParams(t *testing.T) {
   260  	params := map[string]any{
   261  		"usewrapper":   true,
   262  		"resolver.url": "http://localhost",
   263  	}
   264  	config := createDefaultConfigWithParams("YAML", "gradle", params)
   265  	assert.True(t, config.IsSet("usewrapper"))
   266  	assert.True(t, config.IsSet("resolver.url"))
   267  	assert.True(t, config.IsSet("type"))
   268  }