Skip to content

Commit

Permalink
Merge pull request #2024 from bertinatto/test-imported-packages
Browse files Browse the repository at this point in the history
NO-JIRA: add tool to validate test packages imported
  • Loading branch information
openshift-merge-bot[bot] committed Jul 18, 2024
2 parents d96eb3e + a1eaa95 commit 1bead22
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
74 changes: 74 additions & 0 deletions openshift-hack/cmd/go-imports-diff/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"flag"
"fmt"
"go/parser"
"go/token"
"os"
"sort"
"strings"

"k8s.io/apimachinery/pkg/util/sets"
)

const testPackagePrefix = "k8s.io/kubernetes/test/e2e"

func main() {
// Parse flags
excludeList := flag.String("exclude", "", "Comma-separated list of imports to be ignored")
flag.Parse()

// Parse positional arguments
args := flag.Args()
if len(args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <baseFile> <compareFile>\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
baseFile := args[0]
compareFile := args[1]

// Parse the base file
baseNode, err := parser.ParseFile(token.NewFileSet(), baseFile, nil, parser.AllErrors)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse go file %s: %v\n", baseFile, err)
os.Exit(1)
}

// Create a slice containing imports of base file
baseImports := sets.New[string]()
for _, imp := range baseNode.Imports {
v := strings.Trim(imp.Path.Value, `"`)
if !strings.Contains(v, testPackagePrefix) {
continue
}
baseImports.Insert(v)
}

// Parse file that is compared with the base one
compareNode, err := parser.ParseFile(token.NewFileSet(), compareFile, nil, parser.AllErrors)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse go file %s: %v\n", baseFile, err)
os.Exit(1)
}

// Create a slice containing imports of compare file
compareImports := sets.New[string]()
for _, imp := range compareNode.Imports {
v := strings.Trim(imp.Path.Value, `"`)
if !strings.Contains(v, testPackagePrefix) {
continue
}
compareImports.Insert(v)
}

// Compare imports of both files
exclude := strings.Split(*excludeList, ",")
diff := baseImports.Difference(compareImports).Delete(exclude...).UnsortedList()
if len(diff) > 0 {
sort.Strings(diff)
fmt.Fprintf(os.Stderr, "Imports from %q not in %q:\n\n%s\n", baseFile, compareFile, strings.Join(diff, "\n"))
os.Exit(1)
}
}
16 changes: 16 additions & 0 deletions openshift-hack/verify-test-annotations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"

# Make sure that all packages that define k8s tests are properly imported
EXCLUDE_PACKAGES="\
k8s.io/kubernetes/test/e2e/framework,\
k8s.io/kubernetes/test/e2e/framework/debug/init,\
k8s.io/kubernetes/test/e2e/framework/metrics/init,\
k8s.io/kubernetes/test/e2e/framework/node/init,\
k8s.io/kubernetes/test/e2e/framework/testfiles,\
k8s.io/kubernetes/test/e2e/storage/external,\
k8s.io/kubernetes/test/e2e/testing-manifests,\
k8s.io/kubernetes/test/e2e/windows"

GO111MODULE=on go run ./openshift-hack/cmd/go-imports-diff \
-exclude "$EXCLUDE_PACKAGES" \
test/e2e/e2e_test.go \
openshift-hack/e2e/include.go

# Verify e2e test annotations that indicate openshift compatibility
"${KUBE_ROOT}"/hack/update-test-annotations.sh
git diff --quiet "${KUBE_ROOT}/openshift-hack/e2e/annotate/generated/"

0 comments on commit 1bead22

Please sign in to comment.