github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/cmd/roachtest/blacklist_test.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package main
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"os"
    17  	"sort"
    18  	"strconv"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/google/go-github/github"
    23  	"golang.org/x/oauth2"
    24  )
    25  
    26  const githubAPITokenEnv = "GITHUB_API_TOKEN"
    27  const runBlacklistEnv = "RUN_BLACKLIST_TEST"
    28  
    29  func TestBlacklists(t *testing.T) {
    30  	if _, ok := os.LookupEnv(runBlacklistEnv); !ok {
    31  		t.Skipf("Blackist test is only run if %s is set", runBlacklistEnv)
    32  	}
    33  
    34  	blacklists := map[string]blacklist{
    35  		"hibernate":  hibernateBlackList20_1,
    36  		"pgjdbc":     pgjdbcBlackList20_1,
    37  		"psycopg":    psycopgBlackList20_1,
    38  		"django":     djangoBlacklist20_1,
    39  		"sqlAlchemy": sqlAlchemyBlacklist20_1,
    40  		"libpq":      libPQBlacklist20_1,
    41  		"gopg":       gopgBlackList20_1,
    42  		"pgx":        pgxBlacklist20_1,
    43  	}
    44  	type reasonCount struct {
    45  		reason string
    46  		count  int
    47  		suites map[string]bool
    48  	}
    49  
    50  	var failureMap = make(map[string]*reasonCount, 200)
    51  	for suite, bl := range blacklists {
    52  		for _, reason := range bl {
    53  			if _, ok := failureMap[reason]; !ok {
    54  				failureMap[reason] = &reasonCount{
    55  					reason: reason,
    56  					suites: make(map[string]bool, 10),
    57  				}
    58  			}
    59  			failureMap[reason].count++
    60  			failureMap[reason].suites[suite] = true
    61  		}
    62  	}
    63  
    64  	counts := make([]reasonCount, 0, len(failureMap))
    65  	for _, count := range failureMap {
    66  		counts = append(counts, *count)
    67  	}
    68  	sort.Slice(counts, func(i, j int) bool {
    69  		return counts[i].count > counts[j].count
    70  	})
    71  
    72  	ctx := context.Background()
    73  	// This test exceeds the rate limit for non-authed requests. To run
    74  	// this test locally, set the environment variable GITHUB_API_TOKEN
    75  	// to your personal access token.
    76  	token, ok := os.LookupEnv(githubAPITokenEnv)
    77  	if !ok {
    78  		t.Fatalf("GitHub API token environment variable %s is not set", githubAPITokenEnv)
    79  	}
    80  	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
    81  	tc := oauth2.NewClient(ctx, ts)
    82  	client := github.NewClient(tc)
    83  
    84  	anyClosed := false
    85  	for i := range counts {
    86  		issueTitle := "unknown"
    87  		reason := counts[i].reason
    88  		var issueNum int
    89  		var err error
    90  		state := ""
    91  		if issueNum, err = strconv.Atoi(counts[i].reason); err == nil {
    92  			if issue, _, err := client.Issues.Get(ctx, "cockroachdb", "cockroach", issueNum); err == nil {
    93  				issueTitle = strings.Replace(issue.GetTitle(), ",", " ", -1)
    94  				state = issue.GetState()
    95  				if state != "open" {
    96  					anyClosed = true
    97  				}
    98  			}
    99  			reason = fmt.Sprintf("https://github.com/cockroachdb/cockroach/issues/%d", issueNum)
   100  		}
   101  		suites := ""
   102  		for suite := range counts[i].suites {
   103  			suites += suite + " "
   104  		}
   105  		fmt.Printf("%4d,%6s,%-54s,%s,%s\n", counts[i].count, state, reason, issueTitle, suites)
   106  	}
   107  
   108  	if anyClosed {
   109  		t.Fatal("Some closed issues appear in blacklists")
   110  	}
   111  }