From 136c0f1376d28ab1987a513c1231434d4e084474 Mon Sep 17 00:00:00 2001 From: Eran Turgeman Date: Mon, 11 Dec 2023 16:41:52 +0200 Subject: [PATCH] added artifactory resolution test for Go --- testdata/go/simple-project/README.md | 37 ++++++++++++++ testdata/go/simple-project/go.mod | 10 ++++ testdata/go/simple-project/go.sum | 6 +++ testdata/go/simple-project/hello.go | 11 +++++ utils/tests/utils.go | 4 +- xray_test.go | 73 ++++++++++++++++------------ 6 files changed, 108 insertions(+), 33 deletions(-) create mode 100644 testdata/go/simple-project/README.md create mode 100644 testdata/go/simple-project/go.mod create mode 100644 testdata/go/simple-project/go.sum create mode 100644 testdata/go/simple-project/hello.go diff --git a/testdata/go/simple-project/README.md b/testdata/go/simple-project/README.md new file mode 100644 index 000000000..a0f481f6c --- /dev/null +++ b/testdata/go/simple-project/README.md @@ -0,0 +1,37 @@ +# Golang example + +## Overview +Go repositories are supported by Artifactory since version 5.11.0. +To work with Go repositories you need to use [JFrog CLI](https://www.jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory) and the Go client. + +## Prerequisite +* Install version 1.11 or above of **go**. +* Make sure the **go** command is in your PATH. +* Install [JFrog CLI](https://jfrog.com/getcli/) +* Make sure your Artifactory version is 5.11.0 or above +* Make sure your JFrog CLI version is 2.0.0 or above + +## Running the Example +'cd' to the root project directory + +```console +Configure Artifactory: +> jf c add --url= [credentials flags] + +Configure the project's repositories: +> jf go-config --repo-resolve= --repo-deploy= + +Build the project with go and resolve the project dependencies from Artifactory. +> jf go build --build-name=my-build --build-number=1 + +Publish version v1.0.0 of the package to the repository in Artifactory. +> jf go-publish v1.0.0 --build-name=my-build --build-number=1 + +Collect environment variables and add them to the build info. +> jf rt bce my-build 1 + +Publish the build info to Artifactory. +> jf rt bp my-build 1 +``` + +Learn about [building go packages](https://www.jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory#CLIforJFrogArtifactory-BuildingGoPackages) and about Artifactory and [go registry](https://jfrog.com/integration/go-registry/) integration. diff --git a/testdata/go/simple-project/go.mod b/testdata/go/simple-project/go.mod new file mode 100644 index 000000000..36f447f87 --- /dev/null +++ b/testdata/go/simple-project/go.mod @@ -0,0 +1,10 @@ +module github.com/you/hello + +go 1.20 + +require rsc.io/quote v1.5.2 + +require ( + golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect + rsc.io/sampler v1.3.0 // indirect +) diff --git a/testdata/go/simple-project/go.sum b/testdata/go/simple-project/go.sum new file mode 100644 index 000000000..4a8bcd704 --- /dev/null +++ b/testdata/go/simple-project/go.sum @@ -0,0 +1,6 @@ +golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8= +golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y= +rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0= +rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4= +rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= diff --git a/testdata/go/simple-project/hello.go b/testdata/go/simple-project/hello.go new file mode 100644 index 000000000..0a29866c4 --- /dev/null +++ b/testdata/go/simple-project/hello.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + + "rsc.io/quote" +) + +func main() { + fmt.Println(quote.Hello()) +} diff --git a/utils/tests/utils.go b/utils/tests/utils.go index f143b5c2b..112828aee 100644 --- a/utils/tests/utils.go +++ b/utils/tests/utils.go @@ -398,7 +398,7 @@ func GetNonVirtualRepositories() map[*string]string { TestPip: {&PypiRemoteRepo}, TestPipenv: {&PipenvRemoteRepo}, TestPlugins: {&RtRepo1}, - TestXray: {&NpmRemoteRepo, &NugetRemoteRepo, &YarnRemoteRepo, &GradleRemoteRepo, &MvnRemoteRepo}, + TestXray: {&NpmRemoteRepo, &NugetRemoteRepo, &YarnRemoteRepo, &GradleRemoteRepo, &MvnRemoteRepo, &GoRepo, &GoRemoteRepo}, TestAccess: {&RtRepo1}, TestTransfer: {&RtRepo1, &RtRepo2, &MvnRepo1, &MvnRemoteRepo, &DockerRemoteRepo}, TestLifecycle: {&RtDevRepo, &RtProdRepo}, @@ -422,7 +422,7 @@ func GetVirtualRepositories() map[*string]string { TestPip: {&PypiVirtualRepo}, TestPipenv: {&PipenvVirtualRepo}, TestPlugins: {}, - TestXray: {}, + TestXray: {&GoVirtualRepo}, TestAccess: {}, } return getNeededRepositories(virtualReposMap) diff --git a/xray_test.go b/xray_test.go index b2c6f5a53..0e4ebf1f3 100644 --- a/xray_test.go +++ b/xray_test.go @@ -970,41 +970,42 @@ func TestDependencyResolutionFromArtifactory(t *testing.T) { cacheRepoName string projectType artUtils.ProjectType }{ - /* - { - testProjectPath: []string{"npm", "npmproject"}, - resolveRepoName: tests.NpmRemoteRepo, - cacheRepoName: tests.NpmRemoteRepo, - projectType: artUtils.Npm, - }, - { - testProjectPath: []string{"nuget", "simple-dotnet"}, - resolveRepoName: tests.NugetRemoteRepo, - cacheRepoName: tests.NugetRemoteRepo, - projectType: artUtils.Dotnet, - }, - { - testProjectPath: []string{"yarn", "yarnproject"}, - resolveRepoName: tests.YarnRemoteRepo, - cacheRepoName: tests.YarnRemoteRepo, - projectType: artUtils.Yarn, - }, - - { - testProjectPath: []string{"gradle", "gradleproject"}, - resolveRepoName: tests.GradleRemoteRepo, - cacheRepoName: tests.GradleRemoteRepo, - projectType: artUtils.Gradle, - }, - - */ - + { + testProjectPath: []string{"npm", "npmproject"}, + resolveRepoName: tests.NpmRemoteRepo, + cacheRepoName: tests.NpmRemoteRepo, + projectType: artUtils.Npm, + }, + { + testProjectPath: []string{"nuget", "simple-dotnet"}, + resolveRepoName: tests.NugetRemoteRepo, + cacheRepoName: tests.NugetRemoteRepo, + projectType: artUtils.Dotnet, + }, + { + testProjectPath: []string{"yarn", "yarnproject"}, + resolveRepoName: tests.YarnRemoteRepo, + cacheRepoName: tests.YarnRemoteRepo, + projectType: artUtils.Yarn, + }, + { + testProjectPath: []string{"gradle", "gradleproject"}, + resolveRepoName: tests.GradleRemoteRepo, + cacheRepoName: tests.GradleRemoteRepo, + projectType: artUtils.Gradle, + }, { testProjectPath: []string{"maven", "mavenproject"}, resolveRepoName: tests.MvnRemoteRepo, cacheRepoName: tests.MvnRemoteRepo, projectType: artUtils.Maven, }, + { + testProjectPath: []string{"go", "simple-project"}, + resolveRepoName: tests.GoVirtualRepo, + cacheRepoName: tests.GoRemoteRepo, + projectType: artUtils.Go, + }, } for _, testCase := range testCases { @@ -1035,7 +1036,7 @@ func testSingleTechDependencyResolution(t *testing.T, testProjectPartialPath []s // Before the resolution from Artifactory, we verify whether the repository's cache is empty. assert.Equal(t, "[]\n", output) - deleteLocalCacheIfNeeded(t, projectType) + clearLocalCacheIfNeeded(t, projectType) // We execute 'audit' command on a project that hasn't been installed. With the Artifactory server and repository configuration, our expectation is that dependencies will be resolved from there assert.NoError(t, xrayCli.Exec("audit")) @@ -1047,7 +1048,7 @@ func testSingleTechDependencyResolution(t *testing.T, testProjectPartialPath []s } // In order to ensure dependencies resolution from Artifactory, some package managers require deletion of their local cache -func deleteLocalCacheIfNeeded(t *testing.T, projectType artUtils.ProjectType) { +func clearLocalCacheIfNeeded(t *testing.T, projectType artUtils.ProjectType) { switch projectType { case artUtils.Dotnet: _, err := exec.Command("dotnet", "nuget", "locals", "all", "--clear").CombinedOutput() @@ -1061,5 +1062,15 @@ func deleteLocalCacheIfNeeded(t *testing.T, projectType artUtils.ProjectType) { err = os.RemoveAll(mvnCacheFullPath) assert.NoError(t, err) } + case artUtils.Go: + // In Go, we don't want to clear the entire cache so we delete a specific cached package in order to re-download it from Artifactory + homeDir := fileutils.GetHomeDir() + cachedPackagePath := filepath.Join(homeDir, "go", "pkg", "mod", "cache", "download", "rsc.io") + cachedPackagePathExists, err := fileutils.IsDirExists(cachedPackagePath, false) + assert.NoError(t, err) + if cachedPackagePathExists { + err = os.RemoveAll(cachedPackagePath) + assert.NoError(t, err) + } } }