github.com/osievert/jfrog-cli-core@v1.2.7/artifactory/commands/buildinfo/addgit_test.go (about)

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