github.com/verrazzano/verrazzano@v1.7.1/tools/vz/pkg/internal/util/files/files_test.go (about)

     1  // Copyright (c) 2021, 2024, 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  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/verrazzano/verrazzano/tools/vz/pkg/internal/util/log"
    14  	"go.uber.org/zap"
    15  )
    16  
    17  // TestGetMatchingFileNamesGood Tests that we can find the expected set of files with a matching expression
    18  // GIVEN a call to GetMatchingFileNamesGood
    19  // WHEN with a valid rootDirectory and regular expression
    20  // THEN files that matched will be returned
    21  func TestGetMatchingFileNamesGood(t *testing.T) {
    22  	logger := log.GetDebugEnabledLogger()
    23  	myFiles, err := GetMatchingFileNames(logger, "../../test/json", regexp.MustCompile(`node.*\.json$`))
    24  	assert.Nil(t, err)
    25  	assert.NotNil(t, myFiles)
    26  	assert.True(t, len(myFiles) > 0)
    27  	for _, file := range myFiles {
    28  		assert.True(t, len(checkIsRegularFile(logger, file)) == 0)
    29  	}
    30  	myFiles, err = GetMatchingFileNames(logger, "../../test/json", regexp.MustCompile(`node.*\.none_shall_match`))
    31  	assert.Nil(t, err)
    32  	assert.Nil(t, myFiles)
    33  }
    34  
    35  // TestGetMatchingDirectoryNamesGood Tests that we can find the expected set of files with a matching expression
    36  // GIVEN a call to GetMatchingDirectoryNames
    37  // WHEN with a valid rootDirectory and regular expression
    38  // THEN files that matched will be returned
    39  func TestGetMatchingDirectoriesNamesGood(t *testing.T) {
    40  	logger := log.GetDebugEnabledLogger()
    41  	// the .*son will match directories with names like "json"
    42  	myFiles, err := GetMatchingDirectoryNames(logger, "../../test", regexp.MustCompile(".*son$"))
    43  	assert.Nil(t, err)
    44  	assert.NotNil(t, myFiles)
    45  	assert.True(t, len(myFiles) > 0)
    46  	for _, file := range myFiles {
    47  		assert.True(t, len(checkIsDirectory(logger, file)) == 0)
    48  	}
    49  	myFiles, err = GetMatchingDirectoryNames(logger, "../../test", regexp.MustCompile("none_shall_match"))
    50  	assert.Nil(t, err)
    51  	assert.Nil(t, myFiles)
    52  }
    53  
    54  // TestGetMatchingInvalidInputs Tests that we can find the expected set of files with a matching expression
    55  // GIVEN a call to GetMatching* utilities
    56  // WHEN with invalid inputs
    57  // THEN we get failures as expected
    58  func TestGetMatchingInvalidInputs(t *testing.T) {
    59  	logger := log.GetDebugEnabledLogger()
    60  	_, err := GetMatchingDirectoryNames(logger, "../../test", nil)
    61  	assert.NotNil(t, err)
    62  	filesFound, err := GetMatchingDirectoryNames(logger, "../../test-not-found", regexp.MustCompile(".*son$"))
    63  	assert.Nil(t, err)
    64  	assert.Nil(t, filesFound)
    65  	_, err = GetMatchingFileNames(logger, "../../test", nil)
    66  	assert.NotNil(t, err)
    67  	filesFound, err = GetMatchingFileNames(logger, "../../test-not-found", regexp.MustCompile(".*son$"))
    68  	assert.Nil(t, err)
    69  	assert.Nil(t, filesFound)
    70  
    71  }
    72  
    73  // TestMiscUtils Tests that the misc small utilities work as expected
    74  // GIVEN a call to GetMiscUtils
    75  // WHEN with good and bad inputs
    76  // THEN utility functions behave as expected
    77  func TestMiscUtils(t *testing.T) {
    78  	logger := log.GetDebugEnabledLogger()
    79  	filename := FormFilePathInClusterRoot("../../test/cluster/problem-pods/cluster-snapshot/problem-pods", "default")
    80  	assert.NotNil(t, filename)
    81  	namespaces, err := FindNamespaces(logger, "../../test/cluster/problem-pods/cluster-snapshot")
    82  	assert.Nil(t, err)
    83  	assert.NotNil(t, namespaces)
    84  	assert.True(t, len(namespaces) > 0)
    85  	_, err = FindNamespaces(logger, "../../test/problem-pods/not-found")
    86  	assert.NotNil(t, err)
    87  }
    88  
    89  // TODO: Add more test cases (more expression variants, negative cases, etc...)
    90  
    91  func checkIsDirectory(logger *zap.SugaredLogger, fileName string) string {
    92  	failText := ""
    93  	stat, err := os.Stat(fileName)
    94  	if err != nil {
    95  		logger.Errorf("Stat failed for file: %s", fileName, err)
    96  		failText = fmt.Sprintf("Stat failed for file: %s", fileName)
    97  	} else if !stat.IsDir() {
    98  		failText = fmt.Sprintf("Matched file was not a directory: %s", fileName)
    99  	}
   100  	if len(failText) > 0 {
   101  		logger.Error(failText)
   102  	}
   103  	return failText
   104  }
   105  
   106  func checkIsRegularFile(logger *zap.SugaredLogger, fileName string) string {
   107  	failText := ""
   108  	stat, err := os.Stat(fileName)
   109  	if err != nil {
   110  		logger.Errorf("Stat failed for file: %s", fileName, err)
   111  		failText = fmt.Sprintf("Stat failed for file: %s", fileName)
   112  	} else if stat.IsDir() {
   113  		failText = fmt.Sprintf("Matched file was a directory: %s", fileName)
   114  	} else if !stat.Mode().IsRegular() {
   115  		failText = fmt.Sprintf("Matched file was not a regular file: %s", fileName)
   116  	}
   117  	if len(failText) > 0 {
   118  		logger.Error(failText)
   119  	}
   120  	return failText
   121  }
   122  
   123  // TestGetTimeOfCapture tests that a metadata.json file can be successfully parsed and a time.Time object is created without error
   124  // GIVEN a metadata.json file
   125  // WHEN I call GetTimeOfCapture and pass in this file
   126  // THEN expect it to successfully create a time.Time object with the correct information and no error should be returned.
   127  func TestGetTimeOfCapture(t *testing.T) {
   128  	logger := log.GetDebugEnabledLogger()
   129  	timeObject, err := GetTimeOfCapture(logger, "../../test/cluster/multiple-namespaces-stuck-terminating-on-finalizers/cluster-snapshot")
   130  	assert.NotNil(t, timeObject)
   131  	assert.Nil(t, err)
   132  	assert.True(t, timeObject.UTC().Format(time.RFC3339) == "2024-01-24T13:44:11Z")
   133  
   134  }