Skip to content

Commit

Permalink
UPSTREAM: <carry>: update to ginkgo v2 - squash to tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
soltysh committed Aug 23, 2022
1 parent 3a1ed96 commit 09bc939
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 117 deletions.
6 changes: 4 additions & 2 deletions openshift-hack/conformance-k8s.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ unschedulable="$( ( oc get nodes -o name -l 'node-role.kubernetes.io/master'; )
# Execute Kubernetes prerequisites
make WHAT=cmd/kubectl
make WHAT=test/e2e/e2e.test
make WHAT=vendor/github.com/onsi/ginkgo/ginkgo
make WHAT=vendor/github.com/onsi/ginkgo/v2/ginkgo
PATH="${OS_ROOT}/_output/local/bin/$( os::build::host_platform ):${PATH}"
export PATH

Expand Down Expand Up @@ -89,7 +89,9 @@ TEST_SKIPS="\[Serial\]| session affinity timeout "

# shellcheck disable=SC2086
ginkgo \
-nodes 4 -noColor "-skip=${TEST_SKIPS}" '-focus=\[Conformance\]' \
--timeout="24h" \
--output-interceptor-mode=none \
-nodes 4 -no-color '-skip=\[Serial\]' '-focus=\[Conformance\]' \
${e2e_test} -- \
-report-dir "${test_report_dir}" \
-allowed-not-ready-nodes ${unschedulable} \
Expand Down
112 changes: 49 additions & 63 deletions openshift-hack/e2e/annotate/annotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"sort"
"strings"

"github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/types"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
)

