Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix-jfrog-curl-env' into fix-jfr…
Browse files Browse the repository at this point in the history
…og-curl-env
  • Loading branch information
agrasth committed Feb 25, 2025
2 parents cc21b5b + 8ee6630 commit ad805fa
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions artifactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5543,27 +5543,47 @@ 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)

// Set JFROG_CLI_SERVER_ID and check curl command
_ = os.Setenv(coreutils.ServerID, tests.ServerId)
// Store the original environment variable value (if exists) to restore later.
originalServerID, hasOriginal := os.LookupEnv(coreutils.ServerID)
defer func() {
_ = os.Unsetenv(coreutils.ServerID)
}() // Cleanup after test
err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version")
assert.NoError(t, err)
if hasOriginal {
_ = os.Setenv(coreutils.ServerID, originalServerID)
} else {
_ = os.Unsetenv(coreutils.ServerID)
}
}()

// Set JFROG_CLI_SERVER_ID with --server-id flag (should use the flag)
err = artifactoryCli.WithoutCredentials().Exec("curl", "-XGET", "/api/system/version", "--server-id="+tests.ServerId)
assert.NoError(t, err)
testRuns := []struct {
name string
envVar string
envValue string
args []string
expectError bool
}{
{"defaultConfig", "", "", []string{"curl", "-XGET", "/api/system/version"}, false},
{"serverIdFlag", "", "", []string{"curl", "-XGET", "/api/system/version", "--server-id=" + tests.ServerId}, false},
{"invalidServerId", "", "", []string{"curl", "-XGET", "/api/system/version", "--server-id=not_configured_name_" + tests.ServerId}, true},
{"envVarSet", coreutils.ServerID, tests.ServerId, []string{"curl", "-XGET", "/api/system/version"}, false},
{"envVarWithFlag", coreutils.ServerID, tests.ServerId, []string{"curl", "-XGET", "/api/system/version", "--server-id=" + tests.ServerId}, false},
{"priorityFlagOverEnv", coreutils.ServerID, "wrong_server_id", []string{"curl", "-XGET", "/api/system/version", "--server-id=" + tests.ServerId}, false},
{"priorityEnvOverDefault", coreutils.ServerID, tests.ServerId, []string{"curl", "-XGET", "/api/system/version"}, false},
}

for _, test := range testRuns {
t.Run(test.name, func(t *testing.T) {
if test.envVar != "" {
_ = os.Setenv(test.envVar, test.envValue)
}

err := artifactoryCli.WithoutCredentials().Exec(test.args...)
if test.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}

cleanArtifactoryTest()
}
Expand Down

0 comments on commit ad805fa

Please sign in to comment.