Skip to content

Commit

Permalink
Artifactory resolution tests: Python, Java, Go (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman authored Dec 18, 2023
1 parent f8f45c2 commit ccd80bc
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/xrayTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ jobs:
ref: ${{ github.event.pull_request.head.sha }}

- name: Run Xray tests (without Docker Scan)
run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.xray --jfrog.url=${{ secrets.PLATFORM_URL }} --jfrog.adminToken=${{ secrets.PLATFORM_ADMIN_TOKEN }} --jfrog.user=${{ secrets.PLATFORM_USER }} --test.containerRegistry=${{ secrets.CONTAINER_REGISTRY }}
run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.xray --jfrog.url=${{ secrets.PLATFORM_URL }} --jfrog.adminToken=${{ secrets.PLATFORM_ADMIN_TOKEN }} --jfrog.user=${{ secrets.PLATFORM_USER }} --test.containerRegistry=${{ secrets.CONTAINER_REGISTRY }} --ci.runId=${{ runner.os }}-xray
if: ${{ matrix.os != 'ubuntu' }}

- name: Run Xray tests (with Docker Scan, only on Ubuntu)
run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.xray --test.dockerScan --jfrog.url=${{ secrets.PLATFORM_URL }} --jfrog.adminToken=${{ secrets.PLATFORM_ADMIN_TOKEN }} --test.containerRegistry=${{ secrets.CONTAINER_REGISTRY }}
run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.xray --test.dockerScan --jfrog.url=${{ secrets.PLATFORM_URL }} --jfrog.adminToken=${{ secrets.PLATFORM_ADMIN_TOKEN }} --test.containerRegistry=${{ secrets.CONTAINER_REGISTRY }} --ci.runId=${{ runner.os }}-xray
if: ${{ matrix.os == 'ubuntu' }}
9 changes: 9 additions & 0 deletions testdata/go/simple-project/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/you/hello

go 1.20

require rsc.io/quote v1.5.2

require (
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: 4 additions & 0 deletions testdata/gradle/gradleproject/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
apply plugin: 'groovy'
apply plugin: 'idea'

dependencies {
implementation "junit:junit:4.7"
}

version = 1.0
task initProject(description: 'Initialize project directory structure.') {
doLast {
Expand Down
6 changes: 6 additions & 0 deletions testdata/maven/mavenproject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4 changes: 4 additions & 0 deletions testdata/poetry/projecttomlproject/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr)
13 changes: 13 additions & 0 deletions testdata/poetry/projecttomlproject/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tool.poetry]
name = "my-poetry-project"
version = "1.1.0"
description = ""
authors = ["Severus Snape <[email protected]>"]

[tool.poetry.dependencies]
python = "^3.10"
numpy = "^1.23.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
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},
TestXray: {&NpmRemoteRepo, &NugetRemoteRepo, &YarnRemoteRepo, &GradleRemoteRepo, &MvnRemoteRepo, &GoRepo, &GoRemoteRepo, &PypiRemoteRepo},
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
115 changes: 102 additions & 13 deletions xray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ import (
"github.com/urfave/cli"
)

const (
jvmLaunchEnvVar = "MAVEN_OPTS"
mavenCacheRedirectionVal = "-Dmaven.repo.local="
goCacheEnvVar = "GOMODCACHE"
pipCacheEnvVar = "PIP_CACHE_DIR"
)

var (
xrayDetails *config.ServerDetails
xrayAuth auth.ServiceDetails
Expand Down Expand Up @@ -988,7 +995,45 @@ func TestDependencyResolutionFromArtifactory(t *testing.T) {
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,
},
{
testProjectPath: []string{"pipenv", "pipenvproject"},
resolveRepoName: tests.PypiRemoteRepo,
cacheRepoName: tests.PypiRemoteRepo,
projectType: artUtils.Pipenv,
},
{
testProjectPath: []string{"pip", "setuppyproject"},
resolveRepoName: tests.PypiRemoteRepo,
cacheRepoName: tests.PypiRemoteRepo,
projectType: artUtils.Pip,
},
{
testProjectPath: []string{"poetry", "projecttomlproject"},
resolveRepoName: tests.PypiRemoteRepo,
cacheRepoName: tests.PypiRemoteRepo,
projectType: artUtils.Poetry,
},
}
createJfrogHomeConfig(t, true)
defer cleanTestsHomeEnv()

for _, testCase := range testCases {
t.Run(testCase.projectType.String(), func(t *testing.T) {
Expand All @@ -1008,27 +1053,71 @@ func testSingleTechDependencyResolution(t *testing.T, testProjectPartialPath []s
defer func() {
assert.NoError(t, os.Chdir(rootDir))
}()
createJfrogHomeConfig(t, true)
context := createContext(t, "repo-resolve="+resolveRepoName)

server := &config.ServerDetails{
Url: *tests.JfrogUrl,
ArtifactoryUrl: *tests.JfrogUrl + tests.ArtifactoryEndpoint,
XrayUrl: *tests.JfrogUrl + tests.XrayEndpoint,
AccessToken: *tests.JfrogAccessToken,
ServerId: tests.ServerId,
}
configCmd := coreCmd.NewConfigCommand(coreCmd.AddOrEdit, tests.ServerId).SetDetails(server).SetUseBasicAuthOnly(true).SetInteractive(false)
assert.NoError(t, configCmd.Run())

context := createContext(t, "repo-resolve="+resolveRepoName, "server-id-resolve="+server.ServerId)
err = artCmdUtils.CreateBuildConfig(context, projectType)
assert.NoError(t, err)

artifactoryPathToSearch := cacheRepoName + "-cache/*"
output := artifactoryCli.RunCliCmdWithOutput(t, "s", artifactoryPathToSearch)
// Before the resolution from Artifactory, we verify whether the repository's cache is empty.
assert.Equal(t, "[]\n", output)

if projectType == artUtils.Dotnet {
// In Nuget/Dotnet projects we need to clear local caches so we will resolve dependencies from Artifactory
_, err = exec.Command("dotnet", "nuget", "locals", "all", "--clear").CombinedOutput()
assert.NoError(t, err)
// To ensure a clean state between test cases, we need to verify that the cache remains clear for remote directories shared across multiple test cases.
assert.NoError(t, artifactoryCli.Exec("del", artifactoryPathToSearch))

callbackFunc := clearOrRedirectLocalCacheIfNeeded(t, projectType)
if callbackFunc != nil {
defer func() {
callbackFunc()
}()
}

// 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"))
// Executing the 'audit' command on an uninstalled project, we anticipate the resolution of dependencies from the configured Artifactory server and repository.
assert.NoError(t, xrayCli.WithoutCredentials().Exec("audit"))

// Following resolution from Artifactory, we anticipate the repository's cache to contain data.
output = artifactoryCli.RunCliCmdWithOutput(t, "s", artifactoryPathToSearch, "--fail-no-op")
output := artifactoryCli.RunCliCmdWithOutput(t, "s", artifactoryPathToSearch, "--fail-no-op")
// After the resolution from Artifactory, we verify whether the repository's cache is filled with artifacts.
assert.NotEqual(t, "[]\n", output)
}

// To guarantee that dependencies are resolved from Artifactory, certain package managers may need their local cache to be cleared.
func clearOrRedirectLocalCacheIfNeeded(t *testing.T, projectType artUtils.ProjectType) (callbackFunc func()) {
switch projectType {
case artUtils.Dotnet:
_, err := exec.Command("dotnet", "nuget", "locals", "all", "--clear").CombinedOutput()
assert.NoError(t, err)
case artUtils.Maven:
mavenCacheTempPath, createTempDirCallback := coretests.CreateTempDirWithCallbackAndAssert(t)
envVarCallbackFunc := clientTestUtils.SetEnvWithCallbackAndAssert(t, jvmLaunchEnvVar, mavenCacheRedirectionVal+mavenCacheTempPath)
callbackFunc = func() {
envVarCallbackFunc()
createTempDirCallback()
}
case artUtils.Go:
goTempCachePath, createTempDirCallback := coretests.CreateTempDirWithCallbackAndAssert(t)
envVarCallbackFunc := clientTestUtils.SetEnvWithCallbackAndAssert(t, goCacheEnvVar, goTempCachePath)

callbackFunc = func() {
envVarCallbackFunc()
// To remove the temporary cache in Go and all its contents, appropriate deletion permissions are required.
assert.NoError(t, coreutils.SetPermissionsRecursively(goTempCachePath, 0755))
createTempDirCallback()
}
case artUtils.Pip:
pipTempCachePath, createTempDirCallback := coretests.CreateTempDirWithCallbackAndAssert(t)
envVarCallbackFunc := clientTestUtils.SetEnvWithCallbackAndAssert(t, pipCacheEnvVar, pipTempCachePath)
callbackFunc = func() {
envVarCallbackFunc()
createTempDirCallback()
}
}
return
}

0 comments on commit ccd80bc

Please sign in to comment.