var reHasSig = regexp.MustCompile(`\[sig-[\w-]+\]`)
Expand All @@ -24,10 +24,13 @@ func Run() {
filename := os.Args[len(os.Args)-1]

generator := newGenerator()
ginkgo.WalkTests(generator.generateRename)
ginkgo.GetSuite().BuildTree()
ginkgo.GetSuite().WalkTests(generator.generateRename)

renamer := newRenamerFromGenerated(generator.output)
ginkgo.WalkTests(renamer.updateNodeText)
// generated file has a map[string]string in the following format:
// original k8s name: k8s name with our labels at the end
ginkgo.GetSuite().WalkTests(renamer.updateNodeText)
if len(renamer.missing) > 0 {
var names []string
for name := range renamer.missing {
Expand Down Expand Up @@ -70,24 +73,21 @@ package generated
import (
"fmt"
"github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/types"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/ginkgo/v2/types"
)
var annotations = map[string]string{
%s
}
func init() {
ginkgo.WalkTests(func(name, parentName string, node types.TestNode) {
combined := name
if len(parentName) > 0 {
combined = parentName + " " + combined
}
if updated, ok := annotations[combined]; ok {
node.SetText(updated)
ginkgo.GetSuite().BuildTree()
ginkgo.GetSuite().WalkTests(func(name string, node types.TestSpec) {
if newLabels, ok := annotations[name]; ok {
node.AppendText(newLabels)
} else {
panic(fmt.Sprintf("unable to find test %%s", combined))
panic(fmt.Sprintf("unable to find test %%s", name))
}
})
}
Expand All @@ -106,7 +106,6 @@ func newGenerator() *ginkgoTestRenamer {
var allLabels []string
matches := make(map[string]*regexp.Regexp)
stringMatches := make(map[string][]string)
excludes := make(map[string]*regexp.Regexp)

for label, items := range TestMaps {
sort.Strings(items)
Expand All @@ -124,10 +123,6 @@ func newGenerator() *ginkgoTestRenamer {
matches[label] = regexp.MustCompile(strings.Join(remain, `|`))
}
}
for label, items := range LabelExcludes {
sort.Strings(items)
excludes[label] = regexp.MustCompile(strings.Join(items, `|`))
}
sort.Strings(allLabels)

excludedTestsFilter := regexp.MustCompile(strings.Join(ExcludedTests, `|`))
Expand All @@ -136,7 +131,6 @@ func newGenerator() *ginkgoTestRenamer {
allLabels: allLabels,
stringMatches: stringMatches,
matches: matches,
excludes: excludes,
excludedTestsFilter: excludedTestsFilter,

output: make(map[string]string),
Expand All @@ -151,88 +145,88 @@ func newRenamerFromGenerated(names map[string]string) *ginkgoTestRenamer {
}

type ginkgoTestRenamer struct {
allLabels []string
stringMatches map[string][]string
matches map[string]*regexp.Regexp
excludes map[string]*regexp.Regexp
// keys defined in TestMaps in openshift-hack/e2e/annotate/rules.go
allLabels []string
// exact substrings to match to apply a particular label
stringMatches map[string][]string
// regular expressions to match to apply a particular label
matches map[string]*regexp.Regexp
// regular expression excluding permanently a set of tests
// see ExcludedTests in openshift-hack/e2e/annotate/rules.go
excludedTestsFilter *regexp.Regexp

output map[string]string
// output from the generateRename and also input for updateNodeText
output map[string]string
// map of unmatched test names
missing map[string]struct{}
}

func (r *ginkgoTestRenamer) updateNodeText(name, parentName string, node types.TestNode) {
if updated, ok := r.output[combineNames(parentName, name)]; ok {
node.SetText(updated)
func (r *ginkgoTestRenamer) updateNodeText(name string, node types.TestSpec) {
if newLables, ok := r.output[name]; ok {
node.AppendText(newLables)
} else {
r.missing[combineNames(parentName, name)] = struct{}{}
r.missing[name] = struct{}{}
}
}

func (r *ginkgoTestRenamer) generateRename(name, parentName string, node types.TestNode) {
originalName := name
combinedName := combineNames(parentName, name)

labels := ""
func (r *ginkgoTestRenamer) generateRename(name string, node types.TestSpec) {
newLabels := ""
newName := name
for {
count := 0
for _, label := range r.allLabels {
// never apply a sig label twice
if strings.HasPrefix(label, "[sig-") && strings.Contains(combinedName, "[sig-") {
if strings.HasPrefix(label, "[sig-") && strings.Contains(newName, "[sig-") {
continue
}
if strings.Contains(combinedName, label) {
if strings.Contains(newName, label) {
continue
}

var hasLabel bool
for _, segment := range r.stringMatches[label] {
hasLabel = strings.Contains(combinedName, segment)
hasLabel = strings.Contains(newName, segment)
if hasLabel {
break
}
}
if !hasLabel {
if re := r.matches[label]; re != nil {
hasLabel = r.matches[label].MatchString(combinedName)
hasLabel = r.matches[label].MatchString(newName)
}
}

if hasLabel {
// TODO: remove when we no longer need it
if re, ok := r.excludes[label]; ok && re.MatchString(combinedName) {
continue
}
count++
labels += " " + label
combinedName += " " + label
name += " " + label
newLabels += " " + label
newName += " " + label
}
}
if count == 0 {
break
}
}

if !r.excludedTestsFilter.MatchString(combinedName) {
isSerial := strings.Contains(combinedName, "[Serial]")
isConformance := strings.Contains(combinedName, "[Conformance]")
if !r.excludedTestsFilter.MatchString(newName) {
isSerial := strings.Contains(newName, "[Serial]")
isConformance := strings.Contains(newName, "[Conformance]")
switch {
case isSerial && isConformance:
name += " [Suite:openshift/conformance/serial/minimal]"
newLabels += " [Suite:openshift/conformance/serial/minimal]"
case isSerial:
name += " [Suite:openshift/conformance/serial]"
newLabels += " [Suite:openshift/conformance/serial]"
case isConformance:
name += " [Suite:openshift/conformance/parallel/minimal]"
newLabels += " [Suite:openshift/conformance/parallel/minimal]"
default:
name += " [Suite:openshift/conformance/parallel]"
newLabels += " [Suite:openshift/conformance/parallel]"
}
}
if isGoModulePath(node.CodeLocation().FileName, "k8s.io/kubernetes", "test/e2e") {
name += " [Suite:k8s]"
codeLocations := node.CodeLocations()
if isGoModulePath(codeLocations[len(codeLocations)-1].FileName, "k8s.io/kubernetes", "test/e2e") {
newLabels += " [Suite:k8s]"
}

r.output[combineNames(parentName, originalName)] = name
r.output[name] = newLabels
}

// isGoModulePath returns true if the packagePath reported by reflection is within a
Expand All @@ -242,14 +236,6 @@ func (r *ginkgoTestRenamer) generateRename(name, parentName string, node types.T
//
// historically: ".../vendor/k8s.io/kubernetes/test/e2e"
// go.mod: "k8s.io/kubernetes@0.18.4/test/e2e"
//
func isGoModulePath(packagePath, module, modulePath string) bool {
return regexp.MustCompile(fmt.Sprintf(`\b%s(@[^/]*|)/%s\b`, regexp.QuoteMeta(module), regexp.QuoteMeta(modulePath))).MatchString(packagePath)
}

func combineNames(parentName, name string) string {
if len(parentName) == 0 {
return name
}
return parentName + " " + name
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions openshift-hack/e2e/annotate/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,6 @@ var (
},
}

// labelExcludes temporarily block tests out of a specific suite
LabelExcludes = map[string][]string{}

ExcludedTests = []string{
`\[Disabled:`,
`\[Disruptive\]`,
Expand Down
78 changes: 36 additions & 42 deletions openshift-hack/e2e/annotate/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,63 @@ package annotate
import (
"testing"

"github.com/onsi/ginkgo/types"
"github.com/onsi/ginkgo/v2/types"
)

type testNode struct {
text string
}

func (n *testNode) Type() types.SpecComponentType {
return 0
}
func (n *testNode) CodeLocation() types.CodeLocation {
return types.CodeLocation{}
func (n *testNode) CodeLocations() []types.CodeLocation {
return []types.CodeLocation{{FileName: "k8s.io/kubernetes"}}
}

func (n *testNode) Text() string {
return n.text
}
func (n *testNode) SetText(text string) {
n.text = text
}
func (n *testNode) Flag() types.FlagType {
return 0
}
func (n *testNode) SetFlag(flag types.FlagType) {

func (n *testNode) AppendText(text string) {
n.text += text
}

func TestStockRules(t *testing.T) {
tests := []struct {
name string

testName string
parentName string
testName string

expectedText string
expectedLabel string
expectedText string
}{
{
name: "simple serial match",
parentName: "",
testName: "[Serial] test",
expectedText: "[Serial] test [Suite:openshift/conformance/serial]",
name: "simple serial match",
testName: "[Serial] test",
expectedLabel: " [Suite:openshift/conformance/serial]",
expectedText: "[Serial] test [Suite:openshift/conformance/serial]",
},
{
name: "don't tag skipped",
parentName: "",
testName: `[Serial] example test [Skipped:gce]`,
expectedText: `[Serial] example test [Skipped:gce] [Suite:openshift/conformance/serial]`, // notice that this isn't categorized into any of our buckets
name: "don't tag skipped",
testName: `[Serial] example test [Skipped:gce]`,
expectedLabel: ` [Suite:openshift/conformance/serial]`,
expectedText: `[Serial] example test [Skipped:gce] [Suite:openshift/conformance/serial]`, // notice that this isn't categorized into any of our buckets
},
{
name: "not skipped",
parentName: "",
testName: `[sig-network] Networking Granular Checks: Pods should function for intra-pod communication: http [LinuxOnly] [NodeConformance] [Conformance]`,
expectedText: `[sig-network] Networking Granular Checks: Pods should function for intra-pod communication: http [LinuxOnly] [NodeConformance] [Conformance] [Suite:openshift/conformance/parallel/minimal]`,
name: "not skipped",
testName: `[sig-network] Networking Granular Checks: Pods should function for intra-pod communication: http [LinuxOnly] [NodeConformance] [Conformance]`,
expectedLabel: ` [Suite:openshift/conformance/parallel/minimal]`,
expectedText: `[sig-network] Networking Granular Checks: Pods should function for intra-pod communication: http [LinuxOnly] [NodeConformance] [Conformance] [Suite:openshift/conformance/parallel/minimal]`,
},
{
name: "should skip localssd on gce",
parentName: "",
testName: `[sig-storage] In-tree Volumes [Driver: local][LocalVolumeType: gce-localssd-scsi-fs] [Serial] [Testpattern: Dynamic PV (default fs)] subPath should be able to unmount after the subpath directory is deleted`,
expectedText: `[sig-storage] In-tree Volumes [Driver: local][LocalVolumeType: gce-localssd-scsi-fs] [Serial] [Testpattern: Dynamic PV (default fs)] subPath should be able to unmount after the subpath directory is deleted [Skipped:gce] [Suite:openshift/conformance/serial]`, // notice that this isn't categorized into any of our buckets
name: "should skip localssd on gce",
testName: `[sig-storage] In-tree Volumes [Driver: local][LocalVolumeType: gce-localssd-scsi-fs] [Serial] [Testpattern: Dynamic PV (default fs)] subPath should be able to unmount after the subpath directory is deleted`,
expectedLabel: ` [Skipped:gce] [Suite:openshift/conformance/serial]`,
expectedText: `[sig-storage] In-tree Volumes [Driver: local][LocalVolumeType: gce-localssd-scsi-fs] [Serial] [Testpattern: Dynamic PV (default fs)] subPath should be able to unmount after the subpath directory is deleted [Skipped:gce] [Suite:openshift/conformance/serial]`, // notice that this isn't categorized into any of our buckets
},
{
name: "should skip NetworkPolicy tests on multitenant",
parentName: "[Feature:NetworkPolicy]",
testName: `should do something with NetworkPolicy`,
expectedText: `should do something with NetworkPolicy [Skipped:Network/OpenShiftSDN/Multitenant] [Suite:openshift/conformance/parallel]`,
name: "should skip NetworkPolicy tests on multitenant",
testName: `should do something with NetworkPolicy`,
expectedLabel: ` [Suite:openshift/conformance/parallel]`,
expectedText: `should do something with NetworkPolicy [Suite:openshift/conformance/parallel]`,
},
}

Expand All @@ -76,19 +70,19 @@ func TestStockRules(t *testing.T) {
text: test.testName,
}

testRenamer.generateRename(test.testName, test.parentName, testNode)
changed := testRenamer.output[combineNames(test.parentName, test.testName)]
testRenamer.generateRename(test.testName, testNode)
changed := testRenamer.output[test.testName]

if e, a := test.expectedText, changed; e != a {
if e, a := test.expectedLabel, changed; e != a {
t.Error(a)
}
testRenamer = newRenamerFromGenerated(map[string]string{combineNames(test.parentName, test.testName): test.expectedText})
testRenamer.updateNodeText(test.testName, test.parentName, testNode)
testRenamer = newRenamerFromGenerated(map[string]string{test.testName: test.expectedLabel})
testRenamer.updateNodeText(test.testName, testNode)

if e, a := test.expectedText, testNode.Text(); e != a {
t.Logf(e)
t.Error(a)
}

})
}
}

0 comments on commit 09bc939

Please sign in to comment.