Skip to content

Commit

Permalink
add regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bharath-123 committed Aug 22, 2023
1 parent 0c05a53 commit c52cd9e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions io/logs/logutil_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package logs

import (
"fmt"
"os"
"testing"

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/testing/require"
)

Expand All @@ -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/%s", t.TempDir(), 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/%s", t.TempDir(), 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/%s", t.TempDir(), 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/%s", t.TempDir(), existingDirectory, logFileName))
require.ErrorIs(t, err, errors.New("dir already exists without proper 0700 permissions"))
}

0 comments on commit c52cd9e

Please sign in to comment.