Skip to content

Instantly share code, notes, and snippets.

@knabben
Last active August 12, 2020 17:25
Show Gist options
  • Save knabben/0181a90e8a131d58a55f59c2bee9f494 to your computer and use it in GitHub Desktop.
Save knabben/0181a90e8a131d58a55f59c2bee9f494 to your computer and use it in GitHub Desktop.
Ginkgo Scrapper
import (
"bufio"
"fmt"
"k8s.io/klog/v2"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
)
var (
csvChannel chan string
)
func walkFunction(path string, info os.FileInfo, err error) error {
if !info.IsDir() && strings.HasSuffix(path, "_test.go") {
var lineNum = 1
fd, err := os.Open(path)
if err != nil {
return nil
}
defer fd.Close()
maskIt := `ginkgo.It\("(.*)",`
maskContext := `ginkgo.Context\("(.*)",`
maskDescribe := `framework.KubeDescribe\("(.*)(\[.*\])*",`
maskFramework := `framework.NewDefaultFramework\("(.*)"\)`
scanner := bufio.NewScanner(fd)
scanner.Split(bufio.ScanLines)
kubeDescribe, kubeNewFramework := "", ""
kubeContext := ""
for scanner.Scan() {
line := scanner.Text()
// Kube describe match
if matched, _ := regexp.Match(maskDescribe, []byte(line)); matched {
describeCompile := regexp.MustCompile(maskDescribe)
n := describeCompile.FindAllSubmatch([]byte(line), -1)[0]
kubeDescribe= string(n[1])
//kubeLabels = string(n[2])
}
// Match NewFramework
if matched, _ := regexp.Match(maskFramework, []byte(line)); matched {
describeCompile := regexp.MustCompile(maskFramework)
n := describeCompile.FindAllSubmatch([]byte(line), -1)[0]
kubeNewFramework = string(n[1])
}
// Context match
if matched, _ := regexp.Match(maskContext, []byte(line)); matched {
contextCompile := regexp.MustCompile(maskContext)
n := contextCompile.FindAllSubmatch([]byte(line), -1)[0]
kubeContext = string(n[1])
}
// It match
if matched, _ := regexp.Match(maskIt, []byte(line)); matched {
contextCompile := regexp.MustCompile(maskIt)
n := contextCompile.FindAllSubmatch([]byte(line), -1)[0]
csvChannel <- fmt.Sprintf("%s,%d,%s,%s,%s,%s", info.Name(), lineNum, kubeNewFramework, kubeDescribe, kubeContext, string(n[1]))
}
lineNum += 1
}
}
return nil
}
func appendCSVFile() {
for {
fmt.Println(<- csvChannel)
}
}
func main() {
cwd, err := os.Getwd() // Run from KUBEROOT
if err != nil {
klog.Fatal(err)
}
var wg sync.WaitGroup
csvChannel = make(chan string)
wg.Add(1)
folderPath := cwd + "/test/e2e_node"
go func() {
err = filepath.Walk(folderPath, walkFunction)
if err != nil {
klog.Fatal(err)
}
wg.Done()
}()
go appendCSVFile()
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment