...

Source file src/github.com/linkerd/linkerd2/testutil/annotations.go

Documentation: github.com/linkerd/linkerd2/testutil

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  const (
    12  	envFlag  = "GH_ANNOTATION"
    13  	rootPath = "/linkerd2/"
    14  )
    15  
    16  type level int
    17  
    18  const (
    19  	err level = iota
    20  	warn
    21  )
    22  
    23  func (l level) String() string {
    24  	switch l {
    25  	case err:
    26  		return "error"
    27  	case warn:
    28  		return "warning"
    29  	}
    30  	panic(fmt.Sprintf("invalid level: %d", l))
    31  }
    32  
    33  func echoAnnotation(t *testing.T, l level, args ...interface{}) {
    34  	if _, ok := os.LookupEnv(envFlag); ok {
    35  		_, fileName, fileLine, ok := runtime.Caller(3)
    36  		if !ok {
    37  			panic("Couldn't recover runtime info")
    38  		}
    39  		fileName = fileName[strings.LastIndex(fileName, rootPath)+len(rootPath):]
    40  		// In case of coming from `t.Run(testName, ...)`, only take the first part
    41  		// of the name; the following parts might not be as generic
    42  		parts := strings.Split(t.Name(), "/")
    43  		testName := parts[0]
    44  		for _, arg := range args {
    45  			msg := fmt.Sprintf("%s - %s", testName, arg)
    46  			fmt.Printf("::%s file=%s,line=%d::%s\n", l, fileName, fileLine, msg)
    47  		}
    48  	}
    49  }
    50  
    51  func echoAnnotationErr(t *testing.T, args ...interface{}) {
    52  	echoAnnotation(t, err, args...)
    53  }
    54  
    55  func echoAnnotationWarn(t *testing.T, args ...interface{}) {
    56  	echoAnnotation(t, warn, args...)
    57  }
    58  
    59  // Error is a wrapper around t.Error()
    60  // args are passed to t.Error(args) and each arg will be sent to stdout formatted
    61  // as a GitHub annotation when the envFlag environment variable is set
    62  func Error(t *testing.T, args ...interface{}) {
    63  	t.Helper()
    64  	echoAnnotationErr(t, args...)
    65  	t.Error(args...)
    66  }
    67  
    68  // AnnotatedError is similar to Error() but it also admits a msg string that
    69  // will be used as the GitHub annotation
    70  func AnnotatedError(t *testing.T, msg string, args ...interface{}) {
    71  	t.Helper()
    72  	echoAnnotationErr(t, msg)
    73  	t.Error(args...)
    74  }
    75  
    76  // Errorf is a wrapper around t.Errorf()
    77  // format and args are passed to t.Errorf(format, args) and the formatted
    78  // message will be sent to stdout as a GitHub annotation when the envFlag
    79  // environment variable is set
    80  func Errorf(t *testing.T, format string, args ...interface{}) {
    81  	t.Helper()
    82  	echoAnnotationErr(t, fmt.Sprintf(format, args...))
    83  	t.Errorf(format, args...)
    84  }
    85  
    86  // AnnotatedErrorf is similar to Errorf() but it also admits a msg string that
    87  // will be used as the GitHub annotation
    88  func AnnotatedErrorf(t *testing.T, msg, format string, args ...interface{}) {
    89  	t.Helper()
    90  	echoAnnotationErr(t, msg)
    91  	t.Errorf(format, args...)
    92  }
    93  
    94  // Fatal is a wrapper around t.Fatal()
    95  // args are passed to t.Fatal(args) and each arg will be sent to stdout formatted
    96  // as a GitHub annotation when the envFlag environment variable is set
    97  func Fatal(t *testing.T, args ...interface{}) {
    98  	t.Helper()
    99  	echoAnnotationErr(t, args)
   100  	t.Fatal(args...)
   101  }
   102  
   103  // AnnotatedFatal is similar to Fatal() but it also admits a msg string that
   104  // will be used as the GitHub annotation
   105  func AnnotatedFatal(t *testing.T, msg string, args ...interface{}) {
   106  	t.Helper()
   107  	echoAnnotationErr(t, msg)
   108  	t.Fatal(args...)
   109  }
   110  
   111  // Fatalf is a wrapper around t.Errorf()
   112  // format and args are passed to t.Fatalf(format, args) and the formatted
   113  // message will be sent to stdout as a GitHub annotation when the envFlag
   114  // environment variable is set
   115  func Fatalf(t *testing.T, format string, args ...interface{}) {
   116  	t.Helper()
   117  	echoAnnotationErr(t, fmt.Sprintf(format, args...))
   118  	t.Fatalf(format, args...)
   119  }
   120  
   121  // AnnotatedFatalf is similar to Fatalf() but it also admits a msg string that
   122  // will be used as the GitHub annotation
   123  func AnnotatedFatalf(t *testing.T, msg, format string, args ...interface{}) {
   124  	t.Helper()
   125  	echoAnnotationErr(t, msg)
   126  	t.Fatalf(format, args...)
   127  }
   128  
   129  // AnnotatedWarn is a wrapper around t.Log() but it also admits a msg string that
   130  // will be used as the GitHub warning annotation
   131  func AnnotatedWarn(t *testing.T, msg string, args ...interface{}) {
   132  	t.Helper()
   133  	echoAnnotationWarn(t, msg)
   134  	t.Log(args...)
   135  }
   136  

View as plain text