Read the e2e Go version from go.mod and apply the safe go fix modernizers (#4677)

This commit is contained in:
Junya Okabe
2026-10-02 14:23:15 +02:00
committed by GitHub
parent 1945b42820
commit 5aed393af8
12 changed files with 50 additions and 32 deletions
@@ -66,7 +66,7 @@ func TestControllerMetricsAddress(t *testing.T) {
func indentMetricsValues(values string) string {
result := ""
for _, line := range strings.Split(strings.TrimSuffix(values, "\n"), "\n") {
for line := range strings.SplitSeq(strings.TrimSuffix(values, "\n"), "\n") {
result += " " + line + "\n"
}
return result
@@ -2,6 +2,7 @@ package tests
import (
"fmt"
"maps"
"path/filepath"
"strings"
"testing"
@@ -210,12 +211,8 @@ func TestTemplateListenerScalerValidation(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setValues := make(map[string]string, len(baseValues)+len(tt.setValues))
for key, value := range baseValues {
setValues[key] = value
}
for key, value := range tt.setValues {
setValues[key] = value
}
maps.Copy(setValues, baseValues)
maps.Copy(setValues, tt.setValues)
options := &helm.Options{
Logger: logger.Discard,
@@ -276,12 +273,8 @@ func TestTemplateListenerScalerConfig(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setValues := make(map[string]string, len(baseValues)+len(tt.setValues))
for key, value := range baseValues {
setValues[key] = value
}
for key, value := range tt.setValues {
setValues[key] = value
}
maps.Copy(setValues, baseValues)
maps.Copy(setValues, tt.setValues)
options := &helm.Options{
Logger: logger.Discard,
@@ -1454,7 +1454,7 @@ var _ = Describe("Test EphemeralRunnerSet controller", func() {
).Should(BeEquivalentTo(3), "3 EphemeralRunner should be created")
idleRunnerNames := map[string]struct{}{}
for i := 0; i < 2; i++ {
for i := range 2 {
idleRunner := runnerList.Items[i].DeepCopy()
idleRunner.Status.Phase = v1alpha1.EphemeralRunnerPhaseRunning
idleRunner.Status.RunnerID = i + 101
@@ -470,8 +470,8 @@ func TestPredicateProjectionsCoverEveryStatusField(t *testing.T) {
fieldNames := func(v any) []string {
typ := reflect.TypeOf(v)
names := make([]string, 0, typ.NumField())
for i := 0; i < typ.NumField(); i++ {
names = append(names, typ.Field(i).Name)
for field := range typ.Fields() {
names = append(names, field.Name)
}
sort.Strings(names)
return names
+1 -1
View File
@@ -28,6 +28,7 @@ require (
github.com/teambition/rrule-go v1.8.2
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.28.0
golang.org/x/mod v0.41.0
golang.org/x/net v0.59.0
golang.org/x/oauth2 v0.37.0
golang.org/x/sync v0.23.0
@@ -211,7 +212,6 @@ require (
go.yaml.in/yaml/v4 v4.0.0-rc.6 // indirect
golang.org/x/crypto v0.57.0 // indirect
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect
golang.org/x/mod v0.41.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/term v0.46.0 // indirect
golang.org/x/text v0.42.0 // indirect
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"k8s.io/apimachinery/pkg/util/rand"
)
func FNVHashStringObjects(objs ...interface{}) string {
func FNVHashStringObjects(objs ...any) string {
hash := fnv.New32a()
for _, obj := range objs {
+2 -2
View File
@@ -16,7 +16,7 @@ import (
// DeepHashObject writes specified object to hash using the spew library
// which follows pointers and prints actual values of the nested objects
// ensuring the hash does not change when a pointer changes.
func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
func DeepHashObject(hasher hash.Hash, objectToWrite any) {
hasher.Reset()
printer := spew.ConfigState{
Indent: " ",
@@ -34,7 +34,7 @@ func DeepHashObject(hasher hash.Hash, objectToWrite interface{}) {
//
// Proudly modified and adopted from k8s.io/kubernetes/pkg/util/hash.DeepHashObject and
// k8s.io/kubernetes/pkg/controller.ComputeHash.
func ComputeTemplateHash(template interface{}) string {
func ComputeTemplateHash(template any) string {
hasher := fnv.New32a()
hasher.Reset()
+2 -6
View File
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"slices"
"strconv"
"time"
@@ -78,10 +79,5 @@ func NewLogger(logLevel string, logFormat string) (logr.Logger, error) {
func validLogFormat(logFormat string) bool {
validFormat := []string{"text", "json"}
for _, v := range validFormat {
if v == logFormat {
return true
}
}
return false
return slices.Contains(validFormat, logFormat)
}
+1 -1
View File
@@ -35,7 +35,7 @@ func (t Transport) log(req *http.Request, resp *http.Response) {
return
}
var args []interface{}
var args []any
marked := resp.Header.Get(httpcache.XFromCache) == "1"
+32 -3
View File
@@ -15,6 +15,7 @@ import (
"github.com/google/go-github/v52/github"
"github.com/onsi/gomega"
"github.com/stretchr/testify/require"
"golang.org/x/mod/modfile"
"golang.org/x/oauth2"
"sigs.k8s.io/yaml"
)
@@ -1018,7 +1019,7 @@ type job struct {
func createTestJobs(id, testResultCMNamePrefix string, numJobs int) []job {
var testJobs []job
for i := 0; i < numJobs; i++ {
for i := range numJobs {
name := fmt.Sprintf("test%d", i)
testArg := fmt.Sprintf("%s%d", id, i)
configMapName := testResultCMNamePrefix + testArg
@@ -1050,10 +1051,11 @@ func installActionsWorkflow(t *testing.T, testName, runnerLabel, testResultCMNam
}
kubernetesContainerMode := os.Getenv("TEST_CONTAINER_MODE") == "kubernetes"
goVersion := goVersionFromGoMod(t)
var container string
if kubernetesContainerMode {
container = "golang:1.27.1"
container = "golang:" + goVersion
}
for _, j := range testJobs {
@@ -1118,7 +1120,7 @@ func installActionsWorkflow(t *testing.T, testName, runnerLabel, testResultCMNam
testing.Step{
Uses: "actions/setup-go@v3",
With: &testing.With{
GoVersion: "1.27.1",
GoVersion: goVersion,
},
},
)
@@ -1300,6 +1302,33 @@ kubectl create cm %s$id --from-literal=status=ok
}
}
// goVersionFromGoMod keeps the runner-side Go toolchain in sync with go.mod so
// the version is pinned in exactly one place. Like setup-go's go-version-file,
// the toolchain directive wins over the go directive when both are present.
func goVersionFromGoMod(t *testing.T) string {
t.Helper()
data, err := os.ReadFile(filepath.Join("..", "..", "go.mod"))
if err != nil {
t.Fatal(err)
}
f, err := modfile.ParseLax("go.mod", data, nil)
if err != nil {
t.Fatal(err)
}
if f.Toolchain != nil {
return strings.TrimPrefix(f.Toolchain.Name, "go")
}
if f.Go == nil {
t.Fatal("go.mod has no go directive")
}
return f.Go.Version
}
func verifyActionsWorkflowRun(t *testing.T, ctx context.Context, env *testing.Env, testJobs []job, timeout time.Duration, cmCfg testing.KubectlConfig) {
t.Helper()
+1 -1
View File
@@ -122,7 +122,7 @@ func (k *Kubectl) FindPods(ctx context.Context, label string, cfg KubectlConfig)
}
var pods []string
for _, l := range strings.Split(out, "\n") {
for l := range strings.SplitSeq(out, "\n") {
if l != "" {
pods = append(pods, l)
}
+1 -1
View File
@@ -26,6 +26,6 @@ func (k Cmdr) CombinedOutput(cmd *exec.Cmd) (string, error) {
return string(o), nil
}
func (k Cmdr) Errorf(f string, args ...interface{}) {
func (k Cmdr) Errorf(f string, args ...any) {
fmt.Fprintf(os.Stderr, f+"\n", args...)
}