github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/test/testutils/utils.go (about) 1 package testutils 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "path" 8 "runtime" 9 "strings" 10 "sync" 11 "testing" 12 "time" 13 14 "github.com/0xPolygon/supernets2-node/log" 15 ioprometheusclient "github.com/prometheus/client_model/go" 16 "github.com/prometheus/common/expfmt" 17 "github.com/spf13/afero" 18 ) 19 20 // CreateTestFiles creates the files in the map (path -> content) using the 21 // given afero file system. 22 func CreateTestFiles(appFs afero.Fs, files map[string]string) error { 23 for path, content := range files { 24 f, err := appFs.Create(path) 25 if err != nil { 26 return err 27 } 28 defer func() { 29 if err := f.Close(); err != nil { 30 log.Errorf("Could not close %s: %v", f.Name(), err) 31 } 32 }() 33 _, err = f.WriteString(content) 34 35 if err != nil { 36 return err 37 } 38 } 39 return nil 40 } 41 42 // ParseMetricFamilies parsing prometheus response from http endpoint 43 func ParseMetricFamilies(content io.Reader) (map[string]*ioprometheusclient.MetricFamily, error) { 44 var parser expfmt.TextParser 45 mf, err := parser.TextToMetricFamilies(content) 46 if err != nil { 47 return nil, err 48 } 49 return mf, nil 50 } 51 52 // CheckError checks the given error taking into account if it was expected and 53 // potentially the message it should carry. 54 func CheckError(err error, expected bool, msg string) error { 55 if !expected && err != nil { 56 return fmt.Errorf("Unexpected error %v", err) 57 } 58 if expected { 59 if err == nil { 60 return fmt.Errorf("Expected error didn't happen") 61 } 62 if msg == "" { 63 return fmt.Errorf("Expected error message not defined") 64 } 65 if !strings.HasPrefix(err.Error(), msg) { 66 return fmt.Errorf("Wrong error, expected %q, got %q", msg, err.Error()) 67 } 68 } 69 return nil 70 } 71 72 // ReadBytecode reads the bytecode of the given contract. 73 func ReadBytecode(contractPath string) (string, error) { 74 const basePath = "../../test/contracts/bin" 75 76 _, currentFilename, _, ok := runtime.Caller(0) 77 if !ok { 78 return "", fmt.Errorf("Could not get name of current file") 79 } 80 fullBasePath := path.Join(path.Dir(currentFilename), basePath) 81 82 content, err := os.ReadFile(path.Join(fullBasePath, contractPath)) 83 if err != nil { 84 return "", err 85 } 86 return string(content), nil 87 } 88 89 // GetEnv reads an environment variable, returning a given default value if not 90 // present. 91 func GetEnv(key string, defaultValue string) string { 92 value, exists := os.LookupEnv(key) 93 if exists { 94 return value 95 } 96 return defaultValue 97 } 98 99 // WaitUntil waits for the given WaitGroup to end. The test fails if the 100 // WaitGroup is not finished before the provided timeout. 101 func WaitUntil(t *testing.T, wg *sync.WaitGroup, timeout time.Duration) { 102 done := make(chan struct{}) 103 104 go func() { 105 wg.Wait() 106 close(done) 107 }() 108 109 select { 110 case <-done: 111 case <-time.After(timeout): 112 t.Fatalf("WaitGroup not done, test time expired after %s", timeout) 113 } 114 }