Skip to content

Commit

Permalink
Update hack/jenkins/test-flake-chart/report_flakes/report_flake.go
Browse files Browse the repository at this point in the history
Co-authored-by: Steven Powell <[email protected]>
  • Loading branch information
ComradeProgrammer and spowelljr committed Jun 29, 2024
1 parent fecb7f3 commit 50c8c07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
10 changes: 5 additions & 5 deletions hack/jenkins/test-flake-chart/report_flakes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

const (
MaxItemEnv = 10
maxItemEnv = 10
)

// This program requires three arguments
Expand All @@ -48,24 +48,24 @@ func main() {
pr := os.Args[1]
rootJob := os.Args[2]
// read the environment names
envList, err := ParseEnvironmentList(os.Args[3])
envList, err := parseEnvironmentList(os.Args[3])
if err != nil {
fmt.Printf("failed to read %s, err: %v\n", os.Args[3], err)
os.Exit(1)
}
// fetch the test results
testSummaries, err := TestSummariesFromGCP(ctx, pr, rootJob, envList, client)
testSummaries, err := testSummariesFromGCP(ctx, pr, rootJob, envList, client)
if err != nil {
fmt.Printf("failed to load summaries: %v\n", err)
os.Exit(1)
}
// fetch the pre-calculated flake rates
flakeRates, err := GetFlakeRate(ctx, client)
flakeRates, err := flakeRate(ctx, client)
if err != nil {
fmt.Printf("failed to load flake rates: %v\n", err)
os.Exit(1)
}
// generate and send the message
msg := GenerateCommentMessage(testSummaries, flakeRates, pr, rootJob)
msg := generateCommentMessage(testSummaries, flakeRates, pr, rootJob)
fmt.Println(msg)
}
44 changes: 21 additions & 23 deletions hack/jenkins/test-flake-chart/report_flakes/report_flake.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"cloud.google.com/go/storage"
)

type ShortSummary struct {
type shortSummary struct {
NumberOfTests int
NumberOfFail int
NumberOfPass int
Expand All @@ -51,32 +51,32 @@ type ShortSummary struct {
}
}

// ParseEnvironmentList read the existing environments from the file
func ParseEnvironmentList(listFile string) ([]string, error) {
// parseEnvironmentList reads the existing environments from the file
func parseEnvironmentList(listFile string) ([]string, error) {
data, err := os.ReadFile(listFile)
if err != nil {
return nil, err
}
return strings.Split(strings.TrimSpace(string(data)), "\n"), nil
}

func TestSummariesFromGCP(ctx context.Context, pr, rootJob string, envList []string, client *storage.Client) (map[string]*ShortSummary, error) {
envToSummaries := map[string]*ShortSummary{}
func testSummariesFromGCP(ctx context.Context, pr, rootJob string, envList []string, client *storage.Client) (map[string]*shortSummary, error) {
envToSummaries := map[string]*shortSummary{}
for _, env := range envList {
if summary, err := testSummaryFromGCP(ctx, pr, rootJob, env, client); err == nil {
if summary != nil {
// if the summary is nil(missing) we just skip it
envToSummaries[env] = summary
}
} else {
summary, err := testSummaryFromGCP(ctx, pr, rootJob, env, client)
if err != nil {
return nil, fmt.Errorf("failed to fetch %s test summary from gcp, err: %v", env, err)
}
if summary != nil {
// if the summary is nil(missing) we just skip it
envToSummaries[env] = summary
}
}
return envToSummaries, nil
}

// testFromSummary get the summary of a test on the specified env from the specified summary.
func testSummaryFromGCP(ctx context.Context, pr, rootJob, env string, client *storage.Client) (*ShortSummary, error) {
// testSummaryFromGCP gets the summary of a test for the specified env.
func testSummaryFromGCP(ctx context.Context, pr, rootJob, env string, client *storage.Client) (*shortSummary, error) {

btk := client.Bucket("minikube-builds")
obj := btk.Object(fmt.Sprintf("logs/%s/%s/%s_summary.json", pr, rootJob, env))
Expand All @@ -95,16 +95,16 @@ func testSummaryFromGCP(ctx context.Context, pr, rootJob, env string, client *st
return nil, err
}

var summary ShortSummary
var summary shortSummary
if err = json.Unmarshal(data, &summary); err != nil {
return nil, fmt.Errorf("failed to deserialize the file: %v", err)
}
return &summary, nil

}

// GetFlakeRate downloaded recent flake rate from gcs, and return the map{env->map{testname->flake rate}}
func GetFlakeRate(ctx context.Context, client *storage.Client) (map[string]map[string]float64, error) {
// flakeRate downloads recent flake rates from GCS, and returns a map{env->map{testname->flake rate}}
func flakeRate(ctx context.Context, client *storage.Client) (map[string]map[string]float64, error) {
btk := client.Bucket("minikube-flake-rate")
obj := btk.Object("flake_rates.csv")
reader, err := obj.NewReader(ctx)
Expand Down Expand Up @@ -137,7 +137,7 @@ func GetFlakeRate(ctx context.Context, client *storage.Client) (map[string]map[s
return result, nil
}

func GenerateCommentMessage(summaries map[string]*ShortSummary, flakeRates map[string]map[string]float64, pr, rootJob string) string {
func generateCommentMessage(summaries map[string]*shortSummary, flakeRates map[string]map[string]float64, pr, rootJob string) string {
type failedTest struct {
flakeRate float64
env string
Expand All @@ -150,10 +150,8 @@ func GenerateCommentMessage(summaries map[string]*ShortSummary, flakeRates map[s
for _, test := range summary.FailedTests {
// if we cannot find the test, we assign the flake rate as -1, meaning N/A
flakerate := -1.0
if _, ok := flakeRates[env]; ok {
if v, ok := flakeRates[env][test]; ok {
flakerate = v
}
if v, ok := flakeRates[env][test]; ok {
flakerate = v
}
failedTestList = append(failedTestList,
failedTest{
Expand All @@ -177,7 +175,7 @@ func GenerateCommentMessage(summaries map[string]*ShortSummary, flakeRates map[s
// if an env has too much failures we will just skip it and print a message in the end
tooManyFailures := []string{}
for env, list := range envFailedTestList {
if len(list) > MaxItemEnv {
if len(list) > maxItemEnv {
tooManyFailures = append(tooManyFailures, env)
continue
}
Expand All @@ -196,7 +194,7 @@ func GenerateCommentMessage(summaries map[string]*ShortSummary, flakeRates map[s

builder := strings.Builder{}
builder.WriteString(
fmt.Sprintf("Here are the number of top %d failed tests in each environments with lowest flake rate.\n\n", MaxItemEnv))
fmt.Sprintf("Here are the number of top %d failed tests in each environments with lowest flake rate.\n\n", maxItemEnv))
builder.WriteString(generateMarkdownTable(table))
if len(tooManyFailures) > 0 {

Expand Down

0 comments on commit 50c8c07

Please sign in to comment.