Skip to content

Commit

Permalink
Merge pull request #746 from razor-network/v1.0.3-patch1-incentnet
Browse files Browse the repository at this point in the history
Hotfix log files (#738)
  • Loading branch information
Yashk767 authored Jun 3, 2022
2 parents a5be2fb + 0f6dbae commit 23cc430
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ In the above example for the collection `ethCollectionMean`, new custom job havi

### Logs

User can pass a separate flag --logFile followed with any name for log file along with command. The logs will be stored in ```.razor``` directory.
User can pass a separate flag --logFile followed with any name for log file along with command. The logs will be stored in ```.razor/logs``` directory.

razor cli
```
Expand Down
1 change: 1 addition & 0 deletions cmd/vote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func TestGetLastProposedEpoch(t *testing.T) {
Data: []byte{4, 2},
},
},
time: 10,
contractAbi: abi.ABI{},
unpackErr: errors.New("unpack error"),
},
Expand Down
14 changes: 11 additions & 3 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ func (PathUtils) GetLogFilePath(fileName string) (string, error) {
if err != nil {
return "", err
}
filePath := home + "/" + fileName + ".log"
f, err := OSUtilsInterface.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0600)
fileDir := home + "/logs"
if _, err := OSUtilsInterface.Stat(fileDir); OSUtilsInterface.IsNotExist(err) {
mkdirErr := OSUtilsInterface.Mkdir(fileDir, 0700)
if mkdirErr != nil {
return "", mkdirErr
}
}

logFilepath := fileDir + "/" + fileName + ".log"
f, err := OSUtilsInterface.OpenFile(logFilepath, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
return "", err
}
defer f.Close()
return filePath, nil
return logFilepath, nil
}

//This function returns the config file path
Expand Down
48 changes: 40 additions & 8 deletions path/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,17 @@ func TestGetDefaultPath(t *testing.T) {
}

func TestGetLogFilePath(t *testing.T) {
var fileName string
var fileInfo fs.FileInfo

type args struct {
path string
pathErr error
file *os.File
fileErr error
fileName string
path string
pathErr error
file *os.File
fileErr error
statErr error
isNotExist bool
mkdirErr error
}
tests := []struct {
name string
Expand All @@ -111,9 +116,10 @@ func TestGetLogFilePath(t *testing.T) {
{
name: "Test 1: When GetLogFilePath() executes successfully",
args: args{
path: "./home/.razor",
fileName: "xyz",
path: "./home/.razor",
},
want: "./home/.razor/.log",
want: "./home/.razor/logs/xyz.log",
wantErr: nil,
},
{
Expand All @@ -133,6 +139,28 @@ func TestGetLogFilePath(t *testing.T) {
want: "",
wantErr: errors.New("error in getting file"),
},
{
name: "Test 4: When there is stat error but not mkdir error",
args: args{
fileName: "xyz",
path: "./home/.razor",
statErr: errors.New("file not exists"),
isNotExist: true,
},
want: "./home/.razor/logs/xyz.log",
wantErr: nil,
},
{
name: "Test 5: When there is stat error and mkdir error",
args: args{
path: "./home/.razor",
statErr: errors.New("file not exists"),
isNotExist: true,
mkdirErr: errors.New("mkdir error"),
},
want: "",
wantErr: errors.New("mkdir error"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -144,8 +172,12 @@ func TestGetLogFilePath(t *testing.T) {

pathMock.On("GetDefaultPath").Return(tt.args.path, tt.args.pathErr)
osMock.On("OpenFile", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.file, tt.args.fileErr)
osMock.On("Stat", mock.AnythingOfType("string")).Return(fileInfo, tt.args.statErr)
osMock.On("IsNotExist", mock.Anything).Return(tt.args.isNotExist)
osMock.On("Mkdir", mock.Anything, mock.Anything).Return(tt.args.mkdirErr)

pa := PathUtils{}
got, err := pa.GetLogFilePath(fileName)
got, err := pa.GetLogFilePath(tt.args.fileName)
if got != tt.want {
t.Errorf("GetLogFilePath(), got = %v, want = %v", got, tt.want)
}
Expand Down

0 comments on commit 23cc430

Please sign in to comment.