github.com/jfrog/jfrog-cli@v1.54.1/main_test.go (about) 1 package main 2 3 import ( 4 "flag" 5 "os" 6 "path/filepath" 7 "strconv" 8 "testing" 9 10 "github.com/jfrog/jfrog-cli-core/common/commands" 11 "github.com/jfrog/jfrog-cli-core/utils/coreutils" 12 "github.com/jfrog/jfrog-cli-core/utils/log" 13 14 commandUtils "github.com/jfrog/jfrog-cli-core/artifactory/commands/utils" 15 artifactoryUtils "github.com/jfrog/jfrog-cli-core/artifactory/utils" 16 "github.com/jfrog/jfrog-cli/utils/tests" 17 "github.com/jfrog/jfrog-client-go/artifactory/buildinfo" 18 "github.com/jfrog/jfrog-client-go/utils" 19 "github.com/stretchr/testify/assert" 20 "gopkg.in/yaml.v2" 21 ) 22 23 func TestMain(m *testing.M) { 24 setupIntegrationTests() 25 result := m.Run() 26 tearDownIntegrationTests() 27 os.Exit(result) 28 } 29 30 func setupIntegrationTests() { 31 os.Setenv(coreutils.ReportUsage, "false") 32 // Disable progress bar and confirmation messages. 33 os.Setenv(coreutils.CI, "true") 34 35 flag.Parse() 36 log.SetDefaultLogger() 37 if *tests.TestBintray { 38 InitBintrayTests() 39 } 40 if *tests.TestArtifactory && !*tests.TestArtifactoryProxy { 41 InitArtifactoryTests() 42 } 43 if *tests.TestNpm || *tests.TestGradle || *tests.TestMaven || *tests.TestGo || *tests.TestNuget || *tests.TestPip { 44 InitBuildToolsTests() 45 } 46 if *tests.TestDocker { 47 InitDockerTests() 48 } 49 if *tests.TestDistribution { 50 InitDistributionTests() 51 } 52 if *tests.TestPlugins { 53 InitPluginsTests() 54 } 55 } 56 57 func tearDownIntegrationTests() { 58 if *tests.TestBintray { 59 CleanBintrayTests() 60 } 61 if *tests.TestArtifactory && !*tests.TestArtifactoryProxy { 62 CleanArtifactoryTests() 63 } 64 if *tests.TestNpm || *tests.TestGradle || *tests.TestMaven || *tests.TestGo || *tests.TestNuget || *tests.TestPip || *tests.TestDocker { 65 CleanBuildToolsTests() 66 } 67 if *tests.TestDistribution { 68 CleanDistributionTests() 69 } 70 if *tests.TestPlugins { 71 CleanPluginsTests() 72 } 73 } 74 75 func InitBuildToolsTests() { 76 initArtifactoryCli() 77 cleanUpOldBuilds() 78 cleanUpOldRepositories() 79 tests.AddTimestampToGlobalVars() 80 createRequiredRepos() 81 cleanBuildToolsTest() 82 } 83 84 func CleanBuildToolsTests() { 85 cleanBuildToolsTest() 86 deleteCreatedRepos() 87 } 88 89 func createJfrogHomeConfig(t *testing.T, encryptPassword bool) { 90 wd, err := os.Getwd() 91 assert.NoError(t, err) 92 err = os.Setenv(coreutils.HomeDir, filepath.Join(wd, tests.Out, "jfroghome")) 93 assert.NoError(t, err) 94 var credentials string 95 if *tests.RtAccessToken != "" { 96 credentials = "--access-token=" + *tests.RtAccessToken 97 } else { 98 credentials = "--user=" + *tests.RtUser + " --password=" + *tests.RtPassword 99 } 100 // Delete the default server if exist 101 config, err := commands.GetConfig("default", false) 102 if err == nil && config.ServerId != "" { 103 err = tests.NewJfrogCli(execMain, "jfrog config", "").Exec("rm", "default", "--quiet") 104 } 105 err = tests.NewJfrogCli(execMain, "jfrog config", credentials).Exec("add", "default", "--interactive=false", "--artifactory-url="+*tests.RtUrl, "--enc-password="+strconv.FormatBool(encryptPassword)) 106 assert.NoError(t, err) 107 } 108 109 func prepareHomeDir(t *testing.T) (string, string) { 110 oldHomeDir := os.Getenv(coreutils.HomeDir) 111 // Populate cli config with 'default' server 112 createJfrogHomeConfig(t, true) 113 newHomeDir, err := coreutils.GetJfrogHomeDir() 114 assert.NoError(t, err) 115 return oldHomeDir, newHomeDir 116 } 117 118 func cleanBuildToolsTest() { 119 if *tests.TestNpm || *tests.TestGradle || *tests.TestMaven || *tests.TestGo || *tests.TestNuget || *tests.TestPip { 120 os.Unsetenv(coreutils.HomeDir) 121 tests.CleanFileSystem() 122 } 123 } 124 125 func validateBuildInfo(buildInfo buildinfo.BuildInfo, t *testing.T, expectedDependencies int, expectedArtifacts int, moduleName string, moduleType buildinfo.ModuleType) { 126 if buildInfo.Modules == nil || len(buildInfo.Modules) == 0 { 127 assert.Fail(t, "build info was not generated correctly, no modules were created.") 128 return 129 } 130 validateModule(buildInfo.Modules[0], t, expectedDependencies, expectedArtifacts, 0, moduleName, moduleType) 131 } 132 133 func validateModule(module buildinfo.Module, t *testing.T, expectedDependencies, expectedArtifacts, expectedExcludedArtifacts int, moduleName string, moduleType buildinfo.ModuleType) { 134 assert.Equal(t, moduleName, module.Id, "Unexpected module name") 135 assert.Len(t, module.Dependencies, expectedDependencies, "Incorrect number of dependencies found in the build-info") 136 assert.Len(t, module.Artifacts, expectedArtifacts, "Incorrect number of artifacts found in the build-info") 137 assert.Len(t, module.ExcludedArtifacts, expectedExcludedArtifacts, "Incorrect number of excluded artifacts found in the build-info") 138 assert.Equal(t, module.Type, moduleType) 139 } 140 141 func validateSpecificModule(buildInfo buildinfo.BuildInfo, t *testing.T, expectedDependencies, expectedArtifacts, expectedExcludedArtifacts int, moduleName string, moduleType buildinfo.ModuleType) { 142 for _, module := range buildInfo.Modules { 143 if module.Id == moduleName { 144 validateModule(module, t, expectedDependencies, expectedArtifacts, expectedExcludedArtifacts, moduleName, moduleType) 145 return 146 } 147 } 148 } 149 150 func initArtifactoryCli() { 151 if artifactoryCli != nil { 152 return 153 } 154 *tests.RtUrl = utils.AddTrailingSlashIfNeeded(*tests.RtUrl) 155 artifactoryCli = tests.NewJfrogCli(execMain, "jfrog rt", authenticate(false)) 156 if (*tests.TestArtifactory && !*tests.TestArtifactoryProxy) || *tests.TestPlugins { 157 configCli = createConfigJfrogCLI(authenticate(true)) 158 } 159 } 160 161 func createConfigFileForTest(dirs []string, resolver, deployer string, t *testing.T, confType artifactoryUtils.ProjectType, global bool) error { 162 var filePath string 163 for _, atDir := range dirs { 164 d, err := yaml.Marshal(&commandUtils.ConfigFile{ 165 Version: 1, 166 ConfigType: confType.String(), 167 Resolver: artifactoryUtils.Repository{ 168 Repo: resolver, 169 ServerId: "default", 170 }, 171 Deployer: artifactoryUtils.Repository{ 172 Repo: deployer, 173 ServerId: "default", 174 }, 175 }) 176 if err != nil { 177 return err 178 } 179 if global { 180 filePath = filepath.Join(atDir, "projects") 181 182 } else { 183 filePath = filepath.Join(atDir, ".jfrog", "projects") 184 185 } 186 if _, err := os.Stat(filePath); os.IsNotExist(err) { 187 os.MkdirAll(filePath, 0777) 188 } 189 filePath = filepath.Join(filePath, confType.String()+".yaml") 190 // Create config file to make sure the path is valid 191 f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) 192 assert.NoError(t, err, "Couldn't create file") 193 defer f.Close() 194 _, err = f.Write(d) 195 assert.NoError(t, err) 196 } 197 return nil 198 } 199 200 func runCli(t *testing.T, args ...string) { 201 rtCli := tests.NewJfrogCli(execMain, "jfrog rt", "") 202 err := rtCli.Exec(args...) 203 assert.NoError(t, err) 204 } 205 func runCliWithLegacyBuildtoolsCmd(t *testing.T, args ...string) { 206 rtCli := tests.NewJfrogCli(execMain, "jfrog rt", "") 207 err := rtCli.LegacyBuildToolExec(args...) 208 assert.NoError(t, err) 209 } 210 211 func changeWD(t *testing.T, newPath string) string { 212 prevDir, err := os.Getwd() 213 assert.NoError(t, err) 214 err = os.Chdir(newPath) 215 assert.NoError(t, err) 216 return prevDir 217 } 218 219 // Copy config file from `configFilePath` to `inDir` 220 func createConfigFile(inDir, configFilePath string, t *testing.T) { 221 if _, err := os.Stat(inDir); os.IsNotExist(err) { 222 os.MkdirAll(inDir, 0777) 223 } 224 configFilePath, err := tests.ReplaceTemplateVariables(configFilePath, inDir) 225 assert.NoError(t, err) 226 } 227 228 // setEnvVar sets an environment variable and returns a clean up function that reverts it. 229 func setEnvVar(t *testing.T, key, value string) (cleanUp func()) { 230 oldValue, exist := os.LookupEnv(key) 231 assert.NoError(t, os.Setenv(key, value)) 232 233 if exist { 234 return func() { 235 assert.NoError(t, os.Setenv(key, oldValue)) 236 } 237 } 238 239 return func() { 240 assert.NoError(t, os.Unsetenv(key)) 241 } 242 }