|
7 | 7 | "net/http" |
8 | 8 | "os" |
9 | 9 | "sort" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "sync" |
10 | 13 | "testing" |
11 | 14 | "time" |
12 | 15 |
|
|
20 | 23 | PY_API_URL string |
21 | 24 | GO_API_URL string |
22 | 25 | DEBUG bool |
| 26 | + MAX_PARALLEL int |
23 | 27 | PROJECT_UUID string |
24 | 28 | ProjectAPIPath = [3]string{"/v2/project/%s", "/v4/project/%s", "/v4/project-compat/%s"} |
25 | 29 | ProjectAPIKeyMapping = map[string]string{ |
@@ -88,6 +92,16 @@ func init() { |
88 | 92 | if dbg != "" { |
89 | 93 | DEBUG = true |
90 | 94 | } |
| 95 | + MAX_PARALLEL = 1 |
| 96 | + par := os.Getenv("MAX_PARALLEL") |
| 97 | + if par != "" { |
| 98 | + iPar, err := strconv.Atoi(par) |
| 99 | + if err != nil { |
| 100 | + fmt.Printf("MAX_PARALLEL environment value should be integer >= 1\n") |
| 101 | + } else if iPar > 0 { |
| 102 | + MAX_PARALLEL = iPar |
| 103 | + } |
| 104 | + } |
91 | 105 | PROJECT_UUID = os.Getenv("PROJECT_UUID") |
92 | 106 | } |
93 | 107 |
|
@@ -253,23 +267,7 @@ func compareNestedFields(t *testing.T, pyData, goData, keyMapping map[string]int |
253 | 267 | } |
254 | 268 | } |
255 | 269 |
|
256 | | -func TestProjectCompatAPI(t *testing.T) { |
257 | | - projectId := PROJECT_UUID |
258 | | - if projectId == "" { |
259 | | - projectId = uuid.New().String() |
260 | | - putTestItem("projects", "project_id", projectId, "S", map[string]interface{}{ |
261 | | - "project_name": "CNCF", |
262 | | - "project_icla_enabled": true, |
263 | | - "project_ccla_enabled": true, |
264 | | - "project_ccla_requires_icla_signature": true, |
265 | | - "date_created": "2022-11-21T10:31:31Z", |
266 | | - "date_modified": "2023-02-23T13:14:48Z", |
267 | | - "foundation_sfid": "a09410000182dD2AAI", |
268 | | - "version": "2", |
269 | | - }, DEBUG) |
270 | | - defer deleteTestItem("projects", "project_id", projectId, "S", DEBUG) |
271 | | - } |
272 | | - |
| 270 | +func runProjectCompatAPIForProject(t *testing.T, projectId string) { |
273 | 271 | apiURL := PY_API_URL + fmt.Sprintf(ProjectAPIPath[0], projectId) |
274 | 272 | Debugf("Py API call: %s\n", apiURL) |
275 | 273 | oldResp, err := http.Get(apiURL) |
@@ -324,6 +322,73 @@ func TestProjectCompatAPI(t *testing.T) { |
324 | 322 | } |
325 | 323 | } |
326 | 324 |
|
| 325 | +func TestProjectCompatAPI(t *testing.T) { |
| 326 | + projectId := PROJECT_UUID |
| 327 | + if projectId == "" { |
| 328 | + projectId = uuid.New().String() |
| 329 | + putTestItem("projects", "project_id", projectId, "S", map[string]interface{}{ |
| 330 | + "project_name": "CNCF", |
| 331 | + "project_icla_enabled": true, |
| 332 | + "project_ccla_enabled": true, |
| 333 | + "project_ccla_requires_icla_signature": true, |
| 334 | + "date_created": "2022-11-21T10:31:31Z", |
| 335 | + "date_modified": "2023-02-23T13:14:48Z", |
| 336 | + "foundation_sfid": "a09410000182dD2AAI", |
| 337 | + "version": "2", |
| 338 | + }, DEBUG) |
| 339 | + defer deleteTestItem("projects", "project_id", projectId, "S", DEBUG) |
| 340 | + } |
| 341 | + |
| 342 | + runProjectCompatAPIForProject(t, projectId) |
| 343 | +} |
| 344 | + |
| 345 | +func TestAllProjectsCompatAPI(t *testing.T) { |
| 346 | + allProjects := getAllPrimaryKeys("projects", "project_id", "S") |
| 347 | + |
| 348 | + var failedProjects []string |
| 349 | + var mtx sync.Mutex |
| 350 | + sem := make(chan struct{}, MAX_PARALLEL) |
| 351 | + var wg sync.WaitGroup |
| 352 | + |
| 353 | + for _, projectID := range allProjects { |
| 354 | + projID, ok := projectID.(string) |
| 355 | + if !ok { |
| 356 | + t.Errorf("Expected string project_id, got: %T", projectID) |
| 357 | + continue |
| 358 | + } |
| 359 | + |
| 360 | + wg.Add(1) |
| 361 | + sem <- struct{}{} |
| 362 | + |
| 363 | + go func(projID string) { |
| 364 | + defer wg.Done() |
| 365 | + defer func() { <-sem }() |
| 366 | + |
| 367 | + // Use t.Run in a thread-safe wrapper with a dummy parent test |
| 368 | + t.Run(fmt.Sprintf("ProjectId=%s", projID), func(t *testing.T) { |
| 369 | + runProjectCompatAPIForProject(t, projID) |
| 370 | + if t.Failed() { |
| 371 | + mtx.Lock() |
| 372 | + failedProjects = append(failedProjects, projID) |
| 373 | + mtx.Unlock() |
| 374 | + } |
| 375 | + }) |
| 376 | + }(projID) |
| 377 | + } |
| 378 | + |
| 379 | + wg.Wait() |
| 380 | + |
| 381 | + if len(failedProjects) > 0 { |
| 382 | + fmt.Fprintf(os.Stderr, "\nFailed Project IDs (%d):\n%s\n\n", |
| 383 | + len(failedProjects), |
| 384 | + strings.Join(failedProjects, "\n"), |
| 385 | + ) |
| 386 | + t.Fail() // Mark test as failed |
| 387 | + } else { |
| 388 | + fmt.Println("\nAll projects passed.") |
| 389 | + } |
| 390 | +} |
| 391 | + |
327 | 392 | func TestProjectAPI(t *testing.T) { |
328 | 393 | if TOKEN == "" || XACL == "" { |
329 | 394 | t.Fatalf("TOKEN and XACL environment variables must be set") |
|
0 commit comments