test(apiserver): run TestLoadJobs only when a live server is given

It logged in to a hard-coded http://localhost:8080 with fixed
credentials, so 'go test ./...' failed with connection refused. It now
skips unless XCE_LOAD_TEST_URL is set, with the credentials in
XCE_LOAD_TEST_USER/XCE_LOAD_TEST_PASSWORD; 'go test ./...' is green.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Rongsong Shen 2026-10-02 09:37:43 +00:00
parent 152ef04c71
commit a65146a9ae
Signed by untrusted user: rshen
GPG key ID: 80533536B0EB651C

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"sync"
"testing"
"time"
@ -21,8 +22,8 @@ type AuthResponse struct {
}
// login
func login(username, password string) (string, error) {
const loginURL = "http://localhost:8080/xce/v1/auth/logon" //replace with your login url
func login(baseURL, username, password string) (string, error) {
loginURL := baseURL + "/xce/v1/auth/logon"
// create login request json data
loginData := map[string]string{
@ -61,13 +62,20 @@ func login(username, password string) (string, error) {
}
// TestLoadJobs test function
// TestLoadJobs is a manual load test against a running API server; it is
// skipped unless XCE_LOAD_TEST_URL (e.g. http://localhost:8080) is set, with
// the credentials in XCE_LOAD_TEST_USER / XCE_LOAD_TEST_PASSWORD.
func TestLoadJobs(t *testing.T) {
baseURL := os.Getenv("XCE_LOAD_TEST_URL")
if baseURL == "" {
t.Skip("load test: set XCE_LOAD_TEST_URL (and XCE_LOAD_TEST_USER/XCE_LOAD_TEST_PASSWORD) to run it against a live API server")
}
const numRequests = 100
const username = "user01" // replace with your username
const password = "Welcome@1212" // replace with your password
username := os.Getenv("XCE_LOAD_TEST_USER")
password := os.Getenv("XCE_LOAD_TEST_PASSWORD")
fmt.Println("before login")
// login to get token
token, err := login(username, password)
token, err := login(baseURL, username, password)
if err != nil {
t.Fatalf("Failed to log in: %v", err)
}
@ -81,7 +89,7 @@ func TestLoadJobs(t *testing.T) {
for i := 0; i < numRequests; i++ {
go func() {
defer wg.Done()
req, err := http.NewRequest("GET", "http://localhost:8080/xce/v1/jobs", nil) // replace with your actual url
req, err := http.NewRequest("GET", baseURL+"/xce/v1/jobs", nil)
if err != nil {
t.Error("Error creating request:", err)
return