From 23fafab79e490bb761e898ff87d8905a00e8712b Mon Sep 17 00:00:00 2001 From: attiasas Date: Thu, 21 Mar 2024 17:07:02 +0200 Subject: [PATCH 1/2] Run tests in parallel --- .github/workflows/test.yml | 80 +++++++++++++++++++++++++++++--------- CONTRIBUTING.md | 13 +++++-- artifactory_test.go | 5 ++- audit_test.go | 28 ++++++------- jfrogclisecurity_test.go | 13 +++---- scans_test.go | 11 ++---- tests/config.go | 38 +++++++++++++----- tests/consts.go | 8 ++-- tests/utils/test_utils.go | 39 +++++++++++++++++-- unit_test.go | 12 +++--- xray_test.go | 4 +- 11 files changed, 174 insertions(+), 77 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f203656a..53b327b8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,10 +1,6 @@ name: JFrog CLI Security Tests on: push: - branches: - - '**' - tags-ignore: - - '**' # Triggers the workflow on labeled PRs only. pull_request_target: types: [ labeled ] @@ -13,17 +9,71 @@ concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.ref }} cancel-in-progress: true jobs: - test: + Pretest: if: contains(github.event.pull_request.labels.*.name, 'safe to test') || github.event_name == 'push' + runs-on: ubuntu-latest + steps: + - name: Remove 'safe to test' label + uses: actions-ecosystem/action-remove-labels@v1 + if: ${{ github.event_name != 'push' }} + with: + labels: "safe to test" + + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + + - name: Install Go + uses: actions/setup-go@v3 + with: + go-version: 1.21.x + + - name: Go Cache + uses: actions/cache@v3 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go- + + - name: Check code compilation + run: go vet -v ./... + + tests: + needs: Pretest + name: ${{ matrix.suite.name }} Tests (${{ matrix.os }}) runs-on: ${{ matrix.os }}-latest + env: + JFROG_CLI_LOG_LEVEL: "DEBUG" + GOPROXY: direct + GRADLE_OPTS: -Dorg.gradle.daemon=false strategy: fail-fast: false matrix: + suite: + - name: 'Unit' + testFlags: '--test.unit' + + - name: 'Artifactory Integration' + testFlags: '--test.artifactory --test.unit=false' + + - name: 'Xray Integration' + testFlags: '--test.xray --test.unit=false' + + - name: 'Audit' + testFlags: '--test.audit --test.unit=false' + + - name: 'Scans' + testFlags: '--test.scan --test.unit=false' + ubuntuTestFlags: '--test.scan --test.dockerScan --test.unit=false' + os: [ ubuntu, windows, macos ] - env: - GOPROXY: direct - GRADLE_OPTS: -Dorg.gradle.daemon=false steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + # Install dependencies - name: Install Go uses: actions/setup-go@v3 @@ -62,15 +112,7 @@ jobs: uses: gradle/gradle-build-action@v2 with: gradle-version: 7.6 - # Checkout code - - name: Checkout code - uses: actions/checkout@v3 - with: - ref: ${{ github.event.pull_request.head.sha }} + # Test - - name: Run security tests (without Docker Scan) - run: go test -v github.com/jfrog/jfrog-cli-security --timeout 0 --test.security --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 security tests (with Docker Scan, only on Ubuntu) - run: go test -v github.com/jfrog/jfrog-cli-security --timeout 0 --test.security --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' }} + - name: Run Tests + run: go test -v github.com/jfrog/jfrog-cli-security ${{ matrix.os == 'ubuntu' && matrix.suite.ubuntuTestFlags || matrix.suite.testFlags }} --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 -v -race --timeout 30m -cover \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fd821772..78ab6ea3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -71,10 +71,15 @@ go test -v github.com/jfrog/jfrog-cli-security [test-types] [flags] ### The available test types are: -| Type | Description | -| -------------------- | ------------------ | -| `-test.security` | [Default: true] Security commands integration tests | -| `-test.dockerScan` | [Optional] Docker scan integration tests | +| Type | Description | +| -------------------- | ------------------------------------------- | +| `-test.unit` | [Default: true] Unit tests | +| `-test.artifactory` | [Optional] Artifactory integration tests | +| `-test.xray` | [Optional] Xray commands integration tests | +| `-test.audit` | [Optional] Audit command integration tests | +| `-test.scan` | [Optional] Scan commands integration tests | +| `-test.dockerScan` | [Optional] Docker scan integration tests | +| `-test.all` | [Optional] Activate all available tests | ### Docker Scan tests diff --git a/artifactory_test.go b/artifactory_test.go index 26df2789..0c317dcc 100644 --- a/artifactory_test.go +++ b/artifactory_test.go @@ -2,12 +2,13 @@ package main import ( "errors" - "github.com/stretchr/testify/require" "os" "os/exec" "path/filepath" "testing" + "github.com/stretchr/testify/require" + "github.com/jfrog/jfrog-cli-core/v2/utils/dependencies" "github.com/jfrog/jfrog-client-go/utils/io/fileutils" @@ -33,6 +34,7 @@ import ( // We perform validation on dependency resolution from an Artifactory server during the construction of the dependency tree during 'audit' flow. // This process involves resolving all dependencies required by the project. func TestDependencyResolutionFromArtifactory(t *testing.T) { + securityTestUtils.InitArtifactoryTest(t) testCases := []struct { testProjectPath []string resolveRepoName string @@ -208,6 +210,7 @@ func clearOrRedirectLocalCacheIfNeeded(t *testing.T, projectType project.Project } func TestDownloadAnalyzerManagerIfNeeded(t *testing.T) { + securityTestUtils.InitArtifactoryTest(t) // Configure a new JFrog CLI home dir. tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() diff --git a/audit_test.go b/audit_test.go index 51bafd55..6fcf701d 100644 --- a/audit_test.go +++ b/audit_test.go @@ -35,7 +35,7 @@ func TestXrayAuditNpmSimpleJson(t *testing.T) { } func testXrayAuditNpm(t *testing.T, format string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() npmProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "npm", "npm") @@ -61,7 +61,7 @@ func TestXrayAuditPnpmSimpleJson(t *testing.T) { } func testXrayAuditPnpm(t *testing.T, format string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() npmProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "npm", "npm-no-lock") @@ -117,7 +117,7 @@ func TestXrayAuditYarnV1SimpleJson(t *testing.T) { } func testXrayAuditYarn(t *testing.T, projectDirName string, yarnCmd func()) { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() yarnProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "yarn", projectDirName) @@ -232,7 +232,7 @@ func TestXrayAuditNugetSimpleJson(t *testing.T) { } func testXrayAuditNuget(t *testing.T, projectName, format string, restoreTech string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() projectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "nuget", projectName) @@ -261,7 +261,7 @@ func TestXrayAuditGradleSimpleJson(t *testing.T) { } func testXrayAuditGradle(t *testing.T, format string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() gradleProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "gradle", "gradle") @@ -285,7 +285,7 @@ func TestXrayAuditMavenSimpleJson(t *testing.T) { } func testXrayAuditMaven(t *testing.T, format string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() mvnProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "maven", "maven") @@ -299,7 +299,7 @@ func testXrayAuditMaven(t *testing.T, format string) string { } func TestXrayAuditNoTech(t *testing.T) { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() prevWd := securityTestUtils.ChangeWD(t, tempDirPath) @@ -310,7 +310,7 @@ func TestXrayAuditNoTech(t *testing.T) { } func TestXrayAuditMultiProjects(t *testing.T) { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() multiProject := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects") @@ -350,7 +350,7 @@ func TestXrayAuditPipSimpleJsonWithRequirementsFile(t *testing.T) { } func testXrayAuditPip(t *testing.T, format, requirementsFile string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() pipProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "python", "pip", "pip-project") @@ -379,7 +379,7 @@ func TestXrayAuditPipenvSimpleJson(t *testing.T) { } func testXrayAuditPipenv(t *testing.T, format string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() pipenvProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "python", "pipenv", "pipenv-project") @@ -403,7 +403,7 @@ func TestXrayAuditPoetrySimpleJson(t *testing.T) { } func testXrayAuditPoetry(t *testing.T, format string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() poetryProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "python", "poetry", "poetry-project") @@ -445,7 +445,7 @@ func TestXrayAuditJasNoViolationsSimpleJson(t *testing.T) { } func testXrayAuditJas(t *testing.T, format string, project string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() projectDir := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), filepath.Join("projects", project)) @@ -462,7 +462,7 @@ func testXrayAuditJas(t *testing.T, format string, project string) string { } func TestXrayAuditDetectTech(t *testing.T) { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() mvnProjectPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "maven", "maven") @@ -480,7 +480,7 @@ func TestXrayAuditDetectTech(t *testing.T) { } func TestXrayRecursiveScan(t *testing.T) { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitAuditTest(t, scangraph.GraphScanMinXrayVersion) tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() projectDir := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers") diff --git a/jfrogclisecurity_test.go b/jfrogclisecurity_test.go index 3af50790..815c048e 100644 --- a/jfrogclisecurity_test.go +++ b/jfrogclisecurity_test.go @@ -1,7 +1,6 @@ package main import ( - "flag" "fmt" "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" @@ -17,13 +16,13 @@ import ( ) func TestMain(m *testing.M) { - setupIntegrationTests() + setupTests() result := m.Run() - tearDownIntegrationTests() + tearDownTests() os.Exit(result) } -func setupIntegrationTests() { +func setupTests() { // Disable usage report. if err := os.Setenv(coreutils.ReportUsage, "false"); err != nil { clientLog.Error(fmt.Sprintf("Couldn't set env: %s. Error: %s", coreutils.ReportUsage, err.Error())) @@ -35,15 +34,15 @@ func setupIntegrationTests() { os.Exit(1) } // General - flag.Parse() log.SetDefaultLogger() - // Init + configTests.InitTestFlags() + // Init Integration tests utils.InitTestCliDetails() utils.AuthenticateArtifactory() utils.CreateRequiredRepositories() } -func tearDownIntegrationTests() { +func tearDownTests() { // Important - Virtual repositories must be deleted first utils.DeleteRepos(configTests.CreatedVirtualRepositories) utils.DeleteRepos(configTests.CreatedNonVirtualRepositories) diff --git a/scans_test.go b/scans_test.go index 6d9b6bae..e7bb11ee 100644 --- a/scans_test.go +++ b/scans_test.go @@ -74,13 +74,13 @@ func TestXrayBinaryScanSimpleJsonWithProgress(t *testing.T) { } func testXrayBinaryScan(t *testing.T, format string) string { - securityTestUtils.InitSecurityTest(t, scangraph.GraphScanMinXrayVersion) + securityTestUtils.InitScanTest(t, scangraph.GraphScanMinXrayVersion) binariesPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "binaries", "*") return securityTests.PlatformCli.RunCliCmdWithOutput(t, "scan", binariesPath, "--licenses", "--format="+format) } func TestXrayBinaryScanWithBypassArchiveLimits(t *testing.T) { - securityTestUtils.InitSecurityTest(t, scan.BypassArchiveLimitsMinXrayVersion) + securityTestUtils.InitScanTest(t, scan.BypassArchiveLimitsMinXrayVersion) unsetEnv := clientTestUtils.SetEnvWithCallbackAndAssert(t, "JF_INDEXER_COMPRESS_MAXENTITIES", "10") defer unsetEnv() binariesPath := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "binaries", "*") @@ -131,11 +131,8 @@ func TestDockerScan(t *testing.T) { } func initNativeDockerWithXrayTest(t *testing.T) func() { - if !*securityTests.TestDockerScan || !*securityTests.TestSecurity { - t.Skip("Skipping Docker scan test. To run Xray Docker test add the '-test.dockerScan=true' and '-test.security=true' options.") - } + securityTestUtils.InitDockerScanTest(t, scan.DockerScanMinXrayVersion) oldHomeDir := os.Getenv(coreutils.HomeDir) - securityTestUtils.ValidateXrayVersion(t, scan.DockerScanMinXrayVersion) // Create server config to use with the command. securityTestUtils.CreateJfrogHomeConfig(t, true) // Add docker scan mock command @@ -236,7 +233,7 @@ func createTestWatch(t *testing.T) (string, func()) { // Curation tests func TestCurationAudit(t *testing.T) { - securityTestUtils.InitSecurityTest(t, "") + securityTestUtils.InitScanTest(t, "") tempDirPath, createTempDirCallback := coreTests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() multiProject := filepath.Join(filepath.FromSlash(securityTestUtils.GetTestResourcesPath()), "projects", "package-managers", "npm") diff --git a/tests/config.go b/tests/config.go index 9fe0641e..37783044 100644 --- a/tests/config.go +++ b/tests/config.go @@ -7,6 +7,7 @@ import ( "github.com/jfrog/jfrog-cli-core/v2/utils/config" "github.com/jfrog/jfrog-client-go/auth" "github.com/jfrog/jfrog-client-go/utils/io/httputils" + clientLog "github.com/jfrog/jfrog-client-go/utils/log" coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests" ) @@ -29,8 +30,13 @@ var ( // Test flags var ( - TestSecurity *bool - TestDockerScan *bool + AllTests *bool + TestUnit *bool + TestArtifactory *bool + TestXray *bool + TestAudit *bool + TestScan *bool + TestDockerScan *bool JfrogUrl *string JfrogUser *string @@ -41,13 +47,16 @@ var ( ContainerRegistry *string - HideUnitTestLog *bool - SkipUnitTests *bool - ciRunId *string + ciRunId *string ) func init() { - TestSecurity = flag.Bool("test.security", true, "Test Security") + AllTests = flag.Bool("test.all", false, "Run all tests") + TestUnit = flag.Bool("test.unit", true, "Unit tests") + TestArtifactory = flag.Bool("test.artifactory", false, "Test Artifactory integration") + TestXray = flag.Bool("test.xray", false, "Test Xray integration") + TestAudit = flag.Bool("test.audit", false, "Test Audit command") + TestScan = flag.Bool("test.scan", false, "Test Scan commands") TestDockerScan = flag.Bool("test.dockerScan", false, "Test Docker scan") JfrogUrl = flag.String("jfrog.url", "http://localhost:8081/", "JFrog platform url") @@ -59,8 +68,19 @@ func init() { ContainerRegistry = flag.String("test.containerRegistry", "localhost:8082", "Container registry") - HideUnitTestLog = flag.Bool("test.hideUnitTestLog", false, "Hide unit tests logs and print it in a file") - SkipUnitTests = flag.Bool("test.skipUnitTests", false, "Skip unit tests") - ciRunId = flag.String("ci.runId", "", "A unique identifier used as a suffix to create repositories and builds in the tests") } + +func InitTestFlags() { + flag.Parse() + shouldRunAllTests := *AllTests + if shouldRunAllTests { + clientLog.Info("All tests flag is set. Running all tests.") + *TestUnit = true + *TestArtifactory = true + *TestXray = true + *TestAudit = true + *TestScan = true + *TestDockerScan = true + } +} diff --git a/tests/consts.go b/tests/consts.go index 2b39678b..2f9c7017 100644 --- a/tests/consts.go +++ b/tests/consts.go @@ -86,8 +86,8 @@ var reposConfigMap = map[*string]string{ // Return local and remote repositories for the test suites, respectfully func GetNonVirtualRepositories() map[*string]string { nonVirtualReposMap := map[*bool][]*string{ - TestDockerScan: {&DockerLocalRepo, &DockerRemoteRepo}, - TestSecurity: {&NpmRemoteRepo, &NugetRemoteRepo, &YarnRemoteRepo, &GradleRemoteRepo, &MvnRemoteRepo, &GoRepo, &GoRemoteRepo, &PypiRemoteRepo}, + TestArtifactory: {&NpmRemoteRepo, &NugetRemoteRepo, &YarnRemoteRepo, &GradleRemoteRepo, &MvnRemoteRepo, &GoRepo, &GoRemoteRepo, &PypiRemoteRepo}, + TestDockerScan: {&DockerLocalRepo, &DockerRemoteRepo}, } return getNeededRepositories(nonVirtualReposMap) } @@ -95,8 +95,8 @@ func GetNonVirtualRepositories() map[*string]string { // Return virtual repositories for the test suites, respectfully func GetVirtualRepositories() map[*string]string { virtualReposMap := map[*bool][]*string{ - TestDockerScan: {&DockerVirtualRepo}, - TestSecurity: {&GoVirtualRepo}, + TestDockerScan: {&DockerVirtualRepo}, + TestArtifactory: {&GoVirtualRepo}, } return getNeededRepositories(virtualReposMap) } diff --git a/tests/utils/test_utils.go b/tests/utils/test_utils.go index 0988f4b4..79be3c0a 100644 --- a/tests/utils/test_utils.go +++ b/tests/utils/test_utils.go @@ -16,9 +16,42 @@ import ( clientTests "github.com/jfrog/jfrog-client-go/utils/tests" ) -func InitSecurityTest(t *testing.T, minVersion string) { - if !*configTests.TestSecurity { - t.Skip("Skipping Security test. To run Security test add the '-test.security=true' option.") +func InitUnitTest(t *testing.T) { + if !*configTests.TestUnit { + t.Skip("Skipping unit tests.") + } +} + +func InitArtifactoryTest(t *testing.T) { + if !*configTests.TestArtifactory { + t.Skip("Skipping Artifactory test. To run Artifactory test add the '-test.artifactory=true' option.") + } +} + +func InitXrayTest(t *testing.T, minVersion string) { + if !*configTests.TestXray { + t.Skip("Skipping Xray test. To run Xray test add the '-test.xray=true' option.") + } + ValidateXrayVersion(t, minVersion) +} + +func InitAuditTest(t *testing.T, minVersion string) { + if !*configTests.TestAudit { + t.Skip("Skipping audit test. To run Audit test add the '-test.audit=true' option.") + } + ValidateXrayVersion(t, minVersion) +} + +func InitScanTest(t *testing.T, minVersion string) { + if !*configTests.TestScan { + t.Skip("Skipping scan test. To run Scan test add the '-test.scan=true' option.") + } + ValidateXrayVersion(t, minVersion) +} + +func InitDockerScanTest(t *testing.T, minVersion string) { + if !*configTests.TestDockerScan || !*configTests.TestScan { + t.Skip("Skipping Docker scan test. To run Xray Docker test add the '-test.dockerScan=true' and '-test.scan=true' options.") } ValidateXrayVersion(t, minVersion) } diff --git a/unit_test.go b/unit_test.go index 2b7bcc39..d97fd814 100644 --- a/unit_test.go +++ b/unit_test.go @@ -2,15 +2,15 @@ package main import ( "github.com/stretchr/testify/assert" + "os" + "testing" coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests" - configTests "github.com/jfrog/jfrog-cli-security/tests" clientTests "github.com/jfrog/jfrog-client-go/utils/tests" clientLog "github.com/jfrog/jfrog-client-go/utils/log" - "os" - "testing" + "github.com/jfrog/jfrog-cli-security/tests/utils" ) const ( @@ -18,9 +18,7 @@ const ( ) func TestUnitTests(t *testing.T) { - if *configTests.SkipUnitTests { - t.Skip("Skipping unit tests.") - } + utils.InitUnitTest(t) // Create temp jfrog home cleanUpJfrogHome, err := coreTests.SetJfrogHome() if err != nil { @@ -32,5 +30,5 @@ func TestUnitTests(t *testing.T) { packages := clientTests.GetTestPackages("./...") packages = clientTests.ExcludeTestsPackage(packages, CliIntegrationTests) - assert.NoError(t, clientTests.RunTests(packages, *configTests.HideUnitTestLog)) + assert.NoError(t, clientTests.RunTests(packages, false)) } diff --git a/xray_test.go b/xray_test.go index 695162dc..37e827a5 100644 --- a/xray_test.go +++ b/xray_test.go @@ -11,7 +11,7 @@ import ( ) func TestXrayCurl(t *testing.T) { - securityTestUtils.InitSecurityTest(t, "") + securityTestUtils.InitXrayTest(t, "") // Configure a new server named "default". securityTestUtils.CreateJfrogHomeConfig(t, true) defer securityTestUtils.CleanTestsHomeEnv() @@ -27,7 +27,7 @@ func TestXrayCurl(t *testing.T) { } func TestXrayOfflineDBSyncV3(t *testing.T) { - securityTestUtils.InitSecurityTest(t, "") + securityTestUtils.InitXrayTest(t, "") // Validate license-id err := securityTests.PlatformCli.WithoutCredentials().Exec("xr", "ou") assert.EqualError(t, err, "Mandatory flag 'license-id' is missing") From 91b0cb1085b6e6d0fb221132bc45e8ebd9a12b33 Mon Sep 17 00:00:00 2001 From: attiasas Date: Mon, 25 Mar 2024 12:03:04 +0200 Subject: [PATCH 2/2] CR review --- tests/config.go | 7 ++----- tests/utils/test_utils.go | 10 +++++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/config.go b/tests/config.go index 37783044..a833247c 100644 --- a/tests/config.go +++ b/tests/config.go @@ -5,11 +5,10 @@ import ( "github.com/jfrog/jfrog-cli-core/v2/plugins/components" "github.com/jfrog/jfrog-cli-core/v2/utils/config" + coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests" "github.com/jfrog/jfrog-client-go/auth" "github.com/jfrog/jfrog-client-go/utils/io/httputils" clientLog "github.com/jfrog/jfrog-client-go/utils/log" - - coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests" ) // Integration tests - global variables @@ -46,8 +45,7 @@ var ( JfrogAccessToken *string ContainerRegistry *string - - ciRunId *string + ciRunId *string ) func init() { @@ -67,7 +65,6 @@ func init() { JfrogAccessToken = flag.String("jfrog.adminToken", "", "JFrog platform admin token") ContainerRegistry = flag.String("test.containerRegistry", "localhost:8082", "Container registry") - ciRunId = flag.String("ci.runId", "", "A unique identifier used as a suffix to create repositories and builds in the tests") } diff --git a/tests/utils/test_utils.go b/tests/utils/test_utils.go index 79be3c0a..70ad3533 100644 --- a/tests/utils/test_utils.go +++ b/tests/utils/test_utils.go @@ -24,34 +24,34 @@ func InitUnitTest(t *testing.T) { func InitArtifactoryTest(t *testing.T) { if !*configTests.TestArtifactory { - t.Skip("Skipping Artifactory test. To run Artifactory test add the '-test.artifactory=true' option.") + t.Skip("Skipping Artifactory test. To run Artifactory tests, add the '-test.artifactory=true' option.") } } func InitXrayTest(t *testing.T, minVersion string) { if !*configTests.TestXray { - t.Skip("Skipping Xray test. To run Xray test add the '-test.xray=true' option.") + t.Skip("Skipping Xray test. To run Xray tests, add the '-test.xray=true' option.") } ValidateXrayVersion(t, minVersion) } func InitAuditTest(t *testing.T, minVersion string) { if !*configTests.TestAudit { - t.Skip("Skipping audit test. To run Audit test add the '-test.audit=true' option.") + t.Skip("Skipping audit test. To run Audit tests, add the '-test.audit=true' option.") } ValidateXrayVersion(t, minVersion) } func InitScanTest(t *testing.T, minVersion string) { if !*configTests.TestScan { - t.Skip("Skipping scan test. To run Scan test add the '-test.scan=true' option.") + t.Skip("Skipping scan test. To run Scan tests, add the '-test.scan=true' option.") } ValidateXrayVersion(t, minVersion) } func InitDockerScanTest(t *testing.T, minVersion string) { if !*configTests.TestDockerScan || !*configTests.TestScan { - t.Skip("Skipping Docker scan test. To run Xray Docker test add the '-test.dockerScan=true' and '-test.scan=true' options.") + t.Skip("Skipping Docker scan test. To run Xray Docker tests, add the '-test.dockerScan=true' and '-test.scan=true' options.") } ValidateXrayVersion(t, minVersion) }