From 2920247b871179ffad3ffd6960ba5816c9ece088 Mon Sep 17 00:00:00 2001 From: Bharath Vedartham Date: Tue, 22 Aug 2023 02:13:38 +0530 Subject: [PATCH] add regression tests --- io/logs/logutil_test.go | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/io/logs/logutil_test.go b/io/logs/logutil_test.go index 1a667e0d03a4..58c0ca6bcae3 100644 --- a/io/logs/logutil_test.go +++ b/io/logs/logutil_test.go @@ -1,8 +1,11 @@ package logs import ( + "fmt" + "os" "testing" + "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/v4/testing/require" ) @@ -24,3 +27,58 @@ func TestMaskCredentialsLogging(t *testing.T) { require.Equal(t, MaskCredentialsLogging(test.url), test.maskedUrl) } } + +func TestConfigurePersistantLogging(t *testing.T) { + // 1. Test creation of file in an existing parent directory + logFileName := "test.log" + existingDirectory := "./existing-testing-dir" + err := os.Mkdir(existingDirectory, 0700) + if err != nil { + return + } + + err = ConfigurePersistentLogging(fmt.Sprintf("%s/%s", existingDirectory, logFileName)) + require.NoError(t, err) + + err = os.RemoveAll(existingDirectory) + if err != nil { + return + } + + // 2. Test creation of file along with parent directory + nonExistingDirectory := "./non-existing-testing-dir" + + err = ConfigurePersistentLogging(fmt.Sprintf("%s/%s", nonExistingDirectory, logFileName)) + require.NoError(t, err) + + err = os.RemoveAll(nonExistingDirectory) + if err != nil { + return + } + + // 3. Test creation of file in an existing parent directory with a non-existing sub-directory + existingDirectory = "./existing-testing-dir" + nonExistingSubDirectory := "non-existing-sub-dir" + err = os.Mkdir(existingDirectory, 0700) + if err != nil { + return + } + + err = ConfigurePersistentLogging(fmt.Sprintf("%s/%s/%s", existingDirectory, nonExistingSubDirectory, logFileName)) + require.NoError(t, err) + + err = os.RemoveAll(existingDirectory) + if err != nil { + return + } + + // 4. Create log file in a directory without 700 permissions + existingDirectory = "./existing-testing-dir" + err = os.Mkdir(existingDirectory, 0750) + if err != nil { + return + } + + err = ConfigurePersistentLogging(fmt.Sprintf("%s/%s", existingDirectory, logFileName)) + require.ErrorIs(t, err, errors.New("dir already exists without proper 0700 permissions")) +}