Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rt curl ServerID environment variable usage #2882

Merged
merged 7 commits into from
Feb 26, 2025
13 changes: 13 additions & 0 deletions artifactory/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,19 @@ func curlCmd(c *cli.Context) error {
if err != nil {
return err
}

// Check if --server-id is explicitly passed in arguments
flagIndex, _, _, err := coreutils.FindFlag("--server-id", cliutils.ExtractCommand(c))
if err != nil {
return err
}
// If --server-id is NOT present, then we check for JFROG_CLI_SERVER_ID env variable
if flagIndex == -1 {
if artDetails, err := cliutils.CreateArtifactoryDetailsByFlags(c); err == nil && artDetails.ArtifactoryUrl != "" {
rtCurlCommand.SetServerDetails(artDetails)
rtCurlCommand.SetUrl(artDetails.ArtifactoryUrl)
}
}
return commands.Exec(rtCurlCommand)
}

Expand Down
44 changes: 35 additions & 9 deletions artifactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5543,15 +5543,41 @@ func TestArtifactoryCurl(t *testing.T) {
_, err := createServerConfigAndReturnPassphrase(t)
defer deleteServerConfig(t)
assert.NoError(t, err)
// Check curl command with config default server
err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version")
assert.NoError(t, err)
// Check curl command with '--server-id' flag
err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id="+tests.ServerId)
assert.NoError(t, err)
// Check curl command with invalid server id - should get an error.
err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id=not_configured_name_"+tests.ServerId)
assert.Error(t, err)

baseArgs := []string{"curl", "-XGET", "/api/system/version"}

testRuns := []struct {
testName string
serverIDEnvValue string
expectedErr bool
serverID string
}{
{"defaultConfig", "", false, ""},
{"serverIdFlag", "", false, tests.ServerId},
{"invalidServerId", "", true, "not_configured_name_" + tests.ServerId},
{"envVarSet", tests.ServerId, false, ""},
{"envVarWithFlag", tests.ServerId, false, tests.ServerId},
{"priorityFlagOverEnv", "wrong_server_id", false, tests.ServerId},
{"priorityEnvOverDefault", tests.ServerId, false, ""},
}

for _, test := range testRuns {
t.Run(test.testName, func(t *testing.T) {
setEnvCallBack := clientTestUtils.SetEnvWithCallbackAndAssert(t, coreutils.ServerID, test.serverIDEnvValue)

args := append([]string{}, baseArgs...)
if test.serverID != "" {
args = append(args, "--server-id="+test.serverID)
}
err = artifactoryCli.WithoutCredentials().Exec(args...)
if test.expectedErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
setEnvCallBack()
})
}

cleanArtifactoryTest()
}
Expand Down
Loading