github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/buildinfo/addgit_test.go (about) 1 package buildinfo 2 3 import ( 4 "fmt" 5 "github.com/jfrog/jfrog-cli-go/artifactory/utils" 6 "github.com/jfrog/jfrog-cli-go/utils/cliutils" 7 "github.com/jfrog/jfrog-cli-go/utils/log" 8 "github.com/jfrog/jfrog-cli-go/utils/tests" 9 "github.com/jfrog/jfrog-client-go/artifactory/buildinfo" 10 "os" 11 "path/filepath" 12 "strings" 13 "testing" 14 ) 15 16 const ( 17 withGit = "git_test_.git_suffix" 18 withoutGit = "git_test_no_.git_suffix" 19 buildName = "TestExtractGitUrl" 20 ) 21 22 func init() { 23 log.SetDefaultLogger() 24 } 25 26 func TestExtractGitUrlWithDotGit(t *testing.T) { 27 runTest(t, withGit) 28 } 29 30 func TestExtractGitUrlWithoutDotGit(t *testing.T) { 31 runTest(t, withoutGit) 32 } 33 34 func runTest(t *testing.T, originalDir string) { 35 baseDir, dotGitPath := tests.PrepareDotGitDir(t, originalDir, filepath.Join("..", "testdata")) 36 buildDir := getBuildDir(t) 37 checkFailureAndClean(t, buildDir, dotGitPath) 38 partials := getBuildInfoPartials(baseDir, t, buildName, "1") 39 checkFailureAndClean(t, buildDir, dotGitPath) 40 checkVCSUrl(partials, t) 41 tests.RemovePath(buildDir, t) 42 tests.RenamePath(dotGitPath, filepath.Join(filepath.Join("..", "testdata"), originalDir), t) 43 } 44 45 // Clean the environment if fails 46 func checkFailureAndClean(t *testing.T, buildDir string, oldPath string) { 47 if t.Failed() { 48 t.Log("Performing cleanup...") 49 tests.RemovePath(buildDir, t) 50 tests.RenamePath(oldPath, filepath.Join(filepath.Join("..", "testdata"), withGit), t) 51 t.FailNow() 52 } 53 } 54 55 func getBuildInfoPartials(baseDir string, t *testing.T, buildName string, buildNumber string) buildinfo.Partials { 56 buildAddGitConfiguration := new(BuildAddGitCommand).SetDotGitPath(baseDir).SetBuildConfiguration(&utils.BuildConfiguration{BuildName: buildName, BuildNumber: buildNumber}) 57 err := buildAddGitConfiguration.Run() 58 if err != nil { 59 t.Error("Cannot run build add git due to: " + err.Error()) 60 return nil 61 } 62 partials, err := utils.ReadPartialBuildInfoFiles(buildName, buildNumber) 63 if err != nil { 64 t.Error("Cannot read partial build info due to: " + err.Error()) 65 return nil 66 } 67 return partials 68 } 69 70 func getBuildDir(t *testing.T) string { 71 buildDir, err := utils.GetBuildDir(buildName, "1") 72 if err != nil { 73 t.Error("Cannot create temp dir due to: " + err.Error()) 74 return "" 75 } 76 return buildDir 77 } 78 79 func checkVCSUrl(partials buildinfo.Partials, t *testing.T) { 80 for _, partial := range partials { 81 if partial.Vcs != nil { 82 url := partial.Vcs.Url 83 urlSplitted := strings.Split(url, ".git") 84 if len(urlSplitted) != 2 { 85 t.Error("Argumanets value is different then two: ", urlSplitted) 86 break 87 } 88 } else { 89 t.Error("VCS cannot be nil") 90 break 91 } 92 } 93 } 94 95 func TestPopulateIssuesConfigurations(t *testing.T) { 96 // Test success scenario 97 expectedIssuesConfiguration := &IssuesConfiguration{ 98 ServerID: "local", 99 TrackerName: "TESTING", 100 TrackerUrl: "http://TESTING.com", 101 Regexp: `([a-zA-Z]+-[0-9]*)\s-\s(.*)`, 102 KeyGroupIndex: 1, 103 SummaryGroupIndex: 2, 104 Aggregate: true, 105 AggregationStatus: "RELEASE", 106 LogLimit: 100, 107 } 108 ic := new(IssuesConfiguration) 109 // Build config from file 110 err := ic.populateIssuesConfigsFromSpec(filepath.Join("..", "testdata", "buildissues", "issuesconfig_success.yaml")) 111 // Check they are equal 112 if err != nil { 113 t.Error(fmt.Sprintf("Reading configurations file ended with error: %s", err.Error())) 114 t.FailNow() 115 } 116 if *ic != *expectedIssuesConfiguration { 117 t.Error(fmt.Sprintf("Failed reading configurations file. Expected: %+v Received: %+v", *expectedIssuesConfiguration, *ic)) 118 t.FailNow() 119 } 120 121 // Test failing scenarios 122 failing := []string{ 123 filepath.Join("..", "testdata", "buildissues", "issuesconfig_fail_no_issues.yaml"), 124 filepath.Join("..", "testdata", "buildissues", "issuesconfig_fail_no_server.yaml"), 125 filepath.Join("..", "testdata", "buildissues", "issuesconfig_fail_invalid_groupindex.yaml"), 126 filepath.Join("..", "testdata", "buildissues", "issuesconfig_fail_invalid_aggregate.yaml"), 127 } 128 129 for _, config := range failing { 130 err = ic.populateIssuesConfigsFromSpec(config) 131 if err == nil { 132 t.Error(fmt.Sprintf("Reading configurations file was supposed to end with error: %s", config)) 133 t.FailNow() 134 } 135 } 136 } 137 138 func TestAddGitDoCollect(t *testing.T) { 139 // Create git folder with files 140 originalFolder := "git_issues_.git_suffix" 141 baseDir, dotGitPath := tests.PrepareDotGitDir(t, originalFolder, filepath.Join("..", "testdata")) 142 143 // Create BuildAddGitCommand 144 config := BuildAddGitCommand{ 145 issuesConfig: &IssuesConfiguration{ 146 LogLimit: 100, 147 Aggregate: false, 148 SummaryGroupIndex: 2, 149 KeyGroupIndex: 1, 150 Regexp: `(.+-[0-9]+)\s-\s(.+)`, 151 TrackerName: "test", 152 }, 153 buildConfiguration: &utils.BuildConfiguration{BuildNumber: "1", BuildName: "cli-test-build-issues"}, 154 configFilePath: "", 155 dotGitPath: dotGitPath, 156 } 157 158 // Collect issues 159 issues, err := config.DoCollect(config.issuesConfig, "") 160 if err != nil { 161 t.Error(err) 162 } 163 if len(issues) != 2 { 164 // Error - should be empty 165 t.Errorf("Issues list expected to have 2 issues, instead found %d issues: %v", len(issues), issues) 166 } 167 168 // Clean previous git path 169 tests.RenamePath(dotGitPath, filepath.Join(baseDir, originalFolder), t) 170 // Check if needs to fail 171 if t.Failed() { 172 t.FailNow() 173 } 174 // Set new git path 175 originalFolder = "git_issues2_.git_suffix" 176 baseDir, dotGitPath = tests.PrepareDotGitDir(t, originalFolder, filepath.Join("..", "testdata")) 177 178 // Collect issues - we pass a revision, so only 2 of the 4 existing issues should be collected 179 issues, err = config.DoCollect(config.issuesConfig, "6198a6294722fdc75a570aac505784d2ec0d1818") 180 if err != nil { 181 t.Error(err) 182 } 183 if len(issues) != 2 { 184 // Error - should find 2 issues 185 t.Errorf("Issues list expected to have 2 issues, instead found %d issues: %v", len(issues), issues) 186 } 187 188 // Clean git path 189 tests.RenamePath(dotGitPath, filepath.Join(baseDir, originalFolder), t) 190 } 191 192 func TestRtDetailsFromConfigFile(t *testing.T) { 193 expectedUrl := "http://localhost:8081/artifactory/" 194 expectedUser := "admin" 195 196 homeEnv := os.Getenv(cliutils.HomeDir) 197 if homeEnv == "" { 198 homeEnv = os.Getenv(cliutils.JfrogHomeEnv) 199 } 200 defer os.Setenv(cliutils.HomeDir, homeEnv) 201 baseDir, err := os.Getwd() 202 if err != nil { 203 t.Error(err) 204 } 205 err = os.Setenv(cliutils.HomeDir, filepath.Join(baseDir, "..", "testdata")) 206 if err != nil { 207 t.Error(err) 208 } 209 configFilePath := filepath.Join("..", "testdata", "buildissues", "issuesconfig_success.yaml") 210 config := BuildAddGitCommand{ 211 configFilePath: configFilePath, 212 } 213 details, err := config.RtDetails() 214 if err != nil { 215 t.Error(err) 216 } 217 218 if details.Url != expectedUrl { 219 t.Error(fmt.Sprintf("Expected %s, got %s", expectedUrl, details.Url)) 220 } 221 if details.User != expectedUser { 222 t.Error(fmt.Sprintf("Expected %s, got %s", details.User, expectedUser)) 223 } 224 } 225 226 func TestRtDetailsWithoutConfigFile(t *testing.T) { 227 expectedUrl := "http://localhost:8082/artifactory/" 228 expectedUser := "admin2" 229 230 homeEnv := os.Getenv(cliutils.HomeDir) 231 if homeEnv == "" { 232 homeEnv = os.Getenv(cliutils.JfrogHomeEnv) 233 } 234 defer os.Setenv(cliutils.HomeDir, homeEnv) 235 236 baseDir, err := os.Getwd() 237 if err != nil { 238 t.Error(err) 239 } 240 err = os.Setenv(cliutils.HomeDir, filepath.Join(baseDir, "..", "testdata")) 241 if err != nil { 242 t.Error(err) 243 } 244 245 config := BuildAddGitCommand{} 246 details, err := config.RtDetails() 247 if err != nil { 248 t.Error(err) 249 } 250 251 if details.Url != expectedUrl { 252 t.Error(fmt.Sprintf("Expected %s, got %s", expectedUrl, details.Url)) 253 } 254 255 if details.User != expectedUser { 256 t.Error(fmt.Sprintf("Expected %s, got %s", details.User, expectedUser)) 257 } 258 }