diff --git a/tools/citool/cmd/filter_cmd.go b/tools/citool/cmd/filter_cmd.go index 36f4558aa..e69bc1dd6 100644 --- a/tools/citool/cmd/filter_cmd.go +++ b/tools/citool/cmd/filter_cmd.go @@ -7,7 +7,9 @@ import ( "log" "os" "regexp" + "strconv" "strings" + "time" "github.com/spf13/cobra" "gopkg.in/yaml.v2" @@ -30,6 +32,7 @@ func filterTests(allTests []CITestConf, workflow, testType, ids string, envresol if workflowMatch && typeMatch && idMatch { test.IDSanitized = sanitizeTestID(test.ID) + test.RunsOnSelfHosted = processRunsOnSelfHosted(test.RunsOnSelfHosted) filteredTests = append(filteredTests, test) } if envresolve { @@ -91,6 +94,20 @@ func sanitizeTestID(id string) string { return re.ReplaceAllString(id, "_") } +func processRunsOnSelfHosted(label string) string { + if label == "" { + return "" + } + + runId := os.Getenv("GITHUB_RUN_ID") + if runId == "" { + runId = strconv.FormatInt(time.Now().UnixMilli(), 10) + } + + newIdentifier := "runs-on=" + runId + "/" + return strings.Replace(label, "runs-on/", newIdentifier, 1) +} + // Utility function to check if a slice contains a string. func contains(slice []string, element string) bool { for _, s := range slice { diff --git a/tools/citool/cmd/filter_cmd_test.go b/tools/citool/cmd/filter_cmd_test.go index 81c9c6307..39b0ddba3 100644 --- a/tools/citool/cmd/filter_cmd_test.go +++ b/tools/citool/cmd/filter_cmd_test.go @@ -1,6 +1,7 @@ package cmd import ( + "strings" "testing" ) @@ -62,3 +63,40 @@ func TestFilterTestsIntegration(t *testing.T) { }) } } + +func TestRunsOnSelfHostedID(t *testing.T) { + tests := []CITestConf{ + {ID: "foo", TestEnvType: "typeA", RunsOnSelfHosted: "runs-on/cpu=0/ram=0"}, + {ID: "bar", TestEnvType: "typeA", RunsOnSelfHosted: "runs-on/cpu=1/ram=1"}, + {ID: "baz", TestEnvType: "typeB", RunsOnSelfHosted: "runs-on=foo/cpu=2/ram=2"}, + {ID: "qux", TestEnvType: "typeC"}, + } + + cases := []struct { + description string + inputTestType string + expectedSelfHosted bool + }{ + {"Insert epoch time", "typeA", true}, + {"Dont insert epoch time", "typeB", true}, + {"No self-hosted label", "typeC", false}, + } + + for _, c := range cases { + t.Run(c.description, func(t *testing.T) { + filtered := filterTests(tests, "", c.inputTestType, "", false) + for _, test := range filtered { + t.Logf("Test ID: %s, RunsOnSelfHosted: %s", test.ID, test.RunsOnSelfHosted) + if c.expectedSelfHosted { + if !strings.HasPrefix(test.RunsOnSelfHosted, "runs-on=") { + t.Errorf("Expected RunsOnSelfHosted to start with 'runs-on='. Got: %s", test.RunsOnSelfHosted) + } + } else { + if test.RunsOnSelfHosted != "" { + t.Errorf("Expected RunsOnSelfHosted to be empty. Got: %s", test.RunsOnSelfHosted) + } + } + } + }) + } +}