Skip to content

Commit

Permalink
added artifactory resolution test for Go
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Dec 11, 2023
1 parent b747013 commit 136c0f1
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 33 deletions.
37 changes: 37 additions & 0 deletions testdata/go/simple-project/README.md
Original file line number Diff line number Diff line change
@@ -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=<JFROG_PLATFORM_URL> [credentials flags]

Configure the project's repositories:
> jf go-config --repo-resolve=<GO_RESOLVE_REPO> --repo-deploy=<GO_DEPLOY_REPO>

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 <GO_DEPLOY_REPO> 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.
10 changes: 10 additions & 0 deletions testdata/go/simple-project/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
6 changes: 6 additions & 0 deletions testdata/go/simple-project/go.sum
Original file line number Diff line number Diff line change
@@ -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=
11 changes: 11 additions & 0 deletions testdata/go/simple-project/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"

"rsc.io/quote"
)

func main() {
fmt.Println(quote.Hello())
}
4 changes: 2 additions & 2 deletions utils/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -422,7 +422,7 @@ func GetVirtualRepositories() map[*string]string {
TestPip: {&PypiVirtualRepo},
TestPipenv: {&PipenvVirtualRepo},
TestPlugins: {},
TestXray: {},
TestXray: {&GoVirtualRepo},
TestAccess: {},
}
return getNeededRepositories(virtualReposMap)
Expand Down
73 changes: 42 additions & 31 deletions xray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"))
Expand All @@ -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()
Expand All @@ -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)
}
}
}

0 comments on commit 136c0f1

Please sign in to comment.