Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate regex before building image for e2e test #5783

Merged
merged 9 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 11 additions & 7 deletions .github/workflows/pr-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ jobs:
gh pr checkout ${{ needs.triage.outputs.pr_num }}
git checkout ${{ needs.triage.outputs.commit_sha }}

- name: Run regex checks
env:
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
MESSAGE="$COMMENT_BODY"
REGEX='/run-e2e (.+)'
if [[ "$MESSAGE" =~ $REGEX ]]
then
export E2E_TEST_REGEX="$(echo ${BASH_REMATCH[1]} | head -1)"
fi
Yaxhveer marked this conversation as resolved.
Show resolved Hide resolved

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
Expand Down Expand Up @@ -158,14 +169,7 @@ jobs:
ENABLE_OPENTELEMETRY : true
E2E_IMAGE_TAG: ${{ needs.triage.outputs.image_tag }}
TEST_CLUSTER_NAME: keda-e2e-cluster-pr
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
MESSAGE="$COMMENT_BODY"
REGEX='/run-e2e (.+)'
if [[ "$MESSAGE" =~ $REGEX ]]
then
export E2E_TEST_REGEX="$(echo ${BASH_REMATCH[1]} | head -1)"
fi
Yaxhveer marked this conversation as resolved.
Show resolved Hide resolved
echo "${{ needs.triage.outputs.pr_num }}"
make e2e-test

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Here is an overview of all new **experimental** features:

### Improvements

- **General**: Added Pre Regex check before building image in e2e test ([#5783](https://github.com/kedacore/keda/issues/5783))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this into Other section

- **GCP Scalers**: Added custom time horizon in GCP scalers ([#5778](https://github.com/kedacore/keda/issues/5778))

### Fixes
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ scale-node-pool: az-login ## Scale nodepool.
--resource-group $(TF_AZURE_RESOURCE_GROUP) \
--node-count $(NODE_POOL_SIZE)

.PHONY: e2e-regex-check
e2e-regex-check:
go run -tags e2e-regex ./tests/run-all.go regex-check

.PHONY: e2e-test
e2e-test: get-cluster-context ## Run e2e tests against Azure cluster.
TERMINFO=/etc/terminfo
Expand Down
81 changes: 7 additions & 74 deletions tests/run-all.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import (
"context"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"

"golang.org/x/sync/semaphore"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"

"github.com/kedacore/keda/v2/tests/helper"
"github.com/kedacore/keda/v2/tests/utils"
)

var (
Expand All @@ -41,9 +39,12 @@ type TestResult struct {
func main() {
ctx := context.Background()

e2eRegex := os.Getenv("E2E_TEST_REGEX")
if e2eRegex == "" {
e2eRegex = ".*_test.go"
//
// Get test files
//
regularTestFiles, sequentialTestFiles := utils.RegexTestFiles()
if len(os.Args) > 1 && os.Args[1] == "regex-check" {
return
}

//
Expand All @@ -57,17 +58,6 @@ func main() {
os.Exit(1)
}

//
// Detect test cases
//
regularTestFiles := getRegularTestFiles(e2eRegex)
sequentialTestFiles := getSequentialTestFiles(e2eRegex)
if len(regularTestFiles) == 0 && len(sequentialTestFiles) == 0 {
uninstallKeda(ctx)
fmt.Printf("No test has been executed, please review your regex: '%s'\n", e2eRegex)
os.Exit(1)
}

//
// Execute regular tests
//
Expand Down Expand Up @@ -123,63 +113,6 @@ func executeTest(ctx context.Context, file string, timeout string, tries int) Te
return result
}

func getRegularTestFiles(e2eRegex string) []string {
// We exclude utils and sequential folders and helper files
filter := func(path string, file string) bool {
return !strings.HasPrefix(path, "tests") || // we need this condition to skip non e2e test from execution
strings.Contains(path, "utils") ||
strings.Contains(path, "sequential") ||
!strings.HasSuffix(file, "_test.go")
}
return getTestFiles(e2eRegex, filter)
}

func getSequentialTestFiles(e2eRegex string) []string {
filter := func(path string, file string) bool {
return !strings.HasPrefix(path, "tests") || // we need this condition to skip non e2e test from execution
!strings.Contains(path, "sequential") ||
!strings.HasSuffix(file, "_test.go")
}
return getTestFiles(e2eRegex, filter)
}

func getTestFiles(e2eRegex string, filter func(path string, file string) bool) []string {
testFiles := []string{}
regex, err := regexp.Compile(e2eRegex)

if err != nil {
return testFiles
}

err = filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// We exclude utils and sequential folders and helper files
if filter(path, info.Name()) {
return nil
}
if regex.MatchString(path) {
testFiles = append(testFiles, path)
}

return nil
})

if err != nil {
return []string{}
}

// We randomize the executions
rand.New(rand.NewSource(time.Now().UnixNano()))
rand.Shuffle(len(testFiles), func(i, j int) {
testFiles[i], testFiles[j] = testFiles[j], testFiles[i]
})

return testFiles
}

func executeRegularTests(ctx context.Context, testCases []string) []TestResult {
sem := semaphore.NewWeighted(int64(concurrentTests))
mutex := &sync.RWMutex{}
Expand Down
85 changes: 85 additions & 0 deletions tests/utils/regex_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package utils

import (
"fmt"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)

func RegexTestFiles() ([]string, []string) {
e2eRegex := os.Getenv("E2E_TEST_REGEX")
if e2eRegex == "" {
e2eRegex = ".*_test.go"
}

// Detect test cases
regularTestFiles := getRegularTestFiles(e2eRegex)
sequentialTestFiles := getSequentialTestFiles(e2eRegex)
if len(regularTestFiles) == 0 && len(sequentialTestFiles) == 0 {
fmt.Printf("No test has been executed, please review your regex: '%s'\n", e2eRegex)
os.Exit(1)
}

return regularTestFiles, sequentialTestFiles
}

func getRegularTestFiles(e2eRegex string) []string {
// We exclude utils and sequential folders and helper files
filter := func(path string, file string) bool {
return !strings.HasPrefix(path, "tests") || // we need this condition to skip non e2e test from execution
strings.Contains(path, "utils") ||
strings.Contains(path, "sequential") ||
!strings.HasSuffix(file, "_test.go")
}
return getTestFiles(e2eRegex, filter)
}

func getSequentialTestFiles(e2eRegex string) []string {
filter := func(path string, file string) bool {
return !strings.HasPrefix(path, "tests") || // we need this condition to skip non e2e test from execution
!strings.Contains(path, "sequential") ||
!strings.HasSuffix(file, "_test.go")
}
return getTestFiles(e2eRegex, filter)
}

func getTestFiles(e2eRegex string, filter func(path string, file string) bool) []string {
testFiles := []string{}
regex, err := regexp.Compile(e2eRegex)

if err != nil {
return testFiles
}

err = filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// We exclude utils and sequential folders and helper files
if filter(path, info.Name()) {
return nil
}
if regex.MatchString(path) {
testFiles = append(testFiles, path)
}

return nil
})

if err != nil {
return []string{}
}

// We randomize the executions
rand.New(rand.NewSource(time.Now().UnixNano()))
rand.Shuffle(len(testFiles), func(i, j int) {
testFiles[i], testFiles[j] = testFiles[j], testFiles[i]
})

return testFiles
}
Loading