Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tools/citool/cmd/filter_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"log"
"os"
"regexp"
"strconv"
"strings"
"time"

"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions tools/citool/cmd/filter_cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -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)
}
}
}
})
}
}
Loading