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

     1  package utils
     2  
     3  import (
     4  	"flag"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/codegangsta/cli"
    12  	"github.com/jfrog/jfrog-cli-go/artifactory/utils"
    13  	"github.com/jfrog/jfrog-cli-go/utils/cliutils"
    14  	"github.com/jfrog/jfrog-cli-go/utils/log"
    15  	"github.com/spf13/viper"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func init() {
    20  	os.Setenv(cliutils.LogLevel, "WARN") // Disable "[Info] *** build config successfully created." messages
    21  	log.SetDefaultLogger()
    22  }
    23  
    24  func TestGoConfigFile(t *testing.T) {
    25  	// Set JFROG_CLI_HOME_DIR environment variable
    26  	tempDirPath := createTempEnv(t)
    27  	defer os.RemoveAll(tempDirPath)
    28  
    29  	// Create build config
    30  	context := createContext(ResolutionServerId+"=relServer", ResolutionRepo+"=repo", DeploymentServerId+"=depServer", DeploymentRepo+"=repo-local")
    31  	err := CreateBuildConfig(context, utils.Go)
    32  	assert.NoError(t, err)
    33  
    34  	// Check configuration
    35  	config := checkCommonAndGetConfiguration(t, utils.Go.String(), tempDirPath)
    36  	assert.Equal(t, "relServer", config.GetString("resolver.serverId"))
    37  	assert.Equal(t, "repo", config.GetString("resolver.repo"))
    38  	assert.Equal(t, "depServer", config.GetString("deployer.serverId"))
    39  	assert.Equal(t, "repo-local", config.GetString("deployer.repo"))
    40  }
    41  
    42  func TestPipConfigFile(t *testing.T) {
    43  	// Set JFROG_CLI_HOME_DIR environment variable
    44  	tempDirPath := createTempEnv(t)
    45  	defer os.RemoveAll(tempDirPath)
    46  
    47  	// Create build config
    48  	context := createContext(ResolutionServerId+"=relServer", ResolutionRepo+"=repo", DeploymentServerId+"=depServer", DeploymentRepo+"=repo-local")
    49  	err := CreateBuildConfig(context, utils.Pip)
    50  	assert.NoError(t, err)
    51  
    52  	// Check configuration
    53  	config := checkCommonAndGetConfiguration(t, utils.Pip.String(), tempDirPath)
    54  	assert.Equal(t, "relServer", config.GetString("resolver.serverId"))
    55  	assert.Equal(t, "repo", config.GetString("resolver.repo"))
    56  	assert.Equal(t, "depServer", config.GetString("deployer.serverId"))
    57  	assert.Equal(t, "repo-local", config.GetString("deployer.repo"))
    58  }
    59  
    60  func TestNpmConfigFile(t *testing.T) {
    61  	// Set JFROG_CLI_HOME_DIR environment variable
    62  	tempDirPath := createTempEnv(t)
    63  	defer os.RemoveAll(tempDirPath)
    64  
    65  	// Create build config
    66  	context := createContext(ResolutionServerId+"=relServer", ResolutionRepo+"=repo", DeploymentServerId+"=depServer", DeploymentRepo+"=repo-local")
    67  	err := CreateBuildConfig(context, utils.Npm)
    68  	assert.NoError(t, err)
    69  
    70  	// Check configuration
    71  	config := checkCommonAndGetConfiguration(t, utils.Npm.String(), tempDirPath)
    72  	assert.Equal(t, "relServer", config.GetString("resolver.serverId"))
    73  	assert.Equal(t, "repo", config.GetString("resolver.repo"))
    74  	assert.Equal(t, "depServer", config.GetString("deployer.serverId"))
    75  	assert.Equal(t, "repo-local", config.GetString("deployer.repo"))
    76  }
    77  
    78  func TestNugetConfigFile(t *testing.T) {
    79  	// Set JFROG_CLI_HOME_DIR environment variable
    80  	tempDirPath := createTempEnv(t)
    81  	defer os.RemoveAll(tempDirPath)
    82  
    83  	// Create build config
    84  	context := createContext(ResolutionServerId+"=relServer", ResolutionRepo+"=repo")
    85  	err := CreateBuildConfig(context, utils.Nuget)
    86  	assert.NoError(t, err)
    87  
    88  	// Check configuration
    89  	config := checkCommonAndGetConfiguration(t, utils.Nuget.String(), tempDirPath)
    90  	assert.Equal(t, "relServer", config.GetString("resolver.serverId"))
    91  	assert.Equal(t, "repo", config.GetString("resolver.repo"))
    92  }
    93  
    94  func TestMavenConfigFile(t *testing.T) {
    95  	// Set JFROG_CLI_HOME_DIR environment variable
    96  	tempDirPath := createTempEnv(t)
    97  	defer os.RemoveAll(tempDirPath)
    98  
    99  	// Create build config
   100  	context := createContext(ResolutionServerId+"=relServer", ResolutionReleasesRepo+"=release-repo", ResolutionSnapshotsRepo+"=snapshot-repo",
   101  		DeploymentServerId+"=depServer", DeploymentReleasesRepo+"=release-repo-local", DeploymentSnapshotsRepo+"=snapshot-repo-local")
   102  	err := CreateBuildConfig(context, utils.Maven)
   103  	assert.NoError(t, err)
   104  
   105  	// Check configuration
   106  	config := checkCommonAndGetConfiguration(t, utils.Maven.String(), tempDirPath)
   107  	assert.Equal(t, "relServer", config.GetString("resolver.serverId"))
   108  	assert.Equal(t, "snapshot-repo", config.GetString("resolver.snapshotRepo"))
   109  	assert.Equal(t, "release-repo", config.GetString("resolver.releaseRepo"))
   110  	assert.Equal(t, "depServer", config.GetString("deployer.serverId"))
   111  	assert.Equal(t, "snapshot-repo-local", config.GetString("deployer.snapshotRepo"))
   112  	assert.Equal(t, "release-repo-local", config.GetString("deployer.releaseRepo"))
   113  }
   114  
   115  func TestGradleConfigFile(t *testing.T) {
   116  	// Set JFROG_CLI_HOME_DIR environment variable
   117  	tempDirPath := createTempEnv(t)
   118  	defer os.RemoveAll(tempDirPath)
   119  
   120  	// Create build config
   121  	context := createContext(ResolutionServerId+"=relServer", ResolutionRepo+"=repo", DeploymentServerId+"=depServer", DeploymentRepo+"=repo-local",
   122  		IvyDescPattern+"=[ivy]/[pattern]", IvyArtifactsPattern+"=[artifact]/[pattern]")
   123  	err := CreateBuildConfig(context, utils.Gradle)
   124  	assert.NoError(t, err)
   125  
   126  	// Check configuration
   127  	config := checkCommonAndGetConfiguration(t, utils.Gradle.String(), tempDirPath)
   128  	assert.Equal(t, "relServer", config.GetString("resolver.serverId"))
   129  	assert.Equal(t, "repo", config.GetString("resolver.repo"))
   130  	assert.Equal(t, "depServer", config.GetString("deployer.serverId"))
   131  	assert.Equal(t, "repo-local", config.GetString("deployer.repo"))
   132  	assert.Equal(t, true, config.GetBool("deployer.deployMavenDescriptors"))
   133  	assert.Equal(t, true, config.GetBool("deployer.deployIvyDescriptors"))
   134  	assert.Equal(t, "[ivy]/[pattern]", config.GetString("deployer.ivyPattern"))
   135  	assert.Equal(t, "[artifact]/[pattern]", config.GetString("deployer.artifactPattern"))
   136  	assert.Equal(t, true, config.GetBool("usePlugin"))
   137  	assert.Equal(t, true, config.GetBool("useWrapper"))
   138  }
   139  
   140  func TestGradleConfigFileDefaultPatterns(t *testing.T) {
   141  	// Set JFROG_CLI_HOME_DIR environment variable
   142  	tempDirPath := createTempEnv(t)
   143  	defer os.RemoveAll(tempDirPath)
   144  
   145  	// Create build config
   146  	context := createContext(ResolutionServerId+"=relServer", ResolutionRepo+"=repo", DeploymentServerId+"=depServer", DeploymentRepo+"=repo-local")
   147  	err := CreateBuildConfig(context, utils.Gradle)
   148  	assert.NoError(t, err)
   149  
   150  	// Check configuration
   151  	config := checkCommonAndGetConfiguration(t, utils.Gradle.String(), tempDirPath)
   152  	assert.Equal(t, "relServer", config.GetString("resolver.serverId"))
   153  	assert.Equal(t, "repo", config.GetString("resolver.repo"))
   154  	assert.Equal(t, "depServer", config.GetString("deployer.serverId"))
   155  	assert.Equal(t, "repo-local", config.GetString("deployer.repo"))
   156  	assert.Equal(t, true, config.GetBool("deployer.deployMavenDescriptors"))
   157  	assert.Equal(t, true, config.GetBool("deployer.deployIvyDescriptors"))
   158  	assert.Equal(t, "[organization]/[module]/ivy-[revision].xml", config.GetString("deployer.ivyPattern"))
   159  	assert.Equal(t, "[organization]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]", config.GetString("deployer.artifactPattern"))
   160  	assert.Equal(t, true, config.GetBool("usePlugin"))
   161  	assert.Equal(t, true, config.GetBool("useWrapper"))
   162  }
   163  
   164  func TestValidateConfigResolver(t *testing.T) {
   165  	// Create and check empty config
   166  	configFile := NewConfigFile(utils.Go, createContext())
   167  	err := configFile.validateConfig()
   168  	assert.NoError(t, err)
   169  
   170  	// Check scenarios of serverId and repo
   171  	configFile.Resolver.ServerId = "serverId"
   172  	err = configFile.validateConfig()
   173  	assert.EqualError(t, err, "Resolution repository/ies must be set.")
   174  	configFile.Resolver.Repo = "repo"
   175  	err = configFile.validateConfig()
   176  	assert.NoError(t, err)
   177  	configFile.Resolver.ServerId = ""
   178  	err = configFile.validateConfig()
   179  	assert.EqualError(t, err, "Resolver server ID must be set.")
   180  
   181  	// Check scenarios of serverId and release/snapshot repositories
   182  	configFile.Resolver.ServerId = "serverId"
   183  	configFile.Resolver.SnapshotRepo = "snapshotRepo"
   184  	err = configFile.validateConfig()
   185  	assert.EqualError(t, err, "Resolution snapshot and release repositories must be set.")
   186  	configFile.Resolver.ReleaseRepo = "releaseRepo"
   187  	err = configFile.validateConfig()
   188  	assert.NoError(t, err)
   189  	configFile.Resolver.ServerId = ""
   190  	err = configFile.validateConfig()
   191  	assert.EqualError(t, err, "Resolver server ID must be set.")
   192  }
   193  
   194  func TestValidateConfigDeployer(t *testing.T) {
   195  	// Create and check empty config
   196  	configFile := NewConfigFile(utils.Go, createContext())
   197  	err := configFile.validateConfig()
   198  	assert.NoError(t, err)
   199  
   200  	// Check scenarios of serverId and repo
   201  	configFile.Deployer.ServerId = "serverId"
   202  	err = configFile.validateConfig()
   203  	assert.EqualError(t, err, "Deployment repository/ies must be set.")
   204  	configFile.Deployer.Repo = "repo"
   205  	err = configFile.validateConfig()
   206  	assert.NoError(t, err)
   207  	configFile.Deployer.ServerId = ""
   208  	err = configFile.validateConfig()
   209  	assert.EqualError(t, err, "Deployer server ID must be set.")
   210  
   211  	// Check scenarios of serverId and release/snapshot repositories
   212  	configFile.Deployer.ServerId = "serverId"
   213  	configFile.Deployer.ReleaseRepo = "releaseRepo"
   214  	err = configFile.validateConfig()
   215  	assert.EqualError(t, err, "Deployment snapshot and release repositories must be set.")
   216  	configFile.Deployer.SnapshotRepo = "snapshotRepo"
   217  	err = configFile.validateConfig()
   218  	assert.NoError(t, err)
   219  	configFile.Deployer.ServerId = ""
   220  	err = configFile.validateConfig()
   221  	assert.EqualError(t, err, "Deployer server ID must be set.")
   222  }
   223  
   224  // Set JFROG_CLI_HOME_DIR environment variable to be a new temp directory
   225  func createTempEnv(t *testing.T) string {
   226  	tmpDir, err := ioutil.TempDir("", "configfile_test")
   227  	assert.NoError(t, err)
   228  	err = os.Setenv(cliutils.HomeDir, tmpDir)
   229  	assert.NoError(t, err)
   230  	return tmpDir
   231  }
   232  
   233  // Create new Codegangsta context with all required flags.
   234  func createContext(stringFlags ...string) *cli.Context {
   235  	flagSet := flag.NewFlagSet("TestFlagSet", flag.ContinueOnError)
   236  	flags := setBoolFlags(flagSet, Global, UsesPlugin, UseWrapper, DeployMavenDesc, DeployIvyDesc)
   237  	flags = append(flags, setStringFlags(flagSet, stringFlags...)...)
   238  	flagSet.Parse(flags)
   239  	return cli.NewContext(nil, flagSet, nil)
   240  }
   241  
   242  // Set boolean flags and initialize them to true. Return a slice of them.
   243  func setBoolFlags(flagSet *flag.FlagSet, flags ...string) []string {
   244  	cmdFlags := []string{}
   245  	for _, flag := range flags {
   246  		flagSet.Bool(flag, true, "")
   247  		cmdFlags = append(cmdFlags, "--"+flag)
   248  	}
   249  	return cmdFlags
   250  }
   251  
   252  // Set string flags. Return a slice of their values.
   253  func setStringFlags(flagSet *flag.FlagSet, flags ...string) []string {
   254  	cmdFlags := []string{}
   255  	for _, flag := range flags {
   256  		flagSet.String(strings.Split(flag, "=")[0], "", "")
   257  		cmdFlags = append(cmdFlags, "--"+flag)
   258  	}
   259  	return cmdFlags
   260  }
   261  
   262  // Read yaml configuration from disk, check version and type.
   263  func checkCommonAndGetConfiguration(t *testing.T, projectType string, tempDirPath string) *viper.Viper {
   264  	config, err := utils.ReadConfigFile(filepath.Join(tempDirPath, "projects", projectType+".yaml"), utils.YAML)
   265  	assert.NoError(t, err)
   266  	assert.Equal(t, BUILD_CONF_VERSION, config.GetInt("version"))
   267  	assert.Equal(t, projectType, config.GetString("type"))
   268  	return config
   269  }