github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/utils/buildinfoproperties_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"github.com/jfrog/jfrog-cli-go/utils/log"
     5  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
     6  	"github.com/spf13/viper"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  const (
    12  	host  = "localhost"
    13  	port  = "8888"
    14  	proxy = "http://" + host + ":" + port
    15  )
    16  
    17  func TestCreateDefaultPropertiesFile(t *testing.T) {
    18  	proxyOrg := getOriginalProxyValue()
    19  	setProxy("", t)
    20  
    21  	for index := range ProjectTypes {
    22  		testCreateDefaultPropertiesFile(ProjectType(index), t)
    23  	}
    24  	setProxy(proxyOrg, t)
    25  }
    26  
    27  func testCreateDefaultPropertiesFile(projectType ProjectType, t *testing.T) {
    28  	providedConfig := viper.New()
    29  	providedConfig.Set("type", projectType.String())
    30  
    31  	propsFile, err := CreateBuildInfoPropertiesFile("", "", providedConfig, projectType)
    32  	if err != nil {
    33  		t.Error(err)
    34  	}
    35  	defer os.Remove(propsFile)
    36  
    37  	actualConfig, err := ReadConfigFile(propsFile, PROPERTIES)
    38  	if err != nil {
    39  		t.Error(err)
    40  	}
    41  
    42  	expectedConfig := viper.New()
    43  	for _, partialMapping := range buildTypeConfigMapping[projectType] {
    44  		for propertyKey := range *partialMapping {
    45  			if defaultPropertiesValues[propertyKey] != "" {
    46  				expectedConfig.Set(propertyKey, defaultPropertiesValues[propertyKey])
    47  			}
    48  		}
    49  	}
    50  
    51  	compareViperConfigs(t, actualConfig, expectedConfig, projectType)
    52  }
    53  
    54  func TestCreateSimplePropertiesFileWithProxy(t *testing.T) {
    55  	proxyOrg := getOriginalProxyValue()
    56  	setProxy(proxy, t)
    57  	var propertiesFileConfig = map[string]string{
    58  		"artifactory.resolve.contextUrl": "http://some.url.com",
    59  		"artifactory.publish.contextUrl": "http://some.other.url.com",
    60  		"artifactory.deploy.build.name":  "buildName",
    61  		"artifactory.proxy.host":         host,
    62  		"artifactory.proxy.port":         port,
    63  	}
    64  	createSimplePropertiesFile(t, propertiesFileConfig)
    65  	setProxy(proxyOrg, t)
    66  }
    67  
    68  func TestCreateSimplePropertiesFileWithoutProxy(t *testing.T) {
    69  	proxyOrg := getOriginalProxyValue()
    70  	setProxy("", t)
    71  	var propertiesFileConfig = map[string]string{
    72  		"artifactory.resolve.contextUrl": "http://some.url.com",
    73  		"artifactory.publish.contextUrl": "http://some.other.url.com",
    74  		"artifactory.deploy.build.name":  "buildName",
    75  	}
    76  	createSimplePropertiesFile(t, propertiesFileConfig)
    77  	setProxy(proxyOrg, t)
    78  
    79  }
    80  
    81  func createSimplePropertiesFile(t *testing.T, propertiesFileConfig map[string]string) {
    82  	var yamlConfig = map[string]string{
    83  		RESOLVER_PREFIX + URL: "http://some.url.com",
    84  		DEPLOYER_PREFIX + URL: "http://some.other.url.com",
    85  		BUILD_NAME:            "buildName",
    86  	}
    87  
    88  	vConfig := viper.New()
    89  	vConfig.Set("type", Maven.String())
    90  	for k, v := range yamlConfig {
    91  		vConfig.Set(k, v)
    92  	}
    93  	propsFilePath, err := CreateBuildInfoPropertiesFile("", "", vConfig, Maven)
    94  	if err != nil {
    95  		t.Error(err)
    96  	}
    97  	defer os.Remove(propsFilePath)
    98  
    99  	actualConfig, err := ReadConfigFile(propsFilePath, PROPERTIES)
   100  	if err != nil {
   101  		t.Error(err)
   102  	}
   103  
   104  	expectedConfig := viper.New()
   105  	for _, partialMapping := range buildTypeConfigMapping[Maven] {
   106  		for propertyKey := range *partialMapping {
   107  			if defaultPropertiesValues[propertyKey] != "" {
   108  				expectedConfig.Set(propertyKey, defaultPropertiesValues[propertyKey])
   109  			}
   110  		}
   111  	}
   112  
   113  	for k, v := range propertiesFileConfig {
   114  		expectedConfig.Set(k, v)
   115  	}
   116  
   117  	compareViperConfigs(t, actualConfig, expectedConfig, Maven)
   118  }
   119  
   120  func TestGeneratedBuildInfoFile(t *testing.T) {
   121  	log.SetDefaultLogger()
   122  	var yamlConfig = map[string]string{
   123  		RESOLVER_PREFIX + URL: "http://some.url.com",
   124  		DEPLOYER_PREFIX + URL: "http://some.other.url.com",
   125  	}
   126  	vConfig := viper.New()
   127  	vConfig.Set("type", Maven.String())
   128  	for k, v := range yamlConfig {
   129  		vConfig.Set(k, v)
   130  	}
   131  	propsFilePath, err := CreateBuildInfoPropertiesFile("buildName", "buildNumber", vConfig, Maven)
   132  	if err != nil {
   133  		t.Error(err)
   134  	}
   135  	defer os.Remove(propsFilePath)
   136  
   137  	actualConfig, err := ReadConfigFile(propsFilePath, PROPERTIES)
   138  	if err != nil {
   139  		t.Error(err)
   140  	}
   141  
   142  	generatedBuildInfoKey := "buildInfo.generated.build.info"
   143  	if !actualConfig.IsSet(generatedBuildInfoKey) {
   144  		t.Error(generatedBuildInfoKey, "key does not exists")
   145  	}
   146  	if !fileutils.IsPathExists(actualConfig.GetString(generatedBuildInfoKey), false) {
   147  		t.Error("Path: ", actualConfig.GetString(generatedBuildInfoKey), "does not exists")
   148  	}
   149  	defer os.Remove(actualConfig.GetString(generatedBuildInfoKey))
   150  }
   151  
   152  func compareViperConfigs(t *testing.T, actual, expected *viper.Viper, projectType ProjectType) {
   153  	for _, key := range expected.AllKeys() {
   154  		value := expected.GetString(key)
   155  		if !actual.IsSet(key) {
   156  			t.Error("["+projectType.String()+"]: Expected key was not found:", "'"+key+"'")
   157  			continue
   158  		}
   159  		if actual.GetString(key) != value {
   160  			t.Error("["+projectType.String()+"]: Expected:", "('"+key+"','"+value+"'),", "found:", "('"+key+"','"+actual.GetString(key)+"').")
   161  		}
   162  	}
   163  
   164  	for _, key := range actual.AllKeys() {
   165  		value := actual.GetString(key)
   166  		if !expected.IsSet(key) {
   167  			t.Error("["+projectType.String()+"]: Unexpected key, value found:", "('"+key+"','"+value+"')")
   168  		}
   169  	}
   170  }
   171  
   172  func TestSetProxyIfNeeded(t *testing.T) {
   173  	proxyOrg := getOriginalProxyValue()
   174  	setProxy(proxy, t)
   175  	vConfig := viper.New()
   176  
   177  	err := setProxyIfDefined(vConfig)
   178  	if err != nil {
   179  		t.Error(err)
   180  	}
   181  
   182  	expectedConfig := viper.New()
   183  	expectedConfig.Set(PROXY+HOST, host)
   184  	expectedConfig.Set(PROXY+PORT, port)
   185  	compareViperConfigs(t, vConfig, expectedConfig, Maven)
   186  
   187  	setProxy(proxyOrg, t)
   188  }
   189  
   190  func getOriginalProxyValue() string {
   191  	return os.Getenv(HttpProxy)
   192  }
   193  
   194  func setProxy(proxy string, t *testing.T) {
   195  	err := os.Setenv(HttpProxy, proxy)
   196  	if err != nil {
   197  		t.Error(err)
   198  	}
   199  }