get.porter.sh/porter@v1.3.0/tests/helpers.go (about) 1 package tests 2 3 import ( 4 "crypto/md5" 5 "fmt" 6 "os" 7 "path/filepath" 8 "regexp" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 "github.com/tidwall/gjson" 14 "github.com/tidwall/sjson" 15 ) 16 17 // RequireErrorContains fails the test when the error doesn't contain 18 // AssertFilePermissionsEqual checks that the file permission bits are equal for the specified file. 19 func AssertFilePermissionsEqual(t *testing.T, path string, want os.FileMode, got os.FileMode) bool { 20 want = want.Perm() 21 got = got.Perm() 22 return assert.Equal(t, want, got, 23 "incorrect file permissions on %s. want: %o, got %o", path, want, got) 24 } 25 26 // AssertDirectoryPermissionsEqual checks that all files in the specified path match the desired 27 // file mode. Uses a glob pattern to match. 28 func AssertDirectoryPermissionsEqual(t *testing.T, path string, mode os.FileMode) bool { 29 files, err := filepath.Glob(path) 30 if os.IsNotExist(err) { 31 return true 32 } 33 require.NoError(t, err) 34 35 for _, file := range files { 36 info, err := os.Stat(file) 37 require.NoError(t, err) 38 if info.IsDir() { 39 continue 40 } 41 if !AssertFilePermissionsEqual(t, path, mode, info.Mode()) { 42 return false 43 } 44 } 45 46 return true 47 } 48 49 // the specified substring. This is less fragile than using require.EqualError 50 func RequireErrorContains(t *testing.T, err error, substring string, msgAndArgs ...interface{}) { 51 require.Error(t, err) 52 require.Contains(t, err.Error(), substring, msgAndArgs...) 53 } 54 55 // GenerateDatabaseName comes up with a valid mongodb database name from a Go test name. 56 func GenerateDatabaseName(testName string) string { 57 reg := regexp.MustCompile(`[^a-zA-Z0-9_]+`) 58 59 safeTestName := reg.ReplaceAllString(testName, "_") 60 if len(safeTestName) > 50 { 61 safeTestName = fmt.Sprintf("%x", md5.Sum([]byte(safeTestName))) 62 } 63 return fmt.Sprintf("porter_%s", safeTestName) 64 } 65 66 // This is the same as require.Contains but it prints the output string without 67 // newlines escaped so that it's easier to read. 68 func RequireOutputContains(t *testing.T, output string, substring string, msgAndArgs ...interface{}) { 69 ok := assert.Contains(t, output, substring, msgAndArgs...) 70 if !ok { 71 t.Errorf("%s\ndoes not contain\n%s", output, substring) 72 t.FailNow() 73 } 74 } 75 76 // GRPCDisplayInstallationExpectedJSON converts a json byte string from 77 // a porter.DisplayInstallation to one expected from a GRPC Installation 78 func GRPCDisplayInstallationExpectedJSON(bExpInst []byte) ([]byte, error) { 79 // if no credentialSets or parameterSets add as empty list to 80 // match GRPCInstallation expectations 81 var err error 82 empty := make([]string, 0) 83 emptySets := []string{"credentialSets", "parameterSets"} 84 for _, es := range emptySets { 85 res := gjson.GetBytes(bExpInst, es) 86 if !res.Exists() { 87 bExpInst, err = sjson.SetBytes(bExpInst, es, empty) 88 if err != nil { 89 return nil, err 90 } 91 } 92 } 93 return bExpInst, nil 94 }