From 673638afe7eeb29c8b6399584cf2cd64a602232d Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 31 Jan 2023 18:47:06 +0100 Subject: [PATCH] Use ripgrep to find list of tests Signed-off-by: Kristoffer Dalby --- cmd/gh-action-integration-generator/main.go | 68 +++++++++++++-------- integration/README.md | 3 +- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/cmd/gh-action-integration-generator/main.go b/cmd/gh-action-integration-generator/main.go index 6231ec3..4cd767e 100644 --- a/cmd/gh-action-integration-generator/main.go +++ b/cmd/gh-action-integration-generator/main.go @@ -7,6 +7,8 @@ import ( "fmt" "log" "os" + "os/exec" + "strings" "text/template" ) @@ -70,43 +72,57 @@ jobs: with: name: logs path: "control_logs/*.log" -`)) +`), + ) ) const workflowFilePerm = 0o600 +func findTests() []string { + rgBin, err := exec.LookPath("rg") + if err != nil { + log.Fatalf("failed to find rg (ripgrep) binary") + } + + args := []string{ + "--regexp", "func (Test.+)\\(.*", + "../../integration/", + "--replace", "$1", + "--sort", "path", + "--no-line-number", + "--no-filename", + "--no-heading", + } + + log.Printf("executing: %s %s", rgBin, strings.Join(args, " ")) + + ripgrep := exec.Command( + rgBin, + args..., + ) + + result, err := ripgrep.CombinedOutput() + if err != nil { + log.Printf("out: %s", result) + log.Fatalf("failed to run ripgrep: %s", err) + } + + tests := strings.Split(string(result), "\n") + tests = tests[:len(tests)-1] + + return tests +} + func main() { type testConfig struct { Name string } - // TODO(kradalby): automatic fetch tests at runtime - tests := []string{ - "TestAuthKeyLogoutAndRelogin", - "TestAuthWebFlowAuthenticationPingAll", - "TestAuthWebFlowLogoutAndRelogin", - "TestCreateTailscale", - "TestEnablingRoutes", - "TestHeadscale", - "TestUserCommand", - "TestOIDCAuthenticationPingAll", - "TestOIDCExpireNodesBasedOnTokenExpiry", - "TestPingAllByHostname", - "TestPingAllByIP", - "TestPreAuthKeyCommand", - "TestPreAuthKeyCommandReusableEphemeral", - "TestPreAuthKeyCommandWithoutExpiry", - "TestResolveMagicDNS", - "TestSSHIsBlockedInACL", - "TestSSHMultipleUsersAllToAll", - "TestSSHNoSSHConfigured", - "TestSSHOneUserAllToAll", - "TestSSUserOnlyIsolation", - "TestTaildrop", - "TestTailscaleNodesJoiningHeadcale", - } + tests := findTests() for _, test := range tests { + log.Printf("generating workflow for %s", test) + var content bytes.Buffer if err := jobTemplate.Execute(&content, testConfig{ diff --git a/integration/README.md b/integration/README.md index e5e6b9f..664801f 100644 --- a/integration/README.md +++ b/integration/README.md @@ -11,6 +11,5 @@ Tests are located in files ending with `_test.go` and the framework are located ## Running integration tests on GitHub Actions -Each test currently runs as a separate workflows in GitHub actions, to add new test, add -the new test to the list in `../cmd/gh-action-integration-generator/main.go` and run +Each test currently runs as a separate workflows in GitHub actions, to add new test, run `go generate` inside `../cmd/gh-action-integration-generator/` and commit the result.