Skip to content

Commit d12aaf9

Browse files
authored
feat: add id for self-hosted runner label (#1911)
1 parent 8122475 commit d12aaf9

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

tools/citool/cmd/filter_cmd.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"log"
88
"os"
99
"regexp"
10+
"strconv"
1011
"strings"
12+
"time"
1113

1214
"github.com/spf13/cobra"
1315
"gopkg.in/yaml.v2"
@@ -30,6 +32,7 @@ func filterTests(allTests []CITestConf, workflow, testType, ids string, envresol
3032

3133
if workflowMatch && typeMatch && idMatch {
3234
test.IDSanitized = sanitizeTestID(test.ID)
35+
test.RunsOnSelfHosted = processRunsOnSelfHosted(test.RunsOnSelfHosted)
3336
filteredTests = append(filteredTests, test)
3437
}
3538
if envresolve {
@@ -91,6 +94,20 @@ func sanitizeTestID(id string) string {
9194
return re.ReplaceAllString(id, "_")
9295
}
9396

97+
func processRunsOnSelfHosted(label string) string {
98+
if label == "" {
99+
return ""
100+
}
101+
102+
runId := os.Getenv("GITHUB_RUN_ID")
103+
if runId == "" {
104+
runId = strconv.FormatInt(time.Now().UnixMilli(), 10)
105+
}
106+
107+
newIdentifier := "runs-on=" + runId + "/"
108+
return strings.Replace(label, "runs-on/", newIdentifier, 1)
109+
}
110+
94111
// Utility function to check if a slice contains a string.
95112
func contains(slice []string, element string) bool {
96113
for _, s := range slice {

tools/citool/cmd/filter_cmd_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"strings"
45
"testing"
56
)
67

@@ -62,3 +63,40 @@ func TestFilterTestsIntegration(t *testing.T) {
6263
})
6364
}
6465
}
66+
67+
func TestRunsOnSelfHostedID(t *testing.T) {
68+
tests := []CITestConf{
69+
{ID: "foo", TestEnvType: "typeA", RunsOnSelfHosted: "runs-on/cpu=0/ram=0"},
70+
{ID: "bar", TestEnvType: "typeA", RunsOnSelfHosted: "runs-on/cpu=1/ram=1"},
71+
{ID: "baz", TestEnvType: "typeB", RunsOnSelfHosted: "runs-on=foo/cpu=2/ram=2"},
72+
{ID: "qux", TestEnvType: "typeC"},
73+
}
74+
75+
cases := []struct {
76+
description string
77+
inputTestType string
78+
expectedSelfHosted bool
79+
}{
80+
{"Insert epoch time", "typeA", true},
81+
{"Dont insert epoch time", "typeB", true},
82+
{"No self-hosted label", "typeC", false},
83+
}
84+
85+
for _, c := range cases {
86+
t.Run(c.description, func(t *testing.T) {
87+
filtered := filterTests(tests, "", c.inputTestType, "", false)
88+
for _, test := range filtered {
89+
t.Logf("Test ID: %s, RunsOnSelfHosted: %s", test.ID, test.RunsOnSelfHosted)
90+
if c.expectedSelfHosted {
91+
if !strings.HasPrefix(test.RunsOnSelfHosted, "runs-on=") {
92+
t.Errorf("Expected RunsOnSelfHosted to start with 'runs-on='. Got: %s", test.RunsOnSelfHosted)
93+
}
94+
} else {
95+
if test.RunsOnSelfHosted != "" {
96+
t.Errorf("Expected RunsOnSelfHosted to be empty. Got: %s", test.RunsOnSelfHosted)
97+
}
98+
}
99+
}
100+
})
101+
}
102+
}

0 commit comments

Comments
 (0)