Skip to content

Commit

Permalink
Merge pull request #787 from kmala/release-0.6
Browse files Browse the repository at this point in the history
handle scenario when the file is created but doesn't have content
  • Loading branch information
k8s-ci-robot authored Dec 13, 2024
2 parents 2e9e69b + b491736 commit de99594
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pkg/fileutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func StartLoadDynamicFile(filename string, callBack FileChangeCallBack, stopCh <
defer watcher.Close()
content, err := loadDynamicFile(filename, stopCh)
if err != nil {
logrus.Errorf("startLoadDynamicFile: failed when loading file %s, %v", filename, err)
return
}
err = watcher.Add(filename)
Expand All @@ -68,6 +69,7 @@ func StartLoadDynamicFile(filename string, callBack FileChangeCallBack, stopCh <
}
if err := callBack.CallBackForFileLoad(content); err != nil {
logrus.Errorf("StartLoadDynamicFile: error in callBackForFileLoad, %v", err)
return
}
for {
select {
Expand Down
55 changes: 42 additions & 13 deletions pkg/fileutil/util_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package fileutil

import (
"context"
"fmt"
"os"
"strconv"
"sync"
"testing"
"time"

"k8s.io/apimachinery/pkg/util/wait"
)

var origFileContent = `
Expand All @@ -17,22 +21,26 @@ efgwht2033
`

type testStruct struct {
content string
callCount int
expectedContent string
mutex sync.Mutex
}

func (a *testStruct) CallBackForFileLoad(dynamicContent []byte) error {
a.mutex.Lock()
a.expectedContent = string(dynamicContent)
defer a.mutex.Unlock()
a.callCount++
if len(dynamicContent) == 0 {
return fmt.Errorf("file doesn't contain data")
}
a.expectedContent = string(dynamicContent)
return nil
}

func (a *testStruct) CallBackForFileDeletion() error {
a.mutex.Lock()
a.expectedContent = ""
defer a.mutex.Unlock()
a.expectedContent = ""
return nil
}

Expand Down Expand Up @@ -60,13 +68,25 @@ func TestLoadDynamicFile(t *testing.T) {
}
stopCh := make(chan struct{})
testA := &testStruct{}
StartLoadDynamicFile("/tmp/util_test.txt", testA, stopCh)
f, err := os.CreateTemp("/tmp", "testdata")
if err != nil {
t.Errorf("failed to create a local temp file %v", err)
}
defer os.Remove(f.Name())

StartLoadDynamicFile(f.Name(), testA, stopCh)
defer close(stopCh)
time.Sleep(2 * time.Second)
os.WriteFile("/tmp/util_test.txt", []byte("test"), 0777)
err = os.WriteFile(f.Name(), []byte("test"), 0777)
if err != nil {
t.Errorf("failed to update a temp file %s, err: %v", f.Name(), err)
}
for {
time.Sleep(1 * time.Second)
testA.mutex.Lock()
if testA.callCount != 3 {
t.Errorf("load file should fail twice but call count is only %d", testA.callCount)
}
if testA.expectedContent == "test" {
t.Log("read to test")
testA.mutex.Unlock()
Expand All @@ -75,27 +95,36 @@ func TestLoadDynamicFile(t *testing.T) {
testA.mutex.Unlock()
}
for _, c := range cases {
updateFile(testA, c.input, t)
testA.mutex.Lock()
updateFile(f.Name(), c.input, t)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
//wait until the file reloaded is handled or context timesout
wait.Until(func() {
testA.mutex.Lock()
defer testA.mutex.Unlock()
// if the file is reloaded then cancel such that the wait.Until completes
if testA.expectedContent == c.want {
cancel()
}
}, 100*time.Millisecond, ctx.Done())
cancel()
//Validate the content
if testA.expectedContent != c.want {
t.Errorf(
"Unexpected result: TestLoadDynamicFile: got: %s, wanted %s",
testA.expectedContent,
c.want,
)
}
testA.mutex.Unlock()
}

}

func updateFile(testA *testStruct, origFileContent string, t *testing.T) {
testA.content = origFileContent
func updateFile(fileName, origFileContent string, t *testing.T) {
data := []byte(origFileContent)
err := os.WriteFile("/tmp/util_test.txt", data, 0600)
err := os.WriteFile(fileName, data, 0600)
if err != nil {
t.Errorf("failed to create a local file /tmp/util_test.txt")
t.Errorf("failed to update a temp file %s, err: %v", fileName, err)
}
time.Sleep(1 * time.Second)
}

func TestDeleteDynamicFile(t *testing.T) {
Expand Down

0 comments on commit de99594

Please sign in to comment.