github.com/verrazzano/verrazzano@v1.7.0/tools/vz/pkg/analysis/internal/util/files/files_test.go (about) 1 // Copyright (c) 2021, 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 package files 4 5 import ( 6 "fmt" 7 "os" 8 "regexp" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/verrazzano/verrazzano/tools/vz/pkg/analysis/internal/util/log" 13 "go.uber.org/zap" 14 ) 15 16 // TestGetMatchingFilesGood Tests that we can find the expected set of files with a matching expression 17 // GIVEN a call to GetMatchingDirectories 18 // WHEN with a valid rootDirectory and regular expression 19 // THEN files that matched will be returned 20 func TestGetMatchingFilesGood(t *testing.T) { 21 logger := log.GetDebugEnabledLogger() 22 myFiles, err := GetMatchingFiles(logger, "../../../test/json", regexp.MustCompile(`node.*\.json$`)) 23 assert.Nil(t, err) 24 assert.NotNil(t, myFiles) 25 assert.True(t, len(myFiles) > 0) 26 for _, file := range myFiles { 27 assert.True(t, len(checkIsRegularFile(logger, file)) == 0) 28 } 29 myFiles, err = GetMatchingFiles(logger, "../../../test/json", regexp.MustCompile(`node.*\.none_shall_match`)) 30 assert.Nil(t, err) 31 assert.Nil(t, myFiles) 32 } 33 34 // TestGetMatchingDirectoriesGood Tests that we can find the expected set of files with a matching expression 35 // GIVEN a call to GetMatchingDirectories 36 // WHEN with a valid rootDirectory and regular expression 37 // THEN files that matched will be returned 38 func TestGetMatchingDirectoriesGood(t *testing.T) { 39 logger := log.GetDebugEnabledLogger() 40 // the .*son will match directories with names like "json" 41 myFiles, err := GetMatchingDirectories(logger, "../../../test", regexp.MustCompile(".*son$")) 42 assert.Nil(t, err) 43 assert.NotNil(t, myFiles) 44 assert.True(t, len(myFiles) > 0) 45 for _, file := range myFiles { 46 assert.True(t, len(checkIsDirectory(logger, file)) == 0) 47 } 48 myFiles, err = GetMatchingDirectories(logger, "../../../test", regexp.MustCompile("none_shall_match")) 49 assert.Nil(t, err) 50 assert.Nil(t, myFiles) 51 } 52 53 // TestGetMatchingBad Tests that we can find the expected set of files with a matching expression 54 // GIVEN a call to GetMatching* utilities 55 // WHEN with invalid inputs 56 // THEN we get failures as expected 57 func TestGetMatchingInvalidInputs(t *testing.T) { 58 logger := log.GetDebugEnabledLogger() 59 _, err := GetMatchingDirectories(logger, "../../../test", nil) 60 assert.NotNil(t, err) 61 filesFound, err := GetMatchingDirectories(logger, "../../../test-not-found", regexp.MustCompile(".*son$")) 62 assert.Nil(t, err) 63 assert.Nil(t, filesFound) 64 _, err = GetMatchingFiles(logger, "../../../test", nil) 65 assert.NotNil(t, err) 66 filesFound, err = GetMatchingFiles(logger, "../../../test-not-found", regexp.MustCompile(".*son$")) 67 assert.Nil(t, err) 68 assert.Nil(t, filesFound) 69 70 } 71 72 // TestMiscUtils Tests that the misc small utilities work as expected 73 // GIVEN a call to GetMiscUtils 74 // WHEN with good and bad inputs 75 // THEN utility functions behave as expected 76 func TestMiscUtils(t *testing.T) { 77 logger := log.GetDebugEnabledLogger() 78 filename := FindFileInClusterRoot("../../../test/cluster/problem-pods/cluster-snapshot/problem-pods", "default") 79 assert.NotNil(t, filename) 80 namespaces, err := FindNamespaces(logger, "../../../test/cluster/problem-pods/cluster-snapshot") 81 assert.Nil(t, err) 82 assert.NotNil(t, namespaces) 83 assert.True(t, len(namespaces) > 0) 84 _, err = FindNamespaces(logger, "../../../test/problem-pods/not-found") 85 assert.NotNil(t, err) 86 } 87 88 // TODO: Add more test cases (more expression variants, negative cases, etc...) 89 90 func checkIsDirectory(logger *zap.SugaredLogger, fileName string) string { 91 failText := "" 92 stat, err := os.Stat(fileName) 93 if err != nil { 94 logger.Errorf("Stat failed for file: %s", fileName, err) 95 failText = fmt.Sprintf("Stat failed for file: %s", fileName) 96 } else if !stat.IsDir() { 97 failText = fmt.Sprintf("Matched file was not a directory: %s", fileName) 98 } 99 if len(failText) > 0 { 100 logger.Error(failText) 101 } 102 return failText 103 } 104 105 func checkIsRegularFile(logger *zap.SugaredLogger, fileName string) string { 106 failText := "" 107 stat, err := os.Stat(fileName) 108 if err != nil { 109 logger.Errorf("Stat failed for file: %s", fileName, err) 110 failText = fmt.Sprintf("Stat failed for file: %s", fileName) 111 } else if stat.IsDir() { 112 failText = fmt.Sprintf("Matched file was a directory: %s", fileName) 113 } else if !stat.Mode().IsRegular() { 114 failText = fmt.Sprintf("Matched file was not a regular file: %s", fileName) 115 } 116 if len(failText) > 0 { 117 logger.Error(failText) 118 } 119 return failText 120 }