Skip to content

Commit

Permalink
fix : remove unnecessary logging for crc-exists file (#3676)
Browse files Browse the repository at this point in the history
Remove unnecessary debug log statement for crc-exists file from
filestore Exists() method. Only log debug statement when crc-exists file
is not found.

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia authored and praveenkumar committed Jan 2, 2025
1 parent dd00dac commit d89df4c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/libmachine/persist/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func (s Filestore) SetExists(name string) error {
func (s Filestore) Exists(name string) (bool, error) {
filename := filepath.Join(s.MachinesDir, name, fmt.Sprintf(".%s-exist", name))
_, err := os.Stat(filename)
log.Debugf("Checking file: %s", filename)

if os.IsNotExist(err) {
log.Debugf("file not found: %s", filename)
return false, nil
} else if err == nil {
return true, nil
Expand Down
24 changes: 24 additions & 0 deletions pkg/libmachine/persist/filestore_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package persist

import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/sirupsen/logrus"

"github.com/crc-org/crc/v2/pkg/drivers/none"
"github.com/crc-org/crc/v2/pkg/libmachine/host"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -106,6 +109,27 @@ func TestStoreLoad(t *testing.T) {
assert.NoError(t, json.Unmarshal(rawDataDriver.Data, &realDriver))
}

func TestStoreExists_WhenExecuted_ThenLogFileNotFoundInDebugMode(t *testing.T) {
// Given
var logBuffer bytes.Buffer
logrus.SetOutput(&logBuffer)
oldDebugLevel := logrus.GetLevel()
logrus.SetLevel(logrus.DebugLevel)
defer logrus.SetOutput(os.Stdout)
defer logrus.SetLevel(oldDebugLevel)
store := getTestStore(t)
h := testHost()

// When
exists, err := store.Exists(h.Name)

// Then
assert.NoError(t, err)
assert.False(t, exists)
logContents := logBuffer.String()
assert.Contains(t, logContents, "file not found: ")
}

func testHost() *host.Host {
return &host.Host{
ConfigVersion: host.Version,
Expand Down

0 comments on commit d89df4c

Please sign in to comment.