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

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/jfrog/jfrog-cli-go/inttestutils"
     9  	"github.com/jfrog/jfrog-cli-go/utils/tests"
    10  	"github.com/jfrog/jfrog-client-go/utils"
    11  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  const (
    16  	bundleName    = "cli-test-bundle"
    17  	bundleVersion = "10"
    18  )
    19  
    20  func InitDistributionTests() {
    21  	*tests.RtDistributionUrl = utils.AddTrailingSlashIfNeeded(*tests.RtDistributionUrl)
    22  	InitArtifactoryTests()
    23  	inttestutils.SendGpgKeys(artHttpDetails)
    24  }
    25  
    26  func CleanDistributionTests() {
    27  	inttestutils.DeleteGpgKeys(artHttpDetails)
    28  	CleanArtifactoryTests()
    29  }
    30  
    31  func initDistributionTest(t *testing.T) {
    32  	if !*tests.TestDistribution {
    33  		t.Skip("Distribution is not being tested, skipping...")
    34  	}
    35  	// Delete old release bundle
    36  	artifactoryCli.Exec("rbdel", bundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
    37  	inttestutils.WaitForDeletion(t, bundleName, bundleVersion, artHttpDetails)
    38  }
    39  
    40  func cleanDistributionTest(t *testing.T) {
    41  	artifactoryCli.Exec("rbdel", bundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
    42  	inttestutils.WaitForDeletion(t, bundleName, bundleVersion, artHttpDetails)
    43  	cleanArtifactoryTest()
    44  }
    45  
    46  func TestBundleDownload(t *testing.T) {
    47  	initDistributionTest(t)
    48  
    49  	// Upload files
    50  	specFile, err := tests.CreateSpec(tests.SplitUploadSpecB)
    51  	assert.NoError(t, err)
    52  	artifactoryCli.Exec("u", "--spec="+specFile)
    53  
    54  	// Create and distribute release bundle
    55  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/b1.in", "--sign")
    56  	artifactoryCli.Exec("rbd", bundleName, bundleVersion, "--site=*")
    57  	inttestutils.WaitForDistribution(t, bundleName, bundleVersion, artHttpDetails)
    58  
    59  	// Download by bundle version, b2 and b3 should not be downloaded, b1 should
    60  	artifactoryCli.Exec("dl "+tests.Repo1+"/data/* "+tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+bundleName+"/"+bundleVersion)
    61  
    62  	// Validate files are downloaded by bundle version
    63  	paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
    64  	err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
    65  	assert.NoError(t, err)
    66  
    67  	// Cleanup
    68  	cleanDistributionTest(t)
    69  }
    70  
    71  func TestBundleDownloadUsingSpec(t *testing.T) {
    72  	initDistributionTest(t)
    73  
    74  	// Upload files
    75  	specFile, err := tests.CreateSpec(tests.SplitUploadSpecB)
    76  	assert.NoError(t, err)
    77  	artifactoryCli.Exec("u", "--spec="+specFile)
    78  	inttestutils.WaitForDeletion(t, bundleName, bundleVersion, artHttpDetails)
    79  
    80  	// Create release bundle
    81  	distributionRules, err := tests.CreateSpec(tests.DistributionRules)
    82  	assert.NoError(t, err)
    83  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/b1.in", "--sign")
    84  	artifactoryCli.Exec("rbd", bundleName, bundleVersion, "--dist-rules="+distributionRules)
    85  	inttestutils.WaitForDistribution(t, bundleName, bundleVersion, artHttpDetails)
    86  
    87  	// Download by bundle version, b2 and b3 should not be downloaded, b1 should
    88  	specFile, err = tests.CreateSpec(tests.BundleDownloadSpec)
    89  	assert.NoError(t, err)
    90  	artifactoryCli.Exec("dl", "--spec="+specFile)
    91  
    92  	// Validate files are downloaded by bundle version
    93  	paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
    94  	err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
    95  	assert.NoError(t, err)
    96  
    97  	// Cleanup
    98  	cleanDistributionTest(t)
    99  }
   100  
   101  func TestBundleDownloadNoPattern(t *testing.T) {
   102  	initDistributionTest(t)
   103  
   104  	// Upload files
   105  	specFile, err := tests.CreateSpec(tests.SplitUploadSpecB)
   106  	assert.NoError(t, err)
   107  	artifactoryCli.Exec("u", "--spec="+specFile)
   108  
   109  	// Create release bundle
   110  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/b1.in", "--sign")
   111  	artifactoryCli.Exec("rbd", bundleName, bundleVersion, "--site=*")
   112  	inttestutils.WaitForDistribution(t, bundleName, bundleVersion, artHttpDetails)
   113  
   114  	// Download by bundle name and version with pattern "*", b2 and b3 should not be downloaded, b1 should
   115  	artifactoryCli.Exec("dl", "*", "out/download/simple_by_build/data/", "--bundle="+bundleName+"/"+bundleVersion, "--flat")
   116  
   117  	// Validate files are downloaded by bundle version
   118  	paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
   119  	err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
   120  	assert.NoError(t, err)
   121  
   122  	// Download by bundle name and version version without pattern, b2 and b3 should not be downloaded, b1 should
   123  	tests.CleanFileSystem()
   124  	specFile, err = tests.CreateSpec(tests.BundleDownloadSpecNoPattern)
   125  	artifactoryCli.Exec("dl", "--spec="+specFile, "--flat")
   126  
   127  	// Validate files are downloaded by bundle version
   128  	paths, _ = fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
   129  	err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
   130  	assert.NoError(t, err)
   131  
   132  	// Cleanup
   133  	cleanDistributionTest(t)
   134  }
   135  
   136  func TestBundleExclusions(t *testing.T) {
   137  	initDistributionTest(t)
   138  
   139  	// Upload files
   140  	specFile, err := tests.CreateSpec(tests.SplitUploadSpecB)
   141  	assert.NoError(t, err)
   142  	artifactoryCli.Exec("u", "--spec="+specFile)
   143  
   144  	// Create release bundle. Include b1.in and b2.in. Exclude b3.in.
   145  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/b*.in", "--sign", "--exclusions=*b3.in")
   146  	artifactoryCli.Exec("rbd", bundleName, bundleVersion, "--site=*")
   147  	inttestutils.WaitForDistribution(t, bundleName, bundleVersion, artHttpDetails)
   148  
   149  	// Download by bundle version, b2 and b3 should not be downloaded, b1 should
   150  	artifactoryCli.Exec("dl "+tests.Repo1+"/data/* "+tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+bundleName+"/"+bundleVersion, "--exclusions=*b2.in")
   151  
   152  	// Validate files are downloaded by bundle version
   153  	paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
   154  	err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
   155  	assert.NoError(t, err)
   156  
   157  	// Cleanup
   158  	cleanDistributionTest(t)
   159  }
   160  
   161  func TestBundleCopy(t *testing.T) {
   162  	initDistributionTest(t)
   163  
   164  	// Upload files
   165  	specFileA, err := tests.CreateSpec(tests.SplitUploadSpecA)
   166  	assert.NoError(t, err)
   167  	specFileB, err := tests.CreateSpec(tests.SplitUploadSpecB)
   168  	assert.NoError(t, err)
   169  	artifactoryCli.Exec("u", "--spec="+specFileB)
   170  	artifactoryCli.Exec("u", "--spec="+specFileA)
   171  
   172  	// Create release bundle
   173  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/a*", "--sign")
   174  	artifactoryCli.Exec("rbd", bundleName, bundleVersion, "--site=*")
   175  	inttestutils.WaitForDistribution(t, bundleName, bundleVersion, artHttpDetails)
   176  
   177  	// Copy by bundle name and version
   178  	specFile, err := tests.CreateSpec(tests.CopyByBundleSpec)
   179  	assert.NoError(t, err)
   180  	artifactoryCli.Exec("cp", "--spec="+specFile)
   181  
   182  	// Validate files are moved by bundle version
   183  	cpMvDlByBuildAssertSpec, err := tests.CreateSpec(tests.CpMvDlByBuildAssertSpec)
   184  	assert.NoError(t, err)
   185  	verifyExistInArtifactory(tests.GetBuildCopyExpected(), cpMvDlByBuildAssertSpec, t)
   186  
   187  	// Cleanup
   188  	cleanDistributionTest(t)
   189  }
   190  
   191  func TestBundleSetProperties(t *testing.T) {
   192  	initDistributionTest(t)
   193  
   194  	// Upload a file.
   195  	artifactoryCli.Exec("u", "testsdata/a/a1.in", tests.Repo1+"/a.in")
   196  
   197  	// Create release bundle
   198  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/a.in", "--sign")
   199  	artifactoryCli.Exec("rbd", bundleName, bundleVersion, "--site=*")
   200  	inttestutils.WaitForDistribution(t, bundleName, bundleVersion, artHttpDetails)
   201  
   202  	// Set the 'prop=red' property to the file.
   203  	artifactoryCli.Exec("sp", tests.Repo1+"/a.*", "prop=red", "--bundle="+bundleName+"/"+bundleVersion)
   204  	// Now let's change the property value, by searching for the 'prop=red'.
   205  	specFile, err := tests.CreateSpec(tests.SetDeletePropsSpec)
   206  	assert.NoError(t, err)
   207  	artifactoryCli.Exec("sp", "prop=green", "--spec="+specFile, "--bundle="+bundleName+"/"+bundleVersion)
   208  
   209  	resultItems := searchItemsInArtifactory(t)
   210  	assert.NotZero(t, len(resultItems), "No artifacts were found.")
   211  	for _, item := range resultItems {
   212  		properties := item.Properties
   213  		assert.Equal(t, len(properties), 2, "Failed setting properties on item:", item.GetItemRelativePath())
   214  		for _, prop := range properties {
   215  			if prop.Key == "sha256" {
   216  				continue
   217  			}
   218  			assert.Equal(t, "prop", prop.Key, "Wrong property key")
   219  			assert.Equal(t, "green", prop.Value, "Wrong property value")
   220  		}
   221  	}
   222  	cleanDistributionTest(t)
   223  }
   224  
   225  func TestSignReleaseBundle(t *testing.T) {
   226  	initDistributionTest(t)
   227  
   228  	// Upload files
   229  	specFile, err := tests.CreateSpec(tests.SplitUploadSpecB)
   230  	assert.NoError(t, err)
   231  	artifactoryCli.Exec("u", "--spec="+specFile)
   232  
   233  	// Create a release bundle without --sign and make sure it is not signed
   234  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/b1.in")
   235  	distributableResponse := inttestutils.GetLocalBundle(t, bundleName, bundleVersion, artHttpDetails)
   236  	assert.NotNil(t, distributableResponse)
   237  	assert.Equal(t, inttestutils.Open, distributableResponse.State)
   238  
   239  	// Sign the release bundle and make sure it is signed
   240  	artifactoryCli.Exec("rbs", bundleName, bundleVersion)
   241  	distributableResponse = inttestutils.GetLocalBundle(t, bundleName, bundleVersion, artHttpDetails)
   242  	assert.NotNil(t, distributableResponse)
   243  	assert.Equal(t, inttestutils.Signed, distributableResponse.State)
   244  
   245  	// Cleanup
   246  	cleanDistributionTest(t)
   247  }
   248  
   249  func TestBundleDeleteLocal(t *testing.T) {
   250  	initDistributionTest(t)
   251  
   252  	// Upload files
   253  	specFile, err := tests.CreateSpec(tests.SplitUploadSpecB)
   254  	assert.NoError(t, err)
   255  	artifactoryCli.Exec("u", "--spec="+specFile)
   256  
   257  	// Create a release bundle
   258  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/b1.in", "--sign")
   259  	inttestutils.VerifyLocalBundleExistence(t, bundleName, bundleVersion, true, artHttpDetails)
   260  
   261  	// Delete release bundle locally
   262  	artifactoryCli.Exec("rbdel", bundleName, bundleVersion, "--site=*", "--delete-from-dist", "--quiet")
   263  	inttestutils.VerifyLocalBundleExistence(t, bundleName, bundleVersion, false, artHttpDetails)
   264  
   265  	// Cleanup
   266  	cleanDistributionTest(t)
   267  }
   268  
   269  func TestUpdateReleaseBundle(t *testing.T) {
   270  	initDistributionTest(t)
   271  
   272  	// Upload files
   273  	specFile, err := tests.CreateSpec(tests.SplitUploadSpecB)
   274  	assert.NoError(t, err)
   275  	artifactoryCli.Exec("u", "--spec="+specFile)
   276  
   277  	// Create a release bundle with b2.in
   278  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/b2.in")
   279  	inttestutils.VerifyLocalBundleExistence(t, bundleName, bundleVersion, true, artHttpDetails)
   280  
   281  	// Update release bundle to have b1.in
   282  	artifactoryCli.Exec("rbu", bundleName, bundleVersion, tests.Repo1+"/data/b1.in", "--sign")
   283  
   284  	// Distribute release bundle
   285  	artifactoryCli.Exec("rbd", bundleName, bundleVersion, "--site=*")
   286  	inttestutils.WaitForDistribution(t, bundleName, bundleVersion, artHttpDetails)
   287  
   288  	// Download by bundle version, b2 and b3 should not be downloaded, b1 should
   289  	artifactoryCli.Exec("dl "+tests.Repo1+"/data/* "+tests.Out+fileutils.GetFileSeparator()+"download"+fileutils.GetFileSeparator()+"simple_by_build"+fileutils.GetFileSeparator(), "--bundle="+bundleName+"/"+bundleVersion)
   290  
   291  	// Validate files are downloaded by bundle version
   292  	paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false)
   293  	err = tests.ValidateListsIdentical(tests.GetBuildSimpleDownload(), paths)
   294  	assert.NoError(t, err)
   295  
   296  	// Cleanup
   297  	cleanDistributionTest(t)
   298  }
   299  
   300  func TestCreateBundleText(t *testing.T) {
   301  	initDistributionTest(t)
   302  
   303  	// Upload files
   304  	specFile, err := tests.CreateSpec(tests.SplitUploadSpecB)
   305  	assert.NoError(t, err)
   306  	artifactoryCli.Exec("u", "--spec="+specFile)
   307  
   308  	// Create a release bundle with release notes and description
   309  	releaseNotesPath := filepath.Join(tests.GetTestResourcesPath(), "distribution", "releasenotes.md")
   310  	description := "thisIsADescription"
   311  	artifactoryCli.Exec("rbc", bundleName, bundleVersion, tests.Repo1+"/data/*", "--release-notes-path="+releaseNotesPath, "--desc="+description)
   312  
   313  	// Validate release notes and description
   314  	distributableResponse := inttestutils.GetLocalBundle(t, bundleName, bundleVersion, artHttpDetails)
   315  	if distributableResponse != nil {
   316  		assert.Equal(t, description, distributableResponse.Description)
   317  		releaseNotes, err := ioutil.ReadFile(releaseNotesPath)
   318  		assert.NoError(t, err)
   319  		assert.Equal(t, string(releaseNotes), distributableResponse.ReleaseNotes.Content)
   320  		assert.Equal(t, "markdown", distributableResponse.ReleaseNotes.Syntax)
   321  	}
   322  
   323  	cleanDistributionTest(t)
   324  }