From cfa2079c81330243a1d7412cbec1b132d6c797e4 Mon Sep 17 00:00:00 2001 From: Manan Gupta <35839558+GuptaManan100@users.noreply.github.com> Date: Wed, 20 Dec 2023 21:43:57 +0530 Subject: [PATCH] Add logging for failing tests in CI (#14821) Signed-off-by: Manan Gupta --- go/cmd/vttestserver/cli/main_test.go | 13 +++++-- go/test/endtoend/cluster/cluster_util.go | 45 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/go/cmd/vttestserver/cli/main_test.go b/go/cmd/vttestserver/cli/main_test.go index 39dc8e4ea78..dbaf256c806 100644 --- a/go/cmd/vttestserver/cli/main_test.go +++ b/go/cmd/vttestserver/cli/main_test.go @@ -34,6 +34,7 @@ import ( "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/sqltypes" + "vitess.io/vitess/go/test/endtoend/cluster" "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/tlstest" @@ -189,11 +190,17 @@ func TestCanGetKeyspaces(t *testing.T) { conf := config defer resetConfig(conf) - cluster, err := startCluster() + clusterInstance, err := startCluster() assert.NoError(t, err) - defer cluster.TearDown() + defer clusterInstance.TearDown() - assertGetKeyspaces(t, cluster) + defer func() { + if t.Failed() { + cluster.PrintFiles(t, clusterInstance.Env.Directory(), "vtcombo.INFO", "error.log") + } + }() + + assertGetKeyspaces(t, clusterInstance) } func TestExternalTopoServerConsul(t *testing.T) { diff --git a/go/test/endtoend/cluster/cluster_util.go b/go/test/endtoend/cluster/cluster_util.go index 3d442bbb576..a35c3bf3769 100644 --- a/go/test/endtoend/cluster/cluster_util.go +++ b/go/test/endtoend/cluster/cluster_util.go @@ -29,6 +29,7 @@ import ( "google.golang.org/grpc" "vitess.io/vitess/go/vt/grpcclient" + "vitess.io/vitess/go/vt/log" "vitess.io/vitess/go/vt/vtgate/grpcvtgateconn" "github.com/buger/jsonparser" @@ -498,3 +499,47 @@ func DialVTGate(ctx context.Context, name, addr, username, password string) (*vt vtgateconn.RegisterDialer(dialerName, dialerFunc) return vtgateconn.DialProtocol(ctx, dialerName, addr) } + +// PrintFiles prints the files that are asked for. If no file is specified, all the files are printed. +func PrintFiles(t *testing.T, dir string, files ...string) { + var directories []string + directories = append(directories, dir) + + // Go over the remaining directories to check + for len(directories) > 0 { + // Get one of the directories, and read its contents. + dir = directories[0] + directories = directories[1:] + entries, err := os.ReadDir(dir) + if err != nil { + log.Errorf("Couldn't read directory - %v", dir) + continue + } + for _, entry := range entries { + name := path.Join(dir, entry.Name()) + // For a directory, we add it to our list of directories to check. + if entry.IsDir() { + directories = append(directories, name) + continue + } + // Check if this file should be printed or not. + if len(files) != 0 { + fileFound := false + for _, file := range files { + if strings.EqualFold(entry.Name(), file) { + fileFound = true + break + } + } + if !fileFound { + continue + } + } + // Read and print the file. + res, err := os.ReadFile(name) + require.NoError(t, err) + log.Errorf("READING FILE - %v", name) + log.Errorf("%v", string(res)) + } + } +}