From f9f11adf13c56610ef86dcc533094daad98f8788 Mon Sep 17 00:00:00 2001 From: yahavi Date: Sun, 20 Oct 2024 09:00:56 +0300 Subject: [PATCH] Fix tests --- .github/workflows/accessTests.yml | 7 +- .github/workflows/artifactoryTests.yml | 2 +- .github/workflows/transferTests.yml | 2 +- access_test.go | 138 +++++++++++++------------ artifactory_test.go | 108 ------------------- 5 files changed, 78 insertions(+), 179 deletions(-) diff --git a/.github/workflows/accessTests.yml b/.github/workflows/accessTests.yml index c4e863b4d..45195ef6c 100644 --- a/.github/workflows/accessTests.yml +++ b/.github/workflows/accessTests.yml @@ -30,5 +30,10 @@ jobs: - name: Setup Go with cache uses: jfrog/.github/actions/install-go-with-cache@main + - name: Install local Artifactory + uses: jfrog/.github/actions/install-local-artifactory@main + with: + RTLIC: ${{ secrets.RTLIC }} + - name: Run Access tests - run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.access --jfrog.url=${{ secrets.PLATFORM_URL }} --jfrog.adminToken=${{ secrets.PLATFORM_ADMIN_TOKEN }} --jfrog.user=${{ secrets.PLATFORM_USER }} --ci.runId=${{ runner.os }}-access + run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.access --jfrog.url=http://127.0.0.1:8082 --jfrog.adminToken=${{ env.JFROG_TESTS_LOCAL_ACCESS_TOKEN }} diff --git a/.github/workflows/artifactoryTests.yml b/.github/workflows/artifactoryTests.yml index 304ffebb9..34197921b 100644 --- a/.github/workflows/artifactoryTests.yml +++ b/.github/workflows/artifactoryTests.yml @@ -37,7 +37,7 @@ jobs: RTLIC: ${{ secrets.RTLIC }} - name: Run Artifactory tests - run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.artifactory + run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.artifactory --jfrog.url=http://127.0.0.1:8082 --jfrog.adminToken=${{ env.JFROG_TESTS_LOCAL_ACCESS_TOKEN }} if: ${{ matrix.suite == 'artifactory' }} - name: Run Artifactory projects tests diff --git a/.github/workflows/transferTests.yml b/.github/workflows/transferTests.yml index 32cc3d2ce..6fa487657 100644 --- a/.github/workflows/transferTests.yml +++ b/.github/workflows/transferTests.yml @@ -37,7 +37,7 @@ jobs: JFROG_HOME: ${{ runner.temp }} - name: Run transfer tests - run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.transfer --test.installDataTransferPlugin --jfrog.targetUrl=${{ secrets.PLATFORM_URL }} --jfrog.targetAdminToken=${{ secrets.PLATFORM_ADMIN_TOKEN }} --jfrog.home=${{ runner.temp }} --ci.runId=${{ runner.os }}-transfer-7 + run: go test -v github.com/jfrog/jfrog-cli --timeout 0 --test.transfer --test.installDataTransferPlugin --jfrog.url=http://127.0.0.1:8082 --jfrog.targetUrl=${{ secrets.PLATFORM_URL }} --jfrog.targetAdminToken=${{ secrets.PLATFORM_ADMIN_TOKEN }} --jfrog.home=${{ runner.temp }} --ci.runId=${{ runner.os }}-transfer-7 Transfer-Artifactory-6-Tests: if: contains(github.event.pull_request.labels.*.name, 'safe to test') || github.event_name == 'push' diff --git a/access_test.go b/access_test.go index 0e10177fc..c76fa190b 100644 --- a/access_test.go +++ b/access_test.go @@ -3,6 +3,9 @@ package main import ( "encoding/json" "fmt" + "net/http" + "testing" + "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" "github.com/jfrog/jfrog-cli-core/v2/common/commands" "github.com/jfrog/jfrog-cli-core/v2/utils/config" @@ -16,8 +19,6 @@ import ( "github.com/jfrog/jfrog-client-go/utils/errorutils" "github.com/jfrog/jfrog-client-go/utils/io/httputils" "github.com/stretchr/testify/assert" - "net/http" - "testing" ) var ( @@ -176,90 +177,91 @@ const ( userScope = "applied-permissions/user" ) -var atcTestCases = []struct { - name string - args []string - shouldExpire bool - // The expected expiry or -1 if we use the default expiry value - expectedExpiry int - expectedScope string - expectedRefreshable bool - expectedReference bool -}{ - { - name: "default", - args: []string{"atc"}, - shouldExpire: true, - expectedExpiry: -1, - expectedScope: userScope, - expectedRefreshable: false, - expectedReference: false, - }, - { - name: "explicit user, no expiry", - args: []string{"atc", auth.ExtractUsernameFromAccessToken(*tests.JfrogAccessToken), "--expiry=0"}, - shouldExpire: false, - expectedExpiry: 0, - expectedScope: userScope, - expectedRefreshable: false, - expectedReference: false, - }, - { - name: "refreshable, admin", - args: []string{"atc", "--refreshable", "--grant-admin"}, - shouldExpire: true, - expectedExpiry: -1, - expectedScope: "applied-permissions/admin", - expectedRefreshable: true, - expectedReference: false, - }, - { - name: "reference, custom scope, custom expiry", - args: []string{"atc", "--reference", "--scope=system:metrics:r", "--expiry=123456"}, - shouldExpire: true, - expectedExpiry: 123456, - expectedScope: "system:metrics:r", - expectedRefreshable: false, - expectedReference: true, - }, - { - name: "groups, description", - args: []string{"atc", "--groups=group1,group2", "--description=description"}, - shouldExpire: true, - expectedExpiry: -1, - expectedScope: "applied-permissions/groups:group1,group2", - expectedRefreshable: false, - expectedReference: false, - }, -} - func TestAccessTokenCreate(t *testing.T) { initAccessTest(t) if *tests.JfrogAccessToken == "" { t.Skip("access token create command only supports authorization with access token, but a token is not provided. Skipping...") } - for _, test := range atcTestCases { - t.Run(test.name, func(t *testing.T) { + var testCases = []struct { + name string + args []string + shouldExpire bool + // The expected expiry or -1 if we use the default expiry value + expectedExpiry int + expectedScope string + expectedRefreshable bool + expectedReference bool + }{ + { + name: "default", + args: []string{"atc"}, + shouldExpire: true, + expectedExpiry: -1, + expectedScope: userScope, + expectedRefreshable: false, + expectedReference: false, + }, + { + name: "explicit user, no expiry", + args: []string{"atc", auth.ExtractUsernameFromAccessToken(*tests.JfrogAccessToken), "--expiry=0"}, + shouldExpire: false, + expectedExpiry: 0, + expectedScope: userScope, + expectedRefreshable: false, + expectedReference: false, + }, + { + name: "refreshable, admin", + args: []string{"atc", "--refreshable", "--grant-admin"}, + shouldExpire: true, + expectedExpiry: -1, + expectedScope: "applied-permissions/admin", + expectedRefreshable: true, + expectedReference: false, + }, + { + name: "reference, custom scope, custom expiry", + args: []string{"atc", "--reference", "--scope=system:metrics:r", "--expiry=123456"}, + shouldExpire: true, + expectedExpiry: 123456, + expectedScope: "system:metrics:r", + expectedRefreshable: false, + expectedReference: true, + }, + { + name: "groups, description", + args: []string{"atc", "--groups=group1,group2", "--description=description"}, + shouldExpire: true, + expectedExpiry: -1, + expectedScope: "applied-permissions/groups:group1,group2", + expectedRefreshable: false, + expectedReference: false, + }, + } + + // Discard output logging to prevent negative logs + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { var token auth.CreateTokenResponseData - output := accessCli.RunCliCmdWithOutput(t, test.args...) + output := accessCli.RunCliCmdWithOutput(t, testCase.args...) assert.NoError(t, json.Unmarshal([]byte(output), &token)) defer revokeToken(t, token.TokenId) - if test.shouldExpire { - if test.expectedExpiry == -1 { + if testCase.shouldExpire { + if testCase.expectedExpiry == -1 { // If expectedExpiry is -1, expect the default expiry assert.Positive(t, *token.ExpiresIn) } else { - assert.EqualValues(t, test.expectedExpiry, *token.ExpiresIn) + assert.EqualValues(t, testCase.expectedExpiry, *token.ExpiresIn) } } else { assert.Nil(t, token.ExpiresIn) } assert.NotEmpty(t, token.AccessToken) - assert.Equal(t, test.expectedScope, token.Scope) - assertNotEmptyIfExpected(t, test.expectedRefreshable, token.RefreshToken) - assertNotEmptyIfExpected(t, test.expectedReference, token.ReferenceToken) + assert.Equal(t, testCase.expectedScope, token.Scope) + assertNotEmptyIfExpected(t, testCase.expectedRefreshable, token.RefreshToken) + assertNotEmptyIfExpected(t, testCase.expectedReference, token.ReferenceToken) // Try pinging Artifactory with the new token. assert.NoError(t, coreTests.NewJfrogCli(execMain, "jfrog rt", diff --git a/artifactory_test.go b/artifactory_test.go index 4269dcc85..9ab9cd2b1 100644 --- a/artifactory_test.go +++ b/artifactory_test.go @@ -3439,114 +3439,6 @@ func TestArtifactoryDownloadByBuildNoPatternUsingSimpleDownload(t *testing.T) { cleanArtifactoryTest() } -func TestArtifactoryDownloadByArchiveEntriesCli(t *testing.T) { - initArtifactoryTest(t, "") - uploadSpecFile, err := tests.CreateSpec(tests.ArchiveEntriesUpload) - assert.NoError(t, err) - - // Upload archives - runRt(t, "upload", "--spec="+uploadSpecFile) - - // Trigger archive indexing on the repo. - triggerArchiveIndexing(t) - - // Create executor for running with retries - retryExecutor := createRetryExecutorForArchiveEntries(tests.GetBuildArchiveEntriesDownloadCli(), - []string{"dl", tests.RtRepo1, "out/", "--archive-entries=(*)c1.in", "--flat=true"}) - - // Perform download by archive-entries only the archives containing c1.in, and validate results - assert.NoError(t, retryExecutor.Execute()) - - // Cleanup - cleanArtifactoryTest() -} - -func triggerArchiveIndexing(t *testing.T) { - client, err := httpclient.ClientBuilder().Build() - assert.NoError(t, err) - resp, _, err := client.SendPost(serverDetails.ArtifactoryUrl+"api/archiveIndex/"+tests.RtRepo1, []byte{}, artHttpDetails, "") - if err != nil { - assert.NoError(t, err, "archive indexing failed") - return - } - assert.Equal(t, http.StatusAccepted, resp.StatusCode, "archive indexing failed") - // Indexing buffer - time.Sleep(3 * time.Second) -} - -func TestArtifactoryDownloadByArchiveEntriesSpecificPathCli(t *testing.T) { - initArtifactoryTest(t, "") - uploadSpecFile, err := tests.CreateSpec(tests.ArchiveEntriesUpload) - assert.NoError(t, err) - - // Upload archives - runRt(t, "upload", "--spec="+uploadSpecFile) - - // Trigger archive indexing on the repo. - triggerArchiveIndexing(t) - - // Create executor for running with retries - retryExecutor := createRetryExecutorForArchiveEntries(tests.GetBuildArchiveEntriesSpecificPathDownload(), - []string{"dl", tests.RtRepo1, "out/", "--archive-entries=b/c/c1.in", "--flat=true"}) - - // Perform download by archive-entries only the archives containing c1.in, and validate results - assert.NoError(t, retryExecutor.Execute()) - - // Cleanup - cleanArtifactoryTest() -} - -func TestArtifactoryDownloadByArchiveEntriesSpec(t *testing.T) { - initArtifactoryTest(t, "") - uploadSpecFile, err := tests.CreateSpec(tests.ArchiveEntriesUpload) - assert.NoError(t, err) - downloadSpecFile, err := tests.CreateSpec(tests.ArchiveEntriesDownload) - assert.NoError(t, err) - - // Upload archives - runRt(t, "upload", "--spec="+uploadSpecFile) - - // Trigger archive indexing on the repo. - triggerArchiveIndexing(t) - - // Create executor for running with retries - retryExecutor := createRetryExecutorForArchiveEntries(tests.GetBuildArchiveEntriesDownloadSpec(), - []string{"dl", "--spec=" + downloadSpecFile}) - - // Perform download by archive-entries only the archives containing d1.in, and validate results - assert.NoError(t, retryExecutor.Execute()) - - // Cleanup - cleanArtifactoryTest() -} - -func createRetryExecutorForArchiveEntries(expected []string, args []string) *clientutils.RetryExecutor { - return &clientutils.RetryExecutor{ - MaxRetries: 120, - // RetriesIntervalMilliSecs in milliseconds - RetriesIntervalMilliSecs: 1 * 1000, - ErrorMessage: "Waiting for Artifactory to index archives...", - ExecutionHandler: func() (bool, error) { - // Execute the requested cli command - err := artifactoryCli.Exec(args...) - if err != nil { - return true, err - } - err = validateDownloadByArchiveEntries(expected) - if err != nil { - return false, err - } - return false, nil - }, - } -} - -func validateDownloadByArchiveEntries(expected []string) error { - // Validate files are downloaded as expected - paths, _ := fileutils.ListFilesRecursiveWalkIntoDirSymlink(tests.Out, false) - return tests.ValidateListsIdentical(expected, paths) -} - func TestArtifactoryDownloadExcludeByCli(t *testing.T) { initArtifactoryTest(t, "